Skip to content

Commit

Permalink
refactor: change how JavaNativeTypeTemplate#loadMethods registers met…
Browse files Browse the repository at this point in the history
…hods
  • Loading branch information
kindlich committed Nov 7, 2024
1 parent 799da9a commit 0fd9b7b
Showing 1 changed file with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,16 @@ private void loadMethods() {
for (Method method : class_.cls.getMethods()) {
if (isNotAccessible(method) || isOverridden(class_.cls, method))
continue;

if (expansion && !JavaModifiers.isStatic(method.getModifiers()))
continue;

Collection<MethodID> ids = new LinkedList<>();
boolean isStaticExpansion = false;
Collection<MethodID> ids = new LinkedHashSet<>(); // LinkedHashSet to prevent collision if a static method has both @Global and @Method
boolean isStaticExpansionMethod = expansion && method.isAnnotationPresent(ZenCodeType.StaticExpansionMethod.class);
boolean isStaticMethodInsideZc = (!expansion || isStaticExpansionMethod) && JavaModifiers.isStatic(method.getModifiers());

boolean implicit = false;
boolean isStaticMethod = false;

if (method.isAnnotationPresent(ZenCodeType.Operator.class)) {
ZenCodeType.Operator operator = method.getAnnotation(ZenCodeType.Operator.class);
MethodID id = MethodID.operator(OperatorType.valueOf(operator.value().toString()));
Expand All @@ -156,13 +159,13 @@ private void loadMethods() {
if (method.isAnnotationPresent(ZenCodeType.Getter.class)) {
ZenCodeType.Getter getter = method.getAnnotation(ZenCodeType.Getter.class);
String name = getter.value().isEmpty() ? method.getName() : getter.value();
MethodID id = MethodID.getter(name);
MethodID id = isStaticMethodInsideZc ? MethodID.staticGetter(name) : MethodID.getter(name);
ids.add(id);
}
if (method.isAnnotationPresent(ZenCodeType.Setter.class)) {
ZenCodeType.Setter setter = method.getAnnotation(ZenCodeType.Setter.class);
String name = setter.value().isEmpty() ? method.getName() : setter.value();
MethodID id = MethodID.setter(name);
MethodID id = isStaticMethodInsideZc ? MethodID.staticSetter(name) : MethodID.setter(name);
ids.add(id);
}
if (method.isAnnotationPresent(ZenCodeType.Caster.class)) {
Expand All @@ -174,20 +177,17 @@ private void loadMethods() {
if (method.isAnnotationPresent(ZenCodeType.Method.class)) {
ZenCodeType.Method methodAnnotation = method.getAnnotation(ZenCodeType.Method.class);
String name = methodAnnotation.value().isEmpty() ? method.getName() : methodAnnotation.value();
boolean hasStaticModifier = JavaModifiers.isStatic(method.getModifiers());
MethodID id = hasStaticModifier && !expansion ? MethodID.staticMethod(name) : MethodID.instanceMethod(name);
MethodID id = isStaticMethodInsideZc ? MethodID.staticMethod(name) : MethodID.instanceMethod(name);
implicit |= methodAnnotation.implicit();
isStaticMethod |= hasStaticModifier;
ids.add(id);
}
if (expansion && method.isAnnotationPresent(ZenCodeType.StaticExpansionMethod.class)) {
if (isStaticExpansionMethod) {
ZenCodeType.StaticExpansionMethod methodAnnotation = method.getAnnotation(ZenCodeType.StaticExpansionMethod.class);
String name = methodAnnotation.value().isEmpty() ? method.getName() : methodAnnotation.value();
MethodID id = MethodID.staticMethod(name);
ids.add(id);
isStaticExpansion = true;
}
if (!isStaticMethod && method.isAnnotationPresent(ZenCodeGlobals.Global.class) && JavaModifiers.isStatic(method.getModifiers())) {
if (isStaticMethodInsideZc && method.isAnnotationPresent(ZenCodeGlobals.Global.class)) {
ZenCodeGlobals.Global methodAnnotation = method.getAnnotation(ZenCodeGlobals.Global.class);
String name = methodAnnotation.value().isEmpty() ? method.getName() : methodAnnotation.value();
MethodID id = MethodID.staticMethod(name);
Expand All @@ -197,14 +197,13 @@ private void loadMethods() {
continue;

FunctionHeader header = headerConverter.getHeader(typeVariableContext, method);

if (expansion && !isStaticExpansion) {
if (expansion && !isStaticExpansionMethod) {
FunctionParameter[] withoutFirst = Arrays.copyOfRange(header.parameters, 1, header.parameters.length);
header = new FunctionHeader(header.typeParameters, header.getReturnType(), header.thrownType, withoutFirst);
}

for (MethodID id : ids) {
JavaRuntimeMethod runtimeMethod = new JavaRuntimeMethod(class_, target, method, id, header, implicit, expansion && !isStaticExpansion);
JavaRuntimeMethod runtimeMethod = new JavaRuntimeMethod(class_, target, method, id, header, implicit, expansion && !isStaticExpansionMethod);
methods.computeIfAbsent(id, x -> new ArrayList<>()).add(runtimeMethod);
class_.module.getCompiled().setMethodInfo(runtimeMethod, runtimeMethod);
}
Expand Down

0 comments on commit 0fd9b7b

Please sign in to comment.