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

Log4jToSlf4j Single Statement If Could not parse as Java #185

Closed
FlorianWege-HS-KL opened this issue Oct 28, 2024 · 1 comment · Fixed by openrewrite/rewrite#4709
Closed
Labels
bug Something isn't working

Comments

@FlorianWege-HS-KL
Copy link

FlorianWege-HS-KL commented Oct 28, 2024

What version of OpenRewrite are you using?

I am using

  • OpenRewrite v8.38.0
  • Maven plugin v5.43.0
  • rewrite-logging-frameworks v2.15.1

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.

@FlorianWege-HS-KL FlorianWege-HS-KL added the bug Something isn't working label Oct 28, 2024
@FlorianWege-HS-KL FlorianWege-HS-KL changed the title Could not parse as Java Single Statement If Could not parse as Java Oct 28, 2024
@timtebeek timtebeek moved this to Backlog in OpenRewrite Oct 28, 2024
@timtebeek
Copy link
Contributor

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?

@DocumentExample
@Test
void basicParameterization() {
rewriteRun(
spec -> spec.recipe(new ParameterizedLogging("org.slf4j.Logger info(..)", false)),
//language=java
java(
"""
import org.slf4j.Logger;
class Test {
static void method(Logger logger, String name) {
logger.info("Hello " + name + ", nice to meet you " + name);
}
}
""",
"""
import org.slf4j.Logger;
class Test {
static void method(Logger logger, String name) {
logger.info("Hello {}, nice to meet you {}", name, name);
}
}
"""
)
);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants