Bug 735 - Part 1: Update ietf-restconf and ietf-yangtypes to newer versions
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / util / ClassLoaderUtils.java
index faf0eeb101a9a7e7553fa4d784e376eca4ebd03a..15895528936020a89ac8010a4a3b0840b8386c4e 100644 (file)
@@ -1,3 +1,10 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
 package org.opendaylight.yangtools.sal.binding.generator.util;
 
 import static com.google.common.base.Preconditions.checkNotNull;
@@ -18,48 +25,57 @@ public final class ClassLoaderUtils {
         throw new UnsupportedOperationException("Utility class");
     }
 
-    public static <V> V withClassLoader(ClassLoader cls, Callable<V> function) throws Exception {
-        return withClassLoaderAndLock(cls, Optional.<Lock> absent(), function);
-    }
-
-    public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
-        checkNotNull(lock, "Lock should not be null");
-        return withClassLoaderAndLock(cls, Optional.of(lock), function);
-    }
-
-    public static <V> V withClassLoaderAndLock(ClassLoader cls, Optional<Lock> lock, Callable<V> function)
-            throws Exception {
+    public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
         checkNotNull(cls, "Classloader should not be null");
         checkNotNull(function, "Function should not be null");
-        if (lock.isPresent()) {
-            lock.get().lock();
-        }
-        ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
+
+        final ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
         try {
             Thread.currentThread().setContextClassLoader(cls);
             return function.call();
         } finally {
             Thread.currentThread().setContextClassLoader(oldCls);
-            if (lock.isPresent()) {
-                lock.get().unlock();
-            }
         }
     }
 
-    public static Object construct(Constructor<? extends Object> constructor, List<Object> objects)
+    public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Lock lock, final Callable<V> function) throws Exception {
+        checkNotNull(lock, "Lock should not be null");
+
+        lock.lock();
+        try {
+            return withClassLoader(cls, function);
+        } finally {
+            lock.unlock();
+        }
+    }
+
+    /**
+     * @deprecated Use one of the other utility methods.
+     */
+    @Deprecated
+    public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Optional<Lock> lock, final Callable<V> function) throws Exception {
+        if (lock.isPresent()) {
+            return withClassLoaderAndLock(cls, lock.get(), function);
+        } else {
+            return withClassLoader(cls, function);
+        }
+    }
+
+    public static Object construct(final Constructor<? extends Object> constructor, final List<Object> objects)
             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
         Object[] initargs = objects.toArray(new Object[] {});
         return constructor.newInstance(initargs);
     }
 
-    public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
+
+    public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
         if ("byte[]".equals(name)) {
             return byte[].class;
         } else if("char[]".equals(name)) {
             return char[].class;
         }
         try {
-            return Thread.currentThread().getContextClassLoader().loadClass(name);
+            return cls.loadClass(name);
         } catch (ClassNotFoundException e) {
             String[] components = name.split("\\.");
             String potentialOuter;
@@ -68,14 +84,18 @@ public final class ClassLoaderUtils {
 
                     String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
                     String innerName = outerName + "$" + components[length-1];
-                    return Thread.currentThread().getContextClassLoader().loadClass(innerName);
+                    return cls.loadClass(innerName);
             } else {
                 throw e;
             }
         }
     }
 
-    public static Class<?> tryToLoadClassWithTCCL(String fullyQualifiedName) {
+    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) {