use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.
the class CtAnnotationImpl method convertValueToExpression.
private CtExpression convertValueToExpression(Object value) {
CtExpression res;
if (value.getClass().isArray()) {
// Value should be converted to a CtNewArray.
res = getFactory().Core().createNewArray();
Object[] values = (Object[]) value;
res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(value.getClass().getComponentType())));
for (Object o : values) {
((CtNewArray) res).addElement(convertValueToExpression(o));
}
} else if (value instanceof Collection) {
// Value should be converted to a CtNewArray.
res = getFactory().Core().createNewArray();
Collection values = (Collection) value;
res.setType(getFactory().Type().createArrayReference(getFactory().Type().createReference(values.toArray()[0].getClass())));
for (Object o : values) {
((CtNewArray) res).addElement(convertValueToExpression(o));
}
} else if (value instanceof Class) {
// Value should be a field access to a .class.
res = getFactory().Code().createClassAccess(getFactory().Type().createReference((Class) value));
} else if (value instanceof Field) {
// Value should be a field access to a field.
CtFieldReference<Object> variable = getFactory().Field().createReference((Field) value);
variable.setStatic(true);
CtTypeAccess target = getFactory().Code().createTypeAccess(getFactory().Type().createReference(((Field) value).getDeclaringClass()));
CtFieldRead fieldRead = getFactory().Core().createFieldRead();
fieldRead.setVariable(variable);
fieldRead.setTarget(target);
fieldRead.setType(target.getAccessedType());
res = fieldRead;
} else if (isPrimitive(value.getClass()) || value instanceof String) {
// Value should be a literal.
res = getFactory().Code().createLiteral(value);
} else if (value.getClass().isEnum()) {
final CtTypeReference declaringClass = getFactory().Type().createReference(((Enum) value).getDeclaringClass());
final CtFieldReference variableRef = getFactory().Field().createReference(declaringClass, declaringClass, ((Enum) value).name());
CtTypeAccess target = getFactory().Code().createTypeAccess(declaringClass);
CtFieldRead fieldRead = getFactory().Core().createFieldRead();
fieldRead.setVariable(variableRef);
fieldRead.setTarget(target);
fieldRead.setType(declaringClass);
res = fieldRead;
} else {
throw new SpoonException("Please, submit a valid value.");
}
return res;
}
use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.
the class AnnotationTest method testAnnotationValueReflection.
@Test
public void testAnnotationValueReflection() throws Exception {
Factory factory = new Launcher().getFactory();
CtTypeReference reference = factory.createCtTypeReference(PropertyGetter.class);
CtAnnotation annotation = factory.Interface().get(CtNamedElement.class).getMethod("getSimpleName").getAnnotation(reference);
assertEquals("The annotation must have a value", 1, annotation.getValues().size());
assertEquals("NAME", ((CtFieldRead) annotation.getValue("role")).getVariable().getSimpleName());
}
use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.
the class AnnotationValuesTest method testAnnotateWithEnum.
@Test
public void testAnnotateWithEnum() throws Exception {
final Factory factory = createFactory();
final CtClass<Object> target = factory.Class().create("org.example.Tacos");
final CtField<String> field = factory.Field().create(target, new HashSet<>(), factory.Type().STRING, "field");
target.addField(field);
final CtAnnotation<BoundNumber> byteOrder = factory.Annotation().annotate(field, BoundNumber.class, "byteOrder", BoundNumber.ByteOrder.LittleEndian);
assertEquals(byteOrder, on(field).giveMeAnnotation(BoundNumber.class));
assertTrue(on(byteOrder).giveMeAnnotationValue("byteOrder").element instanceof CtFieldRead);
}
use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.
the class TypeReferenceTest method testInvocationWithFieldAccessInNoClasspath.
@Test
public void testInvocationWithFieldAccessInNoClasspath() throws Exception {
// contract: In no classpath mode, if we have field accesses in an invocation, we should build field access and not type access.
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/Demo4.java");
launcher.setSourceOutputDirectory("./target/class-declaration");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Object> demo4 = launcher.getFactory().Class().get("Demo4");
final CtMethod<?> doSomething = demo4.getMethodsByName("doSomething").get(0);
final CtInvocation topInvocation = doSomething.getElements(new TypeFilter<>(CtInvocation.class)).get(0);
assertNotNull(topInvocation.getTarget());
assertTrue(topInvocation.getTarget() instanceof CtInvocation);
assertNotNull(((CtInvocation) topInvocation.getTarget()).getTarget());
assertTrue(((CtInvocation) topInvocation.getTarget()).getTarget() instanceof CtFieldRead);
assertEquals(1, topInvocation.getArguments().size());
assertTrue(topInvocation.getArguments().get(0) instanceof CtFieldRead);
assertEquals("a.foo().bar(b)", topInvocation.toString());
// Class concerned by this bug.
canBeBuilt("./src/test/resources/noclasspath/TestBot.java", 8, true);
}
use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.
the class TypeReferenceTest method testArgumentOfAInvocationIsNotATypeAccess.
@Test
public void testArgumentOfAInvocationIsNotATypeAccess() throws Exception {
// contract: In no classpath, an unknown field specified as argument isn't a CtTypeAccess.
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/resources/noclasspath/Demo3.java");
launcher.setSourceOutputDirectory("./target/class-declaration");
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
final CtClass<Object> demo3 = launcher.getFactory().Class().get("Demo3");
final List<CtFieldRead> fields = demo3.getElements(new TypeFilter<CtFieldRead>(CtFieldRead.class) {
@Override
public boolean matches(CtFieldRead element) {
return "bar".equals(element.getVariable().getSimpleName()) && super.matches(element);
}
});
assertEquals(1, fields.size());
}
Aggregations