How to extract relations between classes using java files instead of class files?

143 Views Asked by At

I'm trying to modify the code below to make it work on java files instead of class files. But I have no idea what are some possible ways to do that. So basically I have to get rid of BCEL ClassParser and extract relations directly from Java files? Any leads?

Function<ClassParser, ClassVisitor> getClassVisitor =
                (ClassParser cp) -> {
                    try {
                        return new ClassVisitor(cp.parse());
                    } catch (IOException e) {
                        throw new UncheckedIOException(e);
                    }
                };
try (JarFile jar = new JarFile(f)) {
                    Stream<JarEntry> entries = enumerationAsStream(jar.entries());
               
                    String methodCalls = entries.
                            flatMap(e -> {
                                if (e.isDirectory() || !e.getName().endsWith(".class"))
                                    return (new ArrayList<String>()).stream();

                                ClassParser cp = new ClassParser(arg, e.getName());
                                System.out.println("Name:"+e.getName());
                                
                                return getClassVisitor.apply(cp).start().methodCalls().stream();
                            }).
                            map(s -> s + "\n").
                            reduce(new StringBuilder(),
                                    StringBuilder::append,
                                    StringBuilder::append).toString();
1

There are 1 best solutions below

2
On

There are many libraries that allow you to parse .java files :

QDOX : https://github.com/paul-hammant/qdox

JavaParser : https://javaparser.org/