Move RuntimeGeneratedMappingService from md-sal to yang-data-impl(codecs + apis)...
[yangtools.git] / code-generator / binding-generator-impl / src / main / java / org / opendaylight / yangtools / sal / binding / generator / util / ClassLoaderUtils.java
1 package org.opendaylight.yangtools.sal.binding.generator.util;
2
3 import static com.google.common.base.Preconditions.checkNotNull;
4
5 import java.lang.reflect.Constructor;
6 import java.lang.reflect.InvocationTargetException;
7 import java.util.Arrays;
8 import java.util.List;
9 import java.util.concurrent.Callable;
10 import java.util.concurrent.locks.Lock;
11
12 import com.google.common.base.Joiner;
13 import com.google.common.base.Optional;
14
15 public final class ClassLoaderUtils {
16
17     private ClassLoaderUtils() {
18         throw new UnsupportedOperationException("Utility class");
19     }
20
21     public static <V> V withClassLoader(ClassLoader cls, Callable<V> function) throws Exception {
22         return withClassLoaderAndLock(cls, Optional.<Lock> absent(), function);
23     }
24
25     public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
26         checkNotNull(lock, "Lock should not be null");
27         return withClassLoaderAndLock(cls, Optional.of(lock), function);
28     }
29
30     public static <V> V withClassLoaderAndLock(ClassLoader cls, Optional<Lock> lock, Callable<V> function)
31             throws Exception {
32         checkNotNull(cls, "Classloader should not be null");
33         checkNotNull(function, "Function should not be null");
34         if (lock.isPresent()) {
35             lock.get().lock();
36         }
37         ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
38         try {
39             Thread.currentThread().setContextClassLoader(cls);
40             return function.call();
41         } finally {
42             Thread.currentThread().setContextClassLoader(oldCls);
43             if (lock.isPresent()) {
44                 lock.get().unlock();
45             }
46         }
47     }
48
49     public static Object construct(Constructor<? extends Object> constructor, List<Object> objects)
50             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
51         Object[] initargs = objects.toArray(new Object[] {});
52         return constructor.newInstance(initargs);
53     }
54
55     public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
56         if ("byte[]".equals(name)) {
57             return byte[].class;
58         } else if("char[]".equals(name)) {
59             return char[].class;
60         }
61         try {
62             return Thread.currentThread().getContextClassLoader().loadClass(name);
63         } catch (ClassNotFoundException e) {
64             String[] components = name.split("\\.");
65             String potentialOuter;
66             int length = components.length;
67             if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
68
69                     String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
70                     String innerName = outerName + "$" + components[length-1];
71                     return Thread.currentThread().getContextClassLoader().loadClass(innerName);
72             } else {
73                 throw e;
74             }
75         }
76     }
77
78     public static Class<?> tryToLoadClassWithTCCL(String fullyQualifiedName) {
79         try {
80             return loadClassWithTCCL(fullyQualifiedName);
81         } catch (ClassNotFoundException e) {
82             return null;
83         }
84     }
85 }