Search in sources :

Example 81 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class NoClasspathTest method test.

@Test
public void test() throws Exception {
    // do we still have a correct model when the complete classpath is not given as input?
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setNoClasspath(true);
    spoon.getEnvironment().setLevel("OFF");
    spoon.addInputResource("./src/test/resources/spoon/test/noclasspath/fields");
    spoon.getEnvironment().setSourceOutputDirectory(new File("target/spooned/apitest"));
    spoon.run();
    Factory factory = spoon.getFactory();
    CtClass<Object> clazz = factory.Class().get("Foo");
    assertEquals("Foo", clazz.getSimpleName());
    CtTypeReference<?> superclass = clazz.getSuperclass();
    // "Unknown" is not in the classpath at all
    assertEquals("Unknown", superclass.getSimpleName());
    try {
        superclass.getActualClass();
        fail();
    } catch (SpoonClassNotFoundException e) {
    // expected
    }
    assertNull(superclass.getDeclaration());
    // should be empty as in noClasspath the actual class cannot be retrieved
    assertTrue(superclass.getAllFields().isEmpty());
    // now we really make sure we don't have the class in the classpath
    try {
        superclass.getActualClass();
        fail();
    } catch (SpoonClassNotFoundException e) {
    // expected
    }
    {
        CtMethod<?> method = clazz.getMethod("method", new CtTypeReference[0]);
        assertNotNull(method);
        List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
        assertEquals(1, invocations.size());
        CtInvocation<?> c = invocations.get(0);
        assertEquals("method", c.getExecutable().getSimpleName());
        assertEquals("x.method()", method.getBody().getStatement(1).toString());
    }
    {
        CtMethod<?> method = clazz.getMethod("m2", new CtTypeReference[0]);
        assertNotNull(method);
        List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
        assertEquals(3, invocations.size());
        CtInvocation<?> c = invocations.get(1);
        assertEquals("second", c.getExecutable().getSimpleName());
        assertEquals("x.first().second().third()", method.getBody().getStatement(1).toString());
    }
    {
        CtMethod<?> method = clazz.getMethod("m1", new CtTypeReference[0]);
        assertNotNull(method);
        List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
        assertEquals(1, invocations.size());
        invocations.get(0);
        assertEquals("x.y.z.method()", method.getBody().getStatement(0).toString());
    }
    {
        CtMethod<?> method = clazz.getMethod("m3", new CtTypeReference[0]);
        assertNotNull(method);
        List<CtInvocation<?>> invocations = method.getElements(new TypeFilter<CtInvocation<?>>(CtInvocation.class));
        assertEquals(1, invocations.size());
        invocations.get(0);
        CtLocalVariable<?> statement = method.getBody().getStatement(0);
        CtFieldAccess<?> fa = (CtFieldAccess<?>) statement.getDefaultExpression();
        assertTrue(fa.getTarget() instanceof CtInvocation);
        assertEquals("field", fa.getVariable().getSimpleName());
        assertEquals("int x = first().field", statement.toString());
    }
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) CtInvocation(spoon.reflect.code.CtInvocation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) SpoonClassNotFoundException(spoon.support.SpoonClassNotFoundException) List(java.util.List) File(java.io.File) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 82 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class NoClasspathTest method testInheritanceInNoClassPathWithClasses.

@Test
public void testInheritanceInNoClassPathWithClasses() throws IOException {
    // contract: when using noclasspath in combination with a source classpath
    // spoon is able to resolve the inheritance between classes contained in source cp
    String sourceInputDirPath = "./src/test/resources/spoon/test/inheritance";
    String targetBinPath = "./target/spoon-nocp-bin";
    Launcher spoon = new Launcher();
    spoon.getEnvironment().setShouldCompile(true);
    spoon.addInputResource(sourceInputDirPath);
    spoon.setBinaryOutputDirectory(targetBinPath);
    spoon.run();
    spoon = new Launcher();
    spoon.getEnvironment().setNoClasspath(true);
    spoon.getEnvironment().setSourceClasspath(new String[] { targetBinPath });
    spoon.addInputResource(sourceInputDirPath + "/AnotherClass.java");
    spoon.buildModel();
    CtType anotherclass = spoon.getFactory().Type().get("org.acme.AnotherClass");
    assertEquals(1, anotherclass.getFields().size());
    CtField field = (CtField) anotherclass.getFields().get(0);
    CtTypeReference myClassReference = spoon.getFactory().Type().createReference("fr.acme.MyClass");
    assertEquals(myClassReference, field.getType());
    assertNotNull(myClassReference.getActualClass());
    CtTypeReference myInterfaceReference = spoon.getFactory().Type().createReference("org.myorganization.MyInterface");
    assertTrue(myClassReference.isSubtypeOf(myInterfaceReference));
    assertTrue(field.getType().isSubtypeOf(myInterfaceReference));
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 83 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class AnnotationTest method annotationOverrideFQNIsOK.

@Test
public void annotationOverrideFQNIsOK() {
    Launcher spoon = new Launcher();
    Factory factory = spoon.getFactory();
    factory.getEnvironment().setNoClasspath(true);
    spoon.addInputResource("./src/test/resources/noclasspath/annotation/issue1307/SpecIterator.java");
    spoon.buildModel();
    List<CtAnnotation> overrideAnnotations = factory.getModel().getElements(new TypeFilter<CtAnnotation>(CtAnnotation.class));
    for (CtAnnotation annotation : overrideAnnotations) {
        CtTypeReference typeRef = annotation.getAnnotationType();
        if (typeRef.getSimpleName().equals("Override")) {
            assertThat(typeRef.getQualifiedName(), is("java.lang.Override"));
        }
    }
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 84 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class CastTest method testTypeAnnotationOnCast.

@Test
public void testTypeAnnotationOnCast() throws Exception {
    final CtType<Castings> aCastings = buildClass(Castings.class);
    final CtLocalVariable local = aCastings.getMethod("bar").getElements(new TypeFilter<>(CtLocalVariable.class)).get(0);
    assertEquals(1, ((CtTypeReference) local.getDefaultExpression().getTypeCasts().get(0)).getAnnotations().size());
    assertEquals("((java.lang.@spoon.test.casts.Castings.TypeAnnotation(integer = 1)" + System.lineSeparator() + "String) (\"\"))", local.getDefaultExpression().toString());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtLocalVariable(spoon.reflect.code.CtLocalVariable) Test(org.junit.Test)

Example 85 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project spoon by INRIA.

the class ReplaceScanner method getGetterType.

private CtTypeReference getGetterType(Factory factory, CtInvocation getter) {
    CtTypeReference getterType;
    final CtTypeReference type = getter.getType();
    if (type instanceof CtTypeParameterReference) {
        getterType = getTypeFromTypeParameterReference((CtTypeParameterReference) getter.getExecutable().getDeclaration().getType());
    } else {
        getterType = type.clone();
    }
    getterType.getActualTypeArguments().clear();
    return getterType;
}
Also used : CtTypeParameterReference(spoon.reflect.reference.CtTypeParameterReference) CtTypeReference(spoon.reflect.reference.CtTypeReference)

Aggregations

CtTypeReference (spoon.reflect.reference.CtTypeReference)121 Test (org.junit.Test)56 Launcher (spoon.Launcher)43 Factory (spoon.reflect.factory.Factory)32 CtMethod (spoon.reflect.declaration.CtMethod)26 CtType (spoon.reflect.declaration.CtType)24 ArrayList (java.util.ArrayList)22 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)17 CtClass (spoon.reflect.declaration.CtClass)16 CtField (spoon.reflect.declaration.CtField)16 List (java.util.List)15 CtElement (spoon.reflect.declaration.CtElement)14 CtInvocation (spoon.reflect.code.CtInvocation)13 CtReference (spoon.reflect.reference.CtReference)13 CtParameter (spoon.reflect.declaration.CtParameter)12 CtExpression (spoon.reflect.code.CtExpression)11 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)11 CtTypeParameterReference (spoon.reflect.reference.CtTypeParameterReference)11 CtStatement (spoon.reflect.code.CtStatement)9 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)9