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.
```
This commit is contained in:
xuwei-k 2017-03-15 11:10:15 +09:00
parent c6b593df95
commit 5b00e7326b
4 changed files with 4 additions and 4 deletions

View File

@ -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 {

View File

@ -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] {

View File

@ -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

View File

@ -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)
})