Skip to content

Commit 53d3772

Browse files
committed
JVM: Don't use deprecated Class#newInstance()
1 parent c43142c commit 53d3772

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

jvm/src/main/scala/org/portablescala/reflect/InstantiatableClass.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.portablescala.reflect
22

3+
import java.lang.reflect.InvocationTargetException
4+
35
/** A wrapper for a class that can be instantiated.
46
*
57
* @param runtimeClass
@@ -18,9 +20,13 @@ final class InstantiatableClass private[reflect] (val runtimeClass: Class[_]) {
1820
*/
1921
def newInstance(): Any = {
2022
try {
21-
runtimeClass.newInstance()
23+
runtimeClass.getDeclaredConstructor().newInstance()
2224
} catch {
23-
case e: IllegalAccessException =>
25+
case e: InvocationTargetException if e.getCause != null =>
26+
throw e.getCause
27+
case e: NoSuchMethodException =>
28+
throw new InstantiationException(runtimeClass.getName).initCause(e)
29+
case _: IllegalAccessException =>
2430
/* The constructor exists but is private; make it look like it does not
2531
* exist at all.
2632
*/

0 commit comments

Comments
 (0)