免费源代码下载整理 - 云代码空间
—— 每天更新整理各种PHP、JSP、ASP源代码,敬请关注我的微博 http://weibo.com/freecodedownload
1 public Object getProperty(Object owner, String fieldName) throws Exception { 2 Class ownerClass = owner.getClass(); 3 4 Field field = ownerClass.getField(fieldName); 5 6 Object property = field.get(owner); 7 8 return property; 9 }
1 public Object getStaticProperty(String className, String fieldName) 2 throws Exception { 3 Class ownerClass = Class.forName(className); 4 5 Field field = ownerClass.getField(fieldName); 6 7 Object property = field.get(ownerClass); 8 9 return property; 10 }
1 public Object invokeMethod(Object owner, String methodName, Object[] args) throws Exception { 2 3 Class ownerClass = owner.getClass(); 4 5 Class[] argsClass = new Class[args.length]; 6 7 for (int i = 0, j = args.length; i < j; i++) { 8 argsClass[i] = args[i].getClass(); 9 } 10 11 Method method = ownerClass.getMethod(methodName, argsClass); 12 13 return method.invoke(owner, args); 14 }
1 public Object invokeStaticMethod(String className, String methodName, 2 Object[] args) throws Exception { 3 Class ownerClass = Class.forName(className); 4 5 Class[] argsClass = new Class[args.length]; 6 7 for (int i = 0, j = args.length; i < j; i++) { 8 argsClass[i] = args[i].getClass(); 9 } 10 11 Method method = ownerClass.getMethod(methodName, argsClass); 12 13 return method.invoke(null, args); 14 }
1 2 public Object newInstance(String className, Object[] args) throws Exception { 3 Class newoneClass = Class.forName(className); 4 5 Class[] argsClass = new Class[args.length]; 6 7 for (int i = 0, j = args.length; i < j; i++) { 8 argsClass[i] = args[i].getClass(); 9 } 10 11 Constructor cons = newoneClass.getConstructor(argsClass); 12 13 return cons.newInstance(args); 14 15 }