2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.yangtools.sal.binding.generator.util;
10 import static com.google.common.base.Preconditions.checkNotNull;
12 import java.lang.reflect.Constructor;
13 import java.lang.reflect.InvocationTargetException;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.concurrent.Callable;
17 import java.util.concurrent.locks.Lock;
19 import com.google.common.base.Joiner;
20 import com.google.common.base.Optional;
22 public final class ClassLoaderUtils {
24 private ClassLoaderUtils() {
25 throw new UnsupportedOperationException("Utility class");
28 public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
29 checkNotNull(cls, "Classloader should not be null");
30 checkNotNull(function, "Function should not be null");
32 final ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
34 Thread.currentThread().setContextClassLoader(cls);
35 return function.call();
37 Thread.currentThread().setContextClassLoader(oldCls);
41 public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Lock lock, final Callable<V> function) throws Exception {
42 checkNotNull(lock, "Lock should not be null");
46 return withClassLoader(cls, function);
53 * @deprecated Use one of the other utility methods.
56 public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Optional<Lock> lock, final Callable<V> function) throws Exception {
57 if (lock.isPresent()) {
58 return withClassLoaderAndLock(cls, lock.get(), function);
60 return withClassLoader(cls, function);
64 public static Object construct(final Constructor<? extends Object> constructor, final List<Object> objects)
65 throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
66 Object[] initargs = objects.toArray(new Object[] {});
67 return constructor.newInstance(initargs);
71 public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
72 if ("byte[]".equals(name)) {
74 } else if("char[]".equals(name)) {
78 return cls.loadClass(name);
79 } catch (ClassNotFoundException e) {
80 String[] components = name.split("\\.");
81 String potentialOuter;
82 int length = components.length;
83 if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
85 String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
86 String innerName = outerName + "$" + components[length-1];
87 return cls.loadClass(innerName);
94 public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
95 return loadClass(Thread.currentThread().getContextClassLoader(), name);
98 public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
100 return loadClassWithTCCL(fullyQualifiedName);
101 } catch (ClassNotFoundException e) {