Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
monperrus committed Sep 28, 2017
1 parent f35f47f commit 7172c69
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ public interface CtTypeInformation {
boolean isAnnotationType();

/**
* Returns true if this element is a generics (eg "T") and false if it is an actual type (eg 'Book" or "String")
* Returns true if it refers to a type parameter (ie not a concrete class, eg "T foo"). It can refer to it directly (eg T), or indirectly (eg List<T>, or Set<List<T>>).
*/
@DerivedProperty
boolean isGenerics();

/**
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/spoon/reflect/reference/CtTypeReference.java
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,5 @@ public interface CtTypeReference<T> extends CtReference, CtActualTypeContainer,
@DerivedProperty
CtTypeParameter getTypeParameterDeclaration();

/**
* Returns true if it refers to at least one type parameter (ie not a concrete class). It can refer to it directly (eg T), or indirectly (eg List&lt;T&gt;, or Set&lt;List&lt;T&gt;&gt;).
*/
@DerivedProperty
boolean isGeneric();


}
Original file line number Diff line number Diff line change
Expand Up @@ -252,14 +252,4 @@ public CtTypeParameterReference clone() {
return (CtTypeParameterReference) super.clone();
}

@Override
public boolean isGeneric() {
if (getDeclaration() instanceof CtTypeParameter) {
return true;
}
if (getBoundingType() != null && getBoundingType().isGeneric()) {
return true;
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -622,11 +622,6 @@ public boolean isEnum() {
}
}

@Override
public boolean isGenerics() {
return false;
}

@Override
public boolean canAccess(CtTypeReference<?> type) {
try {
Expand Down Expand Up @@ -825,12 +820,12 @@ public CtTypeParameter getTypeParameterDeclaration() {
}

@Override
public boolean isGeneric() {
public boolean isGenerics() {
if (getDeclaration() instanceof CtTypeParameter) {
return true;
}
for (CtTypeReference ref : getActualTypeArguments()) {
if (ref.isGeneric()) {
if (ref.isGenerics()) {
return true;
}
}
Expand Down
70 changes: 35 additions & 35 deletions src/test/java/spoon/test/generics/GenericsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -685,143 +685,143 @@ public boolean matches(CtTypeReference element) {

// T
CtTypeReference<?> var1Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "var1")).first(CtVariable.class).getType();
assertEquals(true, var1Ref.isGeneric());
assertEquals(true, var1Ref.isGenerics());

// spoon.test.generics.testclasses.rxjava.Subscriber<? super T>
CtTypeReference<?> sRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "s")).first(CtVariable.class).getType();
assertEquals(true, sRef.isGeneric());
assertEquals(true, sRef.isGenerics());

// spoon.test.generics.testclasses.rxjava.Try<java.util.Optional<java.lang.Object>>
CtTypeReference<?> notificationRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "notification")).first(CtVariable.class).getType();
assertEquals(false, notificationRef.isGeneric());
assertEquals(false, notificationRef.isGenerics());

// java.util.function.Function<? super spoon.test.generics.testclasses.rxjava.Observable<spoon.test.generics.testclasses.rxjava.Try<java.util.Optional<java.lang.Object>>>, ? extends spoon.test.generics.testclasses.rxjava.Publisher<?>>
CtTypeReference<?> managerRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "manager")).first(CtVariable.class).getType();
assertEquals(false, managerRef.isGeneric());
assertEquals(false, managerRef.isGenerics());

// spoon.test.generics.testclasses.rxjava.BehaviorSubject<spoon.test.generics.testclasses.rxjava.Try<java.util.Optional<java.lang.Object>>>
CtTypeReference<?> subjectRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "subject")).first(CtVariable.class).getType();
assertEquals(false, subjectRef.isGeneric());
assertEquals(false, subjectRef.isGenerics());

// spoon.test.generics.testclasses.rxjava.PublisherRedo.RedoSubscriber<T>
CtTypeReference<?> parentRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "parent")).first(CtVariable.class).getType();
assertEquals(true, parentRef.isGeneric());
assertEquals(true, parentRef.isGenerics());

// spoon.test.generics.testclasses.rxjava.Publisher<?>
CtTypeReference<?> actionRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "action")).first(CtVariable.class).getType();
assertEquals(false, actionRef.isGeneric());
assertEquals(false, actionRef.isGenerics());

// spoon.test.generics.testclasses.rxjava.ToNotificationSubscriber
CtTypeReference<?> trucRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "truc")).first(CtVariable.class).getType();
assertEquals(false, trucRef.isGeneric());
assertEquals(false, trucRef.isGenerics());

// java.util.function.Consumer<? super spoon.test.generics.testclasses.rxjava.Try<java.util.Optional<java.lang.Object>>>
CtTypeReference<?> consumerRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "consumer")).first(CtVariable.class).getType();
assertEquals(false, consumerRef.isGeneric());
assertEquals(false, consumerRef.isGenerics());

// S
CtTypeReference<?> sectionRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "section")).first(CtVariable.class).getType();
assertEquals(true, sectionRef.isGeneric());
assertEquals(true, sectionRef.isGenerics());

// X
CtTypeReference<?> paramARef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "paramA")).first(CtVariable.class).getType();
assertEquals(true, paramARef.isGeneric());
assertEquals(true, paramARef.isGenerics());

// spoon.test.generics.testclasses.Tacos
CtTypeReference<?> paramBRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "paramB")).first(CtVariable.class).getType();
assertEquals(false, paramBRef.isGeneric());
assertEquals(false, paramBRef.isGenerics());

// C
CtTypeReference<?> paramCRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "paramC")).first(CtVariable.class).getType();
assertEquals(true, paramCRef.isGeneric());
assertEquals(true, paramCRef.isGenerics());

// R
CtTypeReference<?> cookRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "cook")).first(CtVariable.class).getType();
assertEquals(true, cookRef.isGeneric());
assertEquals(true, cookRef.isGenerics());

// spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>
CtTypeReference<?> clRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "cl")).first(CtVariable.class).getType();
assertEquals(false, clRef.isGeneric());
assertEquals(false, clRef.isGenerics());

// spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>
CtTypeReference<?> disgustRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "disgust")).first(CtVariable.class).getType();
assertEquals(false, disgustRef.isGeneric());
assertEquals(false, disgustRef.isGenerics());

// L
CtTypeReference<?> paramRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "param")).first(CtVariable.class).getType();
assertEquals(true, paramRef.isGeneric());
assertEquals(true, paramRef.isGenerics());

// spoon.reflect.declaration.CtType<? extends spoon.reflect.declaration.CtNamedElement>
CtTypeReference<?> targetTypeRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "targetType")).first(CtVariable.class).getType();
assertEquals(false, targetTypeRef.isGeneric());
assertEquals(false, targetTypeRef.isGenerics());

// spoon.reflect.declaration.CtType<?>
CtTypeReference<?> somethingRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "something")).first(CtVariable.class).getType();
assertEquals(false, somethingRef.isGeneric());
assertEquals(false, somethingRef.isGenerics());

// int
CtTypeReference<?> iRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "i")).first(CtVariable.class).getType();
assertEquals(false, iRef.isGeneric());
assertEquals(false, iRef.isGenerics());

// T
CtTypeReference<?> biduleRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "bidule")).first(CtVariable.class).getType();
assertEquals(true, biduleRef.isGeneric());
assertEquals(true, biduleRef.isGenerics());

// Cook<java.lang.String>
CtTypeReference<?> aClassRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "aClass")).first(CtVariable.class).getType();
assertEquals(false, aClassRef.isGeneric());
assertEquals(false, aClassRef.isGenerics());

// java.util.List<java.util.List<M>>
CtTypeReference<?> list2mRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "list2m")).first(CtVariable.class).getType();
assertEquals(true, list2mRef.isGeneric());
assertEquals(true, list2mRef.isGenerics());

// spoon.test.generics.testclasses.Panini.Subscriber<? extends java.lang.Long>
CtTypeReference<?> tRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "t")).first(CtVariable.class).getType();
assertEquals(false, tRef.isGeneric());
assertEquals(false, tRef.isGenerics());

// spoon.test.generics.testclasses.Spaghetti<B>.Tester
CtTypeReference<?> testerRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "tester")).first(CtVariable.class).getType();
assertEquals(false, testerRef.isGeneric());
assertEquals(false, testerRef.isGenerics());

// spoon.test.generics.testclasses.Spaghetti<B>.Tester
CtTypeReference<?> tester1Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "tester1")).first(CtVariable.class).getType();
assertEquals(false, tester1Ref.isGeneric());
assertEquals(false, tester1Ref.isGenerics());

// spoon.test.generics.testclasses.Spaghetti<B>.That<java.lang.String, java.lang.String>
CtTypeReference<?> fieldRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "field")).first(CtVariable.class).getType();
assertEquals(false, fieldRef.isGeneric());
assertEquals(false, fieldRef.isGenerics());

// spoon.test.generics.testclasses.Spaghetti<java.lang.String>.That<java.lang.String, java.lang.String>
CtTypeReference<?> field1Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "field1")).first(CtVariable.class).getType();
assertEquals(false, field1Ref.isGeneric());
assertEquals(false, field1Ref.isGenerics());

// spoon.test.generics.testclasses.Spaghetti<java.lang.Number>.That<java.lang.String, java.lang.String>
CtTypeReference<?> field2Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "field2")).first(CtVariable.class).getType();
assertEquals(false, field2Ref.isGeneric());
assertEquals(false, field2Ref.isGenerics());

// spoon.test.generics.testclasses.Tacos<K, java.lang.String>.Burritos<K, V>
CtTypeReference<?> burritosRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "burritos")).first(CtVariable.class).getType();
assertEquals(true, burritosRef.isGeneric());
assertEquals(true, burritosRef.isGenerics());

// int
CtTypeReference<?> nbTacosRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "nbTacos")).first(CtVariable.class).getType();
assertEquals(false, nbTacosRef.isGeneric());
assertEquals(false, nbTacosRef.isGenerics());

// java.util.List<java.lang.String>
CtTypeReference<?> lRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "l")).first(CtVariable.class).getType();
assertEquals(false, lRef.isGeneric());
assertEquals(false, lRef.isGenerics());

// java.util.List
CtTypeReference<?> l2Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "l2")).first(CtVariable.class).getType();
assertEquals(false, l2Ref.isGeneric());
assertEquals(false, l2Ref.isGenerics());

// java.util.List<?>
CtTypeReference<?> l3Ref = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "l3")).first(CtVariable.class).getType();
assertEquals(false, l3Ref.isGeneric());
assertEquals(false, l3Ref.isGenerics());

// T
CtTypeReference<?> anObjectRef = factory.getModel().getRootPackage().filterChildren(new NamedElementFilter(CtVariable.class, "anObject")).first(CtVariable.class).getType();
assertEquals(true, anObjectRef.isGeneric());
assertEquals(true, anObjectRef.isGenerics());

}
@Test
Expand Down

0 comments on commit 7172c69

Please sign in to comment.