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

ReplaceConstantWithAnotherConstant fails to replace field annotation arguments #3448

Merged
merged 4 commits into from
Nov 26, 2023

Conversation

SimonVerhoeven
Copy link
Contributor

Note: this only applicable if the field constant is wrapped in curly braces

What's changed?

I've added a testcase to reproduce the issue when replacing a constant within an annotation that's wrapped in curly braces on field level.

What's your motivation?

To help troubleshoot the issue

Anything in particular you'd like reviewers to focus on?

The curly braces are important

Anyone you would like to review specifically?

@timtebeek @joanvr

Have you considered any alternatives or workarounds?

No

Any additional context

Checklist

  • I've added unit tests to cover both positive and negative cases
  • I've added the license header to any new files through ./gradlew licenseFormat
  • I've used the IntelliJ auto-formatter on affected files
  • I've updated the documentation (if applicable)

Note: this only applicable if the field constant is wrapped in curly braces
@timtebeek timtebeek added the bug Something isn't working label Aug 1, 2023
@timtebeek timtebeek marked this pull request as draft August 1, 2023 14:35
@timtebeek timtebeek changed the title draft: add testcase to reproduce field replacement issue ReplaceConstantWithAnotherConstant fails to replace field annotation arguments Aug 1, 2023
@timtebeek
Copy link
Contributor

Much appreciated that you've logged this reproduction test. I've had a brief look if I could resolve this quickly, but no such luck yet; here's the full stacktrace with the reduced example.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

java.lang.AssertionError: Failed to parse sources or run recipe

	at org.openrewrite.test.RewriteTest.lambda$defaultExecutionContext$11(RewriteTest.java:553)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.handleError(RecipeScheduler.java:304)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.lambda$editSources$4(RecipeScheduler.java:246)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.lambda$mapForRecipeRecursively$5(RecipeScheduler.java:330)
	at org.openrewrite.internal.InMemoryLargeSourceSet.lambda$edit$0(InMemoryLargeSourceSet.java:61)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.internal.InMemoryLargeSourceSet.edit(InMemoryLargeSourceSet.java:60)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.mapForRecipeRecursively(RecipeScheduler.java:323)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.editSources(RecipeScheduler.java:201)
	at org.openrewrite.RecipeScheduler.scheduleRun(RecipeScheduler.java:76)
	at org.openrewrite.Recipe.run(Recipe.java:279)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:339)
	at org.openrewrite.test.RewriteTest.rewriteRun(RewriteTest.java:130)
	at org.openrewrite.java.ReplaceConstantWithAnotherConstantTest.replaceConstantInCurlyBracesInAnnotation(ReplaceConstantWithAnotherConstantTest.java:75)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
Caused by: org.openrewrite.internal.RecipeRunException: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:329)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:184)
	at org.openrewrite.java.JavaTemplate.apply(JavaTemplate.java:73)
	at org.openrewrite.java.ReplaceConstantWithAnotherConstant$ReplaceConstantWithAnotherConstantVisitor.replaceFieldAccess(ReplaceConstantWithAnotherConstant.java:107)
	at org.openrewrite.java.ReplaceConstantWithAnotherConstant$ReplaceConstantWithAnotherConstantVisitor.visitFieldAccess(ReplaceConstantWithAnotherConstant.java:74)
	at org.openrewrite.java.ReplaceConstantWithAnotherConstant$ReplaceConstantWithAnotherConstantVisitor.visitFieldAccess(ReplaceConstantWithAnotherConstant.java:56)
	at org.openrewrite.java.tree.J$FieldAccess.acceptJava(J.java:1909)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1284)
	at org.openrewrite.java.JavaVisitor.lambda$visitContainer$34(JavaVisitor.java:1337)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.java.JavaVisitor.visitContainer(JavaVisitor.java:1337)
	at org.openrewrite.java.JavaVisitor.visitNewArray(JavaVisitor.java:915)
	at org.openrewrite.java.tree.J$NewArray.acceptJava(J.java:4018)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1284)
	at org.openrewrite.java.JavaVisitor.lambda$visitContainer$34(JavaVisitor.java:1337)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.java.JavaVisitor.visitContainer(JavaVisitor.java:1337)
	at org.openrewrite.java.JavaVisitor.visitAnnotation(JavaVisitor.java:222)
	at org.openrewrite.java.tree.J$Annotation.acceptJava(J.java:211)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.lambda$visitVariableDeclarations$24(JavaVisitor.java:880)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.java.JavaVisitor.visitVariableDeclarations(JavaVisitor.java:880)
	at org.openrewrite.java.tree.J$VariableDeclarations.acceptJava(J.java:5517)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1284)
	at org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:366)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:365)
	at org.openrewrite.java.tree.J$Block.acceptJava(J.java:767)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.visitClassDeclaration(JavaVisitor.java:453)
	at org.openrewrite.java.tree.J$ClassDeclaration.acceptJava(J.java:1217)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:361)
	at org.openrewrite.java.JavaVisitor.lambda$visitCompilationUnit$10(JavaVisitor.java:466)
	at org.openrewrite.internal.ListUtils.map(ListUtils.java:176)
	at org.openrewrite.java.JavaVisitor.visitCompilationUnit(JavaVisitor.java:466)
	at org.openrewrite.java.tree.J$CompilationUnit.acceptJava(J.java:1520)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:184)
	at org.openrewrite.Preconditions$1.visit(Preconditions.java:51)
	at org.openrewrite.Preconditions$1.visit(Preconditions.java:31)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.lambda$editSources$3(RecipeScheduler.java:231)
	at io.micrometer.core.instrument.AbstractTimer.recordCallable(AbstractTimer.java:147)
	at org.openrewrite.table.RecipeRunStats.recordEdit(RecipeRunStats.java:56)
	at org.openrewrite.RecipeScheduler$RecipeRunCycle.lambda$editSources$4(RecipeScheduler.java:228)
	... 14 more
Caused by: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0
	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckIndex(Preconditions.java:70)
	at java.base/jdk.internal.util.Preconditions.checkIndex(Preconditions.java:266)
	at java.base/java.util.Objects.checkIndex(Objects.java:359)
	at java.base/java.util.ArrayList.get(ArrayList.java:427)
	at org.openrewrite.java.internal.template.JavaTemplateParser.parseExpression(JavaTemplateParser.java:101)
	at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitFieldAccess(JavaTemplateJavaExtension.java:244)
	at org.openrewrite.java.internal.template.JavaTemplateJavaExtension$1.visitFieldAccess(JavaTemplateJavaExtension.java:56)
	at org.openrewrite.java.tree.J$FieldAccess.acceptJava(J.java:1909)
	at org.openrewrite.java.tree.J.accept(J.java:59)
	at org.openrewrite.TreeVisitor.visit(TreeVisitor.java:278)
	... 74 more

# Conflicts:
#	rewrite-java-test/src/test/java/org/openrewrite/java/ReplaceConstantWithAnotherConstantTest.java
@knutwannheden knutwannheden marked this pull request as ready for review November 26, 2023 19:10
@knutwannheden
Copy link
Contributor

It looks like this has been fixed in the meantime. I will just go ahead and merge this extra test case.

@knutwannheden knutwannheden merged commit 931c734 into openrewrite:main Nov 26, 2023
1 check passed
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 this pull request may close these issues.

3 participants