Search in sources :

Example 56 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference 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 57 with CtTypeReference

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

the class TypeReferenceTest method testPackageInNoClasspath.

@Test
public void testPackageInNoClasspath() {
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/Demo.java");
    launcher.setSourceOutputDirectory("./target/class-declaration");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("Demo");
    final Set<CtTypeReference<?>> referencedTypes = aClass.getReferencedTypes();
    boolean containsDemoReference = false;
    boolean containsVoidReference = false;
    boolean containsStringReference = false;
    boolean containsJoinerReference = false;
    for (Iterator<CtTypeReference<?>> iterator = referencedTypes.iterator(); iterator.hasNext(); ) {
        CtTypeReference<?> reference = iterator.next();
        if (reference.toString().equals("Demo")) {
            containsDemoReference = true;
        } else if (reference.toString().equals("void")) {
            containsVoidReference = true;
        } else if (reference.toString().equals("java.lang.String")) {
            containsStringReference = true;
        } else if (reference.toString().equals("com.google.common.base.Joiner")) {
            containsJoinerReference = true;
        }
    }
    assertTrue("Reference to Demo is missing", containsDemoReference);
    assertTrue("Reference to void is missing", containsVoidReference);
    assertTrue("Reference to String is missing", containsStringReference);
    assertTrue("Reference to Joiner is missing", containsJoinerReference);
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 58 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class AmplificationHelper method createAmplifiedTest.

public static CtType createAmplifiedTest(List<CtMethod<?>> ampTest, CtType<?> classTest, Minimizer minimizer) {
    CtType amplifiedTest = classTest.clone();
    final String amplifiedName = classTest.getSimpleName().startsWith("Test") ? classTest.getSimpleName() + "Ampl" : "Ampl" + classTest.getSimpleName();
    amplifiedTest.setSimpleName(amplifiedName);
    classTest.getMethods().stream().filter(AmplificationChecker::isTest).forEach(amplifiedTest::removeMethod);
    if (minimize) {
        ampTest.stream().map(minimizer::minimize).forEach(amplifiedTest::addMethod);
    } else {
        ampTest.forEach(amplifiedTest::addMethod);
    }
    final CtTypeReference classTestReference = classTest.getReference();
    amplifiedTest.getElements(new TypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference element) {
            return element.equals(classTestReference) && super.matches(element);
        }
    }).forEach(ctTypeReference -> ctTypeReference.setSimpleName(amplifiedName));
    classTest.getPackage().addType(amplifiedTest);
    return amplifiedTest;
}
Also used : CtType(spoon.reflect.declaration.CtType) CtTypeReference(spoon.reflect.reference.CtTypeReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter)

Example 59 with CtTypeReference

use of spoon.reflect.reference.CtTypeReference in project dspot by STAMP-project.

the class ConstructorCreator method generateConstructorUsingFactory.

static List<CtExpression<?>> generateConstructorUsingFactory(CtTypeReference type) {
    // this method will return an invocation of method that return the given type.
    // the usage of Factory classes/methods is well spread
    final Factory factory = type.getFactory();
    final List<CtMethod<?>> factoryMethod = factory.getModel().getElements(new FILTER_FACTORY_METHOD(type));
    return factoryMethod.stream().map(method -> factory.createInvocation(factory.createTypeAccess(method.getParent(CtType.class).getReference(), true), method.getReference(), method.getParameters().stream().map(parameter -> ValueCreator.generateRandomValue(parameter.getType())).collect(Collectors.toList()))).collect(Collectors.toList());
}
Also used : TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtTypeReference(spoon.reflect.reference.CtTypeReference) Arrays(java.util.Arrays) List(java.util.List) AmplificationHelper(fr.inria.diversify.utils.AmplificationHelper) CtExpression(spoon.reflect.code.CtExpression) spoon.reflect.declaration(spoon.reflect.declaration) CtConstructorCall(spoon.reflect.code.CtConstructorCall) Factory(spoon.reflect.factory.Factory) Collections(java.util.Collections) Collectors(java.util.stream.Collectors) Factory(spoon.reflect.factory.Factory)

Example 60 with CtTypeReference

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

the class DefaultJavaPrettyPrinter method isImported.

private boolean isImported(CtExecutableReference executableReference) {
    CtImport executableImport = executableReference.getFactory().createImport(executableReference);
    if (this.imports.contains(executableImport)) {
        return true;
    } else {
        if (executableReference.getDeclaringType() == null) {
            return false;
        }
        CtTypeReference staticTypeMemberReference = executableReference.getFactory().Type().createWildcardStaticTypeMemberReference(executableReference.getDeclaringType());
        CtImport staticClassImport = executableReference.getFactory().createImport(staticTypeMemberReference);
        return this.imports.contains(staticClassImport);
    }
}
Also used : CtImport(spoon.reflect.declaration.CtImport) 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