Search in sources :

Example 11 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CtTypeParameterTest method testTypeSame.

@Test
public void testTypeSame() throws Exception {
    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);
    CtConstructor<?> ctModelCons = ctModel.getConstructors().iterator().next();
    CtMethod<?> ctModelMethod = ctModel.getMethodsByName("method").get(0);
    CtMethod<?> ctModelMethod2 = ctModel.getMethodsByName("method2").get(0);
    CtClass<?> ctModelB = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelB")).first();
    CtTypeParameter tpA2 = ctModelB.getFormalCtTypeParameters().get(0);
    CtTypeParameter tpB2 = ctModelB.getFormalCtTypeParameters().get(1);
    CtTypeParameter tpC2 = ctModelB.getFormalCtTypeParameters().get(2);
    CtTypeParameter tpD2 = ctModelB.getFormalCtTypeParameters().get(3);
    CtConstructor<?> ctModelBCons = ctModelB.getConstructors().iterator().next();
    CtMethod<?> ctModelBMethod = ctModelB.getMethodsByName("method").get(0);
    // the type parameters of ErasureModelA and ErasureModelA$ModelB are same if they are on the same position.
    checkIsSame(ctModel.getFormalCtTypeParameters(), ctModelB.getFormalCtTypeParameters(), true);
    // the type parameters of ErasureModelA#constructor and ErasureModelA$ModelB constructor are same, because constructors has same formal type parameters
    // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
    checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBCons.getFormalCtTypeParameters(), true);
    // the type parameters of ctModel ErasureModelA#method and ErasureModelA$ModelB#method are same if they are on the same position.
    checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), true);
    // the type parameters of ctModel ErasureModelA#constructor and ErasureModelA$ModelB#method are never same, because they have different type of scope (Method!=Constructor)
    checkIsSame(ctModelCons.getFormalCtTypeParameters(), ctModelBMethod.getFormalCtTypeParameters(), false);
    // the type parameters of ctModel ErasureModelA#method and ErasureModelA#method2 are same, because they have same formal type parameters
    // https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#jls-8.4.4
    checkIsSame(ctModelMethod.getFormalCtTypeParameters(), ctModelMethod2.getFormalCtTypeParameters(), true);
    CtClass<?> ctModelC = ctModel.filterChildren(new NamedElementFilter<>(CtClass.class, "ModelC")).first();
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtTypeParameter(spoon.reflect.declaration.CtTypeParameter) ErasureModelA(spoon.test.ctType.testclasses.ErasureModelA) NamedElementFilter(spoon.reflect.visitor.filter.NamedElementFilter) Test(org.junit.Test)

Example 12 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CompilationTest method testNewInstanceFromExistingClass.

@Test
public void testNewInstanceFromExistingClass() throws Exception {
    CtClass<Bar> barCtType = (CtClass<Bar>) ModelUtils.buildClass(Bar.class);
    CtReturn<Integer> m = barCtType.getMethod("m").getBody().getStatement(0);
    // we cannot use Bar because it causes a runtime cast exception (2 different Bar from different classloader)
    IBar bar = barCtType.newInstance();
    int value = bar.m();
    assertEquals(1, value);
    // change the return value
    m.setReturnedExpression(m.getFactory().Code().createLiteral(2));
    bar = barCtType.newInstance();
    value = bar.m();
    assertEquals(2, value);
    m.replace(m.getFactory().Code().createCodeSnippetStatement("throw new FooEx()"));
    try {
        bar = barCtType.newInstance();
        value = bar.m();
        fail();
    } catch (Exception ignore) {
    }
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Bar(spoon.test.compilation.testclasses.Bar) IBar(spoon.test.compilation.testclasses.IBar) IBar(spoon.test.compilation.testclasses.IBar) SpoonException(spoon.SpoonException) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) Test(org.junit.Test)

Example 13 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class CompilationTest method testPrecompile.

@Test
public void testPrecompile() {
    // without precompile
    Launcher l = new Launcher();
    l.setArgs(new String[] { "--noclasspath", "-i", "src/test/resources/compilation/" });
    l.buildModel();
    CtClass klass = l.getFactory().Class().get("compilation.Bar");
    // without precompile, actualClass does not exist (an exception is thrown)
    try {
        klass.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
        fail();
    } catch (SpoonClassNotFoundException ignore) {
    }
    // with precompile
    Launcher l2 = new Launcher();
    l2.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/" });
    l2.buildModel();
    CtClass klass2 = l2.getFactory().Class().get("compilation.Bar");
    // with precompile, actualClass is not null
    Class actualClass = klass2.getSuperInterfaces().toArray(new CtTypeReference[0])[0].getActualClass();
    assertNotNull(actualClass);
    assertEquals("IBar", actualClass.getSimpleName());
    // precompile can be used to compile processors on the fly
    Launcher l3 = new Launcher();
    l3.setArgs(new String[] { "--precompile", "--noclasspath", "-i", "src/test/resources/compilation/", "-p", "compilation.SimpleProcessor" });
    l3.run();
}
Also used : CtClass(spoon.reflect.declaration.CtClass) Launcher(spoon.Launcher) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) CtClass(spoon.reflect.declaration.CtClass) Test(org.junit.Test)

Example 14 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class NewClassTest method setUp.

@Before
public void setUp() throws Exception {
    final Factory build = build(Foo.class);
    final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo.class);
    newClasses = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {

        @Override
        public boolean matches(CtNewClass<?> element) {
            return true;
        }
    });
}
Also used : CtClass(spoon.reflect.declaration.CtClass) AbstractFilter(spoon.reflect.visitor.filter.AbstractFilter) CtNewClass(spoon.reflect.code.CtNewClass) Foo(spoon.test.constructorcallnewclass.testclasses.Foo) Factory(spoon.reflect.factory.Factory) Before(org.junit.Before)

Example 15 with CtClass

use of spoon.reflect.declaration.CtClass in project spoon by INRIA.

the class NewClassTest method testMoreThan9NewClass.

@Test
public void testMoreThan9NewClass() throws Exception {
    final Factory build = build(Foo2.class);
    final CtClass<?> foo = (CtClass<?>) build.Type().get(Foo2.class);
    List<CtNewClass<?>> elements = foo.getElements(new AbstractFilter<CtNewClass<?>>(CtNewClass.class) {

        @Override
        public boolean matches(CtNewClass<?> element) {
            return true;
        }
    });
    assertEquals(13, elements.size());
    assertEquals(Foo2.class.getCanonicalName() + "$12", elements.get(11).getAnonymousClass().getQualifiedName());
    assertEquals(Foo2.class.getCanonicalName() + "$12$1", elements.get(12).getAnonymousClass().getQualifiedName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) Factory(spoon.reflect.factory.Factory) Foo2(spoon.test.constructorcallnewclass.testclasses.Foo2) Test(org.junit.Test)

Aggregations

CtClass (spoon.reflect.declaration.CtClass)168 Test (org.junit.Test)151 Launcher (spoon.Launcher)102 Factory (spoon.reflect.factory.Factory)84 CtMethod (spoon.reflect.declaration.CtMethod)42 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)22 CtAnnotation (spoon.reflect.declaration.CtAnnotation)19 SpoonModelBuilder (spoon.SpoonModelBuilder)17 CtInvocation (spoon.reflect.code.CtInvocation)16 File (java.io.File)15 CtTypeReference (spoon.reflect.reference.CtTypeReference)15 OuterAnnotation (spoon.test.annotation.testclasses.Foo.OuterAnnotation)15 Annotation (java.lang.annotation.Annotation)14 AbstractFilter (spoon.reflect.visitor.filter.AbstractFilter)14 AnnotationDefaultAnnotation (spoon.test.annotation.testclasses.AnnotationDefaultAnnotation)14 InnerAnnotation (spoon.test.annotation.testclasses.Foo.InnerAnnotation)14 MiddleAnnotation (spoon.test.annotation.testclasses.Foo.MiddleAnnotation)14 GlobalAnnotation (spoon.test.annotation.testclasses.GlobalAnnotation)14 SuperAnnotation (spoon.test.annotation.testclasses.SuperAnnotation)14 TypeAnnotation (spoon.test.annotation.testclasses.TypeAnnotation)14