Skip to content

rewrite-maven-plugin: Can not rewrite "s3object.getObjectMetadata().getUserMetaDataOf("foo")" #6821

@retomerz

Description

@retomerz

Describe the bug

I am trying to rewrite my app from V1 SDK to V2 SDK. It fails with an error:

[ERROR] The recipe produced an error. Please report this to the recipe author.
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.17.0:dryRun (default-cli) on project myapp: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.17.0:dryRun failed: Error while visiting src\main\java\com\myapp\MyClass.java: java.lang.ClassCastException: class org.openrewrite.java.tree.J$MethodInvocation cannot be cast to class org.openrewrite.java.tree.J$Identifier (org.openrewrite.java.tree.J$MethodInvocation and org.openrewrite.java.tree.J$Identifier are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @20f8f440)

Regression Issue

  • Select this option if this issue appears to be a regression.

Expected Behavior

Plugin should complete successfully.

Current Behavior

[ERROR] The recipe produced an error. Please report this to the recipe author.
[ERROR] Failed to execute goal org.openrewrite.maven:rewrite-maven-plugin:6.17.0:dryRun (default-cli) on project myapp: Execution default-cli of goal org.openrewrite.maven:rewrite-maven-plugin:6.17.0:dryRun failed: Error while visiting src\main\java\com\myapp\MyClass.java: java.lang.ClassCastException: class org.openrewrite.java.tree.J$MethodInvocation cannot be cast to class org.openrewrite.java.tree.J$Identifier (org.openrewrite.java.tree.J$MethodInvocation and org.openrewrite.java.tree.J$Identifier are in unnamed module of loader org.codehaus.plexus.classworlds.realm.ClassRealm @20f8f440)[ERROR]   software.amazon.awssdk.v2migration.S3PutObjectRequestToV2$Visitor.saveMetadataValueAndRemoveStatement(S3PutObjectRequestToV2.java:423)
[ERROR]   software.amazon.awssdk.v2migration.S3PutObjectRequestToV2$Visitor.visitMethodInvocation(S3PutObjectRequestToV2.java:97)
[ERROR]   software.amazon.awssdk.v2migration.S3PutObjectRequestToV2$Visitor.visitMethodInvocation(S3PutObjectRequestToV2.java:84)
[ERROR]   org.openrewrite.java.tree.J$MethodInvocation.acceptJava(J.java:4290)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:60)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
[ERROR]   org.openrewrite.java.JavaVisitor.visitRightPadded(JavaVisitor.java:1310)
[ERROR]   org.openrewrite.java.JavaVisitor.lambda$visitBlock$4(JavaVisitor.java:392)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:245)
[ERROR]   org.openrewrite.internal.ListUtils.map(ListUtils.java:269)
[ERROR]   org.openrewrite.java.JavaVisitor.visitBlock(JavaVisitor.java:391)
[ERROR]   org.openrewrite.java.tree.J$Block.acceptJava(J.java:848)
[ERROR]   org.openrewrite.java.tree.J.accept(J.java:60)
[ERROR]   org.openrewrite.TreeVisitor.visit(TreeVisitor.java:245)
[ERROR]   org.openrewrite.TreeVisitor.visitAndCast(TreeVisitor.java:311)
[ERROR]   ...

Reproduction Steps

Code:

S3Object object = ...
object.getObjectMetadata().getUserMetaDataOf("foo");

Run:

mvn org.openrewrite.maven:rewrite-maven-plugin:6.17.0:dryRun -Drewrite.recipeArtifactCoordinates=software.amazon.awssdk:v2-migration:2.42.21 -Drewrite.activeRecipes=software.amazon.awssdk.v2migration.AwsSdkJavaV1ToV2

Possible Solution

Store return value of getObjectMetadata() into a local var and call getUserMetaDataOf on that:

S3Object object = ...;
ObjectMetadata om = object.getObjectMetadata();
om.getUserMetaDataOf("foo");

Additional Information/Context

No response

AWS Java SDK version used

2.42.21

JDK version used

openjdk version "17.0.15" 2025-04-15 OpenJDK Runtime Environment Temurin-17.0.15+6 (build 17.0.15+6) OpenJDK 64-Bit Server VM Temurin-17.0.15+6 (build 17.0.15+6, mixed mode, sharing)

Operating System and version

Microsoft Windows [Version 10.0.17763.8389]

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.migration-toolThis issue is about the Java SDK v1 to v2 Migration Tool.p2This is a standard priority issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions