Initial commit

This commit is contained in:
2016-08-27 16:18:28 +03:00
commit 1b16a1d67e
29 changed files with 1374 additions and 0 deletions

View File

@@ -0,0 +1,124 @@
package eu.mikroskeem.utils.reflect;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Reflection utils
*
* @author Mark
*/
public class Reflect {
/**
* Find class by name
*
* @param clazz Class to search
* @return Class or null
*/
@Nullable
public static Class<?> getClass(@NotNull String clazz){
try {
return Class.forName(clazz);
} catch (ClassNotFoundException e){
return null;
}
}
/**
* Return whether class exists or not
*
* @param clazz Class to search
* @return Whether class existed or not
*/
public static boolean classExists(@NotNull String clazz){
return getClass(clazz) != null;
}
/**
* Get declared class method (public,protected,private)
*
* @param clazz Class to reflect
* @param method Method to search
* @param arguments Method arguments
* @return Method or null
*/
@Nullable
public static Method getMethod(@NotNull Class<?> clazz, @NotNull String method, Class<?>... arguments){
try {
Method m = clazz.getDeclaredMethod(method, arguments);
m.setAccessible(true);
return m;
} catch (NoSuchMethodException e){
return null;
}
}
/**
* Invoke method and get result
*
* @param method Method to invoke
* @param instance Instance where given method resides (null if static)
* @param args Method arguments
* @return Method result or null
*/
@Nullable
public static Object invokeMethod(@NotNull Method method, @Nullable Object instance, @Nullable Object... args){
try {
return method.invoke(instance, args);
} catch (IllegalAccessException|InvocationTargetException e){
return null;
}
}
/**
* Get declared class field (public,protected,private)
*
* @param clazz Class to reflect
* @param field Field to search
* @return Field or null
*/
@Nullable
public static Field getField(@NotNull Class<?> clazz, @NotNull String field){
try {
Field f = clazz.getDeclaredField(field);
f.setAccessible(true);
return f;
} catch (NoSuchFieldException e){
return null;
}
}
/**
* Read field
*
* @param field Field to read
* @param instance Instance where to read (null if static)
* @return Field contents or null;
*/
@Nullable
public static Object readField(@NotNull Field field, @Nullable Object instance){
try {
return field.get(instance);
} catch (IllegalAccessException e){
return null;
}
}
/**
* Write field
*
* @param field Field to write
* @param instance Instance where field resides (null if static)
* @param value Field new contents
*/
public static void writeField(@NotNull Field field, @Nullable Object instance, @Nullable Object value){
try {
field.set(instance, value);
} catch (IllegalAccessException e){}
}
}

View File

@@ -0,0 +1,6 @@
package eu.mikroskeem.utils.test.reflect;
public class ReflectionTestClass {
public static String FOOBAR = "nope";
public static String BAZFED = "yeah";
}

View File

@@ -0,0 +1,65 @@
package eu.mikroskeem.utils.test.reflect;
import eu.mikroskeem.utils.reflect.Reflect;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class TestReflect {
@Test
public void testGetClass(){
Assert.assertEquals(Reflect.getClass("java.lang.String"), String.class);
Assert.assertNotEquals(Reflect.getClass("java.lang.Integer"), String.class);
}
@Test
public void testClassFind(){
Assert.assertTrue(Reflect.classExists("java.lang.String"));
Assert.assertFalse(Reflect.classExists("foo.bar.baz.xyz"));
}
@Test
public void testGetMethod(){
Class reflectClass = Reflect.getClass("eu.mikroskeem.utils.reflect.Reflect");
Method method = Reflect.getMethod(reflectClass, "getClass", String.class);
Assert.assertNotNull(method);
Method invalidMethod = Reflect.getMethod(reflectClass, "getClass", Integer.class);
Assert.assertNull(invalidMethod);
}
@Test
public void testInvokeMethod(){
Class reflectClass = Reflect.getClass("eu.mikroskeem.utils.reflect.Reflect");
Method method = Reflect.getMethod(reflectClass, "getClass", String.class);
Class<?> resultClazz = (Class)Reflect.invokeMethod(method, null, "java.lang.String");
Assert.assertEquals(resultClazz, String.class);
}
@Test
public void testGetField(){
Class reflectTestClass = Reflect.getClass("eu.mikroskeem.utils.test.reflect.ReflectionTestClass");
Field foobarField = Reflect.getField(reflectTestClass, "FOOBAR");
Assert.assertNotNull(foobarField);
Field invalidField = Reflect.getField(reflectTestClass, "ROOMAZ");
Assert.assertNull(invalidField);
}
@Test
public void testReadField(){
Class reflectTestClass = Reflect.getClass("eu.mikroskeem.utils.test.reflect.ReflectionTestClass");
Field foobarField = Reflect.getField(reflectTestClass, "FOOBAR");
String value = (String)Reflect.readField(foobarField, null);
Assert.assertEquals(value, "nope");
}
@Test
public void testWriteField(){
Class reflectTestClass = Reflect.getClass("eu.mikroskeem.utils.test.reflect.ReflectionTestClass");
Field bazfed = Reflect.getField(reflectTestClass, "BAZFED");
Reflect.writeField(bazfed, null, "no yeah man :(");
String value = (String)Reflect.readField(bazfed, null);
Assert.assertEquals(value, "no yeah man :(");
}
}