Search in sources :

Example 6 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class GenericsTest method testCannotAdaptTypeOfNonTypeScope.

@Test
public void testCannotAdaptTypeOfNonTypeScope() throws Exception {
    // contract: ClassTypingContext doesn't fail on type parameters, which are defined out of the scope of ClassTypingContext
    CtType<?> ctClass = ModelUtils.buildClass(OuterTypeParameter.class);
    // the method defines type parameter, which is used in super of local class
    CtReturn<?> retStmt = (CtReturn<?>) ctClass.getMethodsByName("method").get(0).getBody().getStatements().get(0);
    CtNewClass<?> newClassExpr = (CtNewClass<?>) retStmt.getReturnedExpression();
    CtType<?> declaringType = newClassExpr.getAnonymousClass();
    CtMethod<?> m1 = declaringType.getMethodsByName("iterator").get(0);
    ClassTypingContext c = new ClassTypingContext(declaringType);
    // the adaptation of such type parameter keeps that parameter as it is.
    assertFalse(c.isOverriding(m1, declaringType.getSuperclass().getTypeDeclaration().getMethodsByName("add").get(0)));
    assertTrue(c.isOverriding(m1, declaringType.getSuperclass().getTypeDeclaration().getMethodsByName("iterator").get(0)));
}
Also used : ClassTypingContext(spoon.support.visitor.ClassTypingContext) CtReturn(spoon.reflect.code.CtReturn) CtNewClass(spoon.reflect.code.CtNewClass) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 7 with CtNewClass

use of spoon.reflect.code.CtNewClass 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)

Example 8 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class TypeTest method test.

@Test
public void test() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/TorIntegration.java");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.buildModel();
    CtType<?> ctType = launcher.getFactory().Class().getAll().get(0);
    List<CtNewClass> elements = ctType.getElements(new TypeFilter<>(CtNewClass.class));
    assertEquals(4, elements.size());
    for (int i = 0; i < elements.size(); i++) {
        CtNewClass ctNewClass = elements.get(i);
        assertEquals("android.content.DialogInterface$OnClickListener", ctNewClass.getAnonymousClass().getSuperclass().getQualifiedName());
    }
}
Also used : CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 9 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class TypeFactory method get.

/**
 * Gets a created type from its qualified name.
 *
 * @return a found type or null if does not exist
 */
@SuppressWarnings("unchecked")
public <T> CtType<T> get(final String qualifiedName) {
    int packageIndex = qualifiedName.lastIndexOf(CtPackage.PACKAGE_SEPARATOR);
    CtPackage pack;
    if (packageIndex > 0) {
        pack = factory.Package().get(qualifiedName.substring(0, packageIndex));
    } else {
        pack = factory.Package().getRootPackage();
    }
    if (pack != null) {
        CtType<T> type = pack.getType(qualifiedName.substring(packageIndex + 1));
        if (type != null) {
            return type;
        }
    }
    int inertTypeIndex = qualifiedName.lastIndexOf(CtType.INNERTTYPE_SEPARATOR);
    if (inertTypeIndex > 0) {
        String s = qualifiedName.substring(0, inertTypeIndex);
        CtType<T> t = factory.Type().get(s);
        if (t == null) {
            return null;
        }
        String className = qualifiedName.substring(inertTypeIndex + 1);
        final CtTypeReference<T> reference = t.getReference();
        if (reference.isLocalType()) {
            final List<CtClass<T>> enclosingClasses = t.getElements(new TypeFilter<CtClass<T>>(CtClass.class) {

                @Override
                public boolean matches(CtClass<T> element) {
                    return super.matches(element) && element.getQualifiedName().equals(qualifiedName);
                }
            });
            if (enclosingClasses.size() == 0) {
                return null;
            }
            return enclosingClasses.get(0);
        }
        try {
            // If the class name can't be parsed in integer, the method throws an exception.
            // If the class name is an integer, the class is an anonymous class, otherwise,
            // it is a standard class.
            Integer.parseInt(className);
            final List<CtNewClass> anonymousClasses = t.getElements(new TypeFilter<CtNewClass>(CtNewClass.class) {

                @Override
                public boolean matches(CtNewClass element) {
                    return super.matches(element) && element.getAnonymousClass().getQualifiedName().equals(qualifiedName);
                }
            });
            if (anonymousClasses.size() == 0) {
                return null;
            }
            return anonymousClasses.get(0).getAnonymousClass();
        } catch (NumberFormatException e) {
            return t.getNestedType(className);
        }
    }
    return null;
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) CtPackage(spoon.reflect.declaration.CtPackage)

Example 10 with CtNewClass

use of spoon.reflect.code.CtNewClass in project spoon by INRIA.

the class GenericsTest method testNewClassGenerics.

@Test
public void testNewClassGenerics() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/generics/testclasses/", "-o", "./target/spooned/" });
    final CtClass<?> aTacos = launcher.getFactory().Class().get(Tacos.class);
    final CtMethod<?> m = aTacos.getMethodsByName("m3").get(0);
    final CtNewClass newClass1 = m.getBody().getStatement(0).getElements(new TypeFilter<>(CtNewClass.class)).get(0);
    assertEquals(0, newClass1.getActualTypeArguments().size());
    assertEquals(2, newClass1.getType().getActualTypeArguments().size());
    assertEquals("new javax.lang.model.util.SimpleTypeVisitor7<spoon.test.generics.testclasses.Tacos, java.lang.Void>() {}", newClass1.toString());
    final CtNewClass newClass2 = m.getBody().getStatement(1).getElements(new TypeFilter<>(CtNewClass.class)).get(0);
    assertEquals(0, newClass2.getActualTypeArguments().size());
    assertEquals(2, newClass2.getType().getActualTypeArguments().size());
    assertEquals("new javax.lang.model.util.SimpleTypeVisitor7<spoon.test.generics.testclasses.Tacos, java.lang.Void>() {}", newClass2.toString());
    canBeBuilt("./target/spooned/spoon/test/generics/testclasses/", 8);
}
Also used : CtNewClass(spoon.reflect.code.CtNewClass) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Aggregations

CtNewClass (spoon.reflect.code.CtNewClass)12 Test (org.junit.Test)10 CtClass (spoon.reflect.declaration.CtClass)8 Launcher (spoon.Launcher)6 Factory (spoon.reflect.factory.Factory)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 MainTest (spoon.test.main.MainTest)3 CtModel (spoon.reflect.CtModel)2 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Before (org.junit.Before)1 SpoonModelBuilder (spoon.SpoonModelBuilder)1 CtReturn (spoon.reflect.code.CtReturn)1 CtElement (spoon.reflect.declaration.CtElement)1 CtInterface (spoon.reflect.declaration.CtInterface)1 CtMethod (spoon.reflect.declaration.CtMethod)1 CtPackage (spoon.reflect.declaration.CtPackage)1 CtTypeParameter (spoon.reflect.declaration.CtTypeParameter)1 CompositeFilter (spoon.reflect.visitor.filter.CompositeFilter)1