Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow constructors as method reference entrypoints #982

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandleProxies;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Executable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import net.fabricmc.loader.api.LanguageAdapter;
Expand Down Expand Up @@ -63,14 +66,20 @@ public <T> T create(ModContainer mod, String value, Class<T> type) throws Langua
throw new LanguageAdapterException("Class " + c.getName() + " cannot be cast to " + type.getName() + "!");
}
} else /* length == 2 */ {
List<Method> methodList = new ArrayList<>();
List<Executable> methodList = new ArrayList<>();

for (Method m : c.getDeclaredMethods()) {
if (!(m.getName().equals(methodSplit[1]))) {
continue;
}
boolean isConstructor = methodSplit[1].equals("<init>");

if(isConstructor) {
methodList.addAll(Arrays.asList(c.getDeclaredConstructors()));
} else {
for(Method m : c.getDeclaredMethods()) {
if(!(m.getName().equals(methodSplit[1]))) {
continue;
}

methodList.add(m);
methodList.add(m);
}
}

try {
Expand Down Expand Up @@ -106,10 +115,10 @@ public <T> T create(ModContainer mod, String value, Class<T> type) throws Langua
throw new LanguageAdapterException("Found multiple method entries of name " + value + "!");
}

final Method targetMethod = methodList.get(0);
final Executable targetMethod = methodList.get(0);
Object object = null;

if ((targetMethod.getModifiers() & Modifier.STATIC) == 0) {
if ((targetMethod.getModifiers() & Modifier.STATIC) == 0 && !isConstructor) {
try {
object = c.getDeclaredConstructor().newInstance();
} catch (Exception e) {
Expand All @@ -120,8 +129,11 @@ public <T> T create(ModContainer mod, String value, Class<T> type) throws Langua
MethodHandle handle;

try {
handle = MethodHandles.lookup()
.unreflect(targetMethod);
if(isConstructor) {
handle = MethodHandles.lookup().findConstructor(c, MethodType.methodType(void.class, targetMethod.getParameterTypes()));
} else {
handle = MethodHandles.lookup().unreflect((Method) targetMethod);
}
} catch (Exception ex) {
throw new LanguageAdapterException(ex);
}
Expand Down