1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.runtime; 21 22 import java.util.Vector ; 23 import java.lang.reflect.Modifier ; 24 import java.lang.reflect.Constructor ; 25 import java.lang.reflect.InvocationTargetException ; 26 import java.lang.reflect.Method ; 27 import java.lang.IllegalAccessException ; 28 import java.lang.IllegalArgumentException ; 29 import java.lang.InstantiationException ; 30 31 32 35 public final class CallFunction { 36 37 public static String className; 38 public static String methodName; 39 public static int nArgs; 40 public static Class clazz; 41 42 43 public static String invokeMethod(String _className, String _methodName, Object [] _arguments){ 44 className = _className; 45 methodName = _methodName; 46 int size = _arguments.length-1; 47 Object [] arguments = new Object [size]; 48 Object object= _arguments[0]; 49 clazz =null; 50 try { 51 clazz = ObjectFactory.findProviderClass(className, ObjectFactory.findClassLoader(), true); 52 if (clazz == null) { 53 throw new RuntimeException ("Couldn't load the class"); 54 } 55 }catch (ClassNotFoundException e) { 56 throw new RuntimeException ("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 method; 65 if((method = findMethods(arguments)) == null){ 66 throw new RuntimeException ("Method not found"); 67 } 68 69 try{ 70 Object obj = method.invoke(object,arguments); 71 return obj.toString() ; 72 } 73 catch(IllegalAccessException e){ 74 throw new RuntimeException ("Error: Method is inaccessible"); 75 } 76 catch(IllegalArgumentException e){ 77 throw new RuntimeException ("Error: Number of actual and formal argument differ "); 78 } 79 catch(InvocationTargetException e){ 80 throw new RuntimeException ("Error: underlying constructor throws an exception "); 81 } 82 } 83 else { 84 Constructor constructor; 85 if((constructor = findConstructor(arguments)) == null){ 86 throw new RuntimeException ("Constructor not found"); 87 } 88 try{ 89 Object obs = constructor.newInstance(arguments); 90 return obs.toString() ; 91 }catch(InvocationTargetException e){ 92 throw new RuntimeException ("Error: constructor throws an exception "); 93 } 94 catch(IllegalAccessException e){ 95 throw new RuntimeException ("Error: constructor is inaccessible"); 96 } 97 catch(IllegalArgumentException e){ 98 throw new RuntimeException ("Error: Number of actual and formal argument differ "); 99 } 100 catch(InstantiationException e){ 101 throw new RuntimeException ("Error: Class that declares the underlying constructor represents an abstract class"); 102 } 103 } 104 105 } 106 107 110 private static Constructor findConstructor(Object [] arguments) { 111 Vector constructors =null; 112 113 114 final Constructor [] c_constructors = clazz.getConstructors(); 115 116 for (int i = 0; i < c_constructors.length; i++) { 117 final int mods = c_constructors[i].getModifiers(); 118 if (Modifier.isPublic(mods) && c_constructors[i].getParameterTypes().length == nArgs){ 120 if (constructors == null) { 121 constructors = new Vector (); 122 } 123 constructors.addElement(c_constructors[i]); 124 } 125 } 126 127 128 if (constructors == null) { 129 throw new RuntimeException ("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 final Constructor constructor = (Constructor )constructors.elementAt(i); 138 final Class [] paramTypes = constructor.getParameterTypes(); 139 140 for (j = 0; j < nArgs; j++) { 141 Class 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 164 private static Method findMethods(Object [] arguments) { 165 Vector methods = null; 166 167 final Method [] m_methods = clazz.getMethods(); 168 169 for (int i = 0; i < m_methods.length; i++){ 170 final int mods = m_methods[i].getModifiers(); 171 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 (); 177 } 178 methods.addElement(m_methods[i]); 179 } 180 } 181 182 183 if (methods == null) { 184 throw new RuntimeException ("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 final Method method = (Method )methods.elementAt(i); 192 final Class [] paramTypes = method.getParameterTypes(); 193 194 for (j = 0; j < nArgs; j++) { 195 Class 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 isPrimitive(Class paramType, Object argument){ 220 221 if( argument.getClass() == Integer .class ) 222 return typeCast(paramType,(Integer )argument); 223 else if( argument.getClass() == Float .class ) 224 return typeCast(paramType,(Float )argument); 225 else if( argument.getClass() == Double .class ) 226 return typeCast(paramType,(Double )argument); 227 else if( argument.getClass() == Long .class ) 228 return typeCast(paramType,(Long )argument); 229 else if( argument.getClass() == Boolean .class ) 230 return (Boolean )argument; 231 else if( argument.getClass() == Byte .class ) 232 return (Byte )argument; 233 else 234 return null; 235 } 236 237 static Object typeCast(Class paramType, Double object){ 238 if (paramType == Long.TYPE) 239 return new Long (object.longValue()); 240 else if (paramType == Integer.TYPE) 241 return new Integer (object.intValue()); 242 else if (paramType == Float.TYPE) 243 return new Float (object.floatValue()); 244 else if (paramType == Short.TYPE) 245 return new Short (object.shortValue()); 246 else if (paramType == Byte.TYPE) 247 return new Byte (object.byteValue()); 248 else 249 return object; 250 } 251 252 static Object typeCast(Class paramType, Long object){ 253 if (paramType == Integer.TYPE) 254 return new Integer (object.intValue()); 255 else if (paramType == Float.TYPE) 256 return new Float (object.floatValue()); 257 else if (paramType == Short.TYPE) 258 return new Short (object.shortValue()); 259 else if (paramType == Byte.TYPE) 260 return new Byte (object.byteValue()); 261 else 262 return object; 263 } 264 265 static Object typeCast(Class paramType, Integer object){ 266 if(paramType == Double.TYPE) 267 return new Double (object.doubleValue()); 268 else if (paramType == Float.TYPE) 269 return new Float (object.floatValue()); 270 else if (paramType == Short.TYPE) 271 return new Short (object.shortValue()); 272 else if (paramType == Byte.TYPE) 273 return new Byte (object.byteValue()); 274 else 275 return object; 276 } 277 278 static Object typeCast(Class paramType, Float object){ 279 if(paramType == Double.TYPE) 280 return new Double (object.doubleValue()); 281 else if (paramType == Integer.TYPE) 282 return new Float (object.intValue()); 283 else if (paramType == Short.TYPE) 284 return new Short (object.shortValue()); 285 else if (paramType == Byte.TYPE) 286 return new Byte (object.byteValue()); 287 else 288 return object; 289 } 290 291 } 292 | Popular Tags |