-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9177 from sjrd/make-assert-util-friendly-to-scala…
…-js-again Move reflection-related utils from AssertUtil to new ReflectUtil.
- Loading branch information
Showing
4 changed files
with
45 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Scala (https://www.scala-lang.org) | ||
* | ||
* Copyright EPFL and Lightbend, Inc. | ||
* | ||
* Licensed under Apache License 2.0 | ||
* (http://www.apache.org/licenses/LICENSE-2.0). | ||
* | ||
* See the NOTICE file distributed with this work for | ||
* additional information regarding copyright ownership. | ||
*/ | ||
|
||
package scala.tools.testkit | ||
|
||
import scala.reflect.ClassTag | ||
import scala.util.chaining._ | ||
import java.lang.reflect.{Array => _, _} | ||
|
||
/** This module contains reflection-related utilities. | ||
* | ||
* This object contains methods that will not work on Scala.js nor Scala | ||
* Native, making any test using `ReflectUtil` JVM-only. | ||
*/ | ||
object ReflectUtil { | ||
private lazy val modsField = classOf[Field].getDeclaredField("modifiers").tap(_.setAccessible(true)) | ||
|
||
def getFieldAccessible[T: ClassTag](n: String): Field = | ||
implicitly[ClassTag[T]] | ||
.runtimeClass.getDeclaredField(n) | ||
.tap { f => | ||
if ((f.getModifiers & Modifier.FINAL) != 0) | ||
modsField.setInt(f, f.getModifiers() & ~Modifier.FINAL) | ||
if ((f.getModifiers & Modifier.PUBLIC) == 0) | ||
f.setAccessible(true) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters