use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class GenericsTest method testisGeneric.
@Test
public void testisGeneric() throws Exception {
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
/*
// this code has been used to generate the list of assertEquals below,
// and then each assertEquals was verified
Set<String> s = new HashSet<>();
factory.getModel().getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {
@Override
public boolean matches(CtTypeReference element) {
return super.matches(element) && element.getParent() instanceof CtVariable;
}
}).forEach(x -> {
String simpleName = ((CtVariable) x.getParent()).getSimpleName();
if (!s.contains(simpleName)) {
System.out.println("\t\t// "+x.toString());
System.out.println("\t\tCtTypeReference<?> "+simpleName+"Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, \"" + simpleName+ "\")).first(CtVariable.class).getType();");
System.out.println("\t\tassertEquals("+x.isGeneric() + ", " + simpleName + "Ref.isGeneric());");
System.out.println();
}
s.add(simpleName);
});
*/
// T
CtTypeReference<?> var1Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "var1")).first(CtVariable.class).getType();
assertEquals(true, var1Ref.isGenerics());
// spoon.test.generics.testclasses.rxjava.Subscriber<? super T>
CtTypeReference<?> sRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "s")).first(CtVariable.class).getType();
assertEquals(true, sRef.isGenerics());
// spoon.test.generics.testclasses.rxjava.Try<java.util.Optional<java.lang.Object>>
CtTypeReference<?> notificationRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "notification")).first(CtVariable.class).getType();
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().filterChildren(new NamedElementFilter(CtVariable.class, "manager")).first(CtVariable.class).getType();
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().filterChildren(new NamedElementFilter(CtVariable.class, "subject")).first(CtVariable.class).getType();
assertEquals(false, subjectRef.isGenerics());
// spoon.test.generics.testclasses.rxjava.PublisherRedo.RedoSubscriber<T>
CtTypeReference<?> parentRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "parent")).first(CtVariable.class).getType();
assertEquals(true, parentRef.isGenerics());
// spoon.test.generics.testclasses.rxjava.Publisher<?>
CtTypeReference<?> actionRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "action")).first(CtVariable.class).getType();
assertEquals(false, actionRef.isGenerics());
// spoon.test.generics.testclasses.rxjava.ToNotificationSubscriber
CtTypeReference<?> trucRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "truc")).first(CtVariable.class).getType();
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().filterChildren(new NamedElementFilter(CtVariable.class, "consumer")).first(CtVariable.class).getType();
assertEquals(false, consumerRef.isGenerics());
// S
CtTypeReference<?> sectionRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "section")).first(CtVariable.class).getType();
assertEquals(true, sectionRef.isGenerics());
// X
CtTypeReference<?> paramARef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "paramA")).first(CtVariable.class).getType();
assertEquals(true, paramARef.isGenerics());
// spoon.test.generics.testclasses.Tacos
CtTypeReference<?> paramBRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "paramB")).first(CtVariable.class).getType();
assertEquals(false, paramBRef.isGenerics());
// C
CtTypeReference<?> paramCRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "paramC")).first(CtVariable.class).getType();
assertEquals(true, paramCRef.isGenerics());
// R
CtTypeReference<?> cookRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "cook")).first(CtVariable.class).getType();
assertEquals(true, cookRef.isGenerics());
// spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>
CtTypeReference<?> clRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "cl")).first(CtVariable.class).getType();
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().filterChildren(new NamedElementFilter(CtVariable.class, "disgust")).first(CtVariable.class).getType();
assertEquals(false, disgustRef.isGenerics());
// L
CtTypeReference<?> paramRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "param")).first(CtVariable.class).getType();
assertEquals(true, paramRef.isGenerics());
// spoon.reflect.declaration.CtType<? extends spoon.reflect.declaration.CtNamedElement>
CtTypeReference<?> targetTypeRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "targetType")).first(CtVariable.class).getType();
assertEquals(false, targetTypeRef.isGenerics());
// spoon.reflect.declaration.CtType<?>
CtTypeReference<?> somethingRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "something")).first(CtVariable.class).getType();
assertEquals(false, somethingRef.isGenerics());
// int
CtTypeReference<?> iRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "i")).first(CtVariable.class).getType();
assertEquals(false, iRef.isGenerics());
// T
CtTypeReference<?> biduleRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "bidule")).first(CtVariable.class).getType();
assertEquals(true, biduleRef.isGenerics());
// Cook<java.lang.String>
CtTypeReference<?> aClassRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "aClass")).first(CtVariable.class).getType();
assertEquals(false, aClassRef.isGenerics());
// java.util.List<java.util.List<M>>
CtTypeReference<?> list2mRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "list2m")).first(CtVariable.class).getType();
assertEquals(true, list2mRef.isGenerics());
// spoon.test.generics.testclasses.Panini.Subscriber<? extends java.lang.Long>
CtTypeReference<?> tRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "t")).first(CtVariable.class).getType();
assertEquals(false, tRef.isGenerics());
// spoon.test.generics.testclasses.Spaghetti<B>.Tester
CtTypeReference<?> testerRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "tester")).first(CtVariable.class).getType();
assertEquals(false, testerRef.isGenerics());
// spoon.test.generics.testclasses.Spaghetti<B>.Tester
CtTypeReference<?> tester1Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "tester1")).first(CtVariable.class).getType();
assertEquals(false, tester1Ref.isGenerics());
// spoon.test.generics.testclasses.Spaghetti<B>.That<java.lang.String, java.lang.String>
CtTypeReference<?> fieldRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "field")).first(CtVariable.class).getType();
assertEquals(false, fieldRef.isGenerics());
// spoon.test.generics.testclasses.Spaghetti<java.lang.String>.That<java.lang.String, java.lang.String>
CtTypeReference<?> field1Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "field1")).first(CtVariable.class).getType();
assertEquals(false, field1Ref.isGenerics());
// spoon.test.generics.testclasses.Spaghetti<java.lang.Number>.That<java.lang.String, java.lang.String>
CtTypeReference<?> field2Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "field2")).first(CtVariable.class).getType();
assertEquals(false, field2Ref.isGenerics());
// spoon.test.generics.testclasses.Tacos<K, java.lang.String>.Burritos<K, V>
CtTypeReference<?> burritosRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "burritos")).first(CtVariable.class).getType();
// now that the order of type members is correct
// this burritos is indeed "IBurritos<?, ?> burritos = new Burritos<>()" with no generics
assertEquals(false, burritosRef.isGenerics());
// int
CtTypeReference<?> nbTacosRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "nbTacos")).first(CtVariable.class).getType();
assertEquals(false, nbTacosRef.isGenerics());
// java.util.List<java.lang.String>
CtTypeReference<?> lRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "l")).first(CtVariable.class).getType();
assertEquals(false, lRef.isGenerics());
// java.util.List
CtTypeReference<?> l2Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "l2")).first(CtVariable.class).getType();
assertEquals(false, l2Ref.isGenerics());
// java.util.List<?>
CtTypeReference<?> l3Ref = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "l3")).first(CtVariable.class).getType();
assertEquals(false, l3Ref.isGenerics());
// T
CtTypeReference<?> anObjectRef = factory.getModel().filterChildren(new NamedElementFilter(CtVariable.class, "anObject")).first(CtVariable.class).getType();
assertEquals(true, anObjectRef.isGenerics());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class GenericsTest method testTypeAdapted.
@Test
public void testTypeAdapted() throws Exception {
// contract: one can get the actual value of a generic type in a given context
CtClass<?> ctModel = (CtClass<?>) ModelUtils.buildClass(ErasureModelA.class);
CtTypeParameter tpA = ctModel.getFormalCtTypeParameters().get(0);
CtTypeParameter tpB = ctModel.getFormalCtTypeParameters().get(1);
CtTypeParameter tpC = ctModel.getFormalCtTypeParameters().get(2);
CtTypeParameter tpD = ctModel.getFormalCtTypeParameters().get(3);
CtClass<?> ctModelB = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelB")).first();
ClassTypingContext sth = new ClassTypingContext(ctModelB);
// in ModelB, "A" is "A2"
assertEquals("A2", sth.adaptType(tpA).getQualifiedName());
// in ModelB, "B" is "B2"
assertEquals("B2", sth.adaptType(tpB).getQualifiedName());
// and so on and so forth
assertEquals("C2", sth.adaptType(tpC).getQualifiedName());
assertEquals("D2", sth.adaptType(tpD).getQualifiedName());
CtClass<?> ctModelC = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelC")).first();
ClassTypingContext sthC = new ClassTypingContext(ctModelC);
assertEquals("java.lang.Integer", sthC.adaptType(tpA).getQualifiedName());
assertEquals("java.lang.RuntimeException", sthC.adaptType(tpB).getQualifiedName());
assertEquals("java.lang.IllegalArgumentException", sthC.adaptType(tpC).getQualifiedName());
assertEquals("java.util.List", sthC.adaptType(tpD).getQualifiedName());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class GenericsTest method testBugCommonCollection.
@Test
public void testBugCommonCollection() throws Exception {
try {
CtClass<?> type = build("spoon.test.generics", "BugCollection");
CtField<?> INSTANCE = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE")).get(0);
// assertTrue(INSTANCE.getDefaultExpression().getType().getActualTypeArguments().get(0)
// instanceof CtAnnonTypeParameterReference);
assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE = new spoon.test.generics.ACLass();", INSTANCE.toString());
CtField<?> INSTANCE2 = type.getElements(new NamedElementFilter<>(CtField.class, "INSTANCE2")).get(0);
INSTANCE2.setAnnotations(new ArrayList<CtAnnotation<?>>());
assertEquals("public static final spoon.test.generics.ACLass<?> INSTANCE2 = new spoon.test.generics.ACLass();", INSTANCE2.toString());
CtClass<?> ComparableComparator = type.getPackage().getElements(new NamedElementFilter<>(CtClass.class, "ComparableComparator")).get(0);
assertTrue(ComparableComparator.toString().startsWith("class ComparableComparator<E extends java.lang.Comparable<? super E>>"));
CtField<?> x = type.getElements(new NamedElementFilter<>(CtField.class, "x")).get(0);
CtTypeReference<?> ref = x.getType();
DefaultJavaPrettyPrinter pp = new DefaultJavaPrettyPrinter(new StandardEnvironment());
// qualifed name
assertEquals("java.util.Map$Entry", ref.getQualifiedName());
// toString uses DefaultJavaPrettyPrinter
assertEquals("java.util.Map.Entry", ref.toString());
// now visitCtTypeReference
assertEquals(java.util.Map.class, ref.getDeclaringType().getActualClass());
pp.visitCtTypeReference(ref);
assertEquals("java.util.Map.Entry", pp.getResult().toString());
CtField<?> y = type.getElements(new NamedElementFilter<>(CtField.class, "y")).get(0);
assertEquals("java.util.Map.Entry<?, ?> y;", y.toString());
CtField<?> z = type.getElements(new NamedElementFilter<>(CtField.class, "z")).get(0);
assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> z;", z.toString());
// now as local variables
CtLocalVariable<?> lx = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lx")).get(0);
assertEquals("java.util.Map.Entry lx", lx.toString());
CtLocalVariable<?> ly = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "ly")).get(0);
assertEquals("java.util.Map.Entry<?, ?> ly", ly.toString());
CtLocalVariable<?> lz = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "lz")).get(0);
assertEquals("java.util.Map.Entry<java.lang.String, java.lang.Integer> lz", lz.toString());
CtLocalVariable<?> it = type.getElements(new NamedElementFilter<>(CtLocalVariable.class, "it")).get(0);
assertEquals("java.util.Iterator<java.util.Map.Entry<?, ?>> it", it.toString());
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class FilterTest method classCastExceptionIsNotThrown.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void classCastExceptionIsNotThrown() throws Exception {
Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
NamedElementFilter<CtVariable> nameFilterA = new NamedElementFilter<>(CtVariable.class, "j");
NamedElementFilter<CtVariable> nameFilterB = new NamedElementFilter<>(CtVariable.class, "k");
CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.INTERSECTION, nameFilterA, nameFilterB);
List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
assertTrue(filteredWithCompositeFilter.isEmpty());
}
use of spoon.reflect.visitor.filter.NamedElementFilter in project spoon by INRIA.
the class FilterTest method testParentFunction.
@Test
public void testParentFunction() throws Exception {
// contract: a mapping function which returns all parents of CtElement
final Launcher launcher = new Launcher();
launcher.setArgs(new String[] { "--output-type", "nooutput", "--level", "info" });
launcher.addInputResource("./src/test/java/spoon/test/filters/testclasses");
launcher.run();
CtClass<?> cls = launcher.getFactory().Class().get(Tacos.class);
CtLocalVariable<?> varStrings = cls.filterChildren(new NamedElementFilter<>(CtLocalVariable.class, "strings")).first();
class Context {
CtElement expectedParent;
}
Context context = new Context();
context.expectedParent = varStrings;
varStrings.map(new ParentFunction()).forEach((parent) -> {
context.expectedParent = context.expectedParent.getParent();
assertSame(context.expectedParent, parent);
});
// context.expectedParent is last visited element
// Check that last visited element was root package
assertSame(launcher.getFactory().getModel().getUnnamedModule(), context.expectedParent);
// contract: if includingSelf(false), then parent of input element is first element
assertSame(varStrings.getParent(), varStrings.map(new ParentFunction().includingSelf(false)).first());
// contract: if includingSelf(true), then input element is first element
assertSame(varStrings, varStrings.map(new ParentFunction().includingSelf(true)).first());
// contract: do not fail on unitialized parent
assertNull(factory.Type().createReference("p.T").map(new ParentFunction()).first());
}
Aggregations