KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > yagga > util > MetaUt


1 /*
2  * This file is part of MiniInstaller, a self installer builder for Java
3  * Copyright (C) 2002 Walter Gamba
4  * mailto:walter@yagga.net
5  * http://www.yagga.net/java/miniinstaller
6  *
7  * MiniInstaller is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * MiniInstaller is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  *
21  * As the time of writing, the GNU General Public Licene can be
22  * found at http://www.gnu.org/licenses/gpl.txt
23  *
24  */

25
26 package net.yagga.util;
27
28 import java.lang.reflect.*;
29 /**
30  * Utilities for Meta classes, helper for Reflection and so on
31  * @author Walter Gamba
32  */

33 public class MetaUt {
34
35   /**
36    * Invokes a static method of a given class and given object.
37    * The class must define this static
38    * method which takes any number of arguments
39    * and can return anything.
40    * No arrays if not of the appropraite type needed by the method, i.e. no
41     * conversion is attempted (example from array of String to array of Integers)<BR>
42    * Please note that no overloading/polymorohism of methods is used. The first method that
43    * has the same name as the one passed, and the same number of arguments, is used.<BR>
44    * And of course,
45    * "first" has no predictable meaning, so please be sure that you don't have ambiguities.
46    * @param _class an Object representing the Class of target
47    * @param method the name of the method to invoke
48    * @param args the arguments to the method of this class of the jar file, as Objects
49    * @return an Object containing the return value or values.
50    * @see #simpleInvokeMethod for description
51    */

52   public static Object JavaDoc simpleInvokeStaticMethod(Class JavaDoc _class, String JavaDoc method, Object JavaDoc[] args){
53     return simpleInvokeMethod(_class,null,method,args,true);
54   }
55     /**
56      * Invokes a method of a given class and given object.
57      * By default non static methods can be invoked
58      * @see #simpleInvokeMethod for description
59      */

60     public static Object JavaDoc simpleInvokeMethod(Class JavaDoc _class, Object JavaDoc target, String JavaDoc method, Object JavaDoc[] args){
61       return simpleInvokeMethod(_class,target,method,args,false);
62     }
63
64     /**
65      * Invokes a method of a given class and given object.
66      * The class must define this
67      * method which takes any number of arguments
68      * and can return anything.
69      * No arrays if not of the appropraite type needed by the method, i.e. no
70      * conversion is attempted (example from array of String to array of Integers)<BR>
71   * Please note that no overloading/polymorohism of methods is used. The first method that
72   * has the same name as the one passed, and the same number of arguments, is used.<BR>
73   * And of course,
74   * "first" has no predictable meaning, so please be sure that you don't have ambiguities.
75      * @param _class an Object representing the Class of target
76      * @param target the object on to which to invoke method. Can be null if method is static
77      * @param method the name of the method to invoke
78      * @param args the arguments to the method of this class of the jar file, as Object
79      * @param _static boolean. If true method MUST be static, else no check is made
80      * @return an Object containing th return value or values.
81      */

82     public static Object JavaDoc simpleInvokeMethod(Class JavaDoc _class, Object JavaDoc target, String JavaDoc method, Object JavaDoc[] args, boolean _static)
83     {
84     Class JavaDoc cl=_class;
85     String JavaDoc className=_class.getName();
86     boolean found=false;
87     Method[] methods=cl.getMethods();
88     for(int i=0;i<methods.length;i++)
89         if(methods[i].getName().equals(method))
90         {
91             //controllo che sia statico
92

93             if(_static && !Modifier.isStatic(methods[i].getModifiers())){
94                 Ut.error("Method '"+className+"::"+method+"' MUST be static!");
95                 return null;
96             }
97             //check arguments
98
Class JavaDoc[] params=methods[i].getParameterTypes();
99             if(params.length==args.length)
100             {
101
102                 Object JavaDoc[] actualParams=new Object JavaDoc[params.length];
103                 //every param if differs from appropriate type
104
//is cast to String and then converted to appropraite type
105
for(int j=0;j<params.length;j++)
106                 {
107                     //String arg=args[j];
108
Object JavaDoc arg=args[j];
109
110                     //
111
String JavaDoc name=params[j].getName();
112                     if(arg.getClass().getName().equals(name)){
113                       actualParams[j]=arg;
114                       continue;
115                     }
116                     //if not sam type.. try to convert to String
117
String JavaDoc str= (String JavaDoc)arg.toString();
118                     try{
119                         if(name.equals("java.lang.Integer") || name.equals("int")){
120                             //cast to integer...//
121
try{
122                                 actualParams[j]=new Integer JavaDoc(str);
123                             }catch(NumberFormatException JavaDoc nfe){
124                                 //if error.. try to truncate resulting float
125
Float JavaDoc f=new Float JavaDoc(str);
126                                 actualParams[j]=new Integer JavaDoc(f.intValue());
127                             }
128                         }
129                         else if(name.equals("java.lang.Float") || name.equals("float")
130                         || name.equals("java.lang.Double") || name.equals("double")){
131                             //cast to float...
132
actualParams[j]=new Float JavaDoc(str);
133                         }
134                         else if(name.equals("java.lang.Boolean") || name.equals("boolean")){
135                             actualParams[j]=new Boolean JavaDoc(str);
136                         }
137                         else if(name.equals("java.lang.String")){
138                             actualParams[j]=str;
139                         }
140                         else{
141                             Ut.error("Unknown/Unsupported param of class '"+params[j].getName()+"' as "+(j+1)+"th param in "+className+"::"+method);
142                             actualParams[j]=null;
143                         }
144                     }
145                     catch(NumberFormatException JavaDoc e){
146                         Ut.error("Wrong numeric parameter :'"+arg+"' as "+(j+1)+"th param in "+className+"::"+method);
147                         actualParams[j]=new Integer JavaDoc(0);
148                     }
149                     catch(ClassCastException JavaDoc cce){
150                         Ut.error("Wrong class cast: expecting "+params[j].getName()+", found 'String' as "+(j+1)+"th param in "+className+"::"+method);
151                         actualParams[j]=null;
152                     }
153                 }//end for (param)
154
//now invoke method
155
try{
156                   Object JavaDoc ret=methods[i].invoke(target,actualParams);
157                   found=true;
158                   return ret;
159                 }catch(IllegalAccessException JavaDoc iae){
160                         Ut.error("Cannot access '"+className+"::"+method+"': "+iae);
161                         return null;
162                 }catch(IllegalArgumentException JavaDoc iae2){
163                         Ut.error("Wrong parameters in call to '"+className+"::"+method+"': "+ iae2);
164                         return null;
165                 }catch(InvocationTargetException ite){
166                         Ut.error("Exception during invokation of '"+className+"::"+method+"': "+ite.getTargetException());
167                         return null;
168                 }
169             } //end if param.length==
170
}//end name==
171
if(!found)
172       Ut.error("No method '"+className+"::"+method+"' found, or found with wrong number of parameters");
173     return null;
174   }
175
176 }
Popular Tags