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

Classloading problems in REPL with Scala reflection

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Labels:
      None

      Description

      Scala reflection doesn't work in REPL due to classloading issues, see:

      Welcome to Scala version 2.10.0.r26077-b20111127225325 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
      Type in expressions to have them evaluated.
      Type :help for more information.
      
      scala> import scala.reflect.mirror._
      import scala.reflect.mirror._
      
      scala> class A
      defined class A
      
      scala> classToType(classOf[A])
      java.lang.NullPointerException
      	at scala.reflect.runtime.JavaToScala$class.javaClass(JavaToScala.scala:46)
      	at scala.reflect.runtime.Universe.javaClass(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$class.javaClass(JavaToScala.scala:44)
      	at scala.reflect.runtime.Universe.javaClass(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$class.isJavaClass(JavaToScala.scala:51)
      	at scala.reflect.runtime.Universe.isJavaClass(Universe.scala:11)
      	at scala.reflect.runtime.Loaders$PackageScope.lookupEntry(Loaders.scala:113)
      	at scala.reflect.internal.Types$Type.findDecl(Types.scala:872)
      	at scala.reflect.internal.Types$Type.decl(Types.scala:498)
      	at scala.reflect.internal.SymbolTable.openPackageModule(SymbolTable.scala:185)
      	at scala.reflect.runtime.Loaders$LazyPackageType.complete(Loaders.scala:88)
      	at scala.reflect.internal.Symbols$Symbol.info(Symbols.scala:910)
      	at scala.reflect.runtime.JavaToScala$class.makeScalaPackage(JavaToScala.scala:334)
      	at scala.reflect.runtime.Universe.makeScalaPackage(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$class.packageNameToScala(JavaToScala.scala:322)
      	at scala.reflect.runtime.Universe.packageNameToScala(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$class.makeScalaPackage(JavaToScala.scala:331)
      	at scala.reflect.runtime.Universe.makeScalaPackage(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$$anonfun$packageToScala$1.apply(JavaToScala.scala:314)
      	at scala.reflect.runtime.JavaToScala$$anonfun$packageToScala$1.apply(JavaToScala.scala:314)
      	at scala.reflect.runtime.ConversionUtil$TwoWayCache.toScala(ConversionUtil.scala:30)
      	at scala.reflect.runtime.JavaToScala$class.packageToScala(JavaToScala.scala:313)
      	at scala.reflect.runtime.Universe.packageToScala(Universe.scala:11)
      	at scala.reflect.runtime.JavaToScala$class.scala$reflect$runtime$JavaToScala$$sOwner(JavaToScala.scala:249)
      	at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:375)
      	at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:354)
      	at scala.reflect.runtime.ConversionUtil$TwoWayCache.toScala(ConversionUtil.scala:30)
      	at scala.reflect.runtime.JavaToScala$class.classToScala(JavaToScala.scala:353)
      	at scala.reflect.runtime.Universe.classToScala(Universe.scala:11)
      	at scala.reflect.runtime.Universe.<init>(Universe.scala:41)
      	at scala.reflect.runtime.Mirror.<init>(Mirror.scala:9)
      	at scala.reflect.runtime.Mirror$.<init>(Mirror.scala:46)
      	at scala.reflect.runtime.Mirror$.<clinit>(Mirror.scala)
      	at java.lang.Class.forName0(Native Method)
      	at java.lang.Class.forName(Class.java:247)
      	at scala.reflect.ReflectionUtils$.singletonInstance(ReflectionUtils.scala:34)
      	at scala.reflect.package$.mirror(package.scala:11)
      ...
      

        Issue Links

          Activity

          Hide
          Eugene Burmako added a comment -

          Related to this bug is https://issues.scala-lang.org/browse/SI-5226. Strangely enough, it manifests itself only on Mac (at least, I wasn't able to reproduce it on my Windows laptop). Also, the aforementioned issue also mentions a workaround that seems to work.

          Show
          Eugene Burmako added a comment - Related to this bug is https://issues.scala-lang.org/browse/SI-5226 . Strangely enough, it manifests itself only on Mac (at least, I wasn't able to reproduce it on my Windows laptop). Also, the aforementioned issue also mentions a workaround that seems to work.
          Hide
          Jason Zaugg added a comment -

          Workaround is to use -Yrepl-sync.

          scala>   ~/code/scala scala210 -Yrepl-sync
          Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
          Type in expressions to have them evaluated.
          Type :help for more information.
          
          scala> Thread.currentThread.getContextClassLoader
          res0: ClassLoader = Cl#611660851
          
          scala> reflect.mirror.classToSymbol(classOf[List[_]])
          res1: reflect.mirror.Symbol = class List
          
          scala> 
          
          scala>   ~/code/scala scala210
          Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).
          Type in expressions to have them evaluated.
          Type :help for more information.
          
          scala> Thread.currentThread.getContextClassLoader
          res0: ClassLoader = null
          
          scala> reflect.mirror.classToSymbol(classOf[List[_]])
          java.lang.NullPointerException
          	at scala.reflect.runtime.JavaToScala$class.javaClass(JavaToScala.scala:48)
          	at scala.reflect.runtime.Universe.javaClass(Universe.scala:11)
          
          Show
          Jason Zaugg added a comment - Workaround is to use -Yrepl-sync . scala> ~/code/scala scala210 -Yrepl-sync Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). Type in expressions to have them evaluated. Type :help for more information. scala> Thread.currentThread.getContextClassLoader res0: ClassLoader = Cl#611660851 scala> reflect.mirror.classToSymbol(classOf[List[_]]) res1: reflect.mirror.Symbol = class List scala> scala> ~/code/scala scala210 Welcome to Scala version 2.10.0-M1 (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29). Type in expressions to have them evaluated. Type :help for more information. scala> Thread.currentThread.getContextClassLoader res0: ClassLoader = null scala> reflect.mirror.classToSymbol(classOf[List[_]]) java.lang.NullPointerException at scala.reflect.runtime.JavaToScala$class.javaClass(JavaToScala.scala:48) at scala.reflect.runtime.Universe.javaClass(Universe.scala:11)
          Hide
          Paul Phillips added a comment - - edited

          This fails differently now (and fails the same with -Yrepl-sync) on OSX. Can anyone comment on where the responsibility lies?

          scala> classToType(classOf[A])
          java.lang.AssertionError: assertion failed: <none>/class /<none>/
          	at scala.Predef$.assert(Predef.scala:160)
          	at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:360)
          	at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:354)
          	at scala.reflect.runtime.ConversionUtil$TwoWayCache.toScala(ConversionUtil.scala:31)
          	at scala.reflect.runtime.JavaToScala$class.classToScala(JavaToScala.scala:354)
          	at scala.reflect.runtime.Universe.classToScala(Universe.scala:11)
          	at scala.reflect.runtime.JavaToScala$class.scala$reflect$runtime$JavaToScala$$sOwner(JavaToScala.scala:245)
          
          

          Oh, I see it's calling getSimpleName, that's probably doomed anyway.

          Show
          Paul Phillips added a comment - - edited This fails differently now (and fails the same with -Yrepl-sync) on OSX. Can anyone comment on where the responsibility lies? scala> classToType(classOf[A]) java.lang.AssertionError: assertion failed: <none>/class /<none>/ at scala.Predef$.assert(Predef.scala:160) at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:360) at scala.reflect.runtime.JavaToScala$$anonfun$classToScala$1.apply(JavaToScala.scala:354) at scala.reflect.runtime.ConversionUtil$TwoWayCache.toScala(ConversionUtil.scala:31) at scala.reflect.runtime.JavaToScala$class.classToScala(JavaToScala.scala:354) at scala.reflect.runtime.Universe.classToScala(Universe.scala:11) at scala.reflect.runtime.JavaToScala$class.scala$reflect$runtime$JavaToScala$$sOwner(JavaToScala.scala:245) Oh, I see it's calling getSimpleName, that's probably doomed anyway.
          Hide
          Eugene Burmako added a comment -

          Yeah, speaking of getSimpleName, this is how I went from this bug to SI-5425. Regarding the assertion, I'll take a look once class names are sorted out.

          Show
          Eugene Burmako added a comment - Yeah, speaking of getSimpleName, this is how I went from this bug to SI-5425 . Regarding the assertion, I'll take a look once class names are sorted out.
          Show
          Eugene Burmako added a comment - Fixed by https://github.com/scala/scala/commit/1e0707786b118e3e33379e7acdc75306b45e6547

            People

            • Assignee:
              Eugene Burmako
              Reporter:
              Grzegorz Kossakowski
            • Votes:
              1 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development