Scala Programming Language
  1. Scala Programming Language
  2. SI-2296

IllegalAccessError with mixed Scala/Java due to trait encoding

    Details

      Description

      This issue is present on both trunk and 2.7.x.

      j/J.java:

      package j;
      
      public class J {
        protected void foo() {
          System.out.println("J.foo()");
        }
      }
      

      s/S.scala:

      package s
      
      import j.J
      
      trait S extends J {
        def bar() {
          foo()
        }
      }
      
      class SC extends J with S
      
      object Test {
        def main(args : Array[String]) {
          (new SC).bar()
        }
      }
      

      These compile without error (either compiling the Java first then the Scala; or compiling both the Scala and Java with scalac first, then the Java with javac). On running the following stacktrace is generated,

      java.lang.IllegalAccessError: tried to access method j.J.foo()V from class s.S$$class
      	at s.S$$class.bar(S.scala:7)
      	at s.SC.bar(S.scala:11)
      	at s.Test$$.main(S.scala:15)
      	at s.Test.main(S.scala)
      	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
      	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      	at java.lang.reflect.Method.invoke(Method.java:597)
      	at scala.util.ScalaClassLoader$$$$anonfun$$run$$1.apply(ClassLoader.scala:54)
      	at scala.util.ScalaClassLoader$$class.asContext(ClassLoader.scala:21)
      	at scala.util.URLClassLoader.asContext(ClassLoader.scala:58)
      	at scala.util.ScalaClassLoader$$class.run(ClassLoader.scala:54)
      	at scala.util.URLClassLoader.run(ClassLoader.scala:58)
      	at scala.tools.nsc.ObjectRunner$$.run(ObjectRunner.scala:33)
      	at scala.tools.nsc.MainGenericRunner$$.main(MainGenericRunner.scala:138)
      	at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
      

      Viewing the bytecode for s.S$$class shows why this is the case:

      public abstract class s.S$$class extends java.lang.Object{
      public static void $$init$$(s.S);
        Code:
         0:	return
      
      public static void bar(s.S);
        Code:
         0:	aload_0
         1:	checkcast	SI-12; //class j/J
         4:	invokevirtual	SI-16; //Method j/J.foo:()V
         7:	return
      }
      

      J.foo is invoked from s.S$$class which does not extend j.J despite trait s.S extending j.J.

      It would be great if this could be fixed, but presumably that would involve a significant change to the encoding of traits, so I'd settle for a compile time error for this case.

        Activity

        Hide
        Paul Phillips added a comment -

        I can see this crash up to m2, but it works in m3, which makes me think the original fix was in. I checked in this as a test anyway in b0dd0452fd .

        Show
        Paul Phillips added a comment - I can see this crash up to m2, but it works in m3, which makes me think the original fix was in. I checked in this as a test anyway in b0dd0452fd .
        Hide
        Mirco Dotta added a comment -

        Please, let us know the moment this is backported to the 2.9.x stream, as we have an open ticket on our issue tracker that is related to this issue: http://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001090

        This is of course no high priority (it might as well never be backported).

        Show
        Mirco Dotta added a comment - Please, let us know the moment this is backported to the 2.9.x stream, as we have an open ticket on our issue tracker that is related to this issue: http://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001090 This is of course no high priority (it might as well never be backported).
        Hide
        Hubert Plociniczak added a comment -

        Mirco, this has been backported long time ago (see merge in 2.9.x around 1st of June).

        Show
        Hubert Plociniczak added a comment - Mirco, this has been backported long time ago (see merge in 2.9.x around 1st of June).
        Hide
        Mirco Dotta added a comment - - edited

        I see. Then it looks like another bug is lying around somewhere, as we can still see this issue in the IDE.

        In short, I tried to merge your pull-request (https://github.com/scala-ide/scala-ide/pull/80) but had to rollback right after because of http://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001083. If you want to investigate, I'd be delighted

        Show
        Mirco Dotta added a comment - - edited I see. Then it looks like another bug is lying around somewhere, as we can still see this issue in the IDE. In short, I tried to merge your pull-request ( https://github.com/scala-ide/scala-ide/pull/80 ) but had to rollback right after because of http://scala-ide-portfolio.assembla.com/spaces/scala-ide/tickets/1001083 . If you want to investigate, I'd be delighted
        Hide
        Hubert Plociniczak added a comment -

        Paul, found a counterexample for you: SI-6245.

        Show
        Hubert Plociniczak added a comment - Paul, found a counterexample for you: SI-6245 .

          People

          • Assignee:
            Paul Phillips
            Reporter:
            Miles Sabin
            TracCC:
            Antony Stubbs, Ben Lings, Erik Engbrecht, Ismael Juma, Johannes Rudolph, Matthew Pocock, Paul Phillips, spiros
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development