Skip to content

Commit

Permalink
[SSHD-1145] Deprecate ReflectionUtils#isClassAvailable and use Thread…
Browse files Browse the repository at this point in the history
…Utils#resolveDefaultClass in SecurityProviderRegistrars (#189)

Signed-off-by: Grzegorz Grzybek <[email protected]>
  • Loading branch information
grgrzybek committed May 10, 2021
1 parent db7cbdc commit 4dad0d7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ public static Collection<Field> getMatchingDeclaredFields(Class<?> clazz, Predic
return GenericUtils.selectMatchingMembers(acceptor, clazz.getDeclaredFields());
}

/**
* @deprecated The preferred method is
* {@link org.apache.sshd.common.util.threads.ThreadUtils#resolveDefaultClass(Class, String)}
* which checks all possible ClassLoaders.
* @param cl
* @param className
* @return
*/
@Deprecated
public static boolean isClassAvailable(ClassLoader cl, String className) {
try {
cl.loadClass(className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

import org.apache.sshd.common.util.ExceptionUtils;
import org.apache.sshd.common.util.GenericUtils;
import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.common.util.threads.ThreadUtils;
Expand Down Expand Up @@ -119,8 +118,8 @@ public boolean isSupported() {
return supported.booleanValue();
}

ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(getClass());
supported = ReflectionUtils.isClassAvailable(cl, PROVIDER_CLASS);
Class<?> clazz = ThreadUtils.resolveDefaultClass(getClass(), PROVIDER_CLASS);
supported = clazz != null;
supportHolder.set(supported);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import java.util.concurrent.atomic.AtomicReference;

import org.apache.sshd.common.util.ExceptionUtils;
import org.apache.sshd.common.util.ReflectionUtils;
import org.apache.sshd.common.util.security.AbstractSecurityProviderRegistrar;
import org.apache.sshd.common.util.security.SecurityUtils;
import org.apache.sshd.common.util.threads.ThreadUtils;
Expand Down Expand Up @@ -94,8 +93,8 @@ public boolean isSupported() {
return supported.booleanValue();
}

ClassLoader cl = ThreadUtils.resolveDefaultClassLoader(getClass());
supported = ReflectionUtils.isClassAvailable(cl, "net.i2p.crypto.eddsa.EdDSAKey");
Class<?> clazz = ThreadUtils.resolveDefaultClass(getClass(), "net.i2p.crypto.eddsa.EdDSAKey");
supported = clazz != null;
supportHolder.set(supported);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,21 @@ public static Iterable<ClassLoader> resolveDefaultClassLoaders(Class<?> anchor)
return () -> iterateDefaultClassLoaders(anchor);
}

public static Class<?> resolveDefaultClass(Class<?> anchor, String className) {
return resolveDefaultClass(resolveDefaultClassLoaders(anchor), className);
}

public static Class<?> resolveDefaultClass(Iterable<? extends ClassLoader> cls, String className) {
for (ClassLoader cl : cls) {
try {
return cl.loadClass(className);
} catch (Throwable ignored) {
// Ignore
}
}
return null;
}

public static <T> T createDefaultInstance(
Class<?> anchor, Class<? extends T> targetType, String className)
throws ReflectiveOperationException {
Expand Down

0 comments on commit 4dad0d7

Please sign in to comment.