ftomassetti / analyze-java-code-examples

Some examples of code extracting information from Java source files using JavaParser
99 stars 44 forks source link

ListClassesExample gives compile error on newest version of JavaParser #6

Closed DanielVerloop closed 1 year ago

DanielVerloop commented 2 years ago

Hello,

First of all thank you for making this guide! However, when I try to implement your code I run into an error concerning the JavaParser parse function Specifically in the listClasses method:

        new DirExplorer((level, path, file) -> path.endsWith(".java"), (level, path, file) -> {
            System.out.println(path);
            System.out.println(Strings.repeat("=", path.length()));
            try {
                new VoidVisitorAdapter<Object>() {
                    @Override
                    public void visit(ClassOrInterfaceDeclaration n, Object arg) {
                        super.visit(n, arg);
                        System.out.println(" * " + n.getName());
                    }
                }.visit(JavaParser.parse(file), null);
                System.out.println(); // empty line
            } catch (ParseException | IOException e) {
                new RuntimeException(e);
            }
        }).explore(projectDir);
    }

the function call to visit(JavaParser.parse(file), null) gives a Non-static method 'parse(java.io.File, java.nio.charset.Charset)' cannot be referenced from a static context error. Could this be due to compatibility errors due to versioning?

ftomassetti commented 2 years ago

Hi Daniel, that could the case indeed as JavaParser changed over time. I would try using some old version of JavaParser or adapt it. The people on the gitter channel of JavaParser may help