1 25 26 package net.yagga.util; 27 28 import java.lang.reflect.*; 29 33 public class MetaUt { 34 35 52 public static Object simpleInvokeStaticMethod(Class _class, String method, Object [] args){ 53 return simpleInvokeMethod(_class,null,method,args,true); 54 } 55 60 public static Object simpleInvokeMethod(Class _class, Object target, String method, Object [] args){ 61 return simpleInvokeMethod(_class,target,method,args,false); 62 } 63 64 82 public static Object simpleInvokeMethod(Class _class, Object target, String method, Object [] args, boolean _static) 83 { 84 Class cl=_class; 85 String 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 93 if(_static && !Modifier.isStatic(methods[i].getModifiers())){ 94 Ut.error("Method '"+className+"::"+method+"' MUST be static!"); 95 return null; 96 } 97 Class [] params=methods[i].getParameterTypes(); 99 if(params.length==args.length) 100 { 101 102 Object [] actualParams=new Object [params.length]; 103 for(int j=0;j<params.length;j++) 106 { 107 Object arg=args[j]; 109 110 String name=params[j].getName(); 112 if(arg.getClass().getName().equals(name)){ 113 actualParams[j]=arg; 114 continue; 115 } 116 String str= (String )arg.toString(); 118 try{ 119 if(name.equals("java.lang.Integer") || name.equals("int")){ 120 try{ 122 actualParams[j]=new Integer (str); 123 }catch(NumberFormatException nfe){ 124 Float f=new Float (str); 126 actualParams[j]=new Integer (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 actualParams[j]=new Float (str); 133 } 134 else if(name.equals("java.lang.Boolean") || name.equals("boolean")){ 135 actualParams[j]=new Boolean (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 e){ 146 Ut.error("Wrong numeric parameter :'"+arg+"' as "+(j+1)+"th param in "+className+"::"+method); 147 actualParams[j]=new Integer (0); 148 } 149 catch(ClassCastException 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 } try{ 156 Object ret=methods[i].invoke(target,actualParams); 157 found=true; 158 return ret; 159 }catch(IllegalAccessException iae){ 160 Ut.error("Cannot access '"+className+"::"+method+"': "+iae); 161 return null; 162 }catch(IllegalArgumentException 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 } } 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 |