diff options
author | Paul Phillips <paulp@improving.org> | 2013-04-02 11:05:54 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2013-04-03 01:29:21 -0700 |
commit | 7168743a909c6be9c607f856fdc3e789b53271a6 (patch) | |
tree | 472b8bcdadf9cd1d9dd45a97f2a55e264ee0062a | |
parent | b575c1ed131a578a18239e3922c1ea8b7dd188a2 (diff) | |
download | scala-7168743a909c6be9c607f856fdc3e789b53271a6.tar.gz scala-7168743a909c6be9c607f856fdc3e789b53271a6.tar.bz2 scala-7168743a909c6be9c607f856fdc3e789b53271a6.zip |
Added ensureAccessible to reflection library.
This method comes up with some frequency and there are a lot
of ways to write it either unsafely (failing to catch a likely
exception) or with inadequate generality (as was done in the
pre-existing version of this method in ScalaRunTime) so I
thought it warranted a place in the standard library.
-rw-r--r-- | src/library/scala/reflect/package.scala | 14 | ||||
-rw-r--r-- | src/library/scala/runtime/ScalaRunTime.scala | 8 |
2 files changed, 15 insertions, 7 deletions
diff --git a/src/library/scala/reflect/package.scala b/src/library/scala/reflect/package.scala index 10e6d7d9a4..97d7da3f2d 100644 --- a/src/library/scala/reflect/package.scala +++ b/src/library/scala/reflect/package.scala @@ -1,5 +1,7 @@ package scala +import java.lang.reflect.{ AccessibleObject => jAccessibleObject } + package object reflect { // in the new scheme of things ClassManifests are aliased to ClassTags @@ -42,6 +44,18 @@ package object reflect { def classTag[T](implicit ctag: ClassTag[T]) = ctag + /** Make a java reflection object accessible, if it is not already + * and it is possible to do so. If a SecurityException is thrown in the + * attempt, it is caught and discarded. + */ + def ensureAccessible[T <: jAccessibleObject](m: T): T = { + if (!m.isAccessible) { + try m setAccessible true + catch { case _: SecurityException => } // does nothing + } + m + } + // anchor for the class tag materialization macro emitted during tag materialization in Implicits.scala // implementation is hardwired into `scala.reflect.reify.Taggers` // using the mechanism implemented in `scala.tools.reflect.FastTrack` diff --git a/src/library/scala/runtime/ScalaRunTime.scala b/src/library/scala/runtime/ScalaRunTime.scala index 753dd0205e..ea1f392e2b 100644 --- a/src/library/scala/runtime/ScalaRunTime.scala +++ b/src/library/scala/runtime/ScalaRunTime.scala @@ -158,13 +158,7 @@ object ScalaRunTime { // Java bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4071957 // More background at ticket #2318. - def ensureAccessible(m: JMethod): JMethod = { - if (!m.isAccessible) { - try m setAccessible true - catch { case _: SecurityException => () } - } - m - } + def ensureAccessible(m: JMethod): JMethod = scala.reflect.ensureAccessible(m) def checkInitialized[T <: AnyRef](x: T): T = if (x == null) throw new UninitializedError else x |