Skip to content

Commit

Permalink
Upgrade javaparser and enhance the fail message for parse exceptions (#…
Browse files Browse the repository at this point in the history
…241)

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.
  • Loading branch information
nimakarimipour authored Oct 3, 2024
1 parent 56410a6 commit 83fcf31
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
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
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 All @@ -33,6 +34,7 @@
import edu.ucr.cs.riple.injector.changes.AnnotationChange;
import edu.ucr.cs.riple.injector.changes.ChangeVisitor;
import edu.ucr.cs.riple.injector.changes.RemoveAnnotation;
import edu.ucr.cs.riple.injector.exceptions.ParseException;
import edu.ucr.cs.riple.injector.modifications.Modification;
import edu.ucr.cs.riple.injector.offsets.FileOffsetStore;
import java.io.IOException;
Expand Down Expand Up @@ -171,6 +173,9 @@ 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
throw new ParseException(path, e);
} catch (NoSuchFileException e) {
return null;
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright (c) 2024 University of California, Riverside.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package edu.ucr.cs.riple.injector.exceptions;

import com.github.javaparser.ParseProblemException;
import java.nio.file.Path;

/**
* Exception indicating that an error occurred while parsing a source file.
*
* <p>This serves as a wrapper for the {@link ParseProblemException} class, providing a more concise
* representation of the underlying issue.
*/
public class ParseException extends RuntimeException {

public ParseException(Path path, ParseProblemException exception) {
super(retrieveExceptionMessage(path, exception), exception);
}

private static String retrieveExceptionMessage(Path path, ParseProblemException e) {
String message = e.getMessage();
// If the message contains the stack trace, we should remove it. It does not contain any useful
// information.
int index = message.indexOf("Problem stacktrace :");
message = index == -1 ? message : message.substring(0, index);
return "javaparser was not able to parse file at: " + path + "\nParse problem:" + message;
}
}

0 comments on commit 83fcf31

Please sign in to comment.