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

Scala Classloader with java.lang.ClassLoader.registerAsParallelCapable #11429

Open
ajithme opened this Issue Mar 12, 2019 · 4 comments

Comments

Projects
None yet
2 participants
@ajithme
Copy link

ajithme commented Mar 12, 2019

Currently i have a custom classloader abc.MyClassLoaderwhich extends java.net.URLClassLoader#URLClassLoader, and i want to enable my custom classloader as parallel capable. To do this i need to call a static method from my custom classloader java.lang.ClassLoader#registerAsParallelCapable

In java custom classloader this would be as straightforward as having registration done in a static block, but currently i see that for scala i need to do this is a companion object and not in my custom class, but it will not take affect as inside java.lang.ClassLoader#registerAsParallelCapable the class name is registered via its name which is got by Reflection.getCallerClass() which will give companion class name abc.MyClassLoader$ instead of actual class abc.MyClassLoader thus failing to enforce parallel capability for classloader

@ajithme

This comment has been minimized.

Copy link
Author

ajithme commented Mar 12, 2019

@som-snytt

This comment has been minimized.

Copy link

som-snytt commented Mar 13, 2019

You need SIP-30 which is in dotty but not yet Scala 2? The annotation requests the member as a static member of the companion class. It looks like it works.

dotty $ cat sandbox/C.scala

import java.net._

class C extends URLClassLoader(new Array[URL](0))
object C { @annotation.static val x = { ClassLoader.registerAsParallelCapable(); () } }
dotty $ ./bin/dotc sandbox/C.scala
dotty $ javap -private -verbose C.class
[snip]
  private static {};
    descriptor: ()V
    flags: ACC_PRIVATE, ACC_STATIC
    Code:
      stack=1, locals=0, args_size=0
         0: invokestatic  #25                 // Method java/lang/ClassLoader.registerAsParallelCapable:()Z
         3: pop
         4: getstatic     #30                 // Field scala/runtime/BoxedUnit.UNIT:Lscala/runtime/BoxedUnit;
         7: putstatic     #32                 // Field x:Lscala/runtime/BoxedUnit;
        10: return
@ajithme

This comment has been minimized.

Copy link
Author

ajithme commented Mar 13, 2019

@som-snytt Thanks for explanation. Unfortunately i cannot upgrade to higher scala version now. So i suppose moving my classloader implementation to java is the only option.?

@som-snytt

This comment has been minimized.

Copy link

som-snytt commented Mar 13, 2019

I haven't experimented, and maybe there is a better technique. You could write a minimal shim in Java that delegates to code in Scala. I see the mechanism requires superclasses to be registered, so extending the Scala class is not an option.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.