Search in sources :

Example 11 with CtFieldRead

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

the class MetamodelTest method testGetterSetterFroRole.

@Test
public void testGetterSetterFroRole() {
    // contract: all roles in spoon metamodel must at least have a setter and a getter
    SpoonAPI interfaces = new Launcher();
    interfaces.addInputResource("src/main/java/spoon/reflect/declaration");
    interfaces.addInputResource("src/main/java/spoon/reflect/code");
    interfaces.addInputResource("src/main/java/spoon/reflect/reference");
    interfaces.buildModel();
    Factory factory = interfaces.getFactory();
    CtTypeReference propertyGetter = factory.Type().get(PropertyGetter.class).getReference();
    CtTypeReference propertySetter = factory.Type().get(PropertySetter.class).getReference();
    Set<String> expectedRoles = Arrays.stream(CtRole.values()).map(r -> r.name()).collect(Collectors.toSet());
    List<CtMethod<?>> getters = interfaces.getModel().getElements(new AnnotationFilter<CtMethod<?>>(PropertyGetter.class));
    Set<String> getterRoles = getters.stream().map(g -> ((CtFieldRead) g.getAnnotation(propertyGetter).getValue("role")).getVariable().getSimpleName()).collect(Collectors.toSet());
    Set<CtMethod<?>> isNotGetter = getters.stream().filter(m -> !(m.getSimpleName().startsWith("get") || m.getSimpleName().startsWith("is"))).collect(Collectors.toSet());
    List<CtMethod<?>> setters = interfaces.getModel().getElements(new AnnotationFilter<CtMethod<?>>(PropertySetter.class));
    Set<String> setterRoles = setters.stream().map(g -> ((CtFieldRead) g.getAnnotation(propertySetter).getValue("role")).getVariable().getSimpleName()).collect(Collectors.toSet());
    Set<CtMethod<?>> isNotSetter = setters.stream().filter(m -> !(m.getSimpleName().startsWith("set") || m.getSimpleName().startsWith("add") || m.getSimpleName().startsWith("insert") || m.getSimpleName().startsWith("remove"))).collect(Collectors.toSet());
    Assert.assertEquals(expectedRoles, getterRoles);
    Assert.assertEquals(expectedRoles, setterRoles);
    Assert.assertEquals(Collections.EMPTY_SET, isNotGetter);
    Assert.assertEquals(Collections.EMPTY_SET, isNotSetter);
}
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) PropertySetter(spoon.reflect.annotations.PropertySetter) Factory(spoon.reflect.factory.Factory) CtTypeReference(spoon.reflect.reference.CtTypeReference) PropertyGetter(spoon.reflect.annotations.PropertyGetter) Launcher(spoon.Launcher) SpoonAPI(spoon.SpoonAPI) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 12 with CtFieldRead

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

the class TypeTest method testTypeAccessOnPrimitive.

@Test
public void testTypeAccessOnPrimitive() throws Exception {
    Factory factory = createFactory();
    CtClass<?> clazz = // 
    factory.Code().createCodeSnippetStatement(// 
    "class X {" + // 
    "public void foo() {" + // 
    " Class klass=null;" + // 
    "  boolean x= (klass == short.class);" + "}};").compile();
    CtMethod<?> foo = (CtMethod<?>) clazz.getMethods().toArray()[0];
    CtBlock<?> body = foo.getBody();
    CtLocalVariable<?> ass = body.getStatement(1);
    CtBinaryOperator<?> op = (CtBinaryOperator<?>) ass.getDefaultExpression();
    assertEquals("Class", op.getLeftHandOperand().getType().getSimpleName());
    assertFalse(op.getLeftHandOperand().getType().isPrimitive());
    assertEquals("Class", op.getRightHandOperand().getType().getSimpleName());
    assertTrue(op.getRightHandOperand() instanceof CtFieldRead);
    assertFalse(op.getRightHandOperand().getType().isPrimitive());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) CtBinaryOperator(spoon.reflect.code.CtBinaryOperator) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 13 with CtFieldRead

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

the class TargetedExpressionTest method testCastWriteWithGenerics.

@Test
public void testCastWriteWithGenerics() throws Exception {
    final Factory factory = build(Pozole.class);
    final CtClass<Object> aPozole = factory.Class().get(Pozole.class);
    final CtConstructor<Object> aConstructor = aPozole.getConstructor(aPozole.getReference());
    final List<CtFieldRead> elements = aConstructor.getElements(new TypeFilter<>(CtFieldRead.class));
    assertEquals(1, elements.size());
    assertEquals("((spoon.test.targeted.testclasses.Pozole<T>) (v1))", elements.get(0).getTarget().toString());
}
Also used : CtFieldRead(spoon.reflect.code.CtFieldRead) Factory(spoon.reflect.factory.Factory) Test(org.junit.Test)

Example 14 with CtFieldRead

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

the class AccessTest method testCanVisitFieldAccessAndSubClasses.

@Test
public void testCanVisitFieldAccessAndSubClasses() throws Exception {
    final Factory factory = build(FieldAccessSample.class);
    final List<CtFieldRead<?>> fieldsRead = Query.getElements(factory, new AbstractFilter<CtFieldRead<?>>(CtFieldRead.class) {

        @Override
        public boolean matches(CtFieldRead<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(2, fieldsRead.size());
    final List<CtFieldWrite<?>> fieldsWrite = Query.getElements(factory, new AbstractFilter<CtFieldWrite<?>>(CtFieldWrite.class) {

        @Override
        public boolean matches(CtFieldWrite<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(3, fieldsWrite.size());
    final List<CtFieldAccess<?>> fieldsAccess = Query.getElements(factory, new AbstractFilter<CtFieldAccess<?>>(CtFieldAccess.class) {

        @Override
        public boolean matches(CtFieldAccess<?> element) {
            return super.matches(element);
        }
    });
    assertEquals(5, fieldsAccess.size());
}
Also used : CtFieldAccess(spoon.reflect.code.CtFieldAccess) CtFieldRead(spoon.reflect.code.CtFieldRead) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) CtFieldWrite(spoon.reflect.code.CtFieldWrite) MainTest(spoon.test.main.MainTest) 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