Search in sources :

Example 1 with CtFieldRead

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;
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) CtExpression(spoon.reflect.code.CtExpression) SpoonException(spoon.SpoonException) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtNewArray(spoon.reflect.code.CtNewArray) CtField(spoon.reflect.declaration.CtField) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) Field(java.lang.reflect.Field) CtTypeReference(spoon.reflect.reference.CtTypeReference) Collection(java.util.Collection) CtTypeAccess(spoon.reflect.code.CtTypeAccess)

Example 2 with CtFieldRead

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());
}
Also used : CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtFieldRead(spoon.reflect.code.CtFieldRead) CtTypeReference(spoon.reflect.reference.CtTypeReference) Factory(spoon.reflect.factory.Factory) Launcher(spoon.Launcher) Test(org.junit.Test)

Example 3 with CtFieldRead

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);
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) BoundNumber(spoon.test.annotation.testclasses.BoundNumber) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 4 with 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);
}
Also used : CtInvocation(spoon.reflect.code.CtInvocation) CtFieldRead(spoon.reflect.code.CtFieldRead) Launcher(spoon.Launcher) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) ReferenceTypeFilter(spoon.reflect.visitor.filter.ReferenceTypeFilter) Test(org.junit.Test)

Example 5 with CtFieldRead

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());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtFieldRead (spoon.reflect.code.CtFieldRead)14 Test (org.junit.Test)13 Launcher (spoon.Launcher)8 Factory (spoon.reflect.factory.Factory)8 CtMethod (spoon.reflect.declaration.CtMethod)4 CtTypeReference (spoon.reflect.reference.CtTypeReference)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 MetamodelPropertyField (spoon.reflect.annotations.MetamodelPropertyField)3 CtExpression (spoon.reflect.code.CtExpression)3 CtNewArray (spoon.reflect.code.CtNewArray)3 CtAnnotation (spoon.reflect.declaration.CtAnnotation)3 CtField (spoon.reflect.declaration.CtField)3 ModelUtils.createFactory (spoon.testing.utils.ModelUtils.createFactory)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 MatcherAssert.assertThat (org.hamcrest.MatcherAssert.assertThat)2