Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ClassValueCompat to support systems without java.lang.ClassValue
- Loading branch information
Showing
7 changed files
with
117 additions
and
68 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,47 @@ | ||
/* | ||
* 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.compat | ||
|
||
import scala.util.Try | ||
|
||
private[scala] abstract class ClassValueCompat[T] { | ||
|
||
private def checkClassValueAvailability(): Boolean = Try { | ||
Class.forName("java.lang.ClassValue", false, getClass.getClassLoader) | ||
true | ||
}.getOrElse(false) | ||
|
||
private val instance = if (checkClassValueAvailability()) new JavaClassValue() else new FallbackClassValue() | ||
|
||
private class JavaClassValue extends ClassValue[T] with ClassValueInterface { | ||
override def computeValue(`type`: Class[_]): T = ClassValueCompat.this.computeValue(`type`) | ||
} | ||
|
||
private class FallbackClassValue extends ClassValueInterface { | ||
override def get(param1Class: Class[_]): T = ClassValueCompat.this.get(param1Class) | ||
|
||
override def remove(param1Class: Class[_]): Unit = {} | ||
} | ||
|
||
private trait ClassValueInterface { | ||
def get(param1Class: Class[_]): T | ||
def remove(param1Class: Class[_]): Unit | ||
} | ||
|
||
def get(`type`: Class[_]): T = instance.get(`type`) | ||
|
||
def remove(`type`: Class[_]): Unit = instance.remove(`type`) | ||
|
||
protected def computeValue(`type`: Class[_]): T | ||
|
||
} |
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 was deleted.
Oops, something went wrong.
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,48 @@ | ||
/* | ||
* 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.runtime | ||
|
||
import java.io.Serializable | ||
import java.security.PrivilegedActionException | ||
import java.security.PrivilegedExceptionAction | ||
import compat.ClassValueCompat | ||
|
||
private[runtime] object ModuleSerializationProxy { | ||
private val instances = new ClassValueCompat[Object] { | ||
override protected def computeValue(`type`: Class[_]): Object = { | ||
try { | ||
java.security.AccessController.doPrivileged(new PrivilegedExceptionAction[Object] { | ||
override def run(): Object = { | ||
`type`.getField("MODULE$").get(null) | ||
} | ||
}) | ||
} catch { | ||
case e: PrivilegedActionException => | ||
rethrowRuntime(e.getCause) | ||
} | ||
} | ||
} | ||
|
||
private def rethrowRuntime(e: Throwable): Object = { | ||
val cause = e.getCause | ||
cause match { | ||
case exception: RuntimeException => throw exception | ||
case _ => throw new RuntimeException(cause) | ||
} | ||
} | ||
} | ||
|
||
@SerialVersionUID(1L) | ||
final class ModuleSerializationProxy(moduleClass: Class[_]) extends Serializable { | ||
@SuppressWarnings(Array("unused")) private def readResolve = ModuleSerializationProxy.instances.get(moduleClass) | ||
} |
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