001: package net.sourceforge.pmd.dcd;
002:
003: import java.lang.reflect.Constructor;
004: import java.lang.reflect.Field;
005: import java.lang.reflect.Method;
006:
007: /**
008: * ClassLoader utilities. Useful for extracting additional details from a class
009: * hierarchy beyond the basic standard Java Reflection APIs.
010: */
011: public class ClassLoaderUtil {
012:
013: public static final String CLINIT = "<clinit>";
014:
015: public static final String INIT = "<init>";
016:
017: public static String fromInternalForm(String internalForm) {
018: return internalForm.replace('/', '.');
019: }
020:
021: public static String toInternalForm(String internalForm) {
022: return internalForm.replace('.', '/');
023: }
024:
025: public static Class getClass(String name) {
026: try {
027: return ClassLoaderUtil.class.getClassLoader().loadClass(
028: name);
029: } catch (ClassNotFoundException e) {
030: throw new RuntimeException(e);
031: }
032: }
033:
034: public static Field getField(Class type, String name) {
035: try {
036: return myGetField(type, name);
037: } catch (NoSuchFieldException e) {
038: throw new RuntimeException(e);
039: }
040: }
041:
042: private static Field myGetField(Class type, String name)
043: throws NoSuchFieldException {
044: // Scan the type hierarchy just like Class.getField(String) using
045: // Class.getDeclaredField(String)
046: try {
047: return type.getDeclaredField(name);
048: } catch (NoSuchFieldException e) {
049: // Try the super interfaces
050: for (Class super Interface : type.getInterfaces()) {
051: try {
052: return myGetField(super Interface, name);
053: } catch (NoSuchFieldException e2) {
054: // Okay
055: }
056: }
057: // Try the super classes
058: if (type.getSuperclass() != null) {
059: return myGetField(type.getSuperclass(), name);
060: } else {
061: throw new NoSuchFieldException(type.getName() + "."
062: + name);
063: }
064: }
065: }
066:
067: public static Method getMethod(Class type, String name,
068: Class... parameterTypes) {
069: try {
070: return myGetMethod(type, name, parameterTypes);
071: } catch (NoSuchMethodException e) {
072: throw new RuntimeException(e);
073: }
074: }
075:
076: private static Method myGetMethod(Class type, String name,
077: Class... parameterTypes) throws NoSuchMethodException {
078: // Scan the type hierarchy just like Class.getMethod(String, Class[])
079: // using Class.getDeclaredMethod(String, Class[])
080: // System.out.println("type: " + type);
081: // System.out.println("name: " + name);
082: // System.out
083: // .println("parameterTypes: " + Arrays.toString(parameterTypes));
084: try {
085: // System.out.println("Checking getDeclaredMethod");
086: // for (Method m : type.getDeclaredMethods()) {
087: // System.out.println("\t" + m);
088: // }
089: return type.getDeclaredMethod(name, parameterTypes);
090: } catch (NoSuchMethodException e) {
091: try {
092: // Try the super classes
093: if (type.getSuperclass() != null) {
094: // System.out.println("Checking super: "
095: // + type.getSuperclass());
096: return myGetMethod(type.getSuperclass(), name,
097: parameterTypes);
098: }
099: } catch (NoSuchMethodException e2) {
100: // Okay
101: }
102: // Try the super interfaces
103: for (Class super Interface : type.getInterfaces()) {
104: try {
105: // System.out.println("Checking super interface: "
106: // + superInterface);
107: return myGetMethod(super Interface, name,
108: parameterTypes);
109: } catch (NoSuchMethodException e3) {
110: // Okay
111: }
112: }
113: throw new NoSuchMethodException(type.getName() + "."
114: + getMethodSignature(name, parameterTypes));
115: }
116: }
117:
118: public static Constructor getConstructor(Class type, String name,
119: Class... parameterTypes) {
120: try {
121: return type.getDeclaredConstructor(parameterTypes);
122: } catch (NoSuchMethodException e) {
123: throw new RuntimeException(e);
124: }
125: }
126:
127: public static String getMethodSignature(String name,
128: Class... parameterTypes) {
129: StringBuilder builder = new StringBuilder();
130: builder.append(name);
131: if (!(name.equals(CLINIT) || name.equals(INIT))) {
132: builder.append("(");
133: if (parameterTypes != null) {
134: for (int i = 0; i < parameterTypes.length; i++) {
135: if (i > 0) {
136: builder.append(", ");
137: }
138: builder.append(parameterTypes[i].getName());
139: }
140: }
141: builder.append(")");
142: }
143: return builder.toString();
144: }
145:
146: public static Class[] getParameterTypes(
147: String... parameterTypeNames) {
148: Class[] parameterTypes = new Class[parameterTypeNames.length];
149: for (int i = 0; i < parameterTypeNames.length; i++) {
150: parameterTypes[i] = getClass(parameterTypeNames[i]);
151: }
152: return parameterTypes;
153: }
154:
155: public static boolean isOverridenMethod(Class clazz, Method method,
156: boolean checkThisClass) {
157: try {
158: if (checkThisClass) {
159: clazz.getDeclaredMethod(method.getName(), method
160: .getParameterTypes());
161: return true;
162: }
163: } catch (NoSuchMethodException e) {
164: }
165: // Check super class
166: if (clazz.getSuperclass() != null) {
167: if (isOverridenMethod(clazz.getSuperclass(), method, true)) {
168: return true;
169: }
170: }
171: // Check interfaces
172: for (Class anInterface : clazz.getInterfaces()) {
173: if (isOverridenMethod(anInterface, method, true)) {
174: return true;
175: }
176: }
177: return false;
178: }
179: }
|