Add method to construct classes using reflection

This commit is contained in:
Mark Vainomaa 2017-02-24 15:45:00 +02:00
parent de3429bd89
commit 224780a84d
3 changed files with 59 additions and 0 deletions

View File

@ -1,11 +1,16 @@
package eu.mikroskeem.utils.reflect; package eu.mikroskeem.utils.reflect;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** /**
* Reflection utils * Reflection utils
@ -39,6 +44,27 @@ public class Reflect {
return getClass(clazz) != null; return getClass(clazz) != null;
} }
/**
* Construct class with arguments. Note: primitive types aren't supported
*
*
* @param clazz Class to construct
* @param arguments Class constructor arguments
* @return Class instance or null
*/
public static Object fastConstruct(@NotNull Class<?> clazz, TypeWrapper... arguments){
try {
Class<?>[] tArgs = Stream.of(arguments).map(TypeWrapper::getType).collect(Collectors.toList()).toArray(new Class[0]);
Object[] args = Stream.of(arguments).map(TypeWrapper::getValue).collect(Collectors.toList()).toArray();
Constructor constructor = clazz.getDeclaredConstructor(tArgs);
constructor.setAccessible(true);
return constructor.newInstance(args);
} catch (NoSuchMethodException|InstantiationException|IllegalAccessException|InvocationTargetException e){
e.printStackTrace();
return null;
}
}
/** /**
* Get declared class method (public,protected,private) * Get declared class method (public,protected,private)
* *
@ -188,4 +214,15 @@ public class Reflect {
} }
return null; return null;
} }
/**
* Type/value wrapper for reflective constructing
*/
@RequiredArgsConstructor
@Getter
public static class TypeWrapper {
private final Object value;
private final Class<?> type;
}
} }

View File

@ -0,0 +1,6 @@
package eu.mikroskeem.utils.test.reflect;
public class DifferentConstructors {
public DifferentConstructors(Integer a, Integer b, int c, boolean d){}
private DifferentConstructors(String foo, String bar){}
}

View File

@ -112,4 +112,20 @@ public class TestReflect {
Assert.assertTrue(Reflect.classExists(className)); Assert.assertTrue(Reflect.classExists(className));
Assert.assertEquals("hey", testFieldContent); Assert.assertEquals("hey", testFieldContent);
} }
@Test
public void testConstructing(){
Assert.assertNotNull(Reflect.fastConstruct(
DifferentConstructors.class,
new Reflect.TypeWrapper(1, Integer.class),
new Reflect.TypeWrapper(2, Integer.class),
new Reflect.TypeWrapper(3, int.class),
new Reflect.TypeWrapper(true, boolean.class)
));
Assert.assertNotNull(Reflect.fastConstruct(
DifferentConstructors.class,
new Reflect.TypeWrapper("kek", String.class),
new Reflect.TypeWrapper("kek", String.class)
));
}
} }