Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade javaparser and enhance the fail message for parse exceptions #241

Merged
merged 9 commits into from
Oct 3, 2024

Conversation

nimakarimipour
Copy link
Member

@nimakarimipour nimakarimipour commented Sep 28, 2024

This PR upgrades the javaparser library to version 3.26.2 and enhances the fail message for parse exceptions, providing more context and clarity.

Before this change, a parse exception would display a message like the following, which lacks sufficient context:

Exception in thread "main" com.github.javaparser.ParseProblemException: (line 699,col 20) Parse error. Found ":", expected "("
Problem stacktrace : 
  com.github.javaparser.GeneratedJavaParser.generateParseException(GeneratedJavaParser.java:14419)
  com.github.javaparser.GeneratedJavaParser.jj_consume_token(GeneratedJavaParser.java:14264)
  com.github.javaparser.GeneratedJavaParser.PatternList(GeneratedJavaParser.java:4128)
  com.github.javaparser.GeneratedJavaParser.RecordPatternExpression(GeneratedJavaParser.java:4114)
  com.github.javaparser.GeneratedJavaParser.PatternExpression(GeneratedJavaParser.java:4069)
  com.github.javaparser.GeneratedJavaParser.SwitchEntry(GeneratedJavaParser.java:6491)
  com.github.javaparser.GeneratedJavaParser.SwitchStatement(GeneratedJavaParser.java:6381)
  com.github.javaparser.GeneratedJavaParser.Statement(GeneratedJavaParser.java:5828)
  com.github.javaparser.GeneratedJavaParser.BlockStatement(GeneratedJavaParser.java:6079)
  com.github.javaparser.GeneratedJavaParser.Statements(GeneratedJavaParser.java:2811)
  com.github.javaparser.GeneratedJavaParser.Block(GeneratedJavaParser.java:5955)
  com.github.javaparser.GeneratedJavaParser.MethodDeclaration(GeneratedJavaParser.java:2201)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBodyDeclaration(GeneratedJavaParser.java:1796)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBody(GeneratedJavaParser.java:1286)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceDeclaration(GeneratedJavaParser.java:538)
  com.github.javaparser.GeneratedJavaParser.CompilationUnit(GeneratedJavaParser.java:156)
  com.github.javaparser.JavaParser.parse(JavaParser.java:125)
  com.github.javaparser.JavaParser.parse(JavaParser.java:231)
  com.github.javaparser.JavaParserAdapter.parse(JavaParserAdapter.java:99)
  com.github.javaparser.StaticJavaParser.parse(StaticJavaParser.java:173)
  edu.ucr.cs.riple.injector.Injector.parse(Injector.java:173)
  edu.ucr.cs.riple.core.registries.field.FieldRegistry$1.build(FieldRegistry.java:115)
  edu.ucr.cs.riple.core.registries.field.FieldRegistry$1.build(FieldRegistry.java:97)
  edu.ucr.cs.riple.core.registries.Registry.populateContent(Registry.java:122)
  edu.ucr.cs.riple.core.registries.Registry.lambda$new$0(Registry.java:91)
  java.base/java.lang.Iterable.forEach(Iterable.java:75)
  edu.ucr.cs.riple.core.registries.Registry.<init>(Registry.java:88)
  edu.ucr.cs.riple.core.registries.field.FieldRegistry.<init>(FieldRegistry.java:82)
  edu.ucr.cs.riple.core.module.ModuleInfo.<init>(ModuleInfo.java:94)
  edu.ucr.cs.riple.core.module.ModuleInfo.<init>(ModuleInfo.java:75)
  edu.ucr.cs.riple.core.Context.<init>(Context.java:78)
  edu.ucr.cs.riple.core.Annotator.<init>(Annotator.java:63)
  edu.ucr.cs.riple.core.Main.main(Main.java:45)
(line 709,col 14) Parse error. Found "case", expected "}"

With this update, the error message now includes the file path, making it easier to locate the issue:

javaparser was not able to parse file at: /path_to_Foojava
Parse problem: (line 699,col 20) Parse error. Found ":", expected "("

This change involves both the library upgrade and an improvement to the error message but both are included in this single PR rather than separated due to the small scope of changes.

@nimakarimipour nimakarimipour self-assigned this Sep 28, 2024
@nimakarimipour nimakarimipour changed the title Upgrade javaparser and add a log for failure Upgrade javaparser and enhance the fail message for parse exceptions Sep 28, 2024
Comment on lines 176 to 190
// The original exception is not useful for the user. We should provide a more informative one
String message = e.getMessage();
// If the message contains the stack trace, we should remove it.
int index = message.indexOf("Problem stacktrace :");
message = index == -1 ? message : message.substring(0, index);
System.err.println(
"javaparser was not able to parse file at: "
+ path
+ "\n"
+ message
+ "\n"
+ "Shutting down.");
// Exit with error code 1 to indicate failure.
System.exit(1);
return null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than printing and calling System.exit here, why not create a wrapper exception object with the desired message and rethrow that? It just seems like this is not the right place to decide if this is a fatal error that should exit the JVM. Plus, directly calling System.exit makes it very difficult to write a test for this scenario.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, that sounds better. 7e4dfb1

Copy link
Member

@msridhar msridhar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One nit but otherwise looks good to me

@nimakarimipour nimakarimipour merged commit 83fcf31 into master Oct 3, 2024
7 checks passed
@nimakarimipour nimakarimipour deleted the nimak/update-javaparser branch October 3, 2024 18:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants