Search in sources :

Example 1 with SpoonResource

use of spoon.compiler.SpoonResource 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 2 with SpoonResource

use of spoon.compiler.SpoonResource in project spoon by INRIA.

the class TypeReferenceTest method doNotCloseLoader.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void doNotCloseLoader() throws Exception {
    /* Given the following scenario:
		 * 	- ClassA has a field of ClassB.
		 *	- ClassB has a field of ClassC.
		 * 	- Spoon only models ClassA.
		 *
		 * We want to get the field of ClassB, which should be accessible because
		 * the definitions of ClassB and ClassC were provided in the class path.
		 */
    SpoonModelBuilder comp = new Launcher().createCompiler();
    Factory factory = comp.getFactory();
    String qualifiedName = "spoontest.a.ClassA";
    String referenceQualifiedName = "spoontest.b.ClassB";
    // we only create the model for ClassA
    List<SpoonResource> fileToBeSpooned = SpoonResourceHelper.resources("./src/test/resources/reference-test-2/" + qualifiedName.replace('.', '/') + ".java");
    comp.addInputSources(fileToBeSpooned);
    // for ClassA
    assertEquals(1, fileToBeSpooned.size());
    // Spoon requires the binary version of dependencies
    List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test-2/ReferenceTest2.jar");
    String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
    factory.getEnvironment().setSourceClasspath(dependencyClasspath);
    assertEquals(1, classpath.size());
    // now we can build the model
    comp.build();
    // we can get the model of ClassA
    CtType<?> theClass = factory.Type().get(qualifiedName);
    // we get ClassA's field of type ClassB
    List<CtField<?>> fields = theClass.getFields();
    assertEquals(1, fields.size());
    CtField<?> bField = fields.get(0);
    CtTypeReference referencedType = bField.getType();
    assertEquals(referenceQualifiedName, referencedType.getQualifiedName());
    // we get ClassB's field of type ClassC
    Collection<CtFieldReference<?>> fieldsOfB = referencedType.getAllFields();
    if (fieldsOfB.size() == 2) {
        // Jacoco instruments all dependencies with an agent.
        // So, when we use reflection on ClassB, we don't have one field but two fields.
        // First, it is the field of ClassB. Second, it is the field of Jacoco.
        final CtFieldReference<?> potentialJacoco = (CtFieldReference<?>) fieldsOfB.toArray()[1];
        if ("$jacocoData".equals(potentialJacoco.getSimpleName())) {
            fieldsOfB.remove(potentialJacoco);
        }
    }
    assertEquals(1, fieldsOfB.size());
    CtFieldReference<?> cField = fieldsOfB.iterator().next();
    assertEquals("spoontest.c.ClassC", cField.getType().getQualifiedName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtField(spoon.reflect.declaration.CtField) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 3 with SpoonResource

use of spoon.compiler.SpoonResource in project spoon by INRIA.

the class DefaultPrettyPrinterTest method printerCanPrintInvocationWithoutException.

@Test
public void printerCanPrintInvocationWithoutException() throws Exception {
    String packageName = "spoon.test.subclass.prettyprinter";
    String className = "DefaultPrettyPrinterExample";
    String qualifiedName = packageName + "." + className;
    SpoonModelBuilder comp = new Launcher().createCompiler();
    List<SpoonResource> fileToBeSpooned = SpoonResourceHelper.resources("./src/test/resources/printer-test/" + qualifiedName.replace('.', '/') + ".java");
    assertEquals(1, fileToBeSpooned.size());
    comp.addInputSources(fileToBeSpooned);
    List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/printer-test/DefaultPrettyPrinterDependency.jar");
    assertEquals(1, classpath.size());
    comp.setSourceClasspath(classpath.get(0).getPath());
    comp.build();
    Factory factory = comp.getFactory();
    CtType<?> theClass = factory.Type().get(qualifiedName);
    List<CtInvocation<?>> elements = Query.getElements(theClass, new TypeFilter<CtInvocation<?>>(CtInvocation.class));
    assertEquals(3, elements.size());
    CtInvocation<?> mathAbsInvocation = elements.get(1);
    assertEquals("java.lang.Math.abs(message.length())", mathAbsInvocation.toString());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) CtInvocation(spoon.reflect.code.CtInvocation) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 4 with SpoonResource

use of spoon.compiler.SpoonResource in project spoon by INRIA.

the class DefaultPrettyPrinterTest method superInvocationWithEnclosingInstance.

@Test
public void superInvocationWithEnclosingInstance() throws Exception {
    /**
     * To extend a nested class an enclosing instance must be provided
     * to call the super constructor.
     */
    String sourcePath = "./src/test/resources/spoon/test/prettyprinter/NestedSuperCall.java";
    List<SpoonResource> files = SpoonResourceHelper.resources(sourcePath);
    assertEquals(1, files.size());
    SpoonModelBuilder comp = new Launcher().createCompiler();
    comp.addInputSources(files);
    comp.build();
    Factory factory = comp.getFactory();
    CtType<?> theClass = factory.Type().get("spoon.test.prettyprinter.NestedSuperCall");
    assertTrue(theClass.toString().contains("nc.super(\"a\")"));
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) Launcher(spoon.Launcher) Factory(spoon.reflect.factory.Factory) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Example 5 with SpoonResource

use of spoon.compiler.SpoonResource in project spoon by INRIA.

the class TypeReferenceTest method loadReferencedClassFromClasspath.

@SuppressWarnings("rawtypes")
@Test
public void loadReferencedClassFromClasspath() throws Exception {
    SpoonModelBuilder comp = new Launcher().createCompiler();
    Factory factory = comp.getFactory();
    String packageName = "spoon.test.reference";
    String className = "ReferencingClass";
    String qualifiedName = packageName + "." + className;
    String referencedQualifiedName = packageName + "." + "ReferencedClass";
    // we only create the model for ReferecingClass
    List<SpoonResource> fileToBeSpooned = SpoonResourceHelper.resources("./src/test/resources/reference-test/input/" + qualifiedName.replace('.', '/') + ".java");
    comp.addInputSources(fileToBeSpooned);
    // for ReferecingClass
    assertEquals(1, fileToBeSpooned.size());
    // Spoon requires the binary version of ReferencedClass
    List<SpoonResource> classpath = SpoonResourceHelper.resources("./src/test/resources/reference-test/ReferenceTest.jar");
    String[] dependencyClasspath = new String[] { classpath.get(0).getPath() };
    factory.getEnvironment().setSourceClasspath(dependencyClasspath);
    assertEquals(1, classpath.size());
    // now we can build the model
    comp.build();
    // we can get the model of ReferecingClass
    CtType<?> theClass = factory.Type().get(qualifiedName);
    // now we retrieve the reference to ReferencedClass
    CtTypeReference referencedType = null;
    ReferenceTypeFilter<CtTypeReference> referenceTypeFilter = new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class);
    List<CtTypeReference> elements = Query.getElements(theClass, referenceTypeFilter);
    for (CtTypeReference reference : elements) {
        if (reference.getQualifiedName().equals(referencedQualifiedName)) {
            referencedType = reference;
            break;
        }
    }
    assertFalse(referencedType == null);
    // we can get the actual class from the reference, because it is loaded from the class path
    Class referencedClass = referencedType.getActualClass();
    assertEquals(referencedQualifiedName, referencedClass.getName());
}
Also used : SpoonModelBuilder(spoon.SpoonModelBuilder) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) ModelUtils.buildClass(spoon.testing.utils.ModelUtils.buildClass) CtNewClass(spoon.reflect.code.CtNewClass) CtClass(spoon.reflect.declaration.CtClass) SpoonResource(spoon.compiler.SpoonResource) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)7 Launcher (spoon.Launcher)7 SpoonResource (spoon.compiler.SpoonResource)7 Factory (spoon.reflect.factory.Factory)6 SpoonModelBuilder (spoon.SpoonModelBuilder)5 CtTypeReference (spoon.reflect.reference.CtTypeReference)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 CtAssignment (spoon.reflect.code.CtAssignment)1 CtInvocation (spoon.reflect.code.CtInvocation)1 CtNewClass (spoon.reflect.code.CtNewClass)1 CtClass (spoon.reflect.declaration.CtClass)1 CtEnum (spoon.reflect.declaration.CtEnum)1 CtField (spoon.reflect.declaration.CtField)1 CtType (spoon.reflect.declaration.CtType)1 CtFieldReference (spoon.reflect.reference.CtFieldReference)1 ReferenceTypeFilter (spoon.reflect.visitor.filter.ReferenceTypeFilter)1 VirtualFile (spoon.support.compiler.VirtualFile)1 ModelUtils.buildClass (spoon.testing.utils.ModelUtils.buildClass)1