Search in sources :

Example 21 with CtTypeMember

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;
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) CtField(spoon.reflect.declaration.CtField) TemplateParameter(spoon.template.TemplateParameter) Parameter(spoon.template.Parameter)

Example 22 with CtTypeMember

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);
    }
}
Also used : CtTypeMember(spoon.reflect.declaration.CtTypeMember) Launcher(spoon.Launcher) ModelUtils.createFactory(spoon.testing.utils.ModelUtils.createFactory) Factory(spoon.reflect.factory.Factory) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

CtTypeMember (spoon.reflect.declaration.CtTypeMember)22 CtType (spoon.reflect.declaration.CtType)9 CtField (spoon.reflect.declaration.CtField)6 CtConstructor (spoon.reflect.declaration.CtConstructor)5 CtMethod (spoon.reflect.declaration.CtMethod)5 Factory (spoon.reflect.factory.Factory)5 ArrayList (java.util.ArrayList)4 Test (org.junit.Test)4 Launcher (spoon.Launcher)3 CtBlock (spoon.reflect.code.CtBlock)3 CtElement (spoon.reflect.declaration.CtElement)3 NamedElementFilter (spoon.reflect.visitor.filter.NamedElementFilter)3 ClassTypingContext (spoon.support.visitor.ClassTypingContext)3 File (java.io.File)2 Annotation (java.lang.annotation.Annotation)2 Comparator (java.util.Comparator)2 HashSet (java.util.HashSet)2 List (java.util.List)2 CtAnnotation (spoon.reflect.declaration.CtAnnotation)2 CtAnnotationType (spoon.reflect.declaration.CtAnnotationType)2