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
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.yangtools.sal.binding.generator.util;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
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;
18
19 import com.google.common.base.Joiner;
20 import com.google.common.base.Optional;
21
22 public final class ClassLoaderUtils {
23
24     private ClassLoaderUtils() {
25         throw new UnsupportedOperationException("Utility class");
26     }
27
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");
31
32         final ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
33         try {
34             Thread.currentThread().setContextClassLoader(cls);
35             return function.call();
36         } finally {
37             Thread.currentThread().setContextClassLoader(oldCls);
38         }
39     }
40
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");
43
44         lock.lock();
45         try {
46             return withClassLoader(cls, function);
47         } finally {
48             lock.unlock();
49         }
50     }
51
52     /**
53      * @deprecated Use one of the other utility methods.
54      */
55     @Deprecated
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);
59         } else {
60             return withClassLoader(cls, function);
61         }
62     }
63
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);
68     }
69
70
71     public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
72         if ("byte[]".equals(name)) {
73             return byte[].class;
74         } else if("char[]".equals(name)) {
75             return char[].class;
76         }
77         try {
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))) {
84
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);
88             } else {
89                 throw e;
90             }
91         }
92     }
93
94     public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
95         return loadClass(Thread.currentThread().getContextClassLoader(), name);
96     }
97
98     public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
99         try {
100             return loadClassWithTCCL(fullyQualifiedName);
101         } catch (ClassNotFoundException e) {
102             return null;
103         }
104     }
105 }