INRIA / spoon

Spoon is a metaprogramming library to analyze and transform Java source code. :spoon: is made with :heart:, :beers: and :sparkles:. It parses source files to build a well-designed AST with powerful analysis and transformation API.

Home Page:http://spoon.gforge.inria.fr/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Bug]: CtAnnotation Type in Annonymous Class is null

Ruil1n opened this issue · comments

Describe the bug

When I run ctAnnotation.getType(),it cause NPE.

Source code you are trying to analyze/transform

public class Controller{
    public Response query(Request request) {
        return execute(new A.SecureService<Request>() {
            @Override
            public void check() {

            }
        });
    }
}

Source code for your Spoon processing

import spoon.Launcher;
import spoon.reflect.declaration.CtAnnotation;
import spoon.reflect.declaration.CtClass;
import spoon.reflect.declaration.CtMethod;

import java.util.Map;

public class SpoonTest {
    public static void main(String[] args) {
        CtClass l = Launcher.parseClass("public class Controller{\n" +
                "    @Request\n" +
                "    public Response query(Request request) {\n" +
                "        return execute(new A.SecureService<Request>() {\n" +
                "            @Override\n" +
                "            public void check() {\n" +
                "\n" +
                "            }\n" +
                "        });\n" +
                "    }\n" +
                "}");
        CtAnnotation ctAnnotation = ((CtMethod)((CtClass)((Map)l.getMetadata("spoon.reflect.factory.TypeFactory-AnnonymousTypeCache")).get("110")).getTypeMembers().get(0)).getAnnotations().get(0);
        System.out.println(ctAnnotation.getType().getQualifiedName());

    }
}

Actual output

Exception in thread "main" java.lang.NullPointerException
	at SpoonTest.main(SpoonTest.java:22)

Expected output

java.lang.Override

Spoon Version

spoon-core-10.4.3-beta-6

JVM Version

11.0.2

What operating system are you using?

Mac

This metadata is an implementation detail, there is no guarantee that it will be present at any time.

What exactly are you trying to achieve? Most likely there's a better way.

This metadata is an implementation detail, there is no guarantee that it will be present at any time.

What exactly are you trying to achieve? Most likely there's a better way.

I hope to get the annotation of the "check" method -> @Override

You can achieve that by using e.g. CtClass#getMethodsByName and CtMethod#getAnnotations.

(Note in your example, you should use CtClass<?> instead of CtClass, otherwise you might encounter confusing compilation errors)

You can achieve that by using e.g. CtClass#getMethodsByName and CtMethod#getAnnotations.

(Note in your example, you should use CtClass<?> instead of CtClass, otherwise you might encounter confusing compilation errors)

I'm looking to directly find the 'check' method rather than by name. Is there another way? I tried CtClass#getAnonymousExecutables and CtClass#getMethods but it didn't work.

I'm looking to directly find the 'check' method rather than by name. Is there another way? I tried CtClass#getAnonymousExecutables and CtClass#getMethods but it didn't work.

You can also loop through the CtClass#getMethods list and check for methods that have that annotation present. Alternatively, you could query the model for the annotation and check if getParent() is a CtMethod.

If you have any problem with that, it would be helpful to see your code, the input, the result, and the expected result.