Skip to content

Commit

Permalink
ResolvedType.getAllTypes() returns parent, this, and interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
krzysiekbielicki committed Oct 15, 2020
1 parent 473e70b commit a254402
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
31 changes: 28 additions & 3 deletions src/main/java/com/fasterxml/classmate/ResolvedType.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.fasterxml.classmate;

import com.fasterxml.classmate.members.RawConstructor;
import com.fasterxml.classmate.members.RawField;
import com.fasterxml.classmate.members.RawMethod;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Type;
import java.util.*;

import com.fasterxml.classmate.members.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public abstract class ResolvedType
implements Type
Expand Down Expand Up @@ -96,6 +101,26 @@ public final boolean canCreateSubtype(Class<?> subtype) {
*/
public abstract List<ResolvedType> getImplementedInterfaces();

/**
* Returns Ordered list of parent, current and interfaces of this type.
*
* @return List of parent, current and interfaces of this type containing at least current type
*/
public List<ResolvedType> getAllTypes() {
List<ResolvedType> allTypes = new ArrayList<ResolvedType>();
ResolvedType parentClass = getParentClass();
if (parentClass != null) {
allTypes.add(parentClass);
}
allTypes.add(this);
List<ResolvedType> implementedInterfaces = getImplementedInterfaces();
if (implementedInterfaces != null) {
allTypes.addAll(implementedInterfaces);
}

return allTypes;
};

/**
* Returns list of generic type declarations for this type, in order they
* are declared in class description.
Expand Down
26 changes: 25 additions & 1 deletion src/test/java/com/fasterxml/classmate/ResolvedTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
import java.io.Serializable;
import java.nio.CharBuffer;
import java.util.List;
import java.util.Set;

import org.junit.Test;

import com.fasterxml.classmate.types.ResolvedArrayType;
import com.fasterxml.classmate.types.ResolvedInterfaceType;
import com.fasterxml.classmate.types.ResolvedObjectType;

import static org.hamcrest.MatcherAssert.assertThat;

public class ResolvedTypeTest extends BaseTest
{
// For [Issue#16]
Expand All @@ -19,7 +22,13 @@ private static class Foo16 extends Bar16 { }
private static class Bar16 extends Zen16<Bar16, Foo16> { }

private static class Zen16<A, B extends A> { }


private static class ClassWithInterfaces implements FirstInterface, SecondInterface { }

interface FirstInterface {}

interface SecondInterface {}

@Test
public void testCanCreateSubtype() {
ResolvedObjectType stringType = ResolvedObjectType.create(String.class, null, null, null);
Expand Down Expand Up @@ -140,4 +149,19 @@ public void testIssue16()
assertEquals(Bar16.class, params.get(0).getErasedType());
assertEquals(Foo16.class, params.get(1).getErasedType());
}

@Test
public void testFindAllClasses() {
TypeResolver resolver = new TypeResolver();
ResolvedType type = resolver.resolve(ClassWithInterfaces.class);

List<ResolvedType> allTypes = type.getAllTypes();

assertNotNull(allTypes);
assertEquals(4, allTypes.size());
assertEquals(Object.class, allTypes.get(0).getErasedType());
assertEquals(ClassWithInterfaces.class, allTypes.get(1).getErasedType());
assertEquals(FirstInterface.class, allTypes.get(2).getErasedType());
assertEquals(SecondInterface.class, allTypes.get(3).getErasedType());
}
}

0 comments on commit a254402

Please sign in to comment.