From c2f15815ec398e905c1e0f1bee1461f21495b461 Mon Sep 17 00:00:00 2001 From: n9Mtq4 Date: Sat, 24 Oct 2015 20:35:14 -0400 Subject: [PATCH] minor changes --- .../com/n9mtq4/reflection/EnhancedProxy.java | 10 ++- .../n9mtq4/reflection/ReflectionHelper.java | 69 +++++++++++++++++-- .../n9mtq4/reflection/ReflectionWrapper.java | 24 +++---- 3 files changed, 81 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/n9mtq4/reflection/EnhancedProxy.java b/src/main/java/com/n9mtq4/reflection/EnhancedProxy.java index 00cd254..d4ee3f3 100644 --- a/src/main/java/com/n9mtq4/reflection/EnhancedProxy.java +++ b/src/main/java/com/n9mtq4/reflection/EnhancedProxy.java @@ -9,7 +9,7 @@ * NOTE: Some javadocs are oracle's. */ @SuppressWarnings("unused") -public class EnhancedProxy implements InvocationHandler, Serializable { +public final class EnhancedProxy implements InvocationHandler, Serializable { /** * Creates a new instance of the interface with the proxy enabled. EX:
@@ -41,8 +41,8 @@ public static Object callChild(Object obj, Method method, Object[] args) throws return method.invoke(obj, args); } - private Object obj; - private EnhancedInvocationHandler handler; + private final Object obj; + private final EnhancedInvocationHandler handler; private EnhancedProxy(Object obj, EnhancedInvocationHandler handler) { this.obj = obj; @@ -50,10 +50,8 @@ private EnhancedProxy(Object obj, EnhancedInvocationHandler handler) { } @Override - public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { - + public final Object invoke(Object proxy, Method method, Object[] args) throws Throwable { return handler.invoke(obj, proxy, method, args); - } /** diff --git a/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java b/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java index 4b47427..32d92c4 100644 --- a/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java +++ b/src/main/java/com/n9mtq4/reflection/ReflectionHelper.java @@ -47,7 +47,9 @@ public class ReflectionHelper { * Gets the instance of Unsafe. * * @return the instance of Unsafe. + * @deprecated {@link sun.misc.Unsafe} isn't recommended for use. * */ + @Deprecated public static Unsafe getUnsafe() { return getStaticObject("theUnsafe", Unsafe.class); } @@ -2334,10 +2336,11 @@ private static void checkDirectory(File directory, String pckgname, ArrayList * Slightly modified code from - * Bobulous @ https://stackoverflow.com/questions/15868914/how-to-get-2d-array-possible-combinations/15869610 + * http://stackoverflow.com/a/15869610/5196460 */ private static List> combinations(ArrayList> twoDimArray) { // TODO: find a better algorithm + // keep track of the size of each inner String array int sizeArray[] = new int[twoDimArray.size()]; @@ -2365,7 +2368,7 @@ private static List> combinations(ArrayList> twoDim for(int i = 0; i < twoDimArray.size(); ++i) { obs.add(twoDimArray.get(i).get(counterArray[i])); } - combinationList.add(obs); // add new combination to list + combinationList.add(obs);// add new combination to list // Now we need to increment the counterArray so that the next // combination is taken on the next iteration of this loop. @@ -2387,6 +2390,26 @@ private static List> combinations(ArrayList> twoDim // end stuff not written by me + private static List> autoboxingCombinations(ArrayList> twoDimArray) { +// TODO: this isn't perfect, wont work with {int, Integer, int} +// will work with {int, int, Integer} and {Integer, int, int} + for (ArrayList params : twoDimArray) { + for (int i = 0; i < params.size(); i++) { + Class clazz = params.get(i); + params.set(i, isPrimitive(clazz) ? forceGetObjectClass(clazz) : forceGetPrimitiveClass(clazz)); + twoDimArray.add(params); + } + } + for (ArrayList params : twoDimArray) { + for (int i = params.size() - 1; i > params.size(); i--) { + Class clazz = params.get(i); + params.set(i, isPrimitive(clazz) ? forceGetObjectClass(clazz) : forceGetPrimitiveClass(clazz)); + twoDimArray.add(params); + } + } + return combinations(twoDimArray); + } + /** * Find package names starting with. * @@ -2525,6 +2548,7 @@ public static Method getAllDeclaredMethod(String name, Class[] params, Class cla Class currentClass = clazz; while (currentClass != null) { +// loops through all the superclasses Method[] methods = getAllDeclaredMethods(currentClass); ArrayList possibleMethods = new ArrayList(); @@ -2559,13 +2583,24 @@ public static Method getAllDeclaredMethod(String name, Class[] params, Class cla superClasses.add(classes); } +// try for combinations of the superclasses List> combinations = combinations(superClasses); for (List combination : combinations) { Class[] c = combination.toArray(new Class[combination.size()]); try { Method m = findAllDeclaredMethodByParams(name, c, clazz); return m; - }catch (NoSuchMethodException e) {} + }catch (NoSuchMethodException ignored) {} + } + +// try for combinations, but with support for autobox combos. + List> autoboxCombinations = autoboxingCombinations(superClasses); + for (List combination : autoboxCombinations) { + Class[] c = combination.toArray(new Class[combination.size()]); + try { + Method m = findAllDeclaredMethodByParams(name, c, clazz); + return m; + }catch (NoSuchMethodException ignored) {} } currentClass = currentClass.getSuperclass(); @@ -2629,6 +2664,7 @@ public static boolean isPrimitive(Class clazz) { } /** + * This method manually undoes java's autoboxing. * Primitive types have two classes in java. * The primitive one (ie. int.class) and the * Object one (ie. Integer.class). @@ -2652,6 +2688,7 @@ public static Class getPrimitiveClass(Class clazz) { } /** + * This method manually undoes java's autoboxing. * Primitive types have two classes in java. * The primitive one (ie. int.class) and the * Object one (ie. Integer.class). @@ -2672,6 +2709,30 @@ public static Class getObjectClass(Class clazz) { if (clazz == short.class) return Short.class; if (clazz == int.class) return Integer.class; return null; - } + } + + /** + * Like {@link ReflectionHelper#getPrimitiveClass(Class)}, but + * if getPrimitiveClass returns null, this returns the original class. + * + * @return the Primitive version of the class. Won't return null. + * @see ReflectionHelper#getPrimitiveClass(Class) + * */ + public static Class forceGetPrimitiveClass(Class clazz) { + Class primitiveClass = getPrimitiveClass(clazz); + return primitiveClass == null ? clazz : primitiveClass; + } + + /** + * Like {@link ReflectionHelper#getObjectClass(Class)}, but + * if getObjectClass returns null, this returns the original class. + * + * @return the Object version of the class. Won't return null. + * @see ReflectionHelper#getObjectClass(Class) + * */ + public static Class forceGetObjectClass(Class clazz) { + Class objectClass = getObjectClass(clazz); + return objectClass == null ? clazz : objectClass; + } } diff --git a/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java b/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java index 77e34be..03deb11 100644 --- a/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java +++ b/src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java @@ -25,7 +25,7 @@ * @since v1.1 */ @SuppressWarnings("unused") -public class ReflectionWrapper implements Serializable { +public final class ReflectionWrapper implements Serializable { /** * Wraps a new instance of an object (E) with the ReflectionWrapper. @@ -51,7 +51,7 @@ public static ReflectionWrapper newInstance(Class clazz, Class[] classPar */ public static ReflectionWrapper newInstance(Class clazz, Object... args) { E object = callConstructor(clazz, args); - return new ReflectionWrapper(object); + return attachToObject(object); } /** @@ -65,7 +65,7 @@ public static ReflectionWrapper attachToObject(E object) { return new ReflectionWrapper(object); } - private E object; + private final E object; private ReflectionWrapper(E object) { this.object = object; @@ -77,7 +77,7 @@ private ReflectionWrapper(E object) { * @param fieldName the field name * @return the field */ - public R getField(String fieldName) { + public final R getField(String fieldName) { return getObject(fieldName, object); } @@ -87,7 +87,7 @@ public R getField(String fieldName) { * @param field the field * @return the field */ - public R getField(Field field) { + public final R getField(Field field) { return getObject(field, object); } @@ -97,7 +97,7 @@ public R getField(Field field) { * @param fieldName the field name * @param newValue the new value of the field */ - public void setField(String fieldName, R newValue) { + public final void setField(String fieldName, R newValue) { setObject(newValue, fieldName, object); } @@ -107,7 +107,7 @@ public void setField(String fieldName, R newValue) { * @param field the field * @param newValue the new value of the field */ - public void setField(Field field, R newValue) { + public final void setField(Field field, R newValue) { setObject(newValue, field, object); } @@ -120,7 +120,7 @@ public void setField(Field field, R newValue) { * @param args the args for the method * @return the return of the method, null if the method is void */ - public R callMethod(String name, Object... args) { + public final R callMethod(String name, Object... args) { return callObjectMethod(name, object, args); } @@ -133,7 +133,7 @@ public R callMethod(String name, Object... args) { * @param args the args for the method * @return the return of the method, null if the method is void */ - public R callMethod(String name, Class[] classParams, Object... args) { + public final R callMethod(String name, Class[] classParams, Object... args) { return callObjectMethod(name, object, classParams, args); } @@ -145,7 +145,7 @@ public R callMethod(String name, Class[] classParams, Object... args) { * @param args the args for the method * @return the return of the method, null if the method is void */ - public R callMethod(Method method, Object... args) { + public final R callMethod(Method method, Object... args) { return callObjectMethod(method, object, args); } @@ -155,7 +155,7 @@ public R callMethod(Method method, Object... args) { * * @return the object the wrapper contains */ - public E getWrappedObject() { + public final E getWrappedObject() { return object; } @@ -163,7 +163,7 @@ public E getWrappedObject() { * @see Object#toString() * */ @Override - public String toString() { + public final String toString() { return "ReflectionWrapper: " + object.toString(); }