KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > geronimo > interop > util > JavaMethod


1 /**
2  *
3  * Copyright 2004-2005 The Apache Software Foundation
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.geronimo.interop.util;
19
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21 import java.lang.reflect.Method JavaDoc;
22 import java.lang.reflect.Modifier JavaDoc;
23 import java.util.HashMap JavaDoc;
24
25 import org.apache.geronimo.interop.SystemException;
26
27
28 /**
29  * * Utility methods for obtaining method signatures and calling static
30  * * methods on dynamically loaded classes.
31  */

32 public class JavaMethod {
33     private static HashMap JavaDoc _methodMap = new HashMap JavaDoc();
34
35     public static Method JavaDoc[] add(Method JavaDoc m, Method JavaDoc[] a) {
36         Method JavaDoc[] b = new Method JavaDoc[a.length + 1];
37         System.arraycopy(a, 0, b, 0, a.length);
38         b[a.length] = m;
39         return b;
40     }
41
42     /**
43      * * Return the short signature of a method.
44      * * A short signature is "method-name(parameter-type, ...)".
45      */

46     public static String JavaDoc getShortSignature(String JavaDoc methodName, Class JavaDoc[] parameterTypes) {
47         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
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 JavaDoc getShortSignature(Method JavaDoc m) {
62         return getShortSignature(m.getName(), m.getParameterTypes());
63     }
64
65     /**
66      * * Return the long signature of a method.
67      * * A long signature is "return-type class-name.method-name(parameter-type, ...)".
68      */

69     public static String JavaDoc getLongSignature(Class JavaDoc returnType, String JavaDoc className, String JavaDoc methodName, Class JavaDoc[] parameterTypes) {
70         return JavaType.getName(returnType) + " " + className + "." + getShortSignature(methodName, parameterTypes);
71     }
72
73     public static String JavaDoc getLongSignature(Method JavaDoc m) {
74         return getLongSignature(m.getReturnType(), m.getDeclaringClass().getName(), m.getName(), m.getParameterTypes());
75     }
76
77     public static String JavaDoc getLongSignature(Class JavaDoc c, Method JavaDoc m) {
78         return getLongSignature(m.getReturnType(), c.getName(), m.getName(), m.getParameterTypes());
79     }
80
81     public static Method JavaDoc getMethod(String JavaDoc methodSignature) {
82         Method JavaDoc method = (Method JavaDoc) _methodMap.get(methodSignature);
83         if (method == null) {
84             synchronized (_methodMap) {
85                 method = (Method JavaDoc) _methodMap.get(methodSignature);
86                 if (method == null) {
87                     int parenPos = methodSignature.indexOf('(');
88                     if (parenPos == -1) {
89                         throw new IllegalArgumentException JavaDoc("methodSignature = " + methodSignature);
90                     }
91                     String JavaDoc fullMethodName = methodSignature.substring(0, parenPos);
92                     String JavaDoc className = JavaClass.getNamePrefix(fullMethodName);
93                     String JavaDoc methodName = JavaClass.getNameSuffix(fullMethodName);
94                     String JavaDoc parameters = methodSignature.substring(parenPos);
95                     String JavaDoc shortSig = methodName + parameters;
96                     Class JavaDoc theClass = ThreadContext.loadClass(className);
97                     Method JavaDoc[] 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 JavaDoc("method = " + methodSignature + " (not found)");
111         }
112         return method;
113     }
114
115     public static Method JavaDoc getInstanceMethod(String JavaDoc methodSignature) {
116         Method JavaDoc method = getMethod(methodSignature);
117         if (Modifier.isStatic(method.getModifiers())) {
118             throw new IllegalArgumentException JavaDoc("method = " + methodSignature + " (static)");
119         }
120         return method;
121     }
122
123     public static Method JavaDoc getStaticMethod(String JavaDoc methodSignature) {
124         Method JavaDoc method = getMethod(methodSignature);
125         if (!Modifier.isStatic(method.getModifiers())) {
126             throw new IllegalArgumentException JavaDoc("method = " + methodSignature + " (not static)");
127         }
128         return method;
129     }
130
131     public static Object JavaDoc invokeStatic(String JavaDoc methodSignature, Object JavaDoc p1) {
132         return invokeStatic(methodSignature, new Object JavaDoc[]
133         {
134             p1
135         });
136     }
137
138     public static Object JavaDoc invokeStatic(String JavaDoc methodSignature, Object JavaDoc p1, Object JavaDoc p2) {
139         return invokeStatic(methodSignature, new Object JavaDoc[]
140         {
141             p1, p2
142         });
143     }
144
145     public static Object JavaDoc invokeStatic(String JavaDoc methodSignature, Object JavaDoc p1, Object JavaDoc p2, Object JavaDoc p3) {
146         return invokeStatic(methodSignature, new Object JavaDoc[]
147         {
148             p1, p2, p3
149         });
150     }
151
152     public static Object JavaDoc invokeStatic(String JavaDoc methodSignature, Object JavaDoc[] args) {
153         try {
154             return getStaticMethod(methodSignature).invoke(null, args);
155         } catch (InvocationTargetException JavaDoc ite) {
156             throw new SystemException(ite.getTargetException());
157         } catch (RuntimeException JavaDoc ex) {
158             throw ex;
159         } catch (Exception JavaDoc ex) {
160             throw new SystemException(ex);
161         }
162     }
163 }
164
Popular Tags