Merge branch 'develop'
This commit is contained in:
@@ -84,15 +84,39 @@ public class ReflectionObject {
|
||||
}
|
||||
}
|
||||
|
||||
public List<ReflectionMethod> methodsAllList() {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
Method[] declaredMethods = clazz.getMethods();
|
||||
if (declaredMethods.length > 0) {
|
||||
List<ReflectionMethod> result = new ArrayList<>(declaredMethods.length);
|
||||
|
||||
for (Method method : declaredMethods) {
|
||||
result.add(new ReflectionMethod(object, method));
|
||||
}
|
||||
|
||||
return result;
|
||||
} else {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public ReflectionMethod method(String methodName, Class<?>... parameterTypes) {
|
||||
final Class clazz = object.getClass();
|
||||
|
||||
Method method;
|
||||
try {
|
||||
return new ReflectionMethod(object, clazz.getDeclaredMethod(methodName, parameterTypes));
|
||||
method = clazz.getDeclaredMethod(methodName, parameterTypes);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ReflectionException(e);
|
||||
try {
|
||||
method = clazz.getMethod(methodName, parameterTypes);
|
||||
} catch (NoSuchMethodException e1) {
|
||||
throw new ReflectionException(e1);
|
||||
}
|
||||
}
|
||||
|
||||
return new ReflectionMethod(object, method);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -139,6 +139,23 @@ public class ReflectionObjectTest {
|
||||
assertNotNull(refMethod);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void method_generic() {
|
||||
SomeGenericObject<String, Integer> someGenericObject = new SomeGenericObject<>();
|
||||
String key = "KEY!";
|
||||
int value = 3306;
|
||||
|
||||
ReflectionObject refObj = new ReflectionObject(someGenericObject);
|
||||
|
||||
ReflectionMethod refMethod = refObj.method("genericMethod", Object.class, Object.class);
|
||||
assertNotNull(refMethod);
|
||||
|
||||
refMethod.invoke(key, value);
|
||||
assertFalse(someGenericObject.map.isEmpty());
|
||||
assertTrue(someGenericObject.map.containsKey(key));
|
||||
assertEquals(value, (int) someGenericObject.map.get(key));
|
||||
}
|
||||
|
||||
@Test(expected = ReflectionException.class)
|
||||
public void method_notExists() {
|
||||
ReflectionObject refObj = new ReflectionObject(new SomeObject());
|
||||
|
||||
13
src/test/java/ru/dmitriymx/reflection/SomeGenericObject.java
Normal file
13
src/test/java/ru/dmitriymx/reflection/SomeGenericObject.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package ru.dmitriymx.reflection;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class SomeGenericObject<K, V> {
|
||||
|
||||
public Map<K, V> map = new HashMap<>();
|
||||
|
||||
public void genericMethod(K key, V value) {
|
||||
map.put(key, value);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user