9
0
mirror of https://github.com/VolmitSoftware/Iris.git synced 2026-01-04 15:41:30 +00:00

improve CraftWorldInfo creation

This commit is contained in:
Julian Krings
2024-10-09 12:24:33 +02:00
parent 13369bbf32
commit 03582751c5
9 changed files with 75 additions and 121 deletions

View File

@@ -0,0 +1,42 @@
package com.volmit.iris.util.reflect;
import com.volmit.iris.core.nms.container.Pair;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class Reflect {
public static <T> T newInstance(Class<T> type, Object... initArgs) throws NoSuchMethodException, InvocationTargetException {
var list = Arrays.stream(initArgs)
.map(arg -> new Pair<Class<?>, Object>(arg.getClass(), arg))
.toList();
return newInstance(type, list);
}
public static <T> T newInstance(Class<T> type, List<Pair<Class<?>, Object>> initArgs) throws NoSuchMethodException, InvocationTargetException{
constructors:
for (var c : type.getDeclaredConstructors()) {
var types = c.getParameterTypes();
for (int i = 0; i < types.length; i++) {
if (!types[i].isAssignableFrom(initArgs.get(i).getA()))
continue constructors;
}
c.setAccessible(true);
try {
return (T) c.newInstance(initArgs);
} catch (InstantiationException | IllegalAccessException e) {
throw new InvocationTargetException(e);
}
}
var constructors = Arrays.stream(type.getDeclaredConstructors())
.map(Constructor::toGenericString)
.collect(Collectors.joining("\n"));
throw new NoSuchMethodException("No matching constructor found in:\n" + constructors);
}
}