| 1 | |
package liquibase.util; |
| 2 | |
|
| 3 | |
import java.lang.reflect.InvocationTargetException; |
| 4 | |
import java.lang.reflect.Method; |
| 5 | |
import java.math.BigInteger; |
| 6 | |
import java.util.HashMap; |
| 7 | |
import java.util.Locale; |
| 8 | |
import java.util.Map; |
| 9 | |
|
| 10 | |
import liquibase.statement.DatabaseFunction; |
| 11 | |
|
| 12 | 0 | public class ObjectUtil { |
| 13 | |
|
| 14 | 1 | private static Map<Class<?>, Method[]> methodCache = new HashMap<Class<?>, Method[]>(); |
| 15 | |
|
| 16 | |
public static Object getProperty(Object object, String propertyName) throws IllegalAccessException, |
| 17 | |
InvocationTargetException { |
| 18 | 0 | String methodName = "get" + propertyName.substring(0, 1).toUpperCase(Locale.ENGLISH) |
| 19 | |
+ propertyName.substring(1); |
| 20 | 0 | Method[] methods = object.getClass().getMethods(); |
| 21 | 0 | for (Method method : methods) { |
| 22 | 0 | if (method.getName().equals(methodName) && method.getParameterTypes().length == 0) { |
| 23 | 0 | return method.invoke(object); |
| 24 | |
} |
| 25 | |
} |
| 26 | 0 | throw new RuntimeException("Property not found: " + propertyName); |
| 27 | |
} |
| 28 | |
|
| 29 | |
public static void setProperty(Object object, String propertyName, String propertyValue) |
| 30 | |
throws IllegalAccessException, InvocationTargetException { |
| 31 | 174 | String methodName = "set" + propertyName.substring(0, 1).toUpperCase(Locale.ENGLISH) |
| 32 | |
+ propertyName.substring(1); |
| 33 | |
Method[] methods; |
| 34 | |
|
| 35 | 174 | methods = methodCache.get(object.getClass()); |
| 36 | |
|
| 37 | 174 | if (methods == null) { |
| 38 | 7 | methods = object.getClass().getMethods(); |
| 39 | 7 | methodCache.put(object.getClass(), methods); |
| 40 | |
} |
| 41 | |
|
| 42 | 684 | for (Method method : methods) { |
| 43 | 684 | if (method.getName().equals(methodName)) { |
| 44 | 174 | Class<?> parameterType = method.getParameterTypes()[0]; |
| 45 | 174 | if (method.getParameterTypes().length == 1) { |
| 46 | 174 | if (parameterType.equals(Boolean.class)) { |
| 47 | 45 | method.invoke(object, Boolean.valueOf(propertyValue)); |
| 48 | 45 | return; |
| 49 | 129 | } else if (parameterType.equals(String.class)) { |
| 50 | 129 | method.invoke(object, propertyValue); |
| 51 | 129 | return; |
| 52 | 0 | } else if (parameterType.equals(Integer.class)) { |
| 53 | 0 | method.invoke(object, Integer.valueOf(propertyValue)); |
| 54 | 0 | return; |
| 55 | 0 | } else if (parameterType.equals(Long.class)) { |
| 56 | 0 | method.invoke(object, Long.valueOf(propertyValue)); |
| 57 | 0 | return; |
| 58 | 0 | } else if (parameterType.equals(BigInteger.class)) { |
| 59 | 0 | method.invoke(object, new BigInteger(propertyValue)); |
| 60 | 0 | return; |
| 61 | 0 | } else if (parameterType.equals(DatabaseFunction.class)) { |
| 62 | 0 | method.invoke(object, new DatabaseFunction(propertyValue)); |
| 63 | 0 | return; |
| 64 | |
} |
| 65 | |
} |
| 66 | |
} |
| 67 | |
} |
| 68 | 0 | throw new RuntimeException("Property '" + propertyName + "' not found on object type " |
| 69 | |
+ object.getClass().getName()); |
| 70 | |
} |
| 71 | |
} |