Allow static methods for Scarpet Functions

What was I thinking?
This commit is contained in:
altrisi committed 2021-05-24 12:40:20 +02:00
1 parent 1b39feaab7
commit 05ff870d0b
2 files changed
+6 -8

No files matched your search

@@ -84,9 +84,8 @@ public final class AnnotationParser
* <p>There is a set of requirements for the class and its methods:</p>
* <ul>
* <li>Class must be concrete. That is, no interfaces or abstract classes should be passed</li>
* <li>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.</li>
* <li>Annotated methods must not be {@code static}. See claim above</li>
* <li>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.</li>
* <li>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.</li>
@@ -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);
@@ -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.</p>
*
* <p>Methods annotated with this annotation must not be static and must not declare throwing any checked exceptions.</p>
* <p>Methods annotated with this annotation must not declare throwing any checked exceptions.</p>
*
* <p>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