KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > runtime > CallFunction


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

16 /*
17  * $Id:
18  */

19
20 package org.apache.xalan.xsltc.runtime;
21
22 import java.util.Vector JavaDoc;
23 import java.lang.reflect.Modifier JavaDoc;
24 import java.lang.reflect.Constructor JavaDoc;
25 import java.lang.reflect.InvocationTargetException JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.IllegalAccessException JavaDoc;
28 import java.lang.IllegalArgumentException JavaDoc;
29 import java.lang.InstantiationException JavaDoc;
30
31
32 /**
33  * Resolve the function dynamically
34  */

35 public final class CallFunction {
36
37     public static String JavaDoc className;
38     public static String JavaDoc methodName;
39     public static int nArgs;
40     public static Class JavaDoc clazz;
41
42
43     public static String JavaDoc invokeMethod(String JavaDoc _className, String JavaDoc _methodName, Object JavaDoc [] _arguments){
44         className = _className;
45         methodName = _methodName;
46         int size = _arguments.length-1;
47         Object JavaDoc [] arguments = new Object JavaDoc[size];
48         Object JavaDoc object= _arguments[0];
49         clazz =null;
50         try {
51             clazz = ObjectFactory.findProviderClass(className, ObjectFactory.findClassLoader(), true);
52             if (clazz == null) {
53                    throw new RuntimeException JavaDoc("Couldn't load the class");
54             }
55         }catch (ClassNotFoundException JavaDoc e) {
56             throw new RuntimeException JavaDoc("Couldn't load the class");
57         }
58       
59         for(int i=0,j=1;i<size;i++,++j){
60              arguments[i] = _arguments[j];
61         }
62         nArgs = size;
63         if( methodName != null ){
64               Method JavaDoc method;
65               if((method = findMethods(arguments)) == null){
66                   throw new RuntimeException JavaDoc("Method not found");
67               }
68
69               try{
70                  Object JavaDoc obj = method.invoke(object,arguments);
71                  return obj.toString() ;
72               }
73               catch(IllegalAccessException JavaDoc e){
74                   throw new RuntimeException JavaDoc("Error: Method is inaccessible");
75               }
76               catch(IllegalArgumentException JavaDoc e){
77                    throw new RuntimeException JavaDoc("Error: Number of actual and formal argument differ ");
78               }
79               catch(InvocationTargetException JavaDoc e){
80                    throw new RuntimeException JavaDoc("Error: underlying constructor throws an exception ");
81               }
82         }
83         else {
84             Constructor JavaDoc constructor;
85             if((constructor = findConstructor(arguments)) == null){
86                 throw new RuntimeException JavaDoc("Constructor not found");
87             }
88             try{
89                Object JavaDoc obs = constructor.newInstance(arguments);
90                return obs.toString() ;
91             }catch(InvocationTargetException JavaDoc e){
92                 throw new RuntimeException JavaDoc("Error: constructor throws an exception ");
93             }
94             catch(IllegalAccessException JavaDoc e){
95                 throw new RuntimeException JavaDoc("Error: constructor is inaccessible");
96             }
97             catch(IllegalArgumentException JavaDoc e){
98                 throw new RuntimeException JavaDoc("Error: Number of actual and formal argument differ ");
99             }
100             catch(InstantiationException JavaDoc e){
101                 throw new RuntimeException JavaDoc("Error: Class that declares the underlying constructor represents an abstract class");
102             }
103         }
104
105     }
106
107     /**
108      * Returns a Constructor
109      */

110     private static Constructor JavaDoc findConstructor(Object JavaDoc[] arguments) {
111         Vector JavaDoc constructors =null;
112
113
114             final Constructor JavaDoc[] c_constructors = clazz.getConstructors();
115
116             for (int i = 0; i < c_constructors.length; i++) {
117                 final int mods = c_constructors[i].getModifiers();
118                 // Is it public, static and same number of args ?
119
if (Modifier.isPublic(mods) && c_constructors[i].getParameterTypes().length == nArgs){
120                     if (constructors == null) {
121                         constructors = new Vector JavaDoc();
122                     }
123                     constructors.addElement(c_constructors[i]);
124                 }
125             }
126
127
128         if (constructors == null) {
129         // Method not found in this class
130
throw new RuntimeException JavaDoc("CONSTRUCTOR_NOT_FOUND_ERR" + className +":"+ methodName);
131     }
132
133         int nConstructors = constructors.size();
134         boolean accept=false;
135         for (int j, i = 0; i < nConstructors; i++) {
136         // Check if all parameters to this constructor can be converted
137
final Constructor JavaDoc constructor = (Constructor JavaDoc)constructors.elementAt(i);
138         final Class JavaDoc[] paramTypes = constructor.getParameterTypes();
139
140             for (j = 0; j < nArgs; j++) {
141                 Class JavaDoc argumentClass = arguments[j].getClass();
142                 if (argumentClass == paramTypes[j]){
143                     accept= true;
144                 }
145                 else if(argumentClass.isAssignableFrom(paramTypes[j])){
146                     accept=true;
147                 }
148                 else {
149                      accept =false;
150                      break;
151                 }
152           }
153           if (accept)
154               return constructor;
155         }
156         return null;
157     }
158
159
160
161     /**
162      * Return the Method
163      */

164     private static Method JavaDoc findMethods(Object JavaDoc[] arguments) {
165         Vector JavaDoc methods = null;
166
167             final Method JavaDoc[] m_methods = clazz.getMethods();
168
169             for (int i = 0; i < m_methods.length; i++){
170           final int mods = m_methods[i].getModifiers();
171           // Is it public and same number of args ?
172
if( Modifier.isPublic(mods)
173                      && m_methods[i].getName().equals(methodName)
174                      && m_methods[i].getParameterTypes().length == nArgs){
175             if (methods == null){
176                            methods = new Vector JavaDoc();
177                     }
178                     methods.addElement(m_methods[i]);
179         }
180             }
181
182
183          if (methods == null) {
184         // Method not found in this class
185
throw new RuntimeException JavaDoc("METHOD_NOT_FOUND_ERR" + className +":"+ methodName);
186     }
187         int nMethods = methods.size();
188         boolean accept=false;
189         for (int j, i = 0; i < nMethods; i++) {
190         // Check if all parameters to this constructor can be converted
191
final Method JavaDoc method = (Method JavaDoc)methods.elementAt(i);
192         final Class JavaDoc[] paramTypes = method.getParameterTypes();
193
194             for (j = 0; j < nArgs; j++) {
195                 Class JavaDoc argumentClass = arguments[j].getClass();
196                 if (argumentClass == paramTypes[j]){
197                     accept= true;
198                 }
199                 else if(argumentClass.isAssignableFrom(paramTypes[j])){
200                     accept=true;
201                 }
202                 else if(paramTypes[j].isPrimitive() ){
203                     arguments[j] = isPrimitive(paramTypes[j],arguments[j]);
204                     accept = true;
205                 }
206                 else {
207                     accept =false;
208                     break;
209                 }
210
211             }
212             if (accept)
213                 return method;
214         }
215
216         return null;
217     }
218
219     public static Object JavaDoc isPrimitive(Class JavaDoc paramType, Object JavaDoc argument){
220
221         if( argument.getClass() == Integer JavaDoc.class )
222              return typeCast(paramType,(Integer JavaDoc)argument);
223         else if( argument.getClass() == Float JavaDoc.class )
224              return typeCast(paramType,(Float JavaDoc)argument);
225         else if( argument.getClass() == Double JavaDoc.class )
226              return typeCast(paramType,(Double JavaDoc)argument);
227         else if( argument.getClass() == Long JavaDoc.class )
228              return typeCast(paramType,(Long JavaDoc)argument);
229         else if( argument.getClass() == Boolean JavaDoc.class )
230               return (Boolean JavaDoc)argument;
231         else if( argument.getClass() == Byte JavaDoc.class )
232              return (Byte JavaDoc)argument;
233         else
234             return null;
235     }
236
237     static Object JavaDoc typeCast(Class JavaDoc paramType, Double JavaDoc object){
238         if (paramType == Long.TYPE)
239             return new Long JavaDoc(object.longValue());
240         else if (paramType == Integer.TYPE)
241             return new Integer JavaDoc(object.intValue());
242         else if (paramType == Float.TYPE)
243             return new Float JavaDoc(object.floatValue());
244         else if (paramType == Short.TYPE)
245             return new Short JavaDoc(object.shortValue());
246         else if (paramType == Byte.TYPE)
247             return new Byte JavaDoc(object.byteValue());
248         else
249             return object;
250     }
251
252     static Object JavaDoc typeCast(Class JavaDoc paramType, Long JavaDoc object){
253         if (paramType == Integer.TYPE)
254             return new Integer JavaDoc(object.intValue());
255         else if (paramType == Float.TYPE)
256             return new Float JavaDoc(object.floatValue());
257         else if (paramType == Short.TYPE)
258             return new Short JavaDoc(object.shortValue());
259         else if (paramType == Byte.TYPE)
260             return new Byte JavaDoc(object.byteValue());
261         else
262             return object;
263     }
264
265     static Object JavaDoc typeCast(Class JavaDoc paramType, Integer JavaDoc object){
266         if(paramType == Double.TYPE)
267             return new Double JavaDoc(object.doubleValue());
268         else if (paramType == Float.TYPE)
269             return new Float JavaDoc(object.floatValue());
270         else if (paramType == Short.TYPE)
271             return new Short JavaDoc(object.shortValue());
272         else if (paramType == Byte.TYPE)
273             return new Byte JavaDoc(object.byteValue());
274         else
275             return object;
276     }
277
278     static Object JavaDoc typeCast(Class JavaDoc paramType, Float JavaDoc object){
279         if(paramType == Double.TYPE)
280             return new Double JavaDoc(object.doubleValue());
281         else if (paramType == Integer.TYPE)
282             return new Float JavaDoc(object.intValue());
283         else if (paramType == Short.TYPE)
284             return new Short JavaDoc(object.shortValue());
285         else if (paramType == Byte.TYPE)
286             return new Byte JavaDoc(object.byteValue());
287         else
288             return object;
289     }
290
291 }
292
Popular Tags