001: package com.sun.facelets.util;
002:
003: import java.lang.reflect.Array;
004: import java.lang.reflect.Method;
005: import java.lang.reflect.Modifier;
006: import java.util.Arrays;
007:
008: public class ReflectionUtil {
009: protected static final String[] EMPTY_STRING = new String[0];
010:
011: protected static final String[] PRIMITIVE_NAMES = new String[] {
012: "boolean", "byte", "char", "double", "float", "int",
013: "long", "short", "void" };
014:
015: protected static final Class[] PRIMITIVES = new Class[] {
016: boolean.class, byte.class, char.class, double.class,
017: float.class, int.class, long.class, short.class, Void.TYPE };
018:
019: /**
020: *
021: */
022: private ReflectionUtil() {
023: super ();
024: }
025:
026: public static Class forName(String name)
027: throws ClassNotFoundException {
028: if (null == name || "".equals(name)) {
029: return null;
030: }
031: Class c = forNamePrimitive(name);
032: if (c == null) {
033: if (name.endsWith("[]")) {
034: String nc = name.substring(0, name.length() - 2);
035: c = Class.forName(nc, false, Thread.currentThread()
036: .getContextClassLoader());
037: c = Array.newInstance(c, 0).getClass();
038: } else {
039: c = Class.forName(name, false, Thread.currentThread()
040: .getContextClassLoader());
041: }
042: }
043: return c;
044: }
045:
046: protected static Class forNamePrimitive(String name) {
047: if (name.length() <= 8) {
048: int p = Arrays.binarySearch(PRIMITIVE_NAMES, name);
049: if (p >= 0) {
050: return PRIMITIVES[p];
051: }
052: }
053: return null;
054: }
055:
056: /**
057: * Converts an array of Class names to Class types
058: * @param s
059: * @return
060: * @throws ClassNotFoundException
061: */
062: public static Class[] toTypeArray(String[] s)
063: throws ClassNotFoundException {
064: if (s == null)
065: return null;
066: Class[] c = new Class[s.length];
067: for (int i = 0; i < s.length; i++) {
068: c[i] = forName(s[i]);
069: }
070: return c;
071: }
072:
073: /**
074: * Converts an array of Class types to Class names
075: * @param c
076: * @return
077: */
078: public static String[] toTypeNameArray(Class[] c) {
079: if (c == null)
080: return null;
081: String[] s = new String[c.length];
082: for (int i = 0; i < c.length; i++) {
083: s[i] = c[i].getName();
084: }
085: return s;
086: }
087:
088: /*
089: * Get a public method form a public class or interface of a given method.
090: * Note that if the base is an instance of a non-public class that
091: * implements a public interface, calling Class.getMethod() with the base
092: * will not find the method. To correct this, a version of the
093: * same method must be found in a superclass or interface.
094: **/
095:
096: static private Method getMethod(Class cl, String methodName,
097: Class[] paramTypes) {
098:
099: Method m = null;
100: try {
101: m = cl.getMethod(methodName, paramTypes);
102: } catch (NoSuchMethodException ex) {
103: return null;
104: }
105:
106: Class dclass = m.getDeclaringClass();
107: if (Modifier.isPublic(dclass.getModifiers())) {
108: return m;
109: }
110:
111: Class[] intf = dclass.getInterfaces();
112: for (int i = 0; i < intf.length; i++) {
113: m = getMethod(intf[i], methodName, paramTypes);
114: if (m != null) {
115: return m;
116: }
117: }
118: Class c = dclass.getSuperclass();
119: if (c != null) {
120: m = getMethod(c, methodName, paramTypes);
121: if (m != null) {
122: return m;
123: }
124: }
125: return null;
126: }
127:
128: protected static final String paramString(Class[] types) {
129: if (types != null) {
130: StringBuffer sb = new StringBuffer();
131: for (int i = 0; i < types.length; i++) {
132: sb.append(types[i].getName()).append(", ");
133: }
134: if (sb.length() > 2) {
135: sb.setLength(sb.length() - 2);
136: }
137: return sb.toString();
138: }
139: return null;
140: }
141: }
|