You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mina.apache.org by lg...@apache.org on 2020/12/11 08:51:31 UTC
[mina-sshd] 06/15: [SSHD-1110] Replace Class#newInstance() calls
with Class#getDefaultConstructor().newInstance()
This is an automated email from the ASF dual-hosted git repository.
lgoldstein pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mina-sshd.git
commit 3b1b2712c1193cbd17f4c291b9e597855e7a2075
Author: Lyor Goldstein <lg...@apache.org>
AuthorDate: Thu Dec 10 13:02:36 2020 +0200
[SSHD-1110] Replace Class#newInstance() calls with Class#getDefaultConstructor().newInstance()
---
CHANGES.md | 1 +
.../main/java/org/apache/sshd/cli/client/ScpCommandMain.java | 3 ++-
.../java/org/apache/sshd/cli/client/SftpCommandMain.java | 3 ++-
.../java/org/apache/sshd/cli/client/SshClientCliSupport.java | 3 ++-
.../java/org/apache/sshd/cli/server/SshServerCliSupport.java | 6 +++---
.../java/org/apache/sshd/common/util/ReflectionUtils.java | 12 ++++++++++++
.../org/apache/sshd/common/util/threads/ThreadUtils.java | 11 ++++++-----
.../java/org/apache/sshd/common/cipher/ARCFOUR128Test.java | 1 +
.../java/org/apache/sshd/common/cipher/ARCFOUR256Test.java | 1 +
.../src/main/java/org/apache/sshd/agent/unix/AprLibrary.java | 2 +-
.../sshd/common/io/BuiltinIoServiceFactoryFactories.java | 3 ++-
.../sshd/common/io/DefaultIoServiceFactoryFactory.java | 9 ++++-----
.../test/java/org/apache/sshd/DefaultSetupTestSupport.java | 3 +++
.../test/java/org/apache/sshd/common/cipher/CipherTest.java | 4 +++-
.../apache/sshd/common/signature/OpenSSHCertificateTest.java | 2 ++
.../apache/sshd/common/signature/SignatureFactoriesTest.java | 1 +
16 files changed, 46 insertions(+), 19 deletions(-)
diff --git a/CHANGES.md b/CHANGES.md
index 0da7379..229099b 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -19,6 +19,7 @@
* [SSHD-1085](https://issues.apache.org/jira/browse/SSHD-1085) Added `CliLogger` + more verbosity on `SshClientMain`
* [SSHD-1109](https://issues.apache.org/jira/browse/SSHD-1109) Route tests JUL logging via SLF4JBridgeHandler
* [SSHD-1109](https://issues.apache.org/jira/browse/SSHD-1109) Provide full slf4j logger capabilities to CliLogger + use it in all CLI classes
+* [SSHD-1110](https://issues.apache.org/jira/browse/SSHD-1110) Replace `Class#newInstance()` calls with `Class#getDefaultConstructor().newInstance()`
## Behavioral changes and enhancements
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
index 8b14d4b..662f00b 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/ScpCommandMain.java
@@ -45,6 +45,7 @@ import org.apache.sshd.client.session.ClientSession;
import org.apache.sshd.common.SshConstants;
import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.io.NoCloseInputStream;
import org.apache.sshd.common.util.threads.ThreadUtils;
import org.apache.sshd.scp.client.ScpClient;
@@ -196,7 +197,7 @@ public class ScpCommandMain extends SshClientCliSupport {
try {
ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(ScpClientCreator.class);
Class<?> clazz = cl.loadClass(className);
- return ScpClientCreator.class.cast(clazz.newInstance());
+ return ReflectionUtils.newInstance(clazz, ScpClientCreator.class);
} catch (Exception e) {
stderr.append("WARNING: Failed (").append(e.getClass().getSimpleName()).append(')')
.append(" to instantiate ").append(className)
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
index eb560dd..c827fde 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SftpCommandMain.java
@@ -60,6 +60,7 @@ import org.apache.sshd.common.session.Session;
import org.apache.sshd.common.signature.SignatureFactory;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.apache.sshd.common.util.io.IoUtils;
@@ -280,7 +281,7 @@ public class SftpCommandMain extends SshClientCliSupport implements Channel {
if (GenericUtils.isNotEmpty(factoryName)) {
try {
Class<?> clazz = cl.loadClass(factoryName);
- return SftpClientFactory.class.cast(clazz.newInstance());
+ return ReflectionUtils.newInstance(clazz, SftpClientFactory.class);
} catch (Throwable t) {
System.err.append("Failed (").append(t.getClass().getSimpleName()).append(')')
.append(" to instantiate ").append(factoryName)
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
index 9fbd052..36df4a4 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/client/SshClientCliSupport.java
@@ -79,6 +79,7 @@ import org.apache.sshd.common.keyprovider.FileKeyPairProvider;
import org.apache.sshd.common.mac.Mac;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.OsUtils;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.io.NoCloseOutputStream;
import org.apache.sshd.common.util.net.SshdSocketAddress;
@@ -579,7 +580,7 @@ public abstract class SshClientCliSupport extends CliSupport {
ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(KexExtensionHandler.class);
try {
Class<?> clazz = cl.loadClass(kexExtension);
- KexExtensionHandler handler = KexExtensionHandler.class.cast(clazz.newInstance());
+ KexExtensionHandler handler = ReflectionUtils.newInstance(clazz, KexExtensionHandler.class);
manager.setKexExtensionHandler(handler);
} catch (Exception e) {
stderr.append("ERROR: Failed (").append(e.getClass().getSimpleName()).append(')')
diff --git a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
index 0c00435..d5ec09c 100644
--- a/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
+++ b/sshd-cli/src/main/java/org/apache/sshd/cli/server/SshServerCliSupport.java
@@ -50,6 +50,7 @@ import org.apache.sshd.common.config.keys.KeyUtils;
import org.apache.sshd.common.keyprovider.KeyPairProvider;
import org.apache.sshd.common.keyprovider.MappedKeyPairProvider;
import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.ValidateUtils;
import org.apache.sshd.common.util.io.resource.PathResource;
import org.apache.sshd.common.util.security.SecurityUtils;
@@ -182,7 +183,7 @@ public abstract class SshServerCliSupport extends CliSupport {
for (String fqcn : classes) {
try {
Class<?> clazz = cl.loadClass(fqcn);
- SubsystemFactory factory = SubsystemFactory.class.cast(clazz.newInstance());
+ SubsystemFactory factory = ReflectionUtils.newInstance(clazz, SubsystemFactory.class);
factory = registerSubsystemFactoryListeners(
server, level, stdout, stderr, options, factory);
subsystems.add(factory);
@@ -302,8 +303,7 @@ public abstract class SshServerCliSupport extends CliSupport {
ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(ShellFactory.class);
try {
Class<?> clazz = cl.loadClass(factory);
- Object instance = clazz.newInstance();
- ShellFactory shellFactory = ShellFactory.class.cast(instance);
+ ShellFactory shellFactory = ReflectionUtils.newInstance(clazz, ShellFactory.class);
return useScp ? createScpCommandFactory(level, stdout, stderr, shellFactory) : shellFactory;
} catch (Exception e) {
stderr.append("ERROR: Failed (").append(e.getClass().getSimpleName()).append(')')
diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/ReflectionUtils.java b/sshd-common/src/main/java/org/apache/sshd/common/util/ReflectionUtils.java
index 84a84f9..ffce665 100644
--- a/sshd-common/src/main/java/org/apache/sshd/common/util/ReflectionUtils.java
+++ b/sshd-common/src/main/java/org/apache/sshd/common/util/ReflectionUtils.java
@@ -19,6 +19,7 @@
package org.apache.sshd.common.util;
+import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.function.Function;
@@ -50,4 +51,15 @@ public final class ReflectionUtils {
return false;
}
}
+
+ public static Object newInstance(Class<?> clazz) throws ReflectiveOperationException {
+ return newInstance(clazz, Object.class);
+ }
+
+ @SuppressWarnings("checkstyle:ThrowsCount")
+ public static <T> T newInstance(Class<?> clazz, Class<? extends T> castType) throws ReflectiveOperationException {
+ Constructor<?> ctor = clazz.getDeclaredConstructor();
+ Object instance = ctor.newInstance();
+ return castType.cast(instance);
+ }
}
diff --git a/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java b/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
index 9879b74..8c20928 100644
--- a/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
+++ b/sshd-common/src/main/java/org/apache/sshd/common/util/threads/ThreadUtils.java
@@ -28,6 +28,8 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
+import org.apache.sshd.common.util.ReflectionUtils;
+
/**
* Utility class for thread pools.
*
@@ -75,21 +77,20 @@ public final class ThreadUtils {
}
public static <T> T createDefaultInstance(
- Class<?> anchor, Class<T> targetType, String className)
+ Class<?> anchor, Class<? extends T> targetType, String className)
throws ReflectiveOperationException {
return createDefaultInstance(resolveDefaultClassLoaders(anchor), targetType, className);
}
public static <T> T createDefaultInstance(
- ClassLoader cl, Class<T> targetType, String className)
+ ClassLoader cl, Class<? extends T> targetType, String className)
throws ReflectiveOperationException {
Class<?> instanceType = cl.loadClass(className);
- Object instance = instanceType.newInstance();
- return targetType.cast(instance);
+ return ReflectionUtils.newInstance(instanceType, targetType);
}
public static <T> T createDefaultInstance(
- Iterable<? extends ClassLoader> cls, Class<T> targetType, String className)
+ Iterable<? extends ClassLoader> cls, Class<? extends T> targetType, String className)
throws ReflectiveOperationException {
for (ClassLoader cl : cls) {
try {
diff --git a/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR128Test.java b/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR128Test.java
index 1c37449..9f1fef3 100644
--- a/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR128Test.java
+++ b/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR128Test.java
@@ -33,6 +33,7 @@ public class ARCFOUR128Test extends BaseCipherTest {
}
@Test
+ @SuppressWarnings("deprecation")
public void testEncryptDecrypt() throws Exception {
testEncryptDecrypt(BuiltinCiphers.arcfour128);
}
diff --git a/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR256Test.java b/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR256Test.java
index e9dddf8..682b340 100644
--- a/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR256Test.java
+++ b/sshd-common/src/test/java/org/apache/sshd/common/cipher/ARCFOUR256Test.java
@@ -33,6 +33,7 @@ public class ARCFOUR256Test extends BaseCipherTest {
}
@Test
+ @SuppressWarnings("deprecation")
public void testEncryptDecrypt() throws Exception {
// for RC4 256 bits we need the JCE unlimited strength policy
ensureCipherInformationKeySizeSupported(BuiltinCiphers.arcfour256);
diff --git a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AprLibrary.java b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AprLibrary.java
index 67f0286..43921f4 100644
--- a/sshd-core/src/main/java/org/apache/sshd/agent/unix/AprLibrary.java
+++ b/sshd-core/src/main/java/org/apache/sshd/agent/unix/AprLibrary.java
@@ -97,7 +97,7 @@ public final class AprLibrary {
}
@Override
- @SuppressWarnings("checkstyle:NoFinalizer")
+ @SuppressWarnings({ "checkstyle:NoFinalizer", "deprecation" })
protected void finalize() throws Throwable {
library = null;
Pool.destroy(pool);
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
index 8880f64..e64baf2 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/BuiltinIoServiceFactoryFactories.java
@@ -27,6 +27,7 @@ import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.OptionalFeature;
import org.apache.sshd.common.io.nio2.Nio2ServiceFactoryFactory;
+import org.apache.sshd.common.util.ReflectionUtils;
/**
* @author <a href="mailto:dev@mina.apache.org">Apache MINA SSHD Project</a>
@@ -86,7 +87,7 @@ public enum BuiltinIoServiceFactoryFactories implements NamedFactory<IoServiceFa
public final IoServiceFactoryFactory create() {
Class<? extends IoServiceFactoryFactory> clazz = getFactoryClass();
try {
- return clazz.newInstance();
+ return ReflectionUtils.newInstance(clazz, IoServiceFactoryFactory.class);
} catch (Throwable e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
diff --git a/sshd-core/src/main/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactory.java b/sshd-core/src/main/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactory.java
index 0062579..3695e6d 100644
--- a/sshd-core/src/main/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactory.java
+++ b/sshd-core/src/main/java/org/apache/sshd/common/io/DefaultIoServiceFactoryFactory.java
@@ -26,6 +26,7 @@ import java.util.ServiceLoader;
import org.apache.sshd.common.Factory;
import org.apache.sshd.common.FactoryManager;
import org.apache.sshd.common.util.GenericUtils;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.threads.CloseableExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -143,7 +144,7 @@ public class DefaultIoServiceFactoryFactory extends AbstractIoServiceFactoryFact
return services.removeFirst();
}
- public static <T extends IoServiceFactoryFactory> T newInstance(Class<T> clazz, String factory) {
+ public static <T extends IoServiceFactoryFactory> T newInstance(Class<? extends T> clazz, String factory) {
BuiltinIoServiceFactoryFactories builtin = BuiltinIoServiceFactoryFactories.fromFactoryName(factory);
if (builtin != null) {
IoServiceFactoryFactory builtinInstance = builtin.create();
@@ -155,8 +156,7 @@ public class DefaultIoServiceFactoryFactory extends AbstractIoServiceFactoryFact
if (cl != null) {
try {
Class<?> loaded = cl.loadClass(factory);
- Object factoryInstance = loaded.newInstance();
- return clazz.cast(factoryInstance);
+ return ReflectionUtils.newInstance(loaded, clazz);
} catch (Throwable t) {
LOGGER.trace("Exception while loading factory " + factory, t);
}
@@ -166,8 +166,7 @@ public class DefaultIoServiceFactoryFactory extends AbstractIoServiceFactoryFact
if (cl != clDefault) {
try {
Class<?> loaded = clDefault.loadClass(factory);
- Object factoryInstance = loaded.newInstance();
- return clazz.cast(factoryInstance);
+ return ReflectionUtils.newInstance(loaded, clazz);
} catch (Throwable t) {
LOGGER.trace("Exception while loading factory " + factory, t);
}
diff --git a/sshd-core/src/test/java/org/apache/sshd/DefaultSetupTestSupport.java b/sshd-core/src/test/java/org/apache/sshd/DefaultSetupTestSupport.java
index 451d620..75f3b8b 100644
--- a/sshd-core/src/test/java/org/apache/sshd/DefaultSetupTestSupport.java
+++ b/sshd-core/src/test/java/org/apache/sshd/DefaultSetupTestSupport.java
@@ -64,6 +64,7 @@ public abstract class DefaultSetupTestSupport<M extends AbstractFactoryManager>
}
@Test // SSHD-1004
+ @SuppressWarnings("deprecation")
public void testNoDeprecatedCiphers() {
assertNoDeprecatedFactoryInstanceNames(Cipher.class.getSimpleName(),
EnumSet.of(BuiltinCiphers.arcfour128, BuiltinCiphers.arcfour256, BuiltinCiphers.tripledescbc,
@@ -93,6 +94,7 @@ public abstract class DefaultSetupTestSupport<M extends AbstractFactoryManager>
}
@Test // SSHD-1004
+ @SuppressWarnings("deprecation")
public void testNoDeprecatedSignatures() {
assertNoDeprecatedFactoryInstanceNames(Cipher.class.getSimpleName(),
EnumSet.of(BuiltinSignatures.dsa, BuiltinSignatures.rsa_cert, BuiltinSignatures.dsa_cert),
@@ -107,6 +109,7 @@ public abstract class DefaultSetupTestSupport<M extends AbstractFactoryManager>
}
@Test
+ @SuppressWarnings("deprecation")
public void testNoDeprecatedMacs() {
assertNoDeprecatedFactoryInstanceNames(
Mac.class.getSimpleName(), EnumSet.of(BuiltinMacs.hmacmd5, BuiltinMacs.hmacmd596, BuiltinMacs.hmacsha196),
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/cipher/CipherTest.java b/sshd-core/src/test/java/org/apache/sshd/common/cipher/CipherTest.java
index 04a3d4c..9a8eadc 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/cipher/CipherTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/cipher/CipherTest.java
@@ -33,6 +33,7 @@ import org.apache.sshd.common.NamedFactory;
import org.apache.sshd.common.NamedResource;
import org.apache.sshd.common.channel.Channel;
import org.apache.sshd.common.random.Random;
+import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.buffer.BufferUtils;
import org.apache.sshd.server.SshServer;
import org.apache.sshd.util.test.BaseTestSupport;
@@ -66,6 +67,7 @@ public class CipherTest extends BaseTestSupport {
/*
* NOTE !!! order is important since we build from it the C2S/S2C ciphers proposal
*/
+ @SuppressWarnings("deprecation")
private static final List<Object[]> PARAMETERS = Collections.unmodifiableList(
Arrays.asList(
new Object[] { BuiltinCiphers.aes128cbc, com.jcraft.jsch.jce.AES128CBC.class, NUM_LOADTEST_ROUNDS },
@@ -199,7 +201,7 @@ public class CipherTest extends BaseTestSupport {
static boolean checkCipher(String cipher) {
try {
Class<?> c = Class.forName(cipher);
- com.jcraft.jsch.Cipher jschCipher = (com.jcraft.jsch.Cipher) (c.newInstance());
+ com.jcraft.jsch.Cipher jschCipher = ReflectionUtils.newInstance(c, com.jcraft.jsch.Cipher.class);
jschCipher.init(com.jcraft.jsch.Cipher.ENCRYPT_MODE,
new byte[jschCipher.getBlockSize()],
new byte[jschCipher.getIVSize()]);
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/signature/OpenSSHCertificateTest.java b/sshd-core/src/test/java/org/apache/sshd/common/signature/OpenSSHCertificateTest.java
index 47fadda..dcafd1a 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/signature/OpenSSHCertificateTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/signature/OpenSSHCertificateTest.java
@@ -98,6 +98,7 @@ public class OpenSSHCertificateTest extends BaseTestSupport {
}
@Parameters(name = "type={2}")
+ @SuppressWarnings("deprecation")
public static List<Object[]> parameters() {
List<Object[]> list = new ArrayList<>();
@@ -154,6 +155,7 @@ public class OpenSSHCertificateTest extends BaseTestSupport {
}
@Test // invalid principal, abort
+ @SuppressWarnings("deprecation")
public void testAbortOnInvalidPrincipal() throws Exception {
CoreModuleProperties.ABORT_ON_INVALID_CERTIFICATE.set(client, true);
boolean thrown = false;
diff --git a/sshd-core/src/test/java/org/apache/sshd/common/signature/SignatureFactoriesTest.java b/sshd-core/src/test/java/org/apache/sshd/common/signature/SignatureFactoriesTest.java
index 2809bcd..df91841 100644
--- a/sshd-core/src/test/java/org/apache/sshd/common/signature/SignatureFactoriesTest.java
+++ b/sshd-core/src/test/java/org/apache/sshd/common/signature/SignatureFactoriesTest.java
@@ -89,6 +89,7 @@ public class SignatureFactoriesTest extends BaseTestSupport implements KeyTypeIn
}
@Parameters(name = "type={0}, size={2}")
+ @SuppressWarnings("deprecation")
public static List<Object[]> parameters() {
List<Object[]> list = new ArrayList<>();
addTests(list, KeyPairProvider.SSH_DSS, BuiltinSignatures.dsa, DSS_SIZES, DSSPublicKeyEntryDecoder.INSTANCE);