Search in sources :

Example 6 with CtFieldRead

use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.

the class TypeTest method testTypeAccessForDotClass.

@Test
public void testTypeAccessForDotClass() throws Exception {
    // contract: When we use .class on a type, this must be a CtTypeAccess.
    final String target = "./target/type";
    final Launcher launcher = new Launcher();
    launcher.addInputResource("./src/test/java/spoon/test/type/testclasses");
    launcher.setSourceOutputDirectory(target);
    launcher.getEnvironment().setNoClasspath(true);
    launcher.run();
    final CtClass<Pozole> aPozole = launcher.getFactory().Class().get(Pozole.class);
    final CtMethod<?> make = aPozole.getMethodsByName("make").get(0);
    final List<CtFieldRead<?>> fieldClasses = make.getElements(new TypeFilter<CtFieldRead<?>>(CtFieldRead.class) {

        @Override
        public boolean matches(CtFieldRead<?> element) {
            return "class".equals(element.getVariable().getSimpleName()) && super.matches(element);
        }
    });
    assertEquals(4, fieldClasses.size());
    for (CtFieldRead<?> fieldClass : fieldClasses) {
        assertTrue(fieldClass.getTarget() instanceof CtTypeAccess);
    }
    canBeBuilt(target, 8, true);
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Pozole(spoon.test.type.testclasses.Pozole) Launcher(spoon.Launcher) CtTypeAccess(spoon.reflect.code.CtTypeAccess) Test(org.junit.Test)

Example 7 with CtFieldRead

use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.

the class AccessTest method testVariableAccessInNoClasspath.

@Test
public void testVariableAccessInNoClasspath() throws Exception {
    final Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(true);
    launcher.addInputResource("./src/test/resources/variable");
    launcher.setSourceOutputDirectory("./target/variable/");
    launcher.run();
    final CtClass<Object> aClass = launcher.getFactory().Class().get("org.argouml.uml.ui.behavior.use_cases.PropPanelUseCase");
    final List<CtFieldRead> elements = aClass.getElements(new TypeFilter<>(CtFieldRead.class));
    for (CtFieldRead element : elements) {
        assertNotNull(element.getVariable());
    }
    assertEquals("java.lang.Class mclass = ((java.lang.Class) (org.argouml.model.ModelFacade.USE_CASE))", elements.get(0).getParent().toString());
    assertEquals("new org.argouml.uml.ui.PropPanelButton(this, buttonPanel, _navUpIcon, org.argouml.i18n.Translator.localize(\"UMLMenu\", \"button.go-up\"), \"navigateNamespace\", null)", elements.get(2).getParent().toString());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Launcher(spoon.Launcher) MainTest(spoon.test.main.MainTest) Test(org.junit.Test)

Example 8 with CtFieldRead

use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.

the class FieldTest method testGetDefaultExpression.

@Test
public void testGetDefaultExpression() throws Exception {
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/field/testclasses/A.java");
    spoon.addInputResource("./src/test/java/spoon/test/field/testclasses/BaseClass.java");
    spoon.buildModel();
    final CtClass<A> aClass = spoon.getFactory().Class().get(A.class);
    CtClass<A.ClassB> bClass = aClass.getFactory().Class().get(A.ClassB.class);
    List<CtMethod<?>> methods = bClass.getMethodsByName("getKey");
    assertEquals(1, methods.size());
    CtReturn<?> returnExpression = methods.get(0).getBody().getStatement(0);
    CtFieldRead fieldRead = (CtFieldRead) returnExpression.getReturnedExpression();
    assertEquals("spoon.test.field.testclasses.BaseClass.PREFIX", fieldRead.toString());
    CtField<?> field = fieldRead.getVariable().getDeclaration();
    CtClass<BaseClass> baseClass = aClass.getFactory().Class().get(BaseClass.class);
    CtField<?> expectedField = baseClass.getField("PREFIX");
    assertEquals(expectedField, field);
    VisitorPartialEvaluator visitorPartial = new VisitorPartialEvaluator();
    Object retour = visitorPartial.evaluate(methods.get(0));
    assertTrue(retour != null);
}
Also used : A(spoon.test.field.testclasses.A) CtFieldRead(spoon.reflect.code.CtFieldRead) VisitorPartialEvaluator(spoon.support.reflect.eval.VisitorPartialEvaluator) BaseClass(spoon.test.field.testclasses.BaseClass) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 9 with CtFieldRead

use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.

the class FieldAccessTest method testFieldAccessInAnonymousClass.

@Test
public void testFieldAccessInAnonymousClass() throws Exception {
    final Factory factory = build(Panini.class);
    final CtType<Panini> panini = factory.Type().get(Panini.class);
    final CtFieldRead fieldInAnonymous = panini.getElements(new TypeFilter<>(CtFieldRead.class)).get(0);
    assertEquals("ingredient", fieldInAnonymous.getTarget().toString());
    assertEquals("next", fieldInAnonymous.getVariable().getSimpleName());
    assertEquals("ingredient.next", fieldInAnonymous.toString());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Factory(spoon.reflect.factory.Factory) Panini(spoon.test.fieldaccesses.testclasses.Panini) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) Test(org.junit.Test)

Example 10 with CtFieldRead

use of spoon.reflect.code.CtFieldRead in project spoon by INRIA.

the class MetamodelTest method testRoleOnField.

@Test
public void testRoleOnField() {
    // contract: all non-final fields must be annotated with {@link spoon.reflect.annotations.MetamodelPropertyField}
    SpoonAPI implementations = new Launcher();
    implementations.addInputResource("src/main/java/spoon/support/reflect");
    implementations.buildModel();
    Factory factory = implementations.getFactory();
    CtTypeReference metamodelPropertyField = factory.Type().get(MetamodelPropertyField.class).getReference();
    final List<String> result = new ArrayList();
    List<CtField> fieldWithoutAnnotation = (List<CtField>) implementations.getModel().getElements(new TypeFilter<CtField>(CtField.class) {

        @Override
        public boolean matches(CtField candidate) {
            if (candidate.hasModifier(ModifierKind.FINAL) || candidate.hasModifier(ModifierKind.STATIC) || candidate.hasModifier(ModifierKind.TRANSIENT)) {
                return false;
            }
            if (// not a role
            "parent".equals(candidate.getSimpleName()) || "metadata".equals(candidate.getSimpleName()) || // cache field
            "valueOfMethod".equals(candidate.getSimpleName())) {
                return false;
            }
            CtClass parent = candidate.getParent(CtClass.class);
            return parent != null && (parent.isSubtypeOf(candidate.getFactory().createCtTypeReference(CtReference.class)) || parent.isSubtypeOf(candidate.getFactory().createCtTypeReference(CtElement.class)));
        }
    }).stream().map(x -> {
        result.add(x.toString());
        return x;
    }).filter(f -> f.getAnnotation(metamodelPropertyField) == null).collect(Collectors.toList());
    assertTrue(result.contains("@spoon.reflect.annotations.MetamodelPropertyField(role = spoon.reflect.path.CtRole.IS_SHADOW)\nboolean isShadow;"));
    assertTrue(result.contains("@spoon.reflect.annotations.MetamodelPropertyField(role = spoon.reflect.path.CtRole.TYPE)\nspoon.reflect.reference.CtTypeReference<T> type;"));
    assertTrue(result.size() > 100);
    Assert.assertEquals(Collections.emptyList(), fieldWithoutAnnotation);
    final CtTypeReference propertySetter = factory.Type().get(PropertySetter.class).getReference();
    final CtTypeReference propertyGetter = factory.Type().get(PropertyGetter.class).getReference();
    List<CtField> fields = factory.getModel().getElements(new AnnotationFilter<CtField>(MetamodelPropertyField.class));
    for (CtField field : fields) {
        CtClass parent = field.getParent(CtClass.class);
        CtExpression roleExpression = field.getAnnotation(metamodelPropertyField).getValue("role");
        List<String> roles = new ArrayList<>();
        if (roleExpression instanceof CtFieldRead) {
            roles.add(((CtFieldRead) roleExpression).getVariable().getSimpleName());
        } else if (roleExpression instanceof CtNewArray) {
            List<CtFieldRead> elements = ((CtNewArray) roleExpression).getElements();
            for (int i = 0; i < elements.size(); i++) {
                CtFieldRead ctFieldRead = elements.get(i);
                roles.add(ctFieldRead.getVariable().getSimpleName());
            }
        }
        CtQuery superQuery = parent.map(new SuperInheritanceHierarchyFunction());
        List<CtMethod> methods = superQuery.map((CtType type) -> type.getMethodsAnnotatedWith(propertyGetter, propertySetter)).list();
        boolean setterFound = false;
        boolean getterFound = false;
        for (CtMethod method : methods) {
            CtAnnotation getterAnnotation = method.getAnnotation(propertyGetter);
            CtAnnotation setterAnnotation = method.getAnnotation(propertySetter);
            if (getterAnnotation != null) {
                getterFound |= roles.contains(((CtFieldRead) getterAnnotation.getValue("role")).getVariable().getSimpleName());
            }
            if (setterAnnotation != null) {
                setterFound |= roles.contains(((CtFieldRead) setterAnnotation.getValue("role")).getVariable().getSimpleName());
            }
        }
        assertTrue(roles + " must have a getter in " + parent.getQualifiedName(), getterFound);
        assertTrue(roles + " must have a setter in " + parent.getQualifiedName(), setterFound);
    }
}
Also used : Arrays(java.util.Arrays) Launcher(spoon.Launcher) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) CtRole(spoon.reflect.path.CtRole) ArrayList(java.util.ArrayList) CtType(spoon.reflect.declaration.CtType) CtElement(spoon.reflect.declaration.CtElement) SpoonAPI(spoon.SpoonAPI) CtExpression(spoon.reflect.code.CtExpression) CtNewArray(spoon.reflect.code.CtNewArray) Metamodel(spoon.Metamodel) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) CtQuery(spoon.reflect.visitor.chain.CtQuery) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtField(spoon.reflect.declaration.CtField) CtReference(spoon.reflect.reference.CtReference) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Factory(spoon.reflect.factory.Factory) PropertySetter(spoon.reflect.annotations.PropertySetter) Collectors(java.util.stream.Collectors) CtTypeReference(spoon.reflect.reference.CtTypeReference) List(java.util.List) CtAnnotation(spoon.reflect.declaration.CtAnnotation) AnnotationFilter(spoon.reflect.visitor.filter.AnnotationFilter) CtClass(spoon.reflect.declaration.CtClass) ModifierKind(spoon.reflect.declaration.ModifierKind) CtFieldRead(spoon.reflect.code.CtFieldRead) Assert(org.junit.Assert) Collections(java.util.Collections) PropertyGetter(spoon.reflect.annotations.PropertyGetter) CtMethod(spoon.reflect.declaration.CtMethod) ArrayList(java.util.ArrayList) Factory(spoon.reflect.factory.Factory) TypeFilter(spoon.reflect.visitor.filter.TypeFilter) CtNewArray(spoon.reflect.code.CtNewArray) CtTypeReference(spoon.reflect.reference.CtTypeReference) CtField(spoon.reflect.declaration.CtField) PropertyGetter(spoon.reflect.annotations.PropertyGetter) ArrayList(java.util.ArrayList) List(java.util.List) SpoonAPI(spoon.SpoonAPI) SuperInheritanceHierarchyFunction(spoon.reflect.visitor.filter.SuperInheritanceHierarchyFunction) CtAnnotation(spoon.reflect.declaration.CtAnnotation) CtFieldRead(spoon.reflect.code.CtFieldRead) CtExpression(spoon.reflect.code.CtExpression) PropertySetter(spoon.reflect.annotations.PropertySetter) CtQuery(spoon.reflect.visitor.chain.CtQuery) MetamodelPropertyField(spoon.reflect.annotations.MetamodelPropertyField) CtClass(spoon.reflect.declaration.CtClass) CtType(spoon.reflect.declaration.CtType) Launcher(spoon.Launcher) CtMethod(spoon.reflect.declaration.CtMethod) 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