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-reflect fails to find constructor for a nested class of a trait which has self-type #9528

Open
scabug opened this issue Oct 22, 2015 · 1 comment
Milestone

Comments

@scabug
Copy link

scabug commented Oct 22, 2015

Running this code

import reflect.runtime.currentMirror

trait Baz { self: Baz1 =>
  class Bar
}

class Baz1 extends Baz

object Foo {
  def main (args: Array[String]){
    val x = new Baz1

    val sym = currentMirror.classSymbol(classOf[Baz#Bar])
    val clMirror = currentMirror.reflect(x).reflectClass(sym)

    val constr = clMirror.reflectConstructor(sym.primaryConstructor.asMethod)
    constr.apply()
  }
}

throws an exception:

Exception in thread "main" java.lang.NoSuchMethodException: Baz$Bar.<init>(Baz)
	at java.lang.Class.getConstructor0(Class.java:2902)
	at java.lang.Class.getDeclaredConstructor(Class.java:2066)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$constructorToJava$1.apply(JavaMirrors.scala:1287)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$$anonfun$constructorToJava$1.apply(JavaMirrors.scala:1281)
	at scala.reflect.runtime.TwoWayCaches$TwoWayCache$$anonfun$toJava$1.apply(TwoWayCaches.scala:50)
	at scala.reflect.runtime.Gil$class.gilSynchronized(Gil.scala:19)
	at scala.reflect.runtime.JavaUniverse.gilSynchronized(JavaUniverse.scala:16)
	at scala.reflect.runtime.TwoWayCaches$TwoWayCache.toJava(TwoWayCaches.scala:45)
	at scala.reflect.runtime.JavaMirrors$JavaMirror.constructorToJava(JavaMirrors.scala:1281)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jconstr$lzycompute(JavaMirrors.scala:332)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jconstr(JavaMirrors.scala:332)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaVanillaMethodMirror0.jinvokeraw(JavaMirrors.scala:365)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaMethodMirror.jinvoke(JavaMirrors.scala:339)
	at scala.reflect.runtime.JavaMirrors$JavaMirror$JavaVanillaMethodMirror.apply(JavaMirrors.scala:355)
	at Foo$.main(Foo.scala:17)
	at Foo.main(Foo.scala)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:606)
	at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

The reason is that the Bar constructor takes Baz1 as the first argument instead of Baz.
Removing the self-type makes it work, but isn't an acceptable workaround in my case.

@scabug
Copy link
Author

scabug commented Oct 22, 2015

Imported From: https://issues.scala-lang.org/browse/SI-9528?orig=1
Reporter: @alexeyr
Affected Versions: 2.11.7

@SethTisue SethTisue added this to the Backlog milestone Mar 3, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants