Remove redundant modifiers
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ClassLoaderUtils.java
index 6f84ef81ff8a115adcec1f4aae5e14044014844d..f258cf839deea5fdb6066483924ff4edfdb6699d 100644 (file)
@@ -8,24 +8,22 @@
 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");
@@ -81,7 +79,7 @@ public final class ClassLoaderUtils {
         }
     }
 
-    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);
@@ -104,16 +102,19 @@ public final class ClassLoaderUtils {
         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;
@@ -121,6 +122,19 @@ public final class ClassLoaderUtils {
         }
     }
 
+    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);
     }
@@ -128,7 +142,7 @@ public final class ClassLoaderUtils {
     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;
         }
@@ -156,7 +170,7 @@ public final class ClassLoaderUtils {
         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;
             }