use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class ReplaceScanner method updateField.
private CtField updateField(CtClass listener, CtTypeReference<?> type) {
final CtField field = listener.getField("element");
field.setType(type);
return field;
}
use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class Substitution method checkTemplateContracts.
private static <T> void checkTemplateContracts(CtClass<T> c) {
for (CtField f : c.getFields()) {
Parameter templateParamAnnotation = f.getAnnotation(Parameter.class);
if (templateParamAnnotation != null && !templateParamAnnotation.value().equals("")) {
String proxyName = templateParamAnnotation.value();
// contract: if value, then the field type must be String or CtTypeReference
String fieldTypeQName = f.getType().getQualifiedName();
if (fieldTypeQName.equals(String.class.getName())) {
// contract: the name of the template parameter must correspond to the name of the field
// as found, by Pavel, this is not good contract because it prevents easy refactoring of templates
// we remove it but keep th commented code in case somebody would come up with this bad idae
// if (!f.getSimpleName().equals("_" + f.getAnnotation(Parameter.class).value())) {
// throw new TemplateException("the field name of a proxy template parameter must be called _" + f.getSimpleName());
// }
// contract: if a proxy parameter is declared and named "x" (@Parameter("x")), then a type member named "x" must exist.
boolean found = false;
for (CtTypeMember member : c.getTypeMembers()) {
if (member.getSimpleName().equals(proxyName)) {
found = true;
}
}
if (!found) {
throw new TemplateException("if a proxy parameter is declared and named \"" + proxyName + "\", then a type member named \"\" + proxyName + \"\" must exist.");
}
} else if (fieldTypeQName.equals(CtTypeReference.class.getName())) {
// OK it is CtTypeReference
} else {
throw new TemplateException("proxy template parameter must be typed as String or CtTypeReference, but it is " + fieldTypeQName);
}
}
}
}
use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class Substitution method insertAll.
/**
* Inserts all the methods, fields, constructors, initialization blocks (if
* target is a class), inner types, and super interfaces (except
* {@link Template}) from a given template by substituting all the template
* parameters by their values. Members annotated with
* {@link spoon.template.Local} or {@link Parameter} are not inserted.
*
* @param targetType
* the target type
* @param template
* the source template
*/
public static <T extends Template<?>> void insertAll(CtType<?> targetType, T template) {
CtClass<T> templateClass = getTemplateCtClass(targetType, template);
// insert all the interfaces
insertAllSuperInterfaces(targetType, template);
// insert all the methods
insertAllMethods(targetType, template);
// insert all the constructors and all the initialization blocks (only for classes)
insertAllConstructors(targetType, template);
for (CtTypeMember typeMember : templateClass.getTypeMembers()) {
if (typeMember instanceof CtField) {
// insert all the fields
insertGeneratedField(targetType, template, (CtField<?>) typeMember);
} else if (typeMember instanceof CtType) {
// insert all the inner types
insertGeneratedNestedType(targetType, template, (CtType) typeMember);
}
}
}
use of spoon.reflect.declaration.CtField in project spoon by INRIA.
the class TestModifiers method testGetModifiersHelpers.
@Test
public void testGetModifiersHelpers() {
// contract: the CtModifiable helpers like isPublic, isFinal etc returns right values
Launcher spoon = new Launcher();
spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/AbstractClass.java");
spoon.addInputResource("./src/test/java/spoon/test/modifiers/testclasses/ConcreteClass.java");
spoon.getEnvironment().setShouldCompile(true);
spoon.run();
CtType<?> abstractClass = spoon.getFactory().Type().get(AbstractClass.class);
checkCtModifiableHelpersAssertion(abstractClass, true, false, false, true, false, false);
assertEquals(4, abstractClass.getFields().size());
for (CtField field : abstractClass.getFields()) {
switch(field.getSimpleName()) {
case "privateField":
checkCtModifiableHelpersAssertion(field, false, false, true, false, false, false);
break;
case "protectedField":
checkCtModifiableHelpersAssertion(field, false, true, false, false, false, false);
break;
case "privateStaticField":
checkCtModifiableHelpersAssertion(field, false, false, true, false, false, true);
break;
case "publicFinalField":
checkCtModifiableHelpersAssertion(field, true, false, false, false, true, false);
break;
default:
fail("The field " + field.getSimpleName() + " should be take into account.");
}
}
assertEquals(4, abstractClass.getMethods().size());
for (CtMethod method : abstractClass.getMethods()) {
switch(method.getSimpleName()) {
case "method":
checkCtModifiableHelpersAssertion(method, true, false, false, false, true, true);
break;
case "onlyStatic":
checkCtModifiableHelpersAssertion(method, true, false, false, false, false, true);
break;
case "otherMethod":
checkCtModifiableHelpersAssertion(method, false, true, false, true, false, false);
break;
case "anotherOne":
checkCtModifiableHelpersAssertion(method, false, false, false, true, false, false);
break;
default:
fail("The method " + method.getSimpleName() + " should be taken into account.");
}
}
CtType<?> concreteClass = spoon.getFactory().Type().get("spoon.test.modifiers.testclasses.ConcreteClass");
checkCtModifiableHelpersAssertion(concreteClass, false, false, false, false, true, false);
assertEquals(2, concreteClass.getFields().size());
for (CtField field : concreteClass.getFields()) {
switch(field.getSimpleName()) {
case "className":
checkCtModifiableHelpersAssertion(field, true, false, false, false, true, true);
break;
case "test":
checkCtModifiableHelpersAssertion(field, false, false, true, false, false, true);
break;
default:
fail("The field " + field.getSimpleName() + " should be take into account.");
}
}
assertEquals(2, concreteClass.getMethods().size());
for (CtMethod method : concreteClass.getMethods()) {
switch(method.getSimpleName()) {
case "otherMethod":
checkCtModifiableHelpersAssertion(method, false, true, false, false, false, false);
break;
case "anotherOne":
checkCtModifiableHelpersAssertion(method, false, false, false, false, true, false);
break;
default:
fail("The method " + method.getSimpleName() + " should be taken into account.");
}
}
}
use of spoon.reflect.declaration.CtField 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());
}
Aggregations