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