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());
}
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"));
}
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());
}
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());
}
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());
}
}
Aggregations