0

I am using

javaparser (https://github.com/javaparser/javaparser) and javasymbolsolver (https://github.com/javaparser/javasymbolsolver).

to parse java source code. I am looking to create a basic report from a .java file to list the method signatures, and would be nice if they included the complete java types.

so is a basic merge of the javaparser 'visit' demo, and javasymbolsolver demo, but I'm running into the wall.

blowing up on 'getType' call, with

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

Here is the (non working) code,

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


}
Steve Renyolds
  • 1,341
  • 2
  • 9
  • 10

1 Answers1

2

I'd recommand to add a ReflectionTypeSolver to your CombinedTypeSolver. Then, to get a list of all the MethodDeclaration, I'd use

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

Then I'd wrap each MethodDeclaration (JavaParser) in a JavaParserMethodDeclaration like that

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