Search in sources :

Example 71 with CtTypeReference

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

Example 72 with CtTypeReference

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

the class TypeReferenceTest method testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath.

@Test
public void testAnonymousClassesHaveAnEmptyStringForItsNameInNoClasspath() throws Exception {
    // contract: In no classpath mode, a type reference have an empty string for its name.
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/resources/noclasspath/A.java");
    launcher.setSourceOutputDirectory("./target/class-declaration");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("A");
    final CtClass anonymousClass = aClass.getElements(new TypeFilter<>(CtNewClass.class)).get(0).getAnonymousClass();
    assertEquals("1", anonymousClass.getReference().getSimpleName());
    Set<CtTypeReference<?>> referencedTypes = aClass.getReferencedTypes();
    List<String> referencedTypeNames = referencedTypes.stream().map(Object::toString).collect(Collectors.toList());
    assertEquals(7, referencedTypeNames.size());
    assertTrue(referencedTypeNames.contains("A"));
    assertTrue(referencedTypeNames.contains("example.B"));
    assertTrue(referencedTypeNames.contains("java.lang.Runnable"));
    assertTrue(referencedTypeNames.contains("java.lang.Override"));
    assertTrue(referencedTypeNames.contains("java.lang.Object"));
    assertTrue(referencedTypeNames.contains("A.1"));
    assertTrue(referencedTypeNames.contains("void"));
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtNewClass(spoon.reflect.code.CtNewClass) CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 73 with CtTypeReference

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

the class ReplaceTest method testReplaceAllTypeRefenceWithGenerics.

@Test
public void testReplaceAllTypeRefenceWithGenerics() throws Exception {
    // contract: replace all type references with a generic to the same type reference without generics.
    final Factory factory = build(Tacos.class);
    final List<CtTypeReference> references = Query.getElements(factory, new ReferenceTypeFilter<CtTypeReference>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference reference) {
            return reference.getActualTypeArguments().size() > 0 && super.matches(reference);
        }
    });
    references.get(0).replace(factory.Type().createReference(references.get(0).getQualifiedName()));
    final CtType<Tacos> aTacos = factory.Type().get(Tacos.class);
    final CtMethod<?> aMethod = aTacos.getMethodsByName("m2").get(0);
    final CtTypeReference<Object> expected = factory.Type().createReference("spoon.test.replace.testclasses.Tacos");
    assertEquals(expected, aMethod.getType());
    assertEquals(expected.getTypeDeclaration(), aMethod.getElements(new TypeFilter<>(CtConstructorCall.class)).get(0).getType().getTypeDeclaration());
}
Also used : CtConstructorCall(spoon.reflect.code.CtConstructorCall) CtTypeReference(spoon.reflect.reference.CtTypeReference) Tacos(spoon.test.replace.testclasses.Tacos) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 74 with CtTypeReference

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

the class ReplaceTest method testReplaceAPackageReferenceByAnotherOne.

@Test
public void testReplaceAPackageReferenceByAnotherOne() throws Exception {
    // contract: replace a package reference of a reference to another package.
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "--output-type", "nooutput" });
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/reference-package");
    launcher.run();
    final CtType<Object> panini = launcher.getFactory().Type().get("Panini");
    final CtTypeReference<?> burritos = panini.getElements(new ReferenceTypeFilter<CtTypeReference<?>>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference<?> reference) {
            return "Burritos".equals(reference.getSimpleName()) && super.matches(reference);
        }
    }).get(0);
    assertEquals("com.awesome", burritos.getPackage().toString());
    assertEquals("com.awesome.Burritos", panini.getMethodsByName("m").get(0).getType().toString());
    burritos.getPackage().replace(launcher.getFactory().Package().createReference("com.best"));
    assertEquals("com.best", burritos.getPackage().toString());
    assertEquals("com.best.Burritos", panini.getMethodsByName("m").get(0).getType().toString());
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 75 with CtTypeReference

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

the class RefactoringTest method testRefactoringClassChangeAllCtTypeReferenceAssociatedWithClassConcerned.

@Test
public void testRefactoringClassChangeAllCtTypeReferenceAssociatedWithClassConcerned() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring" });
    launcher.run();
    final CtClass<?> aClass = launcher.getFactory().Class().get(AClass.class);
    assertNotNull(aClass);
    launcher.setArgs(new String[] { "-i", "src/test/java/spoon/test/refactoring/testclasses", "-o", "target/spooned/refactoring", "-p", ThisTransformationProcessor.class.getName() });
    launcher.run();
    final CtClass<?> classNotAccessible = launcher.getFactory().Class().get(AClass.class);
    assertNull(launcher.getFactory().Class().get("spoon.test.refactoring.testclasses.AClass"));
    assertNotNull(classNotAccessible);
    final CtClass<?> aClassX = launcher.getFactory().Class().get("spoon.test.refactoring.testclasses.AClassX");
    assertNotNull(aClassX);
    final List<CtTypeReference<?>> references = Query.getElements(aClassX.getFactory(), new AbstractReferenceFilter<CtTypeReference<?>>(CtTypeReference.class) {

        @Override
        public boolean matches(CtTypeReference<?> reference) {
            return aClassX.getQualifiedName().equals(reference.getQualifiedName());
        }
    });
    assertNotEquals(0, references.size());
    for (CtTypeReference<?> reference : references) {
        assertEquals("AClassX", reference.getSimpleName());
    }
}
Also used : CtTypeReference(spoon.reflect.reference.CtTypeReference) Launcher(spoon.Launcher) Test(org.junit.Test)

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