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

ClassCastException when trying to get Element from Node #4202

Closed
schabe77 opened this issue Aug 2, 2023 · 3 comments · Fixed by #4204
Closed

ClassCastException when trying to get Element from Node #4202

schabe77 opened this issue Aug 2, 2023 · 3 comments · Fixed by #4204
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@schabe77
Copy link

schabe77 commented Aug 2, 2023

Describe the bug

Hi,
I'm using aws-cdk for java. After upgrading the artifact software.amazon.jsii:jsii-runtime from version 1.85.0 to 1.86.0 a lot of my stacks don't compile anymore. Here an example stack

public class RolesAnywhereStack extends Stack {
    public RolesAnywhereStack(App parent) {
        super(parent, "RolesAnywhereStack");
        final Role bastionRole = Role.Builder.create(this, "RolesAnywhereBastionRole")
                .assumedBy(new ServicePrincipal("rolesanywhere.amazonaws.com"))
                .description("Role that is used when connecting via RolesAnywhere")
                .build();
        CfnRole cfnRole = (CfnRole) bastionRole.getNode().getDefaultChild();
    }
}

when running cdk synthesize RolesAnywhereStack a ClassCastException occurs:

Caused by: java.lang.ClassCastException: class software.constructs.Construct cannot be cast to class software.amazon.awscdk.services.iam.CfnRole (software.constructs.Construct and software.amazon.awscdk.services.iam.CfnRole are in unnamed module of loader java.net.URLClassLoader @7486e0ca)

with jsii-runtime 1.85.0 everything works fine.

Expected Behavior

No ClassCastException

Current Behavior

ClassCastException

Reproduction Steps

try to synthesize this stack

public class RolesAnywhereStack extends Stack {
    public RolesAnywhereStack(App parent) {
        super(parent, "RolesAnywhereStack");
        final Role bastionRole = Role.Builder.create(this, "RolesAnywhereBastionRole")
                .assumedBy(new ServicePrincipal("rolesanywhere.amazonaws.com"))
                .description("Role that is used when connecting via RolesAnywhere")
                .build();
        CfnRole cfnRole = (CfnRole) bastionRole.getNode().getDefaultChild();
    }
}

Possible Solution

No response

Additional Information/Context

No response

SDK version used

software.amazon.jsii:jsii-runtime:1.85.0

Environment details (OS name and version, etc.)

Debian openjdk version "17.0.2" 2022-01-18 OpenJDK Runtime Environment (build 17.0.2+8-86) OpenJDK 64-Bit Server VM (build 17.0.2+8-86, mixed mode, sharing)

@schabe77 schabe77 added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Aug 2, 2023
@schabe77 schabe77 changed the title ClassCastException trying to get Element from Node ClassCastException when trying to get Element from Node Aug 2, 2023
@n-orlov
Copy link

n-orlov commented Aug 2, 2023

got similar issue with function.addAlias() (lambda function construct):

java.lang.ClassCastException: class software.constructs.Construct cannot be cast to class software.amazon.awscdk.services.lambda.Alias (software.constructs.Construct and software.amazon.awscdk.services.lambda.Alias are in unnamed module of loader 'app')

Reverting to jsii 1.85.0 helped:

            <dependency>
                <groupId>software.amazon.jsii</groupId>
                <artifactId>jsii-runtime</artifactId>
                <version>1.85.0</version>
            </dependency>

@ielkhalloufi
Copy link

See the other issue how I created a workaround: aws/aws-cdk#26604

RomainMuller added a commit that referenced this issue Aug 2, 2023
The registration was done on the object's prototype, and the value from the constructor was always used, even if that was inherited, such that if a parent type had already been resolved previously, all child types would use the parent's FQN instead of their own.

Addressed this by instead storing the associations in an external WeakMap, and added a test case to validate correct behavior.

Fixes aws/aws-cdk#26604
Fixes #4202
Fixes #4203
@github-actions
Copy link
Contributor

github-actions bot commented Aug 2, 2023

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants