openrewrite / rewrite-logging-frameworks

OpenRewrite recipes for assisting with Java logging migration tasks.
Apache License 2.0
26 stars 21 forks source link

Log4jToSlf4j Single Statement If `Could not parse as Java` #185

Closed FlorianWege-HS-KL closed 5 days ago

FlorianWege-HS-KL commented 1 month ago

What version of OpenRewrite are you using?

I am using

How are you running OpenRewrite?

I am using the Maven plugin, and my project is a single module project.

<plugin>
    <groupId>org.openrewrite.maven</groupId>
    <artifactId>rewrite-maven-plugin</artifactId>
    <version>5.43.0</version>
    <configuration>
        <activeRecipes>
            <recipe>org.openrewrite.java.logging.slf4j.Log4jToSlf4j</recipe>
        </activeRecipes>
    </configuration>
    <dependencies>
        <dependency>
            <groupId>org.openrewrite.recipe</groupId>
            <artifactId>rewrite-logging-frameworks</artifactId>
            <version>2.15.1</version>
        </dependency>
    </dependencies>
</plugin>

What is the smallest, simplest way to reproduce the problem?

mvn rewrite:run

while having the following Java source file:

package test;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;

public class SingleStatementIf {
  Logger log = LogManager.getLogger();

  public void foo() {
    if (log.isDebugEnabled())
      log.debug("first" + "second");

  }

  private String bar() {
    return null;
  }
}

What did you expect to see?

no parse error

What did you see instead?

parse error (see error message/stack trace)

What is the full stack trace of any errors you encountered?

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.43.0:run (default-cli) on project test: Execution default-cli of goal org.openrewrite
.maven:rewrite-maven-plugin:5.43.0:run failed: Error while visiting src\main\java\test\SingleStatementIf.java: java.lang.IllegalArgumentException: Could not parse as Java
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.lambda$compileTemplate$13(JavaTemplateParser.java:264)
[ERROR]   java.base/java.util.Optional.orElseThrow(Optional.java:403)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.compileTemplate(JavaTemplateParser.java:264)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateParser.parseMethodArguments(JavaTemplateParser.java:208)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitMethodInvocation(JavaTemplateJavaExtension.java:391)
[ERROR]   org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitMethodInvocation(JavaTemplateJavaExtension.java:55)
[ERROR]   org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3905)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:157)
[ERROR]   org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:115)
[ERROR]   org.openrewrite.java.logging.ParameterizedLogging$1.visitMethodInvocation(ParameterizedLogging.java:104)
[ERROR]   org.openrewrite.java.logging.ParameterizedLogging$1.visitMethodInvocation(ParameterizedLogging.java:71)
[ERROR]   org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:3905)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
[ERROR]   ...

Debugging the parse instruction there, jp.reset().parse(ctx, stub).findFirst() yields:

Optional[ParseError(id=11ba106c-dbdd-44b4-bd5b-7e8262247dbe, markers=Markers(id=ba142bca-2519-4102-a504-c33dfb29e727, markers=[ParseExceptionResult(id=75552cb1-0dcc-4e28-8987-63f0badcccb1, parserType=ReloadableJava21Parser, exceptionType=StringIndexOutOfBoundsException, message=java.lang.StringIndexOutOfBoundsException: Range [378, 378) out of bounds for length 374
  java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:55)
  java.base/jdk.internal.util.Preconditions$1.apply(Preconditions.java:52)
  java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:213)
  java.base/jdk.internal.util.Preconditions$4.apply(Preconditions.java:210)
  java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:98)
  java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:112)
  java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:349)
  java.base/java.lang.String.checkBoundsBeginEnd(String.java:4865)
  java.base/java.lang.String.substring(String.java:2834)
  org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convert(ReloadableJava21ParserVisitor.java:1667)
  org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convertOrNull(ReloadableJava21ParserVisitor.java:1720)
  org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitMethod(ReloadableJava21ParserVisitor.java:1009)
  org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.visitMethod(ReloadableJava21ParserVisitor.java:76)
  com.sun.tools.javac.tree.JCTree$JCMethodDecl.accept(JCTree.java:948)
  com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:92)
  org.openrewrite.java.isolated.ReloadableJava21ParserVisitor.convert(ReloadableJava21ParserVisitor.java:1672)
  ..., treeType=null)]), sourcePath=test\SingleStatementIf.java, fileAttributes=null, charsetName=UTF-8, charsetBomMarked=false, checksum=null, text=package test;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openrewrite.java.internal.template.__M__;
import org.openrewrite.java.internal.template.__P__;
public class SingleStatementIf{Logger log;
public void foo(){
/*__TEMPLATE__*/
log.debug("first"   "second")/*__TEMPLATE_STOP__*/
}
private String bar(){
return null;
}
}, erroneous=null)]

Are you interested in contributing a fix to OpenRewrite?

I do not know how to fix it at the moment. But I suspect it is the single statement if structure that is not correctly parsed/transformed. When the second string literal "second" in the log statement, the bar function below is removed, or the then branch of the if structure uses curly braces, the run succeeds.

timtebeek commented 1 month ago

hi! This looks to be an issue with one particular recipe; would you mind creating a draft PR containing just one additional test based on this example that matches your case?

https://github.com/openrewrite/rewrite-logging-frameworks/blob/71ac5c422ce2f5e1bbd920fec04c79673c27ea90/src/test/java/org/openrewrite/java/logging/ParameterizedLoggingTest.java#L41-L68