KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > server > core > jmx > Introspector


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 package com.sun.enterprise.admin.server.core.jmx;
25
26 //JDK imports
27
import java.util.Collection JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.ArrayList JavaDoc;
31 import java.lang.reflect.Modifier JavaDoc;
32 import java.lang.reflect.Method JavaDoc;
33 import java.lang.reflect.InvocationTargetException JavaDoc;
34
35 //Admin imports
36
import com.sun.enterprise.admin.common.ParamInfo;
37
38 //JMX imports
39
import javax.management.ReflectionException JavaDoc;
40 import javax.management.MBeanException JavaDoc;
41
42
43 /**
44     A class to provide various introspection routines. This class can be
45     enhanced to make it more specific for other types of objects like MBeans.
46     Provides basic capabilities to introspect any class.
47  
48     @author Kedar Mhaswade
49     @version
50 */

51 public class Introspector
52 {
53
54     protected Class JavaDoc mClassReflected = null;
55     
56     /**
57         Creates new Introspector for given class. The argument may not be
58         null.
59     */

60     public Introspector (Class JavaDoc aClass)
61     {
62         if (aClass == null)
63         {
64             throw new IllegalArgumentException JavaDoc();
65         }
66         mClassReflected = aClass;
67     }
68     
69     /**
70         Tests whether this class implements an interface represented by
71         the given Class Object. The Parameter may not be null. This class
72         implements an interface represented by parameter Class iff
73         <li> parameter is not null &&
74         <li> this class or any of its super classes implement the interface
75             represented by parameter.
76
77         @param aClass the Class that represents the class or interface for
78             which the test is carried out.
79         @return true if this Class implements given interface, false otherwise.
80     */

81     
82     public boolean implementsInterface(Class JavaDoc aClass)
83     {
84         boolean implInterface = false;
85         Class JavaDoc thisClass = mClassReflected;
86
87         while (aClass != null && thisClass != null && !implInterface)
88         {
89             Class JavaDoc[] interfaces = thisClass.getInterfaces();
90             for (int i = 0 ; i < interfaces.length ; i++)
91             {
92                 Class JavaDoc anInterface = interfaces[i];
93                 if (anInterface.getName().equals(aClass.getName()))
94                 {
95                     implInterface = true;
96                     break;
97                 }
98             }
99             thisClass = thisClass.getSuperclass();
100         }
101         return ( implInterface );
102     }
103     
104     /**
105         Tests whether this class extends the given class.
106         Note that no class extends itself.
107      
108         @param aClass a class
109         @return true if this class is a subclass of passed class, false otherwise.
110     */

111
112     public boolean extendsClass(Class JavaDoc aClass)
113     {
114         if (aClass == null)
115         {
116             return false;
117         }
118
119         boolean extendsClass = false;
120         Class JavaDoc superClass = mClassReflected.getSuperclass();
121
122         while (superClass != null && !extendsClass)
123         {
124             if (superClass.getName().equals(aClass.getName()))
125             {
126             extendsClass = true;
127             }
128             superClass = superClass.getSuperclass();
129         }
130
131         return ( extendsClass );
132     }
133     
134     /**
135         Returns a method in this class with given name and signature.
136         <p>
137         Note that the signature for primitive parameters is "int", "boolean",
138         "char" and so on.
139      
140         @param operationName String representing the name of method.
141         @param signature array of Strings, each of which represents fully
142             qualified class name of parameters in order.
143         @return instance of Method if present, null otherwise.
144         @throws ReflectionException if the class represented by any of the
145             signature elements can't be loaded or there is no such method
146             or there is a security constraint.
147     */

148     
149     public Method JavaDoc getMethod(String JavaDoc operationName, String JavaDoc[] signature)
150         throws ClassNotFoundException JavaDoc, NoSuchMethodException JavaDoc, SecurityException JavaDoc
151     {
152         Method JavaDoc method = null;
153         Class JavaDoc[] parameterTypes = null;
154         if (signature != null)
155         {
156             parameterTypes = new Class JavaDoc[signature.length];
157             for (int i = 0 ; i < signature.length ; i++)
158             {
159                 String JavaDoc parameterName = signature[i];
160                 Class JavaDoc primitiveClass = ParamInfo.getPrimitiveClass(parameterName);
161                 boolean parameterIsPrimitive = ( primitiveClass != null );
162                 if (parameterIsPrimitive)
163                 {
164                     parameterTypes[i] = primitiveClass;
165                 }
166                 else
167                 {
168                     parameterTypes[i] = Class.forName(parameterName);
169                 }
170             }
171         }
172         method = mClassReflected.getMethod(operationName, parameterTypes);
173         return ( method );
174     }
175     
176     /**
177         Invokes the given method on this class with given runtime instance
178         of target object and parameter instances of the method.
179      
180         @param method instance of Method object representing operation to invoke.
181         @param targetObject an instance of this class.
182         @param actualParams an array of actual parameters to this method.
183         @return object representing the return value of the method.
184         @throws ReflectionException if the method can't be invoked.
185         @throws MBeanException if the method ifself throws some exception.
186     */

187     
188     public Object JavaDoc invokeMethodOn(Method JavaDoc method, Object JavaDoc targetObject,
189                                      Object JavaDoc[] actualParams)
190             throws java.lang.IllegalAccessException JavaDoc,
191                    java.lang.reflect.InvocationTargetException JavaDoc
192     {
193         Object JavaDoc result = null;
194
195         if (method == null || targetObject == null)// || actualParams == null)
196
{
197             throw new IllegalArgumentException JavaDoc();
198         }
199         result = method.invoke(targetObject, actualParams);
200         return ( result );
201     }
202
203     /*
204         Returns instance of Class that represents the primitive type represented
205         by String like "int", "char", "boolean", "short", "long", "byte",
206         "float" and "double".
207         
208         @return the class as given by various TYPE fields in java.lang wrappers
209             over primitives, null if there is no match.
210     
211     
212     public static Class getPrimitiveClass(String name)
213     {
214         /*
215             This code contains getting the key mapped onto known value
216             in a hashmap.
217         Class primitiveClass = null;
218         HashMap primitiveMap = ParamInfo.mPrimitives;
219         Iterator primitiveClasses = primitiveMap.keySet().iterator();
220         while (primitiveClasses.hasNext())
221         {
222             Class aClass = (Class) primitiveClasses.next();
223             String className = (String) primitiveMap.get(aClass);
224             if (className.equals(name))
225             {
226                 primitiveClass = aClass;
227                 break;
228             }
229         }
230         return ( primitiveClass );
231     }
232     */

233     /**
234         Returns an array of Methods that are <code> callable </callable>
235         instance methods of this class/interface. Thus the returned array contains
236         <li>
237         declared non-abstract public methods
238         <li>
239         declared public methods of superclasses/superinterfaces excluding
240         the classes/interfaces from excludeList param
241      
242         @param excludeList Collection of complete classnames of those classes
243         whose methods should not be included in the returned array. If null,
244         all the methods will be returned. The excludeList can contain
245         the name of this class itself.
246         @return array of Method objects. It will contain zero elements, if
247         there is no method.
248     */

249     public Method JavaDoc[] getCallableInstanceMethods(Collection JavaDoc excludeList)
250     {
251         boolean includeAll = false;
252         if (excludeList == null || excludeList.isEmpty())
253         {
254             includeAll = true;
255         }
256         
257         ArrayList JavaDoc methodList = new ArrayList JavaDoc();
258         Class JavaDoc aClass = mClassReflected;
259         Method JavaDoc[] methods = null;
260         while (aClass != null)
261         {
262             boolean shouldInclude =
263                 includeAll || ! excludeList.contains(aClass.getName());
264             if (shouldInclude)
265             {
266                 Method JavaDoc[] declMethods = aClass.getDeclaredMethods();
267                 for (int i = 0 ; i < declMethods.length ; i++)
268                 {
269                     int modifiers = declMethods[i].getModifiers();
270                     boolean isCallableInstanceMethod = false;
271                     boolean isPublicMethod = Modifier.isPublic(modifiers);
272                     boolean isAbstractMethod = Modifier.isAbstract(modifiers);
273                     boolean isStaticMethod = Modifier.isStatic(modifiers);
274                     
275                     isCallableInstanceMethod = isPublicMethod && !isAbstractMethod
276                         && !isStaticMethod;
277                     if (isCallableInstanceMethod)
278                     {
279                         methodList.add(declMethods[i]);
280                     }
281                 }
282             }
283             aClass = aClass.getSuperclass();
284         }
285         methods = new Method JavaDoc[methodList.size()];
286         return ((Method JavaDoc[])methodList.toArray(methods));
287     }
288     
289     public Method JavaDoc[] getDeclaredConcretePublicMethods()
290     {
291         Method JavaDoc[] declMethods = mClassReflected.getDeclaredMethods();
292         ArrayList JavaDoc publicMethods = new ArrayList JavaDoc();
293         for (int i = 0 ; i < declMethods.length ; i++)
294         {
295             int modifiers = declMethods[i].getModifiers();
296             boolean isPublic = Modifier.isPublic (modifiers);
297             boolean isAbstract = Modifier.isAbstract(modifiers);
298             if (isPublic && !isAbstract)
299             {
300                 publicMethods.add(declMethods[i]);
301             }
302         }
303         Method JavaDoc[] pMethods = new Method JavaDoc[publicMethods.size()];
304         return ( (Method JavaDoc[])publicMethods.toArray (pMethods) );
305     }
306 }
Popular Tags