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
Merged
2 changes: 1 addition & 1 deletion gradle/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def versions = [
errorProne : defaultErrorProneVersion,
errorProneApi : project.hasProperty("epApiVersion") ? epApiVersion : oldestErrorProneApi,
autoService : "1.0-rc7",
javaparser : "3.26.0",
javaparser : "3.26.2",
json : "1.1.1",
guava : "31.0.1-jre",
cli : "1.5.0",
Expand Down
17 changes: 17 additions & 0 deletions injector/src/main/java/edu/ucr/cs/riple/injector/Injector.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import static java.util.stream.Collectors.groupingBy;

import com.github.javaparser.ParseProblemException;
import com.github.javaparser.ParserConfiguration;
import com.github.javaparser.StaticJavaParser;
import com.github.javaparser.ast.CompilationUnit;
Expand Down Expand Up @@ -171,6 +172,22 @@ public static CompilationUnit parse(
StaticJavaParser.setConfiguration(parserConfiguration);
try {
return StaticJavaParser.parse(path);
} catch (ParseProblemException e) {
// 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

} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
Expand Down
Loading