KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gcc > util > JavaMethod


1 /*
2  * Copyright 2004 The Apache Software Foundation or its licensors, as
3  * applicable.
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
14  * implied.
15  *
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19 package gcc.util;
20
21 import gcc.*;
22 import java.lang.reflect.*;
23 import java.util.*;
24
25 /**
26  ** Utility methods for obtaining method signatures and calling static
27  ** methods on dynamically loaded classes.
28  **/

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

45     public static String getShortSignature(String methodName, Class[] parameterTypes)
46     {
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         {
53             if (i > 0)
54             {
55                 sb.append(", ");
56             }
57             sb.append(JavaType.getName(parameterTypes[i]));
58         }
59         sb.append(')');
60         return sb.toString();
61     }
62
63     public static String getShortSignature(Method m)
64     {
65         return getShortSignature(m.getName(), m.getParameterTypes());
66     }
67
68     /**
69      ** Return the long signature of a method.
70      ** A long signature is "return-type class-name.method-name(parameter-type, ...)".
71      **/

72     public static String getLongSignature(Class returnType, String className, String methodName, Class[] parameterTypes)
73     {
74         return JavaType.getName(returnType) + " " + className + "." + getShortSignature(methodName, parameterTypes);
75     }
76
77     public static String getLongSignature(Method m)
78     {
79         return getLongSignature(m.getReturnType(), m.getDeclaringClass().getName(), m.getName(), m.getParameterTypes());
80     }
81
82     public static String getLongSignature(Class c, Method m)
83     {
84         return getLongSignature(m.getReturnType(), c.getName(), m.getName(), m.getParameterTypes());
85     }
86
87     public static Method getMethod(String methodSignature)
88     {
89         Method method = (Method)_methodMap.get(methodSignature);
90         if (method == null)
91         {
92             synchronized (_methodMap)
93             {
94                 method = (Method)_methodMap.get(methodSignature);
95                 if (method == null)
96                 {
97                     int parenPos = methodSignature.indexOf('(');
98                     if (parenPos == -1)
99                     {
100                         throw new IllegalArgumentException("methodSignature = " + methodSignature);
101                     }
102                     String fullMethodName = methodSignature.substring(0, parenPos);
103                     String className = JavaClass.getNamePrefix(fullMethodName);
104                     String methodName = JavaClass.getNameSuffix(fullMethodName);
105                     String parameters = methodSignature.substring(parenPos);
106                     String shortSig = methodName + parameters;
107                     Class theClass = ThreadContext.loadClass(className);
108                     Method[] methods = theClass.getMethods();
109                     int n = methods.length;
110                     for (int i = 0; i < n; i++)
111                     {
112                         method = methods[i];
113                         if (shortSig.equals(JavaMethod.getShortSignature(method)))
114                         {
115                             _methodMap.put(methodSignature, method);
116                             break;
117                         }
118                     }
119                 }
120             }
121         }
122         if (method == null)
123         {
124             throw new IllegalArgumentException("method = " + methodSignature + " (not found)");
125         }
126         return method;
127     }
128
129     public static Method getInstanceMethod(String methodSignature)
130     {
131         Method method = getMethod(methodSignature);
132         if (Modifier.isStatic(method.getModifiers()))
133         {
134             throw new IllegalArgumentException("method = " + methodSignature + " (static)");
135         }
136         return method;
137     }
138
139     public static Method getStaticMethod(String methodSignature)
140     {
141         Method method = getMethod(methodSignature);
142         if (! Modifier.isStatic(method.getModifiers()))
143         {
144             throw new IllegalArgumentException("method = " + methodSignature + " (not static)");
145         }
146         return method;
147     }
148
149     public static Object invokeStatic(String methodSignature, Object p1)
150     {
151         return invokeStatic(methodSignature, new Object[]
152         {
153             p1
154         }
155         );
156     }
157
158     public static Object invokeStatic(String methodSignature, Object p1, Object p2)
159     {
160         return invokeStatic(methodSignature, new Object[]
161         {
162             p1, p2
163         }
164         );
165     }
166
167     public static Object invokeStatic(String methodSignature, Object p1, Object p2, Object p3)
168     {
169         return invokeStatic(methodSignature, new Object[]
170         {
171             p1, p2, p3
172         }
173         );
174     }
175
176     public static Object invokeStatic(String methodSignature, Object[] args)
177     {
178         try
179         {
180             return getStaticMethod(methodSignature).invoke(null, args);
181         }
182         catch (InvocationTargetException ite)
183         {
184             throw new SystemException(ite.getTargetException());
185         }
186         catch (RuntimeException ex)
187         {
188             throw ex;
189         }
190         catch (Exception ex)
191         {
192             throw new SystemException(ex);
193         }
194     }
195 }
196
Popular Tags