-
Notifications
You must be signed in to change notification settings - Fork 60
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
cbt should only show public members of BaseBuild & co #522
Comments
Would be great to have more info with an example and the pain this is causing. Classifying it as backlog for now. |
we should evaluate how performance is affected if using scala reflect here |
That, basically. It makes sense (to me) to have these case classes readily available as soon as you extend a trait, but they only make sense in the scope of the trait: they shouldn't leak out as cbt arguments |
Why not use protected then? Java reflection picks up on that. Anyways, I'd opt for letting users import things explicitly and not using the OO-scopes too much to inject names. Supportive of structuring things for convenient wild card imports though. |
Either Scala is encoding things differently than java reflection expects, or I'm doing something wrong. On a side-note, as
|
@megri ah, that's entirely possible. does protected[this] or private[this] show up via reflection? |
protected[this] shows up as public. private and private[this] is private to java reflection. |
Mh maybe scala protected isn't encoded in Java then. Interesting. I thought it would be.
…On June 16, 2017 6:03:52 PM EDT, megri ***@***.***> wrote:
protected[this] shows up as public. private and private[this] is
private to java reflection.
--
You are receiving this because you commented.
Reply to this email directly or view it on GitHub:
#522 (comment)
|
I have a working prototype at home, I'll commit it so you can have a look when I'm back :) |
cool :)! |
Sorry for not getting back sooner. So what I have right now is a way to reflect on Scala type members a la carte. My issue is that tying this logic into the existing code is troublesome — there's a lot of loosely typed stuff floating around and I'm getting odd errors (nothing is found) when trying to retrofit my solution. Anyway, here is what I've come up with so far import scala.reflect.ClassTag
import scala.reflect.runtime.universe
def methodsOf[T: universe.TypeTag](cls: Class[T]) = {
implicit val ct = ClassTag[T](cls)
val mirror = universe.runtimeMirror(cls.getClassLoader)
def declaredIn(tpe: universe.Type) = {
tpe
.decls
.collect {
case m if
!m.isConstructor &&
m.isPublic && // comment me out to get exceptions!
m.isMethod &&
m.asMethod.paramLists.forall(_.isEmpty) =>
val methodF: T => Any = instance => mirror.reflect(instance).reflectMethod(m.asMethod).apply()
m.name.decodedName.toString -> methodF
}
.toMap
}
mirror
.typeOf[T]
.baseClasses
.dropRight(2) // drop … <: Object <: Any
.foldLeft(Map.empty[String, T => Any]) {
case (map, symbol) =>
map ++ declaredIn(symbol.typeSignature)
}
}
// test
trait Foo {
def takesParameters(x: Int) =
throw new Exception("only arity-0 members should show up")
}
trait Bar {
protected def protectedMember =
throw new Exception("protected members should not be visible")
}
class Impl extends Foo with Bar {
lazy val everything = "is ok!"
}
val instance = new Impl
methodsOf(classOf[Impl]).map { case (key, f) => key -> f(instance) } |
What do we make of this? :) |
you mean when trying to get it into CBT? Just create PRs, I am happy to look into the errors. |
Alright, I'll retrofit is as far as I can and create a branch! |
Currently, protected members are shown as valid arguments when running cbt. The issue is most likely with this method:
cbt/stage2/Lib.scala
Line 56 in 6187113
The method above uses Java reflection to reflect on public members, but should be using scala.reflect for proper introspection
The text was updated successfully, but these errors were encountered: