From 83fcf3102ee9bb9f9b3fab600a6c7dc86c03da82 Mon Sep 17 00:00:00 2001 From: Nima Karimipour Date: Thu, 3 Oct 2024 13:11:12 -0500 Subject: [PATCH] Upgrade javaparser and enhance the fail message for parse exceptions (#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.(Registry.java:88) edu.ucr.cs.riple.core.registries.field.FieldRegistry.(FieldRegistry.java:82) edu.ucr.cs.riple.core.module.ModuleInfo.(ModuleInfo.java:94) edu.ucr.cs.riple.core.module.ModuleInfo.(ModuleInfo.java:75) edu.ucr.cs.riple.core.Context.(Context.java:78) edu.ucr.cs.riple.core.Annotator.(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. --- gradle/dependencies.gradle | 2 +- .../edu/ucr/cs/riple/injector/Injector.java | 5 ++ .../injector/exceptions/ParseException.java | 48 +++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 injector/src/main/java/edu/ucr/cs/riple/injector/exceptions/ParseException.java diff --git a/gradle/dependencies.gradle b/gradle/dependencies.gradle index c2e422942..218205a98 100644 --- a/gradle/dependencies.gradle +++ b/gradle/dependencies.gradle @@ -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", diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/Injector.java b/injector/src/main/java/edu/ucr/cs/riple/injector/Injector.java index d3eb9d7b5..0d417e19f 100644 --- a/injector/src/main/java/edu/ucr/cs/riple/injector/Injector.java +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/Injector.java @@ -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; @@ -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; @@ -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) { diff --git a/injector/src/main/java/edu/ucr/cs/riple/injector/exceptions/ParseException.java b/injector/src/main/java/edu/ucr/cs/riple/injector/exceptions/ParseException.java new file mode 100644 index 000000000..c01f8c054 --- /dev/null +++ b/injector/src/main/java/edu/ucr/cs/riple/injector/exceptions/ParseException.java @@ -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. + * + *

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