Поиск атрибутов через SPOON - PullRequest
       24

Поиск атрибутов через SPOON

0 голосов
/ 13 сентября 2018

Я пытаюсь найти ссылки на атрибуты вот так.

Под атрибутом I подразумевается, что этот метод возвращает все атрибуты, которые определены в классе cd и на которые ссылается "method",

Launcher launcher = new Launcher();
Map<CtMethod<?>, Set<CtField<?>>> mp = new HashMap<CtMethod<?>, Set<CtField<?>>>();
        launcher.addInputResource("src/test/resources/PrintValues.java"); 
        launcher.getEnvironment().setAutoImports(true); // optional
        launcher.getEnvironment().setNoClasspath(true); // optional
        launcher.buildModel();
        //CtModel model = launcher.getModel();

           List<CtMethod<?>> methods=  launcher.getFactory().getModel().getElements(new TypeFilter<CtMethod<?>>(CtMethod.class));
            for(CtMethod<?> method: methods)
            {       
            CtClass<?> cd = method.getParent(CtClass.class);    
            //System.out.println("class name :"+ cd);
            mp.put(method, getImplementAttributeReferences(method,cd)); 


            }    

    }

вот фильтр, который сначала получит метод, а затем обнаружит ссылки на атрибуты и вернется в виде списка.

static List<CtFieldReference<?>> getAttributeReferences(CtMethod<?> method){
    //get the type which declares method
    CtType<?> declaringType = method.getDeclaringType();
    return method
        //visit AST of the method and pass all the nodes of type CtFieldReference to the next query step
        .filterChildren(new TypeFilter<>(CtFieldReference.class))
        //select all the field references whose field belongs to the declaringType
        .select((CtFieldReference<?> fieldRef)->fieldRef.getDeclaration().getDeclaringType() == declaringType)
        //collect all results into List
        .list();

}

Я хочу получить ссылки на атрибуты в форме SET.

public static Set<CtField<?>> getImplementAttributeReferences(CtMethod<?> method, CtClass<?> cd){

            Set<CtField<?>> ImplementAttributeReferences = new HashSet<CtField<?>>();

            for (CtFieldReference<?> field : getAttributeReferences(method)) 
                if (field.getDeclaration().getDeclaringType().equals(cd))
                    ImplementAttributeReferences.add((CtField<?>) field);

            return ImplementAttributeReferences;
        }

Не могли бы вы помочь, почему я получаю исключение NULLpointer.

вот класс printvalues Print values class

...