Skip to content

Commit

Permalink
search for inherited properties
Browse files Browse the repository at this point in the history
  • Loading branch information
n9Mtq4 committed Jun 16, 2015
1 parent f3336a1 commit 5a39daf
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/com/n9mtq4/reflection/ReflectionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.net.URLConnection;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
Expand Down Expand Up @@ -2345,6 +2346,77 @@ public static Class getClassByFullName(String className) {
}
}


/**
* This fixes the issue in java of getDeclaredFields not searching
* the class's inherited methods.
* */
public static Field[] getAllDeclaredFields(Class clazz) {

ArrayList<Field> fields = new ArrayList<Field>();

// loop through all super classes until getSuperClass == null
Class currentClass = clazz;
while (currentClass != null) {
fields.addAll(Arrays.asList(currentClass.getDeclaredFields()));
currentClass = clazz.getSuperclass();
}

Field[] fields1 = new Field[fields.size()];
System.arraycopy(fields.toArray(), 0, fields1, 0, fields.size());
return fields1;

}

/**
* Get a field by name. Also searches inherited fields.
* */
public static Field getAllDeclaredField(String name, Class clazz) throws NoSuchFieldException {
Class currentClass = clazz;
while (currentClass != null) {
try {

Field f = currentClass.getDeclaredField(name);
return f;

}catch (NoSuchFieldException e) {
}
currentClass = clazz.getSuperclass();
}

throw new NoSuchFieldException(name);
}

public static Method[] getAllDeclaredMethods(Class clazz) {
ArrayList<Field> methods = new ArrayList<Field>();

// loop through all super classes until getSuperClass == null
Class currentClass = clazz;
while (currentClass != null) {
methods.addAll(Arrays.asList(currentClass.getDeclaredFields()));
currentClass = clazz.getSuperclass();
}

Method[] methods1 = new Method[methods.size()];
System.arraycopy(methods.toArray(), 0, methods1, 0, methods.size());
return methods1;
}

public static Method getAllDeclaredMethod(String name, Class[] params, Class clazz) throws NoSuchMethodException {
Class currentClass = clazz;
while (currentClass != null) {
try {

Method m = currentClass.getDeclaredMethod(name, params);
return m;

}catch (NoSuchMethodException e) {
}
currentClass = clazz.getSuperclass();
}
throw new NoSuchMethodException(name);
}

/**
* Get class params.
*
Expand Down

0 comments on commit 5a39daf

Please sign in to comment.