| 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 |
|
|
|
|
|
| 43.9% |
Uncovered Elements: 32 (57) |
Complexity: 13 |
Complexity Density: 0.37 |
|
| 12 |
|
public class ObjectUtil { |
| 13 |
|
|
| 14 |
|
private static Map<Class<?>, Method[]> methodCache = new HashMap<Class<?>, Method[]>(); |
| 15 |
|
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.5 |
|
| 16 |
0
|
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 |
|
|
|
|
|
| 51.1% |
Uncovered Elements: 23 (47) |
Complexity: 10 |
Complexity Density: 0.34 |
|
| 29 |
174
|
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 |
174
|
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 |
174
|
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 |
|
} |