From ec47a2d6ed91722093cad4cf647d131e4de27b33 Mon Sep 17 00:00:00 2001 From: Cody <6558800+Bl3nd@users.noreply.github.com> Date: Wed, 2 Oct 2024 21:16:16 -0600 Subject: [PATCH] Switch from using StaticJavaParser to JavaParser so we can handle if the parse was successful. --- .../classcontainer/ClassFileContainer.java | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/main/java/the/bytecode/club/bytecodeviewer/resources/classcontainer/ClassFileContainer.java b/src/main/java/the/bytecode/club/bytecodeviewer/resources/classcontainer/ClassFileContainer.java index a8be62a3..9707c83b 100644 --- a/src/main/java/the/bytecode/club/bytecodeviewer/resources/classcontainer/ClassFileContainer.java +++ b/src/main/java/the/bytecode/club/bytecodeviewer/resources/classcontainer/ClassFileContainer.java @@ -1,6 +1,6 @@ package the.bytecode.club.bytecodeviewer.resources.classcontainer; -import com.github.javaparser.StaticJavaParser; +import com.github.javaparser.*; import com.github.javaparser.ast.CompilationUnit; import com.github.javaparser.resolution.TypeSolver; import com.github.javaparser.symbolsolver.JavaSymbolSolver; @@ -10,7 +10,7 @@ import com.github.javaparser.symbolsolver.resolution.typesolvers.ReflectionTypeS import the.bytecode.club.bytecodeviewer.decompilers.Decompiler; import the.bytecode.club.bytecodeviewer.resources.ResourceContainer; import the.bytecode.club.bytecodeviewer.resources.classcontainer.locations.*; -import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.MyVoidVisitor; +import the.bytecode.club.bytecodeviewer.resources.classcontainer.parser.visitors.MyVoidVisitor; import java.io.IOException; import java.util.ArrayList; @@ -57,8 +57,20 @@ public class ClassFileContainer if (shouldParse()) { TypeSolver typeSolver = new CombinedTypeSolver(new ReflectionTypeSolver(false), new JarTypeSolver(path)); - StaticJavaParser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver)); - CompilationUnit compilationUnit = StaticJavaParser.parse(this.content); + JavaParser parser = new JavaParser(); + parser.getParserConfiguration().setSymbolResolver(new JavaSymbolSolver(typeSolver)); + ParseResult parse = parser.parse(this.content); + if (!parse.isSuccessful()) + { + System.err.println("Failed to parse: " + this.getName()); + parse.getProblems().forEach(System.out::println); + return false; + } + + CompilationUnit compilationUnit = parse.getResult().orElse(null); + if (compilationUnit == null) + return false; + compilationUnit.accept(new MyVoidVisitor(this, compilationUnit), null); return true; } @@ -67,11 +79,6 @@ public class ClassFileContainer { throw new RuntimeException(e); } - catch (Exception e) - { - System.err.println("Parsing error: " + className); - e.printStackTrace(); - } return false; }