From 05ff870d0bbeaacdc8cd00699a6ec1b1a0e6c239 Mon Sep 17 00:00:00 2001
From: altrisi
Date: Mon, 24 May 2021 12:40:20 +0200
Subject: [PATCH] Allow static methods for Scarpet Functions
What was I thinking?
---
.../carpet/script/annotation/AnnotationParser.java | 12 +++++-------
.../carpet/script/annotation/ScarpetFunction.java | 2 +-
2 files changed, 6 insertions(+), 8 deletions(-)
diff --git a/src/main/java/carpet/script/annotation/AnnotationParser.java b/src/main/java/carpet/script/annotation/AnnotationParser.java
index 987ecb0c..a2c759c6 100644
--- a/src/main/java/carpet/script/annotation/AnnotationParser.java
+++ b/src/main/java/carpet/script/annotation/AnnotationParser.java
@@ -84,9 +84,8 @@ public final class AnnotationParser
* There is a set of requirements for the class and its methods:
*
* - Class must be concrete. That is, no interfaces or abstract classes should be passed
- * - Class must have the default constructor (or an equivalent) available. That is done in order to not need the {@code static} modifier in every method, making them faster to
- * code and simpler to look at.
- * - Annotated methods must not be {@code static}. See claim above
+ * - Class must have the default constructor (or an equivalent) available. That is done in order to not need the {@code static} modifier in every
+ * method, making them faster to code and simpler to look at.
* - Annotated methods must not throw checked exceptions. They can throw regular {@link RuntimeException}s (including but not limited to
* {@link InternalExpressionException}).
* Basically, it's fine as long as you don't add a {@code throws} declaration to your methods.
@@ -121,9 +120,7 @@ public final class AnnotationParser
{
if (!method.isAnnotationPresent(ScarpetFunction.class))
continue;
- // Checks
- if (Modifier.isStatic(method.getModifiers()))
- throw new IllegalArgumentException("Annotated method '" + method.getName() + "', provided in '" + clazz + "' must not be static");
+
if (method.getExceptionTypes().length != 0)
throw new IllegalArgumentException("Annotated method '" + method.getName() +"', provided in '"+clazz+"' must not declare checked exceptions");
@@ -212,7 +209,8 @@ public final class AnnotationParser
try
{
MethodHandle tempHandle = MethodHandles.publicLookup().unreflect(method).asFixedArity().asSpreader(Object[].class, this.methodParamCount);
- this.handle = tempHandle.asType(tempHandle.type().changeReturnType(Object.class)).bindTo(instance);
+ tempHandle = tempHandle.asType(tempHandle.type().changeReturnType(Object.class));
+ this.handle = Modifier.isStatic(method.getModifiers()) ? tempHandle : tempHandle.bindTo(instance);
} catch (IllegalAccessException e)
{
throw new IllegalArgumentException(e);
diff --git a/src/main/java/carpet/script/annotation/ScarpetFunction.java b/src/main/java/carpet/script/annotation/ScarpetFunction.java
index 80aa94de..466bda7e 100644
--- a/src/main/java/carpet/script/annotation/ScarpetFunction.java
+++ b/src/main/java/carpet/script/annotation/ScarpetFunction.java
@@ -29,7 +29,7 @@ import carpet.script.value.Value;
* {@link AnnotationParser#parseFunctionClass(Class)} ONCE. The provided {@link Class} must be concrete and provide the default constructor or an
* equivalent to it.
*
- * Methods annotated with this annotation must not be static and must not declare throwing any checked exceptions.
+ * Methods annotated with this annotation must not declare throwing any checked exceptions.
*
* If one of the method's parameters is {@link Context}, Carpet will pass the actual {@link Context} of the expression to the
* method. If one of the method's parameters is {@link Context.Type}, Carpet will pass the Context Type the function was called inside. That