package org.opendaylight.yangtools.util;
import static com.google.common.base.Preconditions.checkNotNull;
-
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
+import com.google.common.base.Splitter;
import com.google.common.base.Supplier;
-
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
-import java.util.Arrays;
import java.util.List;
import java.util.concurrent.Callable;
-
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public final class ClassLoaderUtils {
private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
+ private static final Splitter DOT_SPLITTER = Splitter.on('.');
private ClassLoaderUtils() {
throw new UnsupportedOperationException("Utility class");
}
}
- public static Object construct(final Constructor<? extends Object> constructor, final List<Object> objects)
+ public static Object construct(final Constructor<?> constructor, final List<Object> objects)
throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
final Object[] initargs = objects.toArray();
return constructor.newInstance(initargs);
if ("char[]".equals(name)) {
return char[].class;
}
+ return loadClass0(cls,name);
+ }
+ private static Class<?> loadClass0(final ClassLoader cls, final String name) throws ClassNotFoundException {
try {
return cls.loadClass(name);
- } catch (ClassNotFoundException e) {
- String[] components = name.split("\\.");
- String potentialOuter;
- int length = components.length;
- if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
- String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
- String innerName = outerName + "$" + components[length-1];
+ } catch (final ClassNotFoundException e) {
+ final List<String> components = DOT_SPLITTER.splitToList(name);
+
+ if (isInnerClass(components)) {
+ final int length = components.size() - 1;
+ final String outerName = Joiner.on(".").join(components.subList(0, length));
+ final String innerName = outerName + "$" + components.get(length);
return cls.loadClass(innerName);
} else {
throw e;
}
}
+ private static boolean isInnerClass(final List<String> components) {
+ final int length = components.size();
+ if (length < 2) {
+ return false;
+ }
+
+ final String potentialOuter = components.get(length - 2);
+ if (potentialOuter == null) {
+ return false;
+ }
+ return Character.isUpperCase(potentialOuter.charAt(0));
+ }
+
public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
return loadClass(Thread.currentThread().getContextClassLoader(), name);
}
public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
try {
return loadClassWithTCCL(fullyQualifiedName);
- } catch (ClassNotFoundException e) {
+ } catch (final ClassNotFoundException e) {
LOG.debug("Failed to load class {}", fullyQualifiedName, e);
return null;
}
Preconditions.checkNotNull(subclass);
Preconditions.checkNotNull(genericType);
- for (Type type : subclass.getGenericInterfaces()) {
+ for (final Type type : subclass.getGenericInterfaces()) {
if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) {
return (ParameterizedType) type;
}