// XAAL toolkit // Copyright (C) 2009 Ville Karavirta // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . package xaal.util; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; /** * @author vkaravir * * To change the template for this generated type comment go to * Window>Preferences>Java>Code Generation>Code and Comments */ public class ReflectionHelper { /** * */ public ReflectionHelper() { super(); // TODO Auto-generated constructor stub } /** * @param prop * @param class1 * @return */ public static Method getSetterMethod(String prop, Class cls) { String name = "set" + prop.substring(0, 1).toUpperCase() + prop.substring(1); Method[] m = cls.getMethods(); int i = 0; while (!m[i].getName().equalsIgnoreCase(name)) { i++; if (i == m.length) return null; } return m[i]; } /** * @param m * @param property * @return * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ public static boolean invoke(Method m, String property, Object target) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException { Class[] types = m.getParameterTypes(); if (types.length > 1) return false; if (types.length == 0) { m.invoke(target, null); return true; } Object[] param = null; if (types[0].isAssignableFrom(Integer.TYPE)) { // int param = new Object[] { new Integer(property) }; } else if (types[0].isAssignableFrom(property.getClass())) { // String param = new Object[] { property }; } else if (types[0].isAssignableFrom(Boolean.TYPE)) { // boolean param = new Object[] { Boolean.valueOf(property) }; } m.invoke(target, param); return true; } public static Object invoke(Object param, Object target, String method) { Class c = param.getClass(); Method m = getMethod(new Class[] { c }, target.getClass(), method); while (m == null && c != null) { c = c.getSuperclass(); m = getMethod(new Class[] { c }, target.getClass(), method); } try { if (m != null) return m.invoke(target, new Object[] { param }); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static Object invoke(Object[] param, Object target, String method) { Class[] c = new Class[param.length]; for (int i = 0; i < param.length; i++) c[i] = param[i].getClass(); Method m = getMethod(c, target.getClass(), method); // TODO Enable this /* while (m == null) { c = c.getSuperclass(); m = getMethod(c, target.getClass(), method); }*/ try { return m.invoke(target, param); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } private static Method getMethod(Class[] param, Class cls, String method) { try { Method m = cls.getMethod(method, param); return m; } catch (SecurityException e) { //e.printStackTrace(); return null; } catch (NoSuchMethodException e) { //e.printStackTrace(); return null; } } public static Object getInstanceOfClass(String className) { try { return Class.forName(className).newInstance(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; // TODO Constructors with parameters?? } }