Skip to content

java.lang.IllegalAccessError: failed to access class software.amazon.smithy.model.shapes.NamedMembersShape$Builder #1311

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

Closed
daddykotex opened this issue Jul 21, 2022 · 3 comments

Comments

@daddykotex
Copy link
Contributor

daddykotex commented Jul 21, 2022

Reproduced in a scala REPL, but the same should happen in a regular java project with a dependency on smithy-model 1.22.0:


scala> :paste
// Entering paste mode (ctrl-D to finish)

import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait

val structMember = MemberShape
    .builder()
    .id("test#struct$testing")
    .target("smithy.api#Integer")
    .build()
val struct =
    StructureShape
    .builder()
    .id("test#struct")
    .addMember(structMember)
    .addTrait(new ErrorTrait("client"))
    .build()


// Exiting paste mode, now interpreting.

java.lang.IllegalAccessError: failed to access class software.amazon.smithy.model.shapes.NamedMembersShape$Builder from class $iw (software.amazon.smithy.model.shapes.NamedMembersShape$Builder is in unnamed module of loader sbt.internal.inc.classpath.ClasspathUtil$$anon$2 @2dacc87b; $iw is in unnamed module of loader scala.tools.nsc.interpreter.IMain$TranslatingClassLoader @30001570)
  ... 67 elided

But if I do:

scala> :paste
// Entering paste mode (ctrl-D to finish)

import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait

val structMember = MemberShape
    .builder()
    .id("test#struct$testing")
    .target("smithy.api#Integer")
    .build()
val struct =
    StructureShape
    .builder()
    .id("test#struct")
    .addTrait(new ErrorTrait("client"))
    .addMember(structMember)
    .build()


// Exiting paste mode, now interpreting.

import software.amazon.smithy.model.shapes.MemberShape
import software.amazon.smithy.model.shapes.StructureShape
import software.amazon.smithy.model.traits.ErrorTrait
val structMember: software.amazon.smithy.model.shapes.MemberShape = (member: `test#struct$testing`)
val struct: software.amazon.smithy.model.shapes.StructureShape = (structure: `test#struct`)

It works fine. The only difference is the order of the calls in the builder. If I move .addTrait(new ErrorTrait("client")) after .addMember(structMember), it fails.

@mtdowling
Copy link
Member

I think this might be a Scala compiler issue based on scala/scala3#13841 (comment).

This test case works fine in Java (17.0.3) in the smithy-model package:

    @Test
    public void testBuilderOrder() {
        MemberShape structMember = MemberShape
                .builder()
                .id("test#struct$testing")
                .target("smithy.api#Integer")
                .build();
        StructureShape struct = StructureShape
                .builder()
                .id("test#struct")
                .addMember(structMember)
                .addTrait(new ErrorTrait("client"))
                .build();
    }

I suppose we could flatten NamedMemberShape and NamedMemberShape.Builder into StructureShape and UnionShape. I'm not sure if that would work with Scala though.

@daddykotex
Copy link
Contributor Author

Wow, I assumed it would not be an issue on scala's side without checking. Sorry for the noise.

@kubukoz
Copy link
Contributor

kubukoz commented Jan 30, 2023

Also hit this, for anyone seeing this before the upstream issue in Dotty is fixed, a workaround would be to explicitly upcast the return type of id / addMember to StructureShape.Builder.

For example (with a convenience extension method to avoid extra parentheses nesting:

extension [T](it: T) def upcast[U >: T]: U = it

def mkStruct(shapeId: ShapeId, members: List[MemberShape]) = StructureShape
  .builder()
  .members(members.asJava)
  .upcast[StructureShape.Builder]
  .id(shapeId)
  .upcast[StructureShape.Builder]
  .build()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants