我在用

javaparser(https://github.com/javaparser/javaparser)和
javasymbolsolver(https://github.com/javaparser/javasymbolsolver)。

解析Java源代码。我正在寻找一个从.java文件创建基本报告以列出方法签名的方法,如果它们包括完整的Java类型,那就太好了。

这是javaparser'visit'演示和javasymbolsolver演示的基本合并,但是我遇到了麻烦。

炸毁“ getType”呼叫,

Exception in thread "main" java.lang.UnsupportedOperationException: com.github.javaparser.ast.type.ClassOrInterfaceType
    at me.tomassetti.symbolsolver.javaparsermodel.JavaParserFacade.getTypeConcrete(JavaParserFacade.java:392)


这是(无效)代码,

static CombinedTypeSolver combinedTypeSolver;

public static void main(String[] args) throws Exception {
    combinedTypeSolver = new CombinedTypeSolver();
    combinedTypeSolver.add(new JreTypeSolver());
    combinedTypeSolver.add(new JarTypeSolver("C:/source.jar"));
    combinedTypeSolver.add(new JavaParserTypeSolver(new File("C:/src")));

    FileInputStream in = new FileInputStream("C:/src/source.java");
    CompilationUnit cu = JavaParser.parse(in);
    new MethodVisitor().visit(cu, null);
}

private static class MethodVisitor extends VoidVisitorAdapter<Void> {
    public void visit(MethodDeclaration n, Void arg) {

        Node node = n.getType();
        System.out.println(node);
        TypeUsage typeOfTheNode = JavaParserFacade.get(combinedTypeSolver).getType(node);

        super.visit(n, arg);
    }


}

最佳答案

我建议将ReflectionTypeSolver添加到您的CombinedTypeSolver中。
然后,要获取所有MethodDeclaration的列表,我将使用

List<MethodDeclaration> l = Navigator.findAllNodesOfGivenClass(cu, MethodDeclaration.class);


然后,将每个MethodDeclaration(JavaParser)包装在JavaParserMethodDeclaration中,像这样

for(MethodDeclaration md : l){
     System.out.println(new JavaParserMethodDeclaration(mdec, facade.getTypeSolver()).getQualifiedSignature());
}

关于java - Java源代码解析,查找具有完整类型的方法签名,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42213591/

10-09 01:07