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;
23 * @deprecated Use {@link org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils} instead.
26 public final class ClassLoaderUtils {
28 private ClassLoaderUtils() {
29 throw new UnsupportedOperationException("Utility class");
32 public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
33 checkNotNull(cls, "Classloader should not be null");
34 checkNotNull(function, "Function should not be null");
36 final ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
38 Thread.currentThread().setContextClassLoader(cls);
39 return function.call();
41 Thread.currentThread().setContextClassLoader(oldCls);
45 public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Lock lock, final Callable<V> function) throws Exception {
46 checkNotNull(lock, "Lock should not be null");
50 return withClassLoader(cls, function);
57 * @deprecated Use one of the other utility methods.
60 public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Optional<Lock> lock, final Callable<V> function) throws Exception {
61 if (lock.isPresent()) {
62 return withClassLoaderAndLock(cls, lock.get(), function);
64 return withClassLoader(cls, function);
68 public static Object construct(final Constructor<? extends Object> constructor, final List<Object> objects)
69 throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
70 Object[] initargs = objects.toArray(new Object[] {});
71 return constructor.newInstance(initargs);
75 public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
76 if ("byte[]".equals(name)) {
78 } else if("char[]".equals(name)) {
82 return cls.loadClass(name);
83 } catch (ClassNotFoundException e) {
84 String[] components = name.split("\\.");
85 String potentialOuter;
86 int length = components.length;
87 if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
89 String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
90 String innerName = outerName + "$" + components[length-1];
91 return cls.loadClass(innerName);
98 public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
99 return loadClass(Thread.currentThread().getContextClassLoader(), name);
102 public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
104 return loadClassWithTCCL(fullyQualifiedName);
105 } catch (ClassNotFoundException e) {