ucr-riple / NullAwayAnnotator

A tool to help adapting code bases to NullAway type system.
MIT License
13 stars 6 forks source link

`com.github.javaparser.ParseProblemException` #229

Open boris-petrov opened 6 months ago

boris-petrov commented 6 months ago

Describe the bug That's probably not an issue with this project but rather with javaparser but I promised to write here @nimakarimipour. :smile:

I get the following exception:

Exception in thread "main" com.github.javaparser.ParseProblemException: (line 607,col 49) Parse error. Found "(", expected one of  "!=" "%=" "&" "&&" "&=" "*=" "+=" "-=" "->" "/=" "::" ";" "<<=" "=" "==" ">>=" ">>>=" "?" "^" "^=" "|" "|=" "||"
Problem stacktrace :
  com.github.javaparser.GeneratedJavaParser.generateParseException(GeneratedJavaParser.java:14041)
  com.github.javaparser.GeneratedJavaParser.jj_consume_token(GeneratedJavaParser.java:13886)
  com.github.javaparser.GeneratedJavaParser.ReturnStatement(GeneratedJavaParser.java:7119)
  com.github.javaparser.GeneratedJavaParser.Statement(GeneratedJavaParser.java:5711)
  com.github.javaparser.GeneratedJavaParser.BlockStatement(GeneratedJavaParser.java:5933)
  com.github.javaparser.GeneratedJavaParser.Statements(GeneratedJavaParser.java:2795)
  com.github.javaparser.GeneratedJavaParser.Block(GeneratedJavaParser.java:5810)
  com.github.javaparser.GeneratedJavaParser.MethodDeclaration(GeneratedJavaParser.java:2188)
  com.github.javaparser.GeneratedJavaParser.RecordBodyDeclaration(GeneratedJavaParser.java:1560)
  com.github.javaparser.GeneratedJavaParser.RecordBody(GeneratedJavaParser.java:1410)
  com.github.javaparser.GeneratedJavaParser.RecordDeclaration(GeneratedJavaParser.java:606)
  com.github.javaparser.GeneratedJavaParser.RecordBodyDeclaration(GeneratedJavaParser.java:1521)
  com.github.javaparser.GeneratedJavaParser.RecordBody(GeneratedJavaParser.java:1410)
  com.github.javaparser.GeneratedJavaParser.RecordDeclaration(GeneratedJavaParser.java:606)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBodyDeclaration(GeneratedJavaParser.java:1746)
  com.github.javaparser.GeneratedJavaParser.ClassOrInterfaceBody(GeneratedJavaParser.java:1281)
  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:163)
  edu.ucr.cs.riple.core.registries.field.FieldRegistry.lambda$getBuilder$4(FieldRegistry.java:100)
  edu.ucr.cs.riple.core.registries.Registry.populateContent(Registry.java:117)
  edu.ucr.cs.riple.core.registries.Registry.lambda$new$0(Registry.java:86)
  java.base/java.lang.Iterable.forEach(Iterable.java:75)
  edu.ucr.cs.riple.core.registries.Registry.<init>(Registry.java:83)
  edu.ucr.cs.riple.core.registries.field.FieldRegistry.<init>(FieldRegistry.java:81)
  edu.ucr.cs.riple.core.module.ModuleInfo.<init>(ModuleInfo.java:91)
  edu.ucr.cs.riple.core.module.ModuleInfo.<init>(ModuleInfo.java:73)
  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)

The parsing error is on ( but I managed to find the culprit! The code in question is as follows:

return object instanceof ImmutableEntry<?, ?>(var entry1) && entry1.equals(entry);

Let me know if I should report the issue to the other project or you will. Thanks!

To Reproduce See the Java code above.

Expected behavior No exception.

Stack trace See above.

OS (please complete the following information):

nimakarimipour commented 6 months ago

Hi @boris-petrov Thank you very much for reporting this issue. The code you provided is written with features specifically introduced in java 21 and it seems that Java parser has support for java up to 18 language levels. Unfortunately we cannot resolve this issue at this moment, unless javaparser adds the support for java 21. Let's keep this issue open so we can think more on this and find a solution for this in near future.

That would be great if you report the issue on their GitHub. If it helps you can provide the code snippet below as a unit test that the code compiles successfully with java 21 and fails with java parser

public static void main(String[] args){
    StaticJavaParser.parse("public class Main {" +
            "    boolean run(Object object, ImmutableEntry<?, ?> entry) {" +
            "        return object instanceof ImmutableEntry<?, ?>(var entry1) && entry1.equals(entry);" +
            "    }" +
            "    record ImmutableEntry<K, V>(K key) {}" +
            "}");
  }
boris-petrov commented 6 months ago

@nimakarimipour I think they already have an issue for that so probably no need to open a new one.

As for my case - I'll try to rewrite this code to not use Java 21 features (I don't have that many cases) and then revert it once NullAwayAnnotator has done its job.

jlerbsc commented 3 months ago

Record Patterns is now supported on Javaparser.