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