Allow scarpet function name to be different to annotated method name

This commit is contained in:
altrisi authored and gnembon committed 2023-03-08 20:53:35 +01:00
1 parent 4801534229
commit cc24f79d47
2 files changed
+14 -3

No files matched your search

@@ -77,6 +77,7 @@ import carpet.script.value.Value;
public final class AnnotationParser public final class AnnotationParser
{ {
static final int UNDEFINED_PARAMS = -2; static final int UNDEFINED_PARAMS = -2;
static final String USE_METHOD_NAME = "$METHOD_NAME_MARKER$";
private static final List<ParsedFunction> functionList = new ArrayList<>(); private static final List<ParsedFunction> functionList = new ArrayList<>();
/** /**
@@ -172,7 +173,8 @@ public final class AnnotationParser
private ParsedFunction(Method method, Class<?> originClass, Supplier<Object> instance) private ParsedFunction(Method method, Class<?> originClass, Supplier<Object> instance)
{ {
this.name = method.getName(); ScarpetFunction annotation = method.getAnnotation(ScarpetFunction.class);
this.name = annotation.functionName() == USE_METHOD_NAME ? method.getName() : annotation.functionName();
this.isMethodVarArgs = method.isVarArgs(); this.isMethodVarArgs = method.isVarArgs();
this.methodParamCount = method.getParameterCount(); this.methodParamCount = method.getParameterCount();
@@ -199,7 +201,7 @@ public final class AnnotationParser
int setMaxParams = this.minParams; // Unlimited == Integer.MAX_VALUE int setMaxParams = this.minParams; // Unlimited == Integer.MAX_VALUE
if (this.isEffectivelyVarArgs) if (this.isEffectivelyVarArgs)
{ {
setMaxParams = method.getAnnotation(ScarpetFunction.class).maxParams(); setMaxParams = annotation.maxParams();
if (setMaxParams == UNDEFINED_PARAMS) if (setMaxParams == UNDEFINED_PARAMS)
{ {
throw new IllegalArgumentException("No maximum number of params specified for " + name + ", use ScarpetFunction.UNLIMITED_PARAMS for unlimited. " throw new IllegalArgumentException("No maximum number of params specified for " + name + ", use ScarpetFunction.UNLIMITED_PARAMS for unlimited. "
@@ -237,7 +239,7 @@ public final class AnnotationParser
} }
this.scarpetParamCount = this.isEffectivelyVarArgs ? -1 : this.minParams; this.scarpetParamCount = this.isEffectivelyVarArgs ? -1 : this.minParams;
this.contextType = method.getAnnotation(ScarpetFunction.class).contextType(); this.contextType = annotation.contextType();
} }
@Override @Override
@@ -75,6 +75,15 @@ public @interface ScarpetFunction
*/ */
int maxParams() default AnnotationParser.UNDEFINED_PARAMS; int maxParams() default AnnotationParser.UNDEFINED_PARAMS;
/**
* <p>The name of the function in Scarpet, that by default will be the method name.<p>
*
* <p>The convention in Scarpet is to use names in snake case.</p>
*
* @return The name for this function in Scarpet
*/
String functionName() default AnnotationParser.USE_METHOD_NAME;
/** /**
* <p>Defines the Context Type that will be used when evaluating arguments to annotated methods.</p> * <p>Defines the Context Type that will be used when evaluating arguments to annotated methods.</p>
* *