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;
}
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;
}
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)));
}
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());
}
Aggregations