From 5b00e7326b884eeb1f9a8845f76230f08b61cd62 Mon Sep 17 00:00:00 2001 From: xuwei-k <6b656e6a69@gmail.com> Date: Wed, 15 Mar 2017 11:10:15 +0900 Subject: [PATCH] s/newInstance/getDeclaredConstructor().newInstance() java.lang.Class#newInstance deprecated since Java 9 http://download.java.net/java/jdk9/docs/api/java/lang/Class.html#newInstance-- ``` Deprecated. This method propagates any exception thrown by the nullary constructor, including a checked exception. Use of this method effectively bypasses the compile-time exception checking that would otherwise be performed by the compiler. The Constructor.newInstance method avoids this problem by wrapping any exception thrown by the constructor in a (checked) InvocationTargetException. The call clazz.newInstance() can be replaced by clazz.getDeclaredConstructor().newInstance() The latter sequence of calls is inferred to be able to throw the additional exception types InvocationTargetException and NoSuchMethodException. Both of these exception types are subclasses of ReflectiveOperationException. Creates a new instance of the class represented by this Class object. The class is instantiated as if by a new expression with an empty argument list. The class is initialized if it has not already been initialized. ``` --- main/src/main/scala/sbt/TemplateCommand.scala | 2 +- project/Scripted.scala | 2 +- testing/agent/src/main/java/sbt/ForkMain.java | 2 +- testing/src/main/scala/sbt/TestFramework.scala | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/main/src/main/scala/sbt/TemplateCommand.scala b/main/src/main/scala/sbt/TemplateCommand.scala index 8e461e0d3..2447779d5 100644 --- a/main/src/main/scala/sbt/TemplateCommand.scala +++ b/main/src/main/scala/sbt/TemplateCommand.scala @@ -69,7 +69,7 @@ private[sbt] object TemplateCommandUtil { private def call(interfaceClassName: String, methodName: String, loader: ClassLoader)(argTypes: Class[_]*)(args: AnyRef*): AnyRef = { val interfaceClass = getInterfaceClass(interfaceClassName, loader) - val interface = interfaceClass.newInstance.asInstanceOf[AnyRef] + val interface = interfaceClass.getDeclaredConstructor().newInstance().asInstanceOf[AnyRef] val method = interfaceClass.getMethod(methodName, argTypes: _*) try { method.invoke(interface, args: _*) } catch { diff --git a/project/Scripted.scala b/project/Scripted.scala index 20d8ad7b4..80b1ac8c1 100644 --- a/project/Scripted.scala +++ b/project/Scripted.scala @@ -93,7 +93,7 @@ object Scripted { val noJLine = new classpath.FilteredLoader(scriptedSbtInstance.loader, "jline." :: Nil) val loader = classpath.ClasspathUtilities.toLoader(scriptedSbtClasspath.files, noJLine) val bridgeClass = Class.forName("sbt.test.ScriptedRunner", true, loader) - val bridge = bridgeClass.newInstance.asInstanceOf[SbtScriptedRunner] + val bridge = bridgeClass.getDeclaredConstructor().newInstance().asInstanceOf[SbtScriptedRunner] try { // Using java.util.List to encode File => Unit. val callback = new java.util.AbstractList[File] { diff --git a/testing/agent/src/main/java/sbt/ForkMain.java b/testing/agent/src/main/java/sbt/ForkMain.java index b7871ecde..7ce45cf45 100644 --- a/testing/agent/src/main/java/sbt/ForkMain.java +++ b/testing/agent/src/main/java/sbt/ForkMain.java @@ -227,7 +227,7 @@ final public class ForkMain { Framework framework = null; for (final String implClassName : implClassNames) { try { - final Object rawFramework = Class.forName(implClassName).newInstance(); + final Object rawFramework = Class.forName(implClassName).getDeclaredConstructor().newInstance(); if (rawFramework instanceof Framework) framework = (Framework) rawFramework; else diff --git a/testing/src/main/scala/sbt/TestFramework.scala b/testing/src/main/scala/sbt/TestFramework.scala index b3c462330..9199f4188 100644 --- a/testing/src/main/scala/sbt/TestFramework.scala +++ b/testing/src/main/scala/sbt/TestFramework.scala @@ -31,7 +31,7 @@ case class TestFramework(implClassNames: String*) { frameworkClassNames match { case head :: tail => try { - Some(Class.forName(head, true, loader).newInstance match { + Some(Class.forName(head, true, loader).getDeclaredConstructor().newInstance() match { case newFramework: Framework => newFramework case oldFramework: OldFramework => new FrameworkWrapper(oldFramework) })