use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class EnumsTypeTest method testEnumsType.
@Test
public void testEnumsType() throws Exception {
// contract: shadow enum should still be considered as an enum
Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/reference-test/EnumsRef.java");
Factory factory = launcher.getFactory();
List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/EnumJar.jar");
String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
factory.getEnvironment().setSourceClasspath(dependencyClasspath);
assertEquals(1, classpath.size());
launcher.buildModel();
List<CtAssignment> assignments = Query.getElements(factory, new TypeFilter<>(CtAssignment.class));
CtTypeReference typeRefFromSource = assignments.get(0).getType();
CtType typeFromSource = typeRefFromSource.getTypeDeclaration();
assertTrue(typeRefFromSource.isEnum());
assertTrue(typeFromSource.isEnum());
assertTrue(typeFromSource instanceof CtEnum);
CtTypeReference typeRefFromJar = assignments.get(1).getType();
CtType typeFromJar = typeRefFromJar.getTypeDeclaration();
// fail
assertTrue(typeRefFromJar.isEnum());
// fail
assertTrue(typeFromJar.isEnum());
// fail
assertTrue(typeFromJar instanceof CtEnum);
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class FilterTest method testFieldAccessFilter.
@Test
public void testFieldAccessFilter() throws Exception {
// also specifies VariableAccessFilter since FieldAccessFilter is only a VariableAccessFilter with additional static typing
CtClass<?> foo = factory.Package().get("spoon.test.filters").getType("Foo");
assertEquals("Foo", foo.getSimpleName());
List<CtNamedElement> elements = foo.getElements(new NamedElementFilter<>(CtNamedElement.class, "i"));
assertEquals(1, elements.size());
CtFieldReference<?> ref = (CtFieldReference<?>) (elements.get(0)).getReference();
List<CtFieldAccess<?>> expressions = foo.getElements(new FieldAccessFilter(ref));
assertEquals(2, expressions.size());
final Factory build = build(FieldAccessFilterTacos.class);
final CtType<FieldAccessFilterTacos> fieldAccessFilterTacos = build.Type().get(FieldAccessFilterTacos.class);
try {
List<CtField> fields = fieldAccessFilterTacos.getElements(new TypeFilter<CtField>(CtField.class));
for (CtField ctField : fields) {
fieldAccessFilterTacos.getElements(new FieldAccessFilter(ctField.getReference()));
}
} catch (NullPointerException e) {
fail("FieldAccessFilter must not throw a NPE.");
}
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class FilterTest method unionOfTwoFilters.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void unionOfTwoFilters() throws Exception {
Factory factory = build("spoon.test.testclasses", "SampleClass").getFactory();
TypeFilter<CtNewClass> newClassFilter = new TypeFilter<CtNewClass>(CtNewClass.class);
TypeFilter<CtMethod> methodFilter = new TypeFilter<CtMethod>(CtMethod.class);
CompositeFilter compositeFilter = new CompositeFilter(FilteringOperator.UNION, methodFilter, newClassFilter);
List filteredWithCompositeFilter = Query.getElements(factory, compositeFilter);
List<CtMethod> methods = Query.getElements(factory, methodFilter);
List<CtNewClass> newClasses = Query.getElements(factory, newClassFilter);
List<CtElement> union = new ArrayList<CtElement>();
union.addAll(methods);
union.addAll(newClasses);
assertEquals(methods.size() + newClasses.size(), union.size());
assertEquals(union.size(), filteredWithCompositeFilter.size());
assertTrue(filteredWithCompositeFilter.containsAll(union));
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class GenericsTest method testClassTypingContext.
@Test
public void testClassTypingContext() throws Exception {
// contract: a ClassTypingContext enables one to perform type resolution of generic types
Factory factory = build(new File("src/test/java/spoon/test/generics/testclasses"));
CtClass<?> ctClassCelebrationLunch = factory.Class().get(CelebrationLunch.class);
CtTypeReference<?> typeReferenceOfDisgust = ctClassCelebrationLunch.filterChildren(new NamedElementFilter<>(CtNamedElement.class, "disgust")).map((CtTypedElement te) -> {
return te.getType();
}).first();
assertEquals("spoon.test.generics.testclasses.CelebrationLunch<java.lang.Integer, java.lang.Long, java.lang.Double>.WeddingLunch<spoon.test.generics.testclasses.Mole>", typeReferenceOfDisgust.toString());
// method WeddingLunch#eatMe
CtMethod<?> tWeddingLunch_eatMe = typeReferenceOfDisgust.getDeclaration().filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
CtClass<?> ctClassLunch = factory.Class().get(Lunch.class);
// method Lunch#eatMe
CtMethod<?> ctClassLunch_eatMe = ctClassLunch.filterChildren((CtNamedElement e) -> "eatMe".equals(e.getSimpleName())).first();
// type of first parameter of method WeddingLunch#eatMe
CtTypeReference<?> ctWeddingLunch_X = tWeddingLunch_eatMe.getParameters().get(0).getType();
// X is the type parameter of WeddingLunch
assertEquals("X", ctWeddingLunch_X.getSimpleName());
// type of first parameter of method Lunch#eatMe
CtTypeReference<?> ctClassLunch_A = ctClassLunch_eatMe.getParameters().get(0).getType();
assertEquals("A", ctClassLunch_A.getSimpleName());
// are these two types same?
ClassTypingContext typingContextOfDisgust = new ClassTypingContext(typeReferenceOfDisgust);
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getTypeDeclaration(), typingContextOfDisgust.getAdaptationScope());
// in disgust, X of WeddingLunch is bound to "Model"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctWeddingLunch_X).getQualifiedName());
// adapt A to scope of CelebrationLunch<Integer,Long,Double>.WeddingLunch<Mole>
// in disgust, the A of Lunch is bound to "Mole"
assertEquals("spoon.test.generics.testclasses.Mole", typingContextOfDisgust.adaptType(ctClassLunch_A).getQualifiedName());
// I don't understand the goal and utility of this one
assertEquals("java.lang.Double", typingContextOfDisgust.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
// now we resolve those types, but in the context of the declaration, where no concrete types exist
// are these two types same in scope of CelebrationLunch<K,L,M>.WddingLunch<X> class itself
ClassTypingContext sthOftWeddingLunch_X = new ClassTypingContext(typeReferenceOfDisgust.getDeclaration());
// contract: the class typing context provides its scope
assertSame(typeReferenceOfDisgust.getDeclaration(), sthOftWeddingLunch_X.getAdaptationScope());
// in WeddingLunch "X" is still "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctWeddingLunch_X).getQualifiedName());
// in WeddingLunch the "A" from Lunch of is called "X"
assertEquals("X", sthOftWeddingLunch_X.adaptType(ctClassLunch_A).getQualifiedName());
// ?????
// adapt A to scope of enclosing class of CelebrationLunch<K,L,M>.WddingLunch<X>, which is CelebrationLunch<K,L,M>
assertEquals("M", sthOftWeddingLunch_X.getEnclosingGenericTypeAdapter().adaptType(ctClassLunch_A).getQualifiedName());
}
use of spoon.reflect.factory.Factory in project spoon by INRIA.
the class GenericsTest method testAccessToGenerics.
@Test
public void testAccessToGenerics() throws Exception {
Launcher spoon = new Launcher();
Factory factory = spoon.createFactory();
SpoonModelBuilder compiler = spoon.createCompiler(factory, SpoonResourceHelper.resources("./src/test/java/spoon/test/generics/Foo.java", "./src/test/java/spoon/test/generics/Bar.java"));
compiler.build();
CtClass<?> foo = (CtClass<?>) factory.Type().get(Foo.class);
CtInterface<?> bar = (CtInterface<?>) factory.Type().get(Bar.class);
final CtNewClass<?> newAnonymousBar = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {
@Override
public boolean matches(CtNewClass<?> element) {
return element.getAnonymousClass() != null && element.getAnonymousClass().isAnonymous();
}
}).get(0);
final List<CtTypeParameter> barTypeParamGenerics = bar.getFormalCtTypeParameters();
final CtTypeReference<?> anonymousBar = newAnonymousBar.getType();
assertEquals("Name of the first generic parameter in Bar interface must to be I.", "I", barTypeParamGenerics.get(0).getSimpleName());
assertEquals("Name of the first generic parameter in Bar usage must to be K.", "K", anonymousBar.getActualTypeArguments().get(0).getSimpleName());
assertEquals("Name of the second generic parameter in Bar interface must to be O.", "O", barTypeParamGenerics.get(1).getSimpleName());
assertEquals("Name of the second generic parameter in Bar usage must to be V.", "V", anonymousBar.getActualTypeArguments().get(1).getSimpleName());
}
Aggregations