Reflection and Java package private methods

49 views
Skip to first unread message

Paul Butcher

unread,
Jan 17, 2013, 8:27:44 AM1/17/13
to scala...@googlegroups.com, Eugene Burmako
How can I determine if a method in a Java class is package private? The documentation for privateWithin says:

     *  The java access levels translate as follows:
     *
     *  java private:     isPrivate                  && (privateWithin == NoSymbol)
     *  java package:     !isPrivate && !isProtected && (privateWithin == enclosingPackage)
     *  java protected:   isProtected                && (privateWithin == enclosingPackage)
     *  java public:      !isPrivate && !isProtected && (privateWithin == NoSymbol)

However, here's what I get for getPrefixLength in java.io.File (which is package private):

Welcome to Scala version 2.10.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_09).
Type in expressions to have them evaluated.
Type :help for more information.

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

scala> val t = weakTypeOf[java.io.File]
t: reflect.runtime.universe.Type = java.io.File

scala> val m = t.member(newTermName("getPrefixLength"))
m: reflect.runtime.universe.Symbol = method getPrefixLength

scala> m.isPrivate
res0: Boolean = false

scala> m.isProtected
res1: Boolean = false

scala> m.privateWithin
res2: reflect.runtime.universe.Symbol = <none>

scala> m.isPublic
res3: Boolean = true

--
paul.butcher->msgCount++

Snetterton, Castle Combe, Cadwell Park...
Who says I have a one track mind?

http://www.paulbutcher.com/
LinkedIn: http://www.linkedin.com/in/paulbutcher
MSN: pa...@paulbutcher.com
AIM: paulrabutcher
Skype: paulrabutcher

Eugene Burmako

unread,
Jan 17, 2013, 8:43:33 AM1/17/13
to Paul Butcher, scala...@googlegroups.com, Paul Phillips
Let's also ask Paul.
Reply all
Reply to author
Forward
0 new messages