Search in sources :

Example 11 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class FieldAccessTest method testIncrementationOnAVarIsAUnaryOperator.

@Test
public void testIncrementationOnAVarIsAUnaryOperator() throws Exception {
    // contract: When we use var++, the variable is a read access with an unary operator.
    final CtType<Panini> aMole = buildClass(Panini.class);
    final CtMethod<?> make = aMole.getMethodsByName("make").get(0);
    final List<CtUnaryOperator<?>> unaryOperators = make.getElements(new TypeFilter<CtUnaryOperator<?>>(CtUnaryOperator.class));
    final CtFieldWrite<Object> fieldRead = aMole.getFactory().Core().createFieldWrite();
    fieldRead.setTarget(aMole.getFactory().Code().createThisAccess(aMole.getReference(), true));
    final CtFieldReference fieldReference = aMole.getField("i").getReference();
    fieldRead.setVariable(fieldReference);
    assertEquals(2, unaryOperators.size());
    final CtUnaryOperator<?> first = unaryOperators.get(0);
    assertEquals(UnaryOperatorKind.POSTINC, first.getKind());
    assertEquals(fieldRead, first.getOperand());
    assertEquals("(i)++", first.toString());
    final CtUnaryOperator<?> second = unaryOperators.get(1);
    assertEquals(UnaryOperatorKind.PREINC, second.getKind());
    assertEquals(fieldRead, second.getOperand());
    assertEquals("++(i)", second.toString());
}
Also used : Panini(spoon.test.fieldaccesses.testclasses.Panini) CtUnaryOperator(spoon.reflect.code.CtUnaryOperator) CtFieldReference(spoon.reflect.reference.CtFieldReference) Test(org.junit.Test)

Example 12 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessNoClasspath.

@Test
public void testFieldAccessNoClasspath() throws Exception {
    Launcher launcher = new Launcher();
    launcher.addInputResource("src/test/resources/import-resources/fr/inria/");
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    CtType<?> ctType = launcher.getFactory().Class().get("FooNoClassPath");
    CtFieldAccess ctFieldAccess = ctType.getElements(new TypeFilter<>(CtFieldAccess.class)).get(0);
    assertEquals("(game.board.width)", ctFieldAccess.toString());
    CtFieldReference ctFieldReferenceWith = ctFieldAccess.getVariable();
    assertEquals("width", ctFieldReferenceWith.getSimpleName());
    CtFieldAccess ctFieldAccessBoard = (CtFieldAccess) ctFieldAccess.getTarget();
    assertEquals("game.board", ctFieldAccessBoard.toString());
    CtFieldReference ctFieldReferenceBoard = ctFieldAccessBoard.getVariable();
    assertEquals("board", ctFieldReferenceBoard.getSimpleName());
    CtFieldAccess ctFieldAccessGame = (CtFieldAccess) ctFieldAccessBoard.getTarget();
    assertEquals("game.board", ctFieldAccessBoard.toString());
    CtFieldReference ctFieldReferenceGame = ctFieldAccessGame.getVariable();
    assertEquals("game", ctFieldReferenceGame.getSimpleName());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) Launcher(spoon.Launcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 13 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class TemplateMatcher method findParameterMatcher.

/**
 * returns an appropriate ParameterMatcher defined in a template parameter, or else a default one
 *
 * if a template parameter (field annotated with @Parameter) whose name (field name) is a substring of the template name, it also works
 */
private ParameterMatcher findParameterMatcher(CtNamedElement templateDeclaration) throws InstantiationException, IllegalAccessException {
    if (templateDeclaration == null) {
        return new DefaultParameterMatcher();
    }
    String name = templateDeclaration.getSimpleName();
    CtClass<?> clazz = null;
    try {
        clazz = templateDeclaration.getParent(CtClass.class);
    } catch (ParentNotInitializedException e) {
        Launcher.LOGGER.error(e.getMessage(), e);
    }
    if (clazz == null) {
        return new DefaultParameterMatcher();
    }
    Collection<CtFieldReference<?>> fields = clazz.getAllFields();
    CtFieldReference<?> param = null;
    for (CtFieldReference<?> fieldRef : fields) {
        Parameter p = fieldRef.getDeclaration().getAnnotation(Parameter.class);
        if (p == null) {
            // not a parameter.
            continue;
        }
        String proxy = p.value();
        if (!"".equals(proxy)) {
            if (name.contains(proxy)) {
                param = fieldRef;
                break;
            }
        }
        if (name.contains(fieldRef.getSimpleName())) {
            param = fieldRef;
            break;
        }
    // todo: check for field hack.
    }
    return getParameterInstance(param);
}
Also used : CtClass(spoon.reflect.declaration.CtClass) ParentNotInitializedException(spoon.reflect.declaration.ParentNotInitializedException) DefaultParameterMatcher(spoon.support.template.DefaultParameterMatcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) CtParameter(spoon.reflect.declaration.CtParameter)

Example 14 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class CtTypeReferenceImpl method getDeclaredFieldReferences.

/**
 * Collects all field references of the declared class.
 *
 * @return collection of field references
 */
private Collection<CtFieldReference<?>> getDeclaredFieldReferences() {
    Collection<CtFieldReference<?>> references = new ArrayList<>();
    for (Field f : getDeclaredFields(getActualClass())) {
        references.add(getFactory().Field().createReference(f));
    }
    if (getActualClass().isAnnotation()) {
        for (Method m : getActualClass().getDeclaredMethods()) {
            CtTypeReference<?> retRef = getFactory().Type().createReference(m.getReturnType());
            CtFieldReference<?> fr = getFactory().Field().createReference(this, retRef, m.getName());
            references.add(fr);
        }
    }
    return references;
}
Also used : MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) Field(java.lang.reflect.Field) ArrayList(java.util.ArrayList) CtFieldReference(spoon.reflect.reference.CtFieldReference) Method(java.lang.reflect.Method) CtMethod(spoon.reflect.declaration.CtMethod)

Example 15 with CtFieldReference

use of spoon.reflect.reference.CtFieldReference in project spoon by INRIA.

the class VisibilityTest method testName.

@Test
public void testName() throws Exception {
    final SpoonAPI launcher = new Launcher();
    launcher.run(new String[] { "-i", "./src/test/java/spoon/test/visibility/testclasses/Tacos.java", "-o", "./target/spooned/visibility" });
    final List<CtFieldReference<?>> references = Query.getElements(launcher.getFactory(), new AbstractReferenceFilter<CtFieldReference<?>>(CtFieldReference.class) {

        @Override
        public boolean matches(CtFieldReference<?> reference) {
            return "x".equals(reference.getSimpleName());
        }
    });
    assertEquals(1, references.size());
    final CtFieldReference<?> field = references.get(0);
    assertNotNull(field.getDeclaration());
    final CtClass<?> tacos = launcher.getFactory().Class().get("spoon.test.visibility.testclasses.Tacos");
    assertEquals(tacos, field.getDeclaringType().getDeclaration());
    assertEquals(tacos.getFields().get(0), field.getDeclaration());
}
Also used : Launcher(spoon.Launcher) CtFieldReference(spoon.reflect.reference.CtFieldReference) SpoonAPI(spoon.SpoonAPI) Test(org.junit.Test)

Aggregations

CtFieldReference (spoon.reflect.reference.CtFieldReference)18 Test (org.junit.Test)9 CtTypeReference (spoon.reflect.reference.CtTypeReference)6 Launcher (spoon.Launcher)5 CtField (spoon.reflect.declaration.CtField)5 CtFieldAccess (spoon.reflect.code.CtFieldAccess)4 CtClass (spoon.reflect.declaration.CtClass)4 CtElement (spoon.reflect.declaration.CtElement)4 CtMethod (spoon.reflect.declaration.CtMethod)4 CtExecutableReference (spoon.reflect.reference.CtExecutableReference)4 CtInvocation (spoon.reflect.code.CtInvocation)3 CtImport (spoon.reflect.declaration.CtImport)3 ParentNotInitializedException (spoon.reflect.declaration.ParentNotInitializedException)3 Factory (spoon.reflect.factory.Factory)3 Field (java.lang.reflect.Field)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 SpoonException (spoon.SpoonException)2 MetamodelPropertyField (spoon.reflect.annotations.MetamodelPropertyField)2