Skip to content
Snippets Groups Projects
Commit a6ab2211 authored by felmer's avatar felmer
Browse files

LMS-257 new method (with test): ClassUtils.gatherAllCastableClassesAndInterfacesFor

SVN: 4768
parent 592bf11e
No related branches found
No related tags found
No related merge requests found
......@@ -21,6 +21,9 @@ import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
......@@ -37,6 +40,33 @@ public final class ClassUtils
{
// Can not be instantiated.
}
/**
* Gathers all classes and interfaces the specified object can be casted to.
*/
public static Collection<Class<?>> gatherAllCastableClassesAndInterfacesFor(Object object)
{
assert object != null;
Set<Class<?>> classes = new LinkedHashSet<Class<?>>();
gather(classes, object.getClass());
return classes;
}
private static void gather(Set<Class<?>> classes, Class<?> clazz)
{
classes.add(clazz);
Class<?> superclass = clazz.getSuperclass();
if (superclass != null)
{
gather(classes, superclass);
}
Class<?>[] interfaces = clazz.getInterfaces();
for (Class<?> interfaze : interfaces)
{
gather(classes, interfaze);
}
}
/**
* Returns the currently called <code>Method</code>.
......
......@@ -26,6 +26,8 @@ import static org.testng.AssertJUnit.fail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
......@@ -38,6 +40,29 @@ import org.testng.annotations.Test;
*/
public final class ClassUtilsTest
{
private static interface IA {}
private static interface IExtendingIA extends IA {}
private static interface IB {}
private static class A {}
private static class ExtendingA extends A implements IExtendingIA {}
private static class ExtendingExtendingA extends ExtendingA implements IB, IA {}
@Test
public void testGatherAllCastableClassesAndInterfacesFor()
{
ExtendingExtendingA object = new ExtendingExtendingA();
Collection<Class<?>> classes = ClassUtils.gatherAllCastableClassesAndInterfacesFor(object);
Iterator<Class<?>> iterator = classes.iterator();
assertSame(ExtendingExtendingA.class, iterator.next());
assertSame(ExtendingA.class, iterator.next());
assertSame(A.class, iterator.next());
assertSame(Object.class, iterator.next());
assertSame(IExtendingIA.class, iterator.next());
assertSame(IA.class, iterator.next());
assertSame(IB.class, iterator.next());
assertEquals(false, iterator.hasNext());
}
/**
* Test method for {@link ch.systemsx.cisd.common.utilities.ClassUtils#getCurrentMethod()}.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment