|
| 1 | +package org.tools; |
| 2 | + |
| 3 | +import sun.reflect.ReflectionFactory; |
| 4 | + |
| 5 | +import java.lang.reflect.AccessibleObject; |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.lang.reflect.InvocationTargetException; |
| 9 | + |
| 10 | +@SuppressWarnings("restriction") |
| 11 | +public class Reflections { |
| 12 | + |
| 13 | + public static void setAccessible(AccessibleObject member) { |
| 14 | + String versionStr = System.getProperty("java.version"); |
| 15 | + int javaVersion = Integer.parseInt(versionStr.split("\\.")[0]); |
| 16 | + member.setAccessible(true); |
| 17 | + } |
| 18 | + |
| 19 | + public static Field getField(final Class<?> clazz, final String fieldName) { |
| 20 | + Field field = null; |
| 21 | + try { |
| 22 | + field = clazz.getDeclaredField(fieldName); |
| 23 | + setAccessible(field); |
| 24 | + } catch (NoSuchFieldException ex) { |
| 25 | + if (clazz.getSuperclass() != null) |
| 26 | + field = getField(clazz.getSuperclass(), fieldName); |
| 27 | + } |
| 28 | + return field; |
| 29 | + } |
| 30 | + |
| 31 | + public static void setFieldValue(final Object obj, final String fieldName, final Object value) throws Exception { |
| 32 | + final Field field = getField(obj.getClass(), fieldName); |
| 33 | + field.set(obj, value); |
| 34 | + } |
| 35 | + |
| 36 | + public static Object getFieldValue(final Object obj, final String fieldName) throws Exception { |
| 37 | + final Field field = getField(obj.getClass(), fieldName); |
| 38 | + return field.get(obj); |
| 39 | + } |
| 40 | + |
| 41 | + public static Constructor<?> getFirstCtor(final String name) throws Exception { |
| 42 | + final Constructor<?> ctor = Class.forName(name).getDeclaredConstructors()[0]; |
| 43 | + setAccessible(ctor); |
| 44 | + return ctor; |
| 45 | + } |
| 46 | + |
| 47 | + public static Constructor<?> getFirstCtor(Class clazz) throws Exception { |
| 48 | + final Constructor<?> ctor = clazz.getDeclaredConstructors()[0]; |
| 49 | + setAccessible(ctor); |
| 50 | + return ctor; |
| 51 | + } |
| 52 | + |
| 53 | + public static Object newInstance(String className, Object... args) throws Exception { |
| 54 | + return getFirstCtor(className).newInstance(args); |
| 55 | + } |
| 56 | + |
| 57 | + public static <T> T createWithoutConstructor(Class<T> classToInstantiate) |
| 58 | + throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { |
| 59 | + return createWithConstructor(classToInstantiate, Object.class, new Class[0], new Object[0]); |
| 60 | + } |
| 61 | + |
| 62 | + @SuppressWarnings({"unchecked"}) |
| 63 | + public static <T> T createWithConstructor(Class<T> classToInstantiate, Class<? super T> constructorClass, Class<?>[] consArgTypes, Object[] consArgs) |
| 64 | + throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { |
| 65 | + Constructor<? super T> objCons = constructorClass.getDeclaredConstructor(consArgTypes); |
| 66 | + setAccessible(objCons); |
| 67 | + Constructor<?> sc = ReflectionFactory.getReflectionFactory().newConstructorForSerialization(classToInstantiate, objCons); |
| 68 | + setAccessible(sc); |
| 69 | + return (T) sc.newInstance(consArgs); |
| 70 | + } |
| 71 | + |
| 72 | +} |
0 commit comments