Skip to content

Commit

Permalink
Merge pull request #12 from surli/bugIsSubtypeOf
Browse files Browse the repository at this point in the history
Changes to use only one implementation of isSubTypeOf based on CtTypeReference
  • Loading branch information
pvojtechovsky authored Feb 1, 2017
2 parents 676e064 + 482ff12 commit bdaadb3
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public <C extends CtType<T>> C setSuperInterfaces(Set<CtTypeReference<?>> interf

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
return false;
return getReference().isSubtypeOf(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,7 @@ public boolean isAnonymous() {

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
if ((getSuperclass() != null) && getSuperclass().isSubtypeOf(type)) {
return true;
}
for (CtTypeReference<?> ref : getSuperInterfaces()) {
if (ref.isSubtypeOf(type)) {
return true;
}
}
return false;
return getReference().isSubtypeOf(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public Set<CtMethod<?>> getAllMethods() {

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
for (CtTypeReference<?> ref : getSuperInterfaces()) {
if (ref.isSubtypeOf(type)) {
return true;
}
}
return getSuperclass().isSubtypeOf(type);
return getReference().isSubtypeOf(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,7 @@ public void accept(CtVisitor visitor) {

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
for (CtTypeReference<?> ref : getSuperInterfaces()) {
if (ref.isSubtypeOf(type)) {
return true;
}
}
return false;
return getReference().isSubtypeOf(type);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ public List<CtFieldReference<?>> getDeclaredFields() {

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
return false;
return getReference().isSubtypeOf(type);
}

@Override
@UnsettableProperty
public <M, C extends CtType<Object>> C addMethod(CtMethod<M> method) {
public <M , C extends CtType<Object>> C addMethod(CtMethod<M> method) {
// unsettable property
return (C) this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,6 @@ public boolean isGenerics() {
return true;
}

@Override
public boolean isSubtypeOf(CtTypeReference<?> type) {
return false;
}

@Override
public boolean isPrimitive() {
return false;
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/spoon/test/ctType/CtTypeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@
import spoon.reflect.declaration.CtInterface;
import spoon.reflect.declaration.CtMethod;
import spoon.reflect.declaration.CtType;
import spoon.reflect.declaration.CtTypeMember;
import spoon.reflect.declaration.CtTypeParameter;
import spoon.reflect.factory.Factory;
import spoon.reflect.reference.CtTypeReference;
import spoon.test.ctType.testclasses.X;

import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static spoon.testing.utils.ModelUtils.buildClass;
Expand Down Expand Up @@ -79,4 +84,26 @@ public void testIsSubTypeOf() throws Exception {
//using CtType implementation
assertTrue(xCtType.isSubtypeOf(xCtType.getReference()));
}

@Test
public void testIsSubTypeOfonTypeParameters() throws Exception {
CtType<X> xCtType = buildClass(X.class);

CtType<?> oCtType = xCtType.getFactory().Type().get("spoon.test.ctType.testclasses.O");

List<CtTypeParameter> typeParameters = oCtType.getFormalCtTypeParameters();
assertTrue(typeParameters.size() == 1);

CtType<?> aCtType = typeParameters.get(0);

List<CtMethod<?>> methods = oCtType.getMethodsByName("foo");

assertTrue(methods.size() == 1);

CtMethod<?> fooMethod = methods.get(0);
CtType<?> bCtType = fooMethod.getType().getDeclaration();

assertTrue(bCtType.isSubtypeOf(xCtType.getReference()));
assertTrue(bCtType.isSubtypeOf(aCtType.getReference()));
}
}
6 changes: 6 additions & 0 deletions src/test/java/spoon/test/ctType/testclasses/X.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,9 @@ interface Z {
abstract class W implements Z {
}

class O<A extends X> {
<B extends A> B foo() {
return null;
}
}

0 comments on commit bdaadb3

Please sign in to comment.