Merge "Update .gitignore files"
[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(ClassLoader cls, Callable<V> function) throws Exception {
29         return withClassLoaderAndLock(cls, Optional.<Lock> absent(), function);
30     }
31
32     public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
33         checkNotNull(lock, "Lock should not be null");
34         return withClassLoaderAndLock(cls, Optional.of(lock), function);
35     }
36
37     public static <V> V withClassLoaderAndLock(ClassLoader cls, Optional<Lock> lock, Callable<V> function)
38             throws Exception {
39         checkNotNull(cls, "Classloader should not be null");
40         checkNotNull(function, "Function should not be null");
41         if (lock.isPresent()) {
42             lock.get().lock();
43         }
44         ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
45         try {
46             Thread.currentThread().setContextClassLoader(cls);
47             return function.call();
48         } finally {
49             Thread.currentThread().setContextClassLoader(oldCls);
50             if (lock.isPresent()) {
51                 lock.get().unlock();
52             }
53         }
54     }
55
56     public static Object construct(Constructor<? extends Object> constructor, List<Object> objects)
57             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
58         Object[] initargs = objects.toArray(new Object[] {});
59         return constructor.newInstance(initargs);
60     }
61
62     public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
63         if ("byte[]".equals(name)) {
64             return byte[].class;
65         } else if("char[]".equals(name)) {
66             return char[].class;
67         }
68         try {
69             return Thread.currentThread().getContextClassLoader().loadClass(name);
70         } catch (ClassNotFoundException e) {
71             String[] components = name.split("\\.");
72             String potentialOuter;
73             int length = components.length;
74             if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
75
76                     String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
77                     String innerName = outerName + "$" + components[length-1];
78                     return Thread.currentThread().getContextClassLoader().loadClass(innerName);
79             } else {
80                 throw e;
81             }
82         }
83     }
84
85     public static Class<?> tryToLoadClassWithTCCL(String fullyQualifiedName) {
86         try {
87             return loadClassWithTCCL(fullyQualifiedName);
88         } catch (ClassNotFoundException e) {
89             return null;
90         }
91     }
92 }