use of spoon.reflect.declaration.CtTypeMember 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.CtTypeMember in project spoon by INRIA.
the class TypeTest method testTypeMemberOrder.
@Test
public void testTypeMemberOrder() throws Exception {
// contract: The TypeMembers keeps order of members same like in source file
final String target = "./target/type";
final Launcher launcher = new Launcher();
launcher.addInputResource("./src/test/java/spoon/test/type/testclasses/TypeMembersOrder.java");
launcher.setSourceOutputDirectory(target);
launcher.getEnvironment().setNoClasspath(true);
launcher.run();
Factory f = launcher.getFactory();
final CtClass<?> aTypeMembersOrder = f.Class().get(TypeMembersOrder.class);
{
List<String> typeMemberNames = new ArrayList<>();
for (CtTypeMember typeMember : aTypeMembersOrder.getTypeMembers()) {
typeMemberNames.add(typeMember.getSimpleName());
}
assertEquals(Arrays.asList("<init>", "method1", "field2", "TypeMembersOrder", "method4", "field5", "", "nestedType6", "field7", "method8"), typeMemberNames);
}
{
// contract: newly added type member is at the end
f.createMethod(aTypeMembersOrder, Collections.singleton(ModifierKind.PUBLIC), f.Type().voidType(), "method9", Collections.emptyList(), Collections.emptySet());
List<String> typeMemberNames = new ArrayList<>();
for (CtTypeMember typeMember : aTypeMembersOrder.getTypeMembers()) {
typeMemberNames.add(typeMember.getSimpleName());
}
assertEquals(Arrays.asList("<init>", "method1", "field2", "TypeMembersOrder", "method4", "field5", "", "nestedType6", "field7", "method8", "method9"), typeMemberNames);
}
}
Aggregations