Search in sources :

Example 41 with Factory

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);
}
Also used : CtAssignment(spoon.reflect.code.CtAssignment) CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) CtEnum(spoon.reflect.declaration.CtEnum) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 42 with Factory

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.");
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldReference(spoon.reflect.reference.CtFieldReference) Factory(spoon.reflect.factory.Factory) FieldAccessFilterTacos(spoon.test.filters.testclasses.FieldAccessFilterTacos) CtField(spoon.reflect.declaration.CtField) CtNamedElement(spoon.reflect.declaration.CtNamedElement) FieldAccessFilter(spoon.reflect.visitor.filter.FieldAccessFilter) Test(org.junit.Test)

Example 43 with Factory

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));
}
Also used : CompositeFilter(spoon.reflect.visitor.filter.CompositeFilter) CtElement(spoon.reflect.declaration.CtElement) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtNewClass(spoon.reflect.code.CtNewClass) List(java.util.List) ArrayList(java.util.ArrayList) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 44 with Factory

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());
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTypedElement(spoon.reflect.declaration.CtTypedElement) File(java.io.File) CtNamedElement(spoon.reflect.declaration.CtNamedElement) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 45 with Factory

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());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInterface(spoon.reflect.declaration.CtInterface) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) LikeCtClass(spoon.test.generics.testclasses2.LikeCtClass) CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

Factory (spoon.reflect.factory.Factory)364 Test (org.junit.Test)322 Launcher (spoon.Launcher)154 CtClass (spoon.reflect.declaration.CtClass)87 CtMethod (spoon.reflect.declaration.CtMethod)73 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)65 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)53 File (java.io.File)41 CtStatement (spoon.reflect.code.CtStatement)36 CtTypeReference (spoon.reflect.reference.CtTypeReference)32 CtAnnotation (spoon.reflect.declaration.CtAnnotation)31 CtInvocation (spoon.reflect.code.CtInvocation)30 SpoonModelBuilder (spoon.SpoonModelBuilder)29 DefaultCoreFactory (spoon.support.DefaultCoreFactory)29 List (java.util.List)25 FileSystemFile (spoon.support.compiler.FileSystemFile)25 ArrayList (java.util.ArrayList)24 CtExpression (spoon.reflect.code.CtExpression)20 Annotation (java.lang.annotation.Annotation)19 MainTest (spoon.test.main.MainTest)19