[LANG-908] get method override hierarchy
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1510301 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
019256539d
commit
609319df22
|
@ -19,9 +19,18 @@ package org.apache.commons.lang3.reflect;
|
|||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.TypeVariable;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.ClassUtils;
|
||||
import org.apache.commons.lang3.Validate;
|
||||
import org.apache.commons.lang3.ClassUtils.Interfaces;
|
||||
|
||||
/**
|
||||
* <p>Utility reflection methods focused on methods, originally from Commons BeanUtils.
|
||||
|
@ -518,4 +527,49 @@ public class MethodUtils {
|
|||
}
|
||||
return bestMatch;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hierarchy of overridden methods down to {@code result} respecting generics.
|
||||
* @param method lowest to consider
|
||||
* @param interfacesBehavior whether to search interfaces
|
||||
* @return Collection<Method> in ascending order from sub- to superclass
|
||||
*/
|
||||
public static Set<Method> getOverrideHierarchy(final Method method, Interfaces interfacesBehavior) {
|
||||
Validate.notNull(method);
|
||||
final Set<Method> result = new LinkedHashSet<Method>();
|
||||
result.add(method);
|
||||
|
||||
final Class<?>[] parameterTypes = method.getParameterTypes();
|
||||
|
||||
final Class<?> declaringClass = method.getDeclaringClass();
|
||||
|
||||
final Iterator<Class<?>> hierarchy = ClassUtils.hierarchy(declaringClass, interfacesBehavior).iterator();
|
||||
//skip the declaring class :P
|
||||
hierarchy.next();
|
||||
hierarchyTraversal: while (hierarchy.hasNext()) {
|
||||
final Class<?> c = hierarchy.next();
|
||||
final Method m = getMatchingAccessibleMethod(c, method.getName(), parameterTypes);
|
||||
if (m == null) {
|
||||
continue;
|
||||
}
|
||||
if (Arrays.equals(m.getParameterTypes(), parameterTypes)) {
|
||||
// matches without generics
|
||||
result.add(m);
|
||||
continue;
|
||||
}
|
||||
// necessary to get arguments every time in the case that we are including interfaces
|
||||
final Map<TypeVariable<?>, Type> typeArguments = TypeUtils.getTypeArguments(declaringClass, m.getDeclaringClass());
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
final Type childType = TypeUtils.unrollVariables(typeArguments, method.getGenericParameterTypes()[i]);
|
||||
final Type parentType = TypeUtils.unrollVariables(typeArguments, m.getGenericParameterTypes()[i]);
|
||||
if (!TypeUtils.equals(childType, parentType)) {
|
||||
continue hierarchyTraversal;
|
||||
}
|
||||
}
|
||||
result.add(m);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.apache.commons.lang3.reflect;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
@ -25,14 +26,20 @@ import static org.junit.Assert.assertTrue;
|
|||
import static org.junit.Assert.fail;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
|
||||
import org.apache.commons.lang3.ArrayUtils;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
import org.apache.commons.lang3.mutable.Mutable;
|
||||
import org.apache.commons.lang3.mutable.MutableObject;
|
||||
import org.apache.commons.lang3.ClassUtils.Interfaces;
|
||||
import org.apache.commons.lang3.reflect.testbed.GenericConsumer;
|
||||
import org.apache.commons.lang3.reflect.testbed.GenericParent;
|
||||
import org.apache.commons.lang3.reflect.testbed.StringParameterizedChild;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -377,6 +384,47 @@ public class MethodUtilsTest {
|
|||
singletonArray(null), singletonArray(String.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOverrideHierarchyIncludingInterfaces() {
|
||||
final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class);
|
||||
final Iterator<MethodDescriptor> expected =
|
||||
Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class),
|
||||
new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0]),
|
||||
new MethodDescriptor(GenericConsumer.class, "consume", GenericConsumer.class.getTypeParameters()[0]))
|
||||
.iterator();
|
||||
for (Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.INCLUDE)) {
|
||||
assertTrue(expected.hasNext());
|
||||
final MethodDescriptor md = expected.next();
|
||||
assertEquals(md.declaringClass, m.getDeclaringClass());
|
||||
assertEquals(md.name, m.getName());
|
||||
assertEquals(md.parameterTypes.length, m.getParameterTypes().length);
|
||||
for (int i = 0; i < md.parameterTypes.length; i++) {
|
||||
assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i]));
|
||||
}
|
||||
}
|
||||
assertFalse(expected.hasNext());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetOverrideHierarchyExcludingInterfaces() {
|
||||
final Method method = MethodUtils.getAccessibleMethod(StringParameterizedChild.class, "consume", String.class);
|
||||
final Iterator<MethodDescriptor> expected =
|
||||
Arrays.asList(new MethodDescriptor(StringParameterizedChild.class, "consume", String.class),
|
||||
new MethodDescriptor(GenericParent.class, "consume", GenericParent.class.getTypeParameters()[0]))
|
||||
.iterator();
|
||||
for (Method m : MethodUtils.getOverrideHierarchy(method, Interfaces.EXCLUDE)) {
|
||||
assertTrue(expected.hasNext());
|
||||
final MethodDescriptor md = expected.next();
|
||||
assertEquals(md.declaringClass, m.getDeclaringClass());
|
||||
assertEquals(md.name, m.getName());
|
||||
assertEquals(md.parameterTypes.length, m.getParameterTypes().length);
|
||||
for (int i = 0; i < md.parameterTypes.length; i++) {
|
||||
assertTrue(TypeUtils.equals(md.parameterTypes[i], m.getGenericParameterTypes()[i]));
|
||||
}
|
||||
}
|
||||
assertFalse(expected.hasNext());
|
||||
}
|
||||
|
||||
private void expectMatchingAccessibleMethodParameterTypes(final Class<?> cls,
|
||||
final String methodName, final Class<?>[] requestTypes, final Class<?>[] actualTypes) {
|
||||
final Method m = MethodUtils.getMatchingAccessibleMethod(cls, methodName,
|
||||
|
@ -412,5 +460,16 @@ public class MethodUtilsTest {
|
|||
public static class GrandParentObject {}
|
||||
public static class ParentObject extends GrandParentObject {}
|
||||
public static class ChildObject extends ParentObject implements ChildInterface {}
|
||||
|
||||
|
||||
private static class MethodDescriptor {
|
||||
final Class<?> declaringClass;
|
||||
final String name;
|
||||
final Type[] parameterTypes;
|
||||
|
||||
MethodDescriptor(Class<?> declaringClass, String name, Type... parameterTypes) {
|
||||
this.declaringClass = declaringClass;
|
||||
this.name = name;
|
||||
this.parameterTypes = parameterTypes;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -20,4 +20,8 @@ package org.apache.commons.lang3.reflect.testbed;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class Ambig implements Foo, Bar {
|
||||
|
||||
@Override
|
||||
public void doIt() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,4 +21,6 @@ package org.apache.commons.lang3.reflect.testbed;
|
|||
*/
|
||||
public interface Bar {
|
||||
public static final String VALUE = "bar";
|
||||
|
||||
void doIt();
|
||||
}
|
||||
|
|
|
@ -21,4 +21,6 @@ package org.apache.commons.lang3.reflect.testbed;
|
|||
*/
|
||||
public interface Foo {
|
||||
public static final String VALUE = "foo";
|
||||
|
||||
void doIt();
|
||||
}
|
||||
|
|
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.apache.commons.lang3.reflect.testbed;
|
||||
|
||||
public interface GenericConsumer<T> {
|
||||
void consume(T t);
|
||||
}
|
|
@ -20,6 +20,10 @@ package org.apache.commons.lang3.reflect.testbed;
|
|||
* Class declaring a parameter variable.
|
||||
* @version $Id$
|
||||
*/
|
||||
public class GenericParent<T> {
|
||||
public class GenericParent<T> implements GenericConsumer<T> {
|
||||
|
||||
@Override
|
||||
public void consume(T t) {
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -25,4 +25,8 @@ class Parent implements Foo {
|
|||
int i = 0;
|
||||
@SuppressWarnings("unused")
|
||||
private final double d = 0.0;
|
||||
|
||||
@Override
|
||||
public void doIt() {
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,5 +21,8 @@ package org.apache.commons.lang3.reflect.testbed;
|
|||
* @version $Id$
|
||||
*/
|
||||
public class StringParameterizedChild extends GenericParent<String> {
|
||||
|
||||
@Override
|
||||
public void consume(String t) {
|
||||
super.consume(t);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue