Skip to content

Commit

Permalink
minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Oct 25, 2015
1 parent 14cec38 commit c2f1581
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 22 deletions.
10 changes: 4 additions & 6 deletions src/main/java/com/n9mtq4/reflection/EnhancedProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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:<br>
Expand Down Expand Up @@ -41,19 +41,17 @@ 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;
this.handler = 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);

}

/**
Expand Down
69 changes: 65 additions & 4 deletions src/main/java/com/n9mtq4/reflection/ReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -2334,10 +2336,11 @@ private static void checkDirectory(File directory, String pckgname, ArrayList<Cl
* one String from each String array within the specified two-dimensional
* array.<br>
* 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<List<Class>> combinations(ArrayList<ArrayList<Class>> twoDimArray) {
// TODO: find a better algorithm

// keep track of the size of each inner String array
int sizeArray[] = new int[twoDimArray.size()];

Expand Down Expand Up @@ -2365,7 +2368,7 @@ private static List<List<Class>> combinations(ArrayList<ArrayList<Class>> 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.
Expand All @@ -2387,6 +2390,26 @@ private static List<List<Class>> combinations(ArrayList<ArrayList<Class>> twoDim

// end stuff not written by me

private static List<List<Class>> autoboxingCombinations(ArrayList<ArrayList<Class>> twoDimArray) {
// TODO: this isn't perfect, wont work with {int, Integer, int}
// will work with {int, int, Integer} and {Integer, int, int}
for (ArrayList<Class> 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<Class> 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.
*
Expand Down Expand Up @@ -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<Method> possibleMethods = new ArrayList<Method>();
Expand Down Expand Up @@ -2559,13 +2583,24 @@ public static Method getAllDeclaredMethod(String name, Class[] params, Class cla
superClasses.add(classes);
}

// try for combinations of the superclasses
List<List<Class>> combinations = combinations(superClasses);
for (List<Class> 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<List<Class>> autoboxCombinations = autoboxingCombinations(superClasses);
for (List<Class> 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();
Expand Down Expand Up @@ -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).
Expand All @@ -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).
Expand All @@ -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;
}

}
24 changes: 12 additions & 12 deletions src/main/java/com/n9mtq4/reflection/ReflectionWrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* @since v1.1
*/
@SuppressWarnings("unused")
public class ReflectionWrapper<E> implements Serializable {
public final class ReflectionWrapper<E> implements Serializable {

/**
* Wraps a new instance of an object (E) with the ReflectionWrapper.
Expand All @@ -51,7 +51,7 @@ public static <E> ReflectionWrapper<E> newInstance(Class clazz, Class[] classPar
*/
public static <E> ReflectionWrapper<E> newInstance(Class clazz, Object... args) {
E object = callConstructor(clazz, args);
return new ReflectionWrapper<E>(object);
return attachToObject(object);
}

/**
Expand All @@ -65,7 +65,7 @@ public static <E> ReflectionWrapper<E> attachToObject(E object) {
return new ReflectionWrapper<E>(object);
}

private E object;
private final E object;

private ReflectionWrapper(E object) {
this.object = object;
Expand All @@ -77,7 +77,7 @@ private ReflectionWrapper(E object) {
* @param fieldName the field name
* @return the field
*/
public <R> R getField(String fieldName) {
public final <R> R getField(String fieldName) {
return getObject(fieldName, object);
}

Expand All @@ -87,7 +87,7 @@ public <R> R getField(String fieldName) {
* @param field the field
* @return the field
*/
public <R> R getField(Field field) {
public final <R> R getField(Field field) {
return getObject(field, object);
}

Expand All @@ -97,7 +97,7 @@ public <R> R getField(Field field) {
* @param fieldName the field name
* @param newValue the new value of the field
*/
public <R> void setField(String fieldName, R newValue) {
public final <R> void setField(String fieldName, R newValue) {
setObject(newValue, fieldName, object);
}

Expand All @@ -107,7 +107,7 @@ public <R> void setField(String fieldName, R newValue) {
* @param field the field
* @param newValue the new value of the field
*/
public <R> void setField(Field field, R newValue) {
public final <R> void setField(Field field, R newValue) {
setObject(newValue, field, object);
}

Expand All @@ -120,7 +120,7 @@ public <R> 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> R callMethod(String name, Object... args) {
public final <R> R callMethod(String name, Object... args) {
return callObjectMethod(name, object, args);
}

Expand All @@ -133,7 +133,7 @@ public <R> 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> R callMethod(String name, Class[] classParams, Object... args) {
public final <R> R callMethod(String name, Class[] classParams, Object... args) {
return callObjectMethod(name, object, classParams, args);
}

Expand All @@ -145,7 +145,7 @@ public <R> 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> R callMethod(Method method, Object... args) {
public final <R> R callMethod(Method method, Object... args) {
return callObjectMethod(method, object, args);
}

Expand All @@ -155,15 +155,15 @@ public <R> R callMethod(Method method, Object... args) {
*
* @return the object the wrapper contains
*/
public E getWrappedObject() {
public final E getWrappedObject() {
return object;
}

/**
* @see Object#toString()
* */
@Override
public String toString() {
public final String toString() {
return "ReflectionWrapper: " + object.toString();
}

Expand Down

0 comments on commit c2f1581

Please sign in to comment.