Search in sources :

Example 36 with CtField

use of spoon.reflect.declaration.CtField in project spoon by INRIA.

the class Parameters method getAllTemplateParameterFields.

/**
 * returns all the compile_time fields of a template representing a template parameter
 */
public static List<CtField<?>> getAllTemplateParameterFields(Class<? extends Template<?>> clazz, Factory factory) {
    CtClass<?> c = factory.Class().get(clazz);
    if (c == null) {
        throw new IllegalArgumentException("Template not in template classpath");
    }
    List<CtField<?>> result = new ArrayList<>();
    for (Field f : getAllTemplateParameterFields(clazz)) {
        result.add(c.getField(f.getName()));
    }
    return result;
}
Also used : CtField(spoon.reflect.declaration.CtField) Field(java.lang.reflect.Field) CtField(spoon.reflect.declaration.CtField) ArrayList(java.util.ArrayList)

Example 37 with CtField

use of spoon.reflect.declaration.CtField in project spoon by INRIA.

the class Parameters method getParameterField.

public static CtField<?> getParameterField(CtClass<? extends Template<?>> templateClass, String parameterName) {
    for (CtTypeMember typeMember : templateClass.getTypeMembers()) {
        if (!(typeMember instanceof CtField)) {
            continue;
        }
        CtField<?> f = (CtField<?>) typeMember;
        Parameter p = f.getAnnotation(Parameter.class);
        if (p == null) {
            continue;
        }
        if (f.getSimpleName().equals(parameterName)) {
            return f;
        }
        if (parameterName.equals(p.value())) {
            return f;
        }
    }
    return null;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtField(spoon.reflect.declaration.CtField) TemplateParameter(spoon.template.TemplateParameter) Parameter(spoon.template.Parameter)

Example 38 with CtField

use of spoon.reflect.declaration.CtField in project spoon by INRIA.

the class TestInterfaceWithoutSetup method testModifierFromInterfaceFieldAndMethod.

@Test
public void testModifierFromInterfaceFieldAndMethod() {
    // contract: methods defined in interface are all public and fields are all public and static
    Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/resources/spoon/test/itf/DumbItf.java");
    spoon.getEnvironment().setNoClasspath(false);
    spoon.buildModel();
    CtType dumbType = spoon.getFactory().Type().get("toto.DumbItf");
    assertEquals(2, dumbType.getFields().size());
    CtField fieldImplicit = dumbType.getField("CONSTANT_INT");
    Set<CtExtendedModifier> extendedModifierSet = fieldImplicit.getExtendedModifiers();
    assertEquals(3, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.FINAL, true)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, true)));
    for (CtExtendedModifier extendedModifier : extendedModifierSet) {
        assertTrue(extendedModifier.isImplicit());
    }
    assertEquals(ModifierKind.PUBLIC, fieldImplicit.getVisibility());
    assertTrue(fieldImplicit.hasModifier(ModifierKind.STATIC));
    assertTrue(fieldImplicit.hasModifier(ModifierKind.PUBLIC));
    assertTrue(fieldImplicit.hasModifier(ModifierKind.FINAL));
    CtField fieldExplicit = dumbType.getField("ANOTHER_INT");
    extendedModifierSet = fieldExplicit.getExtendedModifiers();
    assertEquals(3, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.FINAL, true)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, false)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, false)));
    int counter = 0;
    for (CtExtendedModifier extendedModifier : extendedModifierSet) {
        if (extendedModifier.getKind() == ModifierKind.FINAL) {
            assertTrue(extendedModifier.isImplicit());
            counter++;
        } else {
            assertFalse(extendedModifier.isImplicit());
            counter++;
        }
    }
    assertEquals(3, counter);
    assertEquals(ModifierKind.PUBLIC, fieldExplicit.getVisibility());
    assertTrue(fieldExplicit.hasModifier(ModifierKind.STATIC));
    assertTrue(fieldExplicit.hasModifier(ModifierKind.PUBLIC));
    assertTrue(fieldExplicit.hasModifier(ModifierKind.FINAL));
    assertEquals(4, dumbType.getMethods().size());
    CtMethod staticMethod = (CtMethod) dumbType.getMethodsByName("foo").get(0);
    assertTrue(staticMethod.hasModifier(ModifierKind.PUBLIC));
    assertTrue(staticMethod.hasModifier(ModifierKind.STATIC));
    extendedModifierSet = staticMethod.getExtendedModifiers();
    assertEquals(2, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.STATIC, false)));
    CtMethod publicMethod = (CtMethod) dumbType.getMethodsByName("machin").get(0);
    assertTrue(publicMethod.hasModifier(ModifierKind.PUBLIC));
    assertFalse(publicMethod.hasModifier(ModifierKind.STATIC));
    extendedModifierSet = publicMethod.getExtendedModifiers();
    assertEquals(2, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.ABSTRACT, true)));
    CtMethod defaultMethod = (CtMethod) dumbType.getMethodsByName("bla").get(0);
    assertTrue(defaultMethod.hasModifier(ModifierKind.PUBLIC));
    assertTrue(defaultMethod.isDefaultMethod());
    assertFalse(defaultMethod.hasModifier(ModifierKind.STATIC));
    extendedModifierSet = defaultMethod.getExtendedModifiers();
    assertEquals(1, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, true)));
    CtMethod explicitDefaultMethod = (CtMethod) dumbType.getMethodsByName("anotherOne").get(0);
    assertTrue(explicitDefaultMethod.hasModifier(ModifierKind.PUBLIC));
    extendedModifierSet = explicitDefaultMethod.getExtendedModifiers();
    assertEquals(2, extendedModifierSet.size());
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.PUBLIC, false)));
    assertTrue(extendedModifierSet.contains(new CtExtendedModifier(ModifierKind.ABSTRACT, true)));
}
Also used : CtType(spoon.reflect.declaration.CtType) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) CtExtendedModifier(spoon.support.reflect.CtExtendedModifier) CtMethod(spoon.reflect.declaration.CtMethod) Test(org.junit.Test)

Example 39 with CtField

use of spoon.reflect.declaration.CtField in project spoon by INRIA.

the class PackageTest method testGetFQNSimple.

@Test
public void testGetFQNSimple() {
    // contract: CtPackageReference simple name is also the fully qualified name of its referenced package
    final Launcher spoon = new Launcher();
    spoon.addInputResource("./src/test/java/spoon/test/pkg/testclasses/Foo.java");
    spoon.buildModel();
    CtClass fooClass = spoon.getFactory().Class().get(Foo.class);
    CtField field = fooClass.getField("fieldList");
    CtPackageReference fieldPkg = field.getType().getPackage();
    assertEquals("java.util", fieldPkg.getSimpleName());
    assertEquals("java.util", fieldPkg.getQualifiedName());
}
Also used : CtClass(spoon.reflect.declaration.CtClass) CtPackageReference(spoon.reflect.reference.CtPackageReference) CtField(spoon.reflect.declaration.CtField) Launcher(spoon.Launcher) Test(org.junit.Test)

Aggregations

CtField (spoon.reflect.declaration.CtField)39 Test (org.junit.Test)20 Launcher (spoon.Launcher)16 CtType (spoon.reflect.declaration.CtType)16 CtMethod (spoon.reflect.declaration.CtMethod)13 CtTypeReference (spoon.reflect.reference.CtTypeReference)12 ArrayList (java.util.ArrayList)8 CtElement (spoon.reflect.declaration.CtElement)8 CtClass (spoon.reflect.declaration.CtClass)6 CtTypeMember (spoon.reflect.declaration.CtTypeMember)6 Factory (spoon.reflect.factory.Factory)6 List (java.util.List)5 SpoonException (spoon.SpoonException)5 CtExecutable (spoon.reflect.declaration.CtExecutable)5 CtPackage (spoon.reflect.declaration.CtPackage)5 CtParameter (spoon.reflect.declaration.CtParameter)5 CtLocalVariable (spoon.reflect.code.CtLocalVariable)4 CtFieldReference (spoon.reflect.reference.CtFieldReference)4 TypeFilter (spoon.reflect.visitor.filter.TypeFilter)4 Set (java.util.Set)3