1 18 package org.apache.geronimo.interop.util; 19 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.lang.reflect.Modifier ; 23 import java.util.HashMap ; 24 25 import org.apache.geronimo.interop.SystemException; 26 27 28 32 public class JavaMethod { 33 private static HashMap _methodMap = new HashMap (); 34 35 public static Method [] add(Method m, Method [] a) { 36 Method [] b = new Method [a.length + 1]; 37 System.arraycopy(a, 0, b, 0, a.length); 38 b[a.length] = m; 39 return b; 40 } 41 42 46 public static String getShortSignature(String methodName, Class [] parameterTypes) { 47 StringBuffer sb = new StringBuffer (); 48 sb.append(methodName); 49 sb.append('('); 50 int n = parameterTypes.length; 51 for (int i = 0; i < n; i++) { 52 if (i > 0) { 53 sb.append(", "); 54 } 55 sb.append(JavaType.getName(parameterTypes[i])); 56 } 57 sb.append(')'); 58 return sb.toString(); 59 } 60 61 public static String getShortSignature(Method m) { 62 return getShortSignature(m.getName(), m.getParameterTypes()); 63 } 64 65 69 public static String getLongSignature(Class returnType, String className, String methodName, Class [] parameterTypes) { 70 return JavaType.getName(returnType) + " " + className + "." + getShortSignature(methodName, parameterTypes); 71 } 72 73 public static String getLongSignature(Method m) { 74 return getLongSignature(m.getReturnType(), m.getDeclaringClass().getName(), m.getName(), m.getParameterTypes()); 75 } 76 77 public static String getLongSignature(Class c, Method m) { 78 return getLongSignature(m.getReturnType(), c.getName(), m.getName(), m.getParameterTypes()); 79 } 80 81 public static Method getMethod(String methodSignature) { 82 Method method = (Method ) _methodMap.get(methodSignature); 83 if (method == null) { 84 synchronized (_methodMap) { 85 method = (Method ) _methodMap.get(methodSignature); 86 if (method == null) { 87 int parenPos = methodSignature.indexOf('('); 88 if (parenPos == -1) { 89 throw new IllegalArgumentException ("methodSignature = " + methodSignature); 90 } 91 String fullMethodName = methodSignature.substring(0, parenPos); 92 String className = JavaClass.getNamePrefix(fullMethodName); 93 String methodName = JavaClass.getNameSuffix(fullMethodName); 94 String parameters = methodSignature.substring(parenPos); 95 String shortSig = methodName + parameters; 96 Class theClass = ThreadContext.loadClass(className); 97 Method [] methods = theClass.getMethods(); 98 int n = methods.length; 99 for (int i = 0; i < n; i++) { 100 method = methods[i]; 101 if (shortSig.equals(JavaMethod.getShortSignature(method))) { 102 _methodMap.put(methodSignature, method); 103 break; 104 } 105 } 106 } 107 } 108 } 109 if (method == null) { 110 throw new IllegalArgumentException ("method = " + methodSignature + " (not found)"); 111 } 112 return method; 113 } 114 115 public static Method getInstanceMethod(String methodSignature) { 116 Method method = getMethod(methodSignature); 117 if (Modifier.isStatic(method.getModifiers())) { 118 throw new IllegalArgumentException ("method = " + methodSignature + " (static)"); 119 } 120 return method; 121 } 122 123 public static Method getStaticMethod(String methodSignature) { 124 Method method = getMethod(methodSignature); 125 if (!Modifier.isStatic(method.getModifiers())) { 126 throw new IllegalArgumentException ("method = " + methodSignature + " (not static)"); 127 } 128 return method; 129 } 130 131 public static Object invokeStatic(String methodSignature, Object p1) { 132 return invokeStatic(methodSignature, new Object [] 133 { 134 p1 135 }); 136 } 137 138 public static Object invokeStatic(String methodSignature, Object p1, Object p2) { 139 return invokeStatic(methodSignature, new Object [] 140 { 141 p1, p2 142 }); 143 } 144 145 public static Object invokeStatic(String methodSignature, Object p1, Object p2, Object p3) { 146 return invokeStatic(methodSignature, new Object [] 147 { 148 p1, p2, p3 149 }); 150 } 151 152 public static Object invokeStatic(String methodSignature, Object [] args) { 153 try { 154 return getStaticMethod(methodSignature).invoke(null, args); 155 } catch (InvocationTargetException ite) { 156 throw new SystemException(ite.getTargetException()); 157 } catch (RuntimeException ex) { 158 throw ex; 159 } catch (Exception ex) { 160 throw new SystemException(ex); 161 } 162 } 163 } 164 | Popular Tags |