+/*
+ * 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;
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;
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) {