0
votes

I'm writing a library based on JavaParser, using Javasymbol solver to resolve class names on source files. I'm trying to obtain qualified class names of annotations to classes and methods, but at the moment I'm not able to obtain them.

My current code is:

    final CombinedTypeSolver combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new ReflectionTypeSolver());
    combinedTypeSolver.add(new JarTypeSolver(JUNIT_PATH));
    final JavaParserFacade javaParserFacade = JavaParserFacade.get(combinedTypeSolver);
    final CompilationUnit cu = JavaParser.parse("package apackage;\n" +
            "\n" +
            "import org.junit.*;\n" +
            "\n" +
            "public class ClassToAnalyze {\n" +
            "\n" +
            "@Test\n" +
            "    public void test() {\n" +
            "        \n" +
            "    }\n" +
            "}\n" +
            "");
    final TypeDeclaration<?> typeDeclaration = cu.getTypes().get(0);
    final MethodDeclaration method = typeDeclaration.getMethods().get(0);
    final AnnotationExpr annotation = method.getAnnotation(0);
    final Object solve = javaParserFacade.solve(annotation);
    System.out.println(solve);

But an exception is thrown:

java.lang.ClassCastException: com.github.javaparser.symbolsolver.javassistmodel.JavassistInterfaceDeclaration cannot be cast to com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration at com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:265) at jlivedoc.collector.specifications.collectors.MultiProjectCollectorSpecifications.analyzeClass(MultiProjectCollectorSpecifications.java:86) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

Any idea what I'm doing wrong? I'm using javaparser 3.6.5 and javasymbol solver 0.6.3

Thank you all

1

1 Answers

0
votes

I'm not sure of the library that you are using but I do see that the code that you are trying to parse has an issue where @Test should not be at class level but should be applied to the test method, may be you can try fixing that first.

        "package apackage;\n" +
        "\n" +
        "import org.junit.*;\n" +
        "\n" +
        "public class ClassToAnalyze {\n" +
        "\n" +
        "    @Test\n" +
        "    public void test() {\n" +
        "        \n" +
        "    }\n" +
        "}\n" +
        "");