- 
                Notifications
    
You must be signed in to change notification settings  - Fork 469
 
Improve record parsing #5916
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
Improve record parsing #5916
Conversation
        
          
                rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | public J visitLiteral(LiteralTree node, Space fmt) { | ||
| cursor(endPos(node)); | ||
| int endPos = endPos(node); | ||
| Object value = node.getValue(); | ||
| String valueSource = source.substring(((JCLiteral) node).getStartPosition(), endPos(node)); | ||
| 
               | 
          ||
| if (endPos == Position.NOPOS) { | ||
| if (typeMapping.primitive(((JCLiteral) node).typetag) == JavaType.Primitive.String) { | ||
| int quote = source.substring(cursor).startsWith("\"\"\"") ? 3 : 1; | ||
| if (quote == 3) { | ||
| endPos = cursor + quote + source.indexOf("\"\"\"", cursor + quote) + quote; | ||
| } else { | ||
| endPos = cursor + quote + value.toString().length() + quote; | ||
| } | ||
| } else { | ||
| endPos = cursor + indexOf(source.substring(cursor), | ||
| ch -> Character.isWhitespace(ch) || ",;)]}+-*/%=!<>&|^?:.".indexOf(ch) != -1 | ||
| ); | ||
| } | ||
| } | ||
| 
               | 
          ||
| cursor(endPos); | ||
| String valueSource = source.substring(((JCLiteral) node).getStartPosition(), endPos); | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason this had to change at all is because the endpos table is computed in this step
rewrite/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java
Line 224 in 04628ec
| com.sun.tools.javac.util.List<JCTree.JCCompilationUnit> jcCompilationUnits = compiler.parseFiles((List<JavaFileObject>) (List<?>) inputFileObjects, true); | 
and the removal of unapplicable annotations is done here
rewrite/rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17Parser.java
Line 236 in 04628ec
| annotate.unblockAnnotations(); // also flushes once unblocked | 
        
          
                rewrite-java-21/src/main/java/org/openrewrite/java/isolated/ReloadableJava21ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-core/src/main/java/org/openrewrite/internal/StringUtils.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
              
          
                rewrite-java-17/src/main/java/org/openrewrite/java/isolated/ReloadableJava17ParserVisitor.java
              
                Outdated
          
            Show resolved
            Hide resolved
        
      | 
           @Laurens-W should any of the improvements here now be replicated into the Java 25 parser since this wasn't merged yet?  | 
    
          
 Yes, they probably should, I'll update the PR!  | 
    
| 
           tnx for the hard work; i'll retest openrewrite/rewrite-spring#714 when this get's released :)  | 
    
| 
           re-tested with   | 
    
| 
           Thanks for reporting back @tubbynl!  | 
    
What's changed?
Improve the parsing of records to match closer with the Java compiler output
What's your motivation?
We've seen annotations with different targets end up on different elements in the compiler, but this is not reflected in the LST
Any additional context
Remaining TODOs- [ ] Expand the logic around mapping the members, as currently we only considerVariableTreewhen parsing a record- [ ] Add a marker to the LST so we can preserve the original order of the annotations- [ ] Adjust the JavaPrinter so that we become print idempotent againBased on Knut's guidance I've simplified the approach to not require drastic changes to the printer
Checklist