Skip to content

Commit 3ab0514

Browse files
authored
Merge pull request #82 from retronym/topic/msp
Add ModuleSerializationProxy.java to avoid MissingRequirementError
2 parents 5053a1b + ba1bd8f commit 3ab0514

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Scala (https://www.scala-lang.org)
3+
*
4+
* Copyright EPFL and Lightbend, Inc.
5+
*
6+
* Licensed under Apache License 2.0
7+
* (http://www.apache.org/licenses/LICENSE-2.0).
8+
*
9+
* See the NOTICE file distributed with this work for
10+
* additional information regarding copyright ownership.
11+
*/
12+
13+
package scala.runtime;
14+
15+
import java.io.Serializable;
16+
import java.security.AccessController;
17+
import java.security.PrivilegedActionException;
18+
import java.security.PrivilegedExceptionAction;
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
/** A serialization proxy for singleton objects */
23+
public final class ModuleSerializationProxy implements Serializable {
24+
private static final long serialVersionUID = 1L;
25+
private final Class<?> moduleClass;
26+
private static final ClassValue<Object> instances = new ClassValue<Object>() {
27+
@Override
28+
protected Object computeValue(Class<?> type) {
29+
try {
30+
return AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> type.getField("MODULE$").get(null));
31+
} catch (PrivilegedActionException e) {
32+
return rethrowRuntime(e.getCause());
33+
}
34+
}
35+
};
36+
37+
private static Object rethrowRuntime(Throwable e) {
38+
Throwable cause = e.getCause();
39+
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
40+
else throw new RuntimeException(cause);
41+
}
42+
43+
public ModuleSerializationProxy(Class<?> moduleClass) {
44+
this.moduleClass = moduleClass;
45+
}
46+
47+
@SuppressWarnings("unused")
48+
private Object readResolve() {
49+
return instances.get(moduleClass);
50+
}
51+
}

0 commit comments

Comments
 (0)