如何使用Javasymbol求解器获取注释限定名称?

时间:2018-05-28 21:24:55

标签: java javaparser

我正在编写基于JavaParser的库,使用Javasymbol解算器来解析源文件上的类名。我试图获取类和方法的注释的合格类名,但目前我无法获得它们。

我目前的代码是:

    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);

但抛出异常:

java.lang.ClassCastException:com.github.javaparser.symbolsolver.javassistmodel.JavassistInterfaceDeclaration无法强制转换为com.github.javaparser.resolution.declarations.ResolvedAnnotationDeclaration     在com.github.javaparser.symbolsolver.javaparsermodel.JavaParserFacade.solve(JavaParserFacade.java:265)     在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)     在org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50)     在org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)     在org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)     在org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)     在org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)     在org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)     在org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)     在org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)     在org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)     在org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)     在org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)     在org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)

知道我做错了什么吗?我使用的是javaparser 3.6.5和javasymbol solver 0.6.3

谢谢大家

1 个答案:

答案 0 :(得分:0)

我不确定您正在使用的库但是我确实看到您尝试解析的代码存在一个问题,即@Test不应该在类级别但应该应用于测试方法,可能你可以尝试先修复它。

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