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

ReplaceDuplicateStringLiterals throws java.lang.NullPointerException on version 1.19.0 #384

Open
adambir opened this issue Oct 29, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@adambir
Copy link

adambir commented Oct 29, 2024

What version of OpenRewrite are you using?

I am using

  • Maven/Gradle plugin v5.43.2
  • rewrite-static-analysis _ v1.19.0 (no error on version 1.18.0)

How are you running OpenRewrite?

Using the mvn plugin with a multi project module
mvn -U org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run -Drewrite.recipeArtifactCoordinates=org.openrewrite.recipe:rewrite-static-analysis:1.19.0 -Drewrite.activeRecipes=org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals

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

This code snippet alongside a basic pom reproduces the issue

package com.nonsense.test;

public class PojoTester {

	public static Object getValue() {
		Object[] args = null;
			args = new Object[] { "1" };
		return null;
	}
}

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

[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run (default-cli) on project untitled2: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run failed: Error while visiting src/main/java/com/whatever/PojoTester.java: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.openrewrite.java.tree.J$Literal.getValue()" is null
[ERROR]   org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:235)
[ERROR]   org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:218)
[ERROR]   org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5901)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
[ERROR]   org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27(JavaVisitor.java:961)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:177)
[ERROR]   org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:961)
[ERROR]   org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:244)
[ERROR]   org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:30)
[ERROR]   org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5790)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:59)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
[ERROR]   ...
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run (default-cli) on project untitled2: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run failed: Error while visiting src/main/java/com/whatever/PojoTester.java: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.openrewrite.java.tree.J$Literal.getValue()" is null
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:235)
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:218)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5901)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
  org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27(JavaVisitor.java:961)
  org.openrewrite.internal.ListUtils.map(ListUtils.java:177)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:961)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:244)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:30)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5790)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  ...
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:375)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
Caused by: org.apache.maven.plugin.PluginExecutionException: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:5.43.2:run failed: Error while visiting src/main/java/com/whatever/PojoTester.java: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.openrewrite.java.tree.J$Literal.getValue()" is null
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:235)
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:218)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5901)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
  org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27(JavaVisitor.java:961)
  org.openrewrite.internal.ListUtils.map(ListUtils.java:177)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:961)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:244)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:30)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5790)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  ...
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:148)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
Caused by: java.lang.RuntimeException: Error while visiting src/main/java/com/whatever/PojoTester.java: java.lang.NullPointerException: Cannot invoke "Object.toString()" because the return value of "org.openrewrite.java.tree.J$Literal.getValue()" is null
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:235)
  org.openrewrite.staticanalysis.ReplaceDuplicateStringLiterals$DuplicateLiteralInfo$1.visitVariable(ReplaceDuplicateStringLiterals.java:218)
  org.openrewrite.java.tree.J$VariableDeclarations$NamedVariable.acceptJava(J.java:5901)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1367)
  org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27(JavaVisitor.java:961)
  org.openrewrite.internal.ListUtils.map(ListUtils.java:177)
  org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:961)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:244)
  org.openrewrite.java.JavaIsoVisitor.visitVariableDeclarations(JavaIsoVisitor.java:30)
  org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5790)
  org.openrewrite.java.tree.J.accept(J.java:59)
  org.openrewrite.TreeVisitor.visit(TreeVisitor.java:250)
  org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:320)
  ...
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.lambda$preVisit$0 (AbstractRewriteBaseRunMojo.java:380)
    at java.util.Optional.ifPresent (Optional.java:178)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.preVisit (AbstractRewriteBaseRunMojo.java:377)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer$1.preVisit (AbstractRewriteBaseRunMojo.java:373)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo_ResultsContainer_1_JavaVisitor.preVisit (AbstractRewriteBaseRunMojo_ResultsContainer_1_JavaVisitor.zig:81)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:247)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.visitRightPadded (JavaVisitor.java:1367)
    at org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$27 (JavaVisitor.java:961)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:177)
    at org.openrewrite.java.JavaVisitor.visitVariableDeclarations (JavaVisitor.java:961)
    at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava (J.java:5790)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.visitRightPadded (JavaVisitor.java:1367)
    at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4 (JavaVisitor.java:397)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:177)
    at org.openrewrite.java.JavaVisitor.visitBlock (JavaVisitor.java:396)
    at org.openrewrite.java.tree.J$Block.acceptJava (J.java:838)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.visitMethodDeclaration (JavaVisitor.java:875)
    at org.openrewrite.java.tree.J$MethodDeclaration.acceptJava (J.java:3651)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.visitRightPadded (JavaVisitor.java:1367)
    at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4 (JavaVisitor.java:397)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:177)
    at org.openrewrite.java.JavaVisitor.visitBlock (JavaVisitor.java:396)
    at org.openrewrite.java.tree.J$Block.acceptJava (J.java:838)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.visitClassDeclaration (JavaVisitor.java:482)
    at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava (J.java:1278)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.TreeVisitor.visitAndCast (TreeVisitor.java:320)
    at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$9 (JavaVisitor.java:495)
    at org.openrewrite.internal.ListUtils.map (ListUtils.java:177)
    at org.openrewrite.java.JavaVisitor.visitCompilationUnit (JavaVisitor.java:495)
    at org.openrewrite.java.tree.J$CompilationUnit.acceptJava (J.java:1549)
    at org.openrewrite.java.tree.J.accept (J.java:59)
    at org.openrewrite.TreeVisitor.visit (TreeVisitor.java:250)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer.getRecipeErrors (AbstractRewriteBaseRunMojo.java:384)
    at org.openrewrite.maven.AbstractRewriteBaseRunMojo$ResultsContainer.getFirstException (AbstractRewriteBaseRunMojo.java:364)
    at org.openrewrite.maven.AbstractRewriteRunMojo.execute (AbstractRewriteRunMojo.java:65)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2 (MojoExecutor.java:370)
    at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute (MojoExecutor.java:351)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:171)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:163)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:298)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:960)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:293)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:196)
    at jdk.internal.reflect.DirectMethodHandleAccessor.invoke (DirectMethodHandleAccessor.java:103)
    at java.lang.reflect.Method.invoke (Method.java:580)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:283)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:226)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:407)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:348)
[ERROR] 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/PluginExecutionException
@adambir adambir added the bug Something isn't working label Oct 29, 2024
@timtebeek
Copy link
Contributor

hi @adambir ; thanks for trying out both the latest and previous version, and reporting your findings here. It looks like @lkerford might know more, based on recent changes:

@lkerford would you mind taking a first look at this?
Or alternatively @adambir : would you want to help out by creating a unit test based on this example on a draft PR?

@Issue("https://github.com/openrewrite/rewrite/issues/1740")
@Test
void sourcesEnabled() {
rewriteRun(
srcTestJava(
//language=java
java(
"""
class A {
final String val1 = "value";
final String val2 = "value";
final String val3 = "value";
}
""",
"""
class A {
private static final String VALUE = "value";
final String val1 = VALUE;
final String val2 = VALUE;
final String val3 = VALUE;
}
"""
)
)
);
}

adambir added a commit to adambir/rewrite-static-analysis that referenced this issue Oct 30, 2024
@adambir
Copy link
Author

adambir commented Oct 30, 2024

Added a test case in #385 as suggested

adambir added a commit to adambir/rewrite-static-analysis that referenced this issue Oct 30, 2024
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
Status: No status
Development

No branches or pull requests

2 participants