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

reflection still very non-deterministic #6573

Open
scabug opened this issue Oct 27, 2012 · 5 comments
Open

reflection still very non-deterministic #6573

scabug opened this issue Oct 27, 2012 · 5 comments
Milestone

Comments

@scabug
Copy link

scabug commented Oct 27, 2012

scala> import scala.reflect.runtime.universe._
import scala.reflect.runtime.universe._

scala> rootMirror.staticPackage("java.lang").typeSignature.members.size
res0: Int = 16

scala> rootMirror.staticPackage("java.lang").typeSignature.members.flatMap(_.typeSignature.members)
res1: Iterable[reflect.runtime.universe.Symbol] = List(value serialVersionUID, constructor StringBuilder, ....)

scala> rootMirror.staticPackage("java.lang").typeSignature.members.size
res2: Int = 31
@scabug
Copy link
Author

scabug commented Oct 27, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6573?orig=1
Reporter: @paulp
Affected Versions: 2.10.0-RC1, 2.11.0-M1

@scabug
Copy link
Author

scabug commented Oct 28, 2012

@xeno-by said (edited on Oct 28, 2012 5:36:51 AM UTC):
Reflection uses classloaders, therefore it cannot exhaustively enumerate members of packages. Any ideas how to defeat that?

@scabug
Copy link
Author

scabug commented Oct 28, 2012

@retronym said:
While it can't enumerate members of packages in general, it can be done if you make some assumptions about the classloader (See, for example, Spring's PathMatchingResourcePatternResolver.)

I don't suggest to offer that functionality out of the box (maintaining and supporting that sort of code is expensive), but you might offer a hook at the point of mirror creation where the caller could provide a package enumerator.

@scabug
Copy link
Author

scabug commented Apr 29, 2013

@clhodapp said:
Also see Reflections, which is more focused on finding classes.

@scabug
Copy link
Author

scabug commented Apr 29, 2013

@clhodapp said (edited on Apr 29, 2013 3:42:50 AM UTC):
It's worse than you claim: simply being loaded is not enough to make a class show up:

package foo

import reflect.runtime.universe._
import reflect.runtime.{currentMirror=>cm}

class C

object Main extends App {

	val pkg = cm.staticPackage("foo")
	println(pkg.moduleClass.typeSignature.members)

	pkg.moduleClass.typeSignature.member(newTypeName("Main"))
	println(pkg.moduleClass.typeSignature.members)

	cm.classLoader.loadClass("foo.C")
	println(pkg.moduleClass.typeSignature.members)

	pkg.moduleClass.typeSignature.member(newTypeName("C"))
	println(pkg.moduleClass.typeSignature.members)

}
[info] Running foo.Main
Scope{

}
Scope{
  object Main;
  class Main extends
}
Scope{
  object Main;
  class Main extends
}
Scope{
  object C;
  class C extends ;
  object Main;
  class Main extends
}

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

1 participant