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
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));
nimakarimipour marked this conversation as resolved.
Show resolved Hide resolved
}

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;
}
}
Loading