Move ClassLoaderUtils
[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 /**
23  * @deprecated Use {@link org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils} instead.
24  */
25 @Deprecated
26 public final class ClassLoaderUtils {
27
28     private ClassLoaderUtils() {
29         throw new UnsupportedOperationException("Utility class");
30     }
31
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");
35
36         final ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
37         try {
38             Thread.currentThread().setContextClassLoader(cls);
39             return function.call();
40         } finally {
41             Thread.currentThread().setContextClassLoader(oldCls);
42         }
43     }
44
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");
47
48         lock.lock();
49         try {
50             return withClassLoader(cls, function);
51         } finally {
52             lock.unlock();
53         }
54     }
55
56     /**
57      * @deprecated Use one of the other utility methods.
58      */
59     @Deprecated
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);
63         } else {
64             return withClassLoader(cls, function);
65         }
66     }
67
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);
72     }
73
74
75     public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
76         if ("byte[]".equals(name)) {
77             return byte[].class;
78         } else if("char[]".equals(name)) {
79             return char[].class;
80         }
81         try {
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))) {
88
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);
92             } else {
93                 throw e;
94             }
95         }
96     }
97
98     public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
99         return loadClass(Thread.currentThread().getContextClassLoader(), name);
100     }
101
102     public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
103         try {
104             return loadClassWithTCCL(fullyQualifiedName);
105         } catch (ClassNotFoundException e) {
106             return null;
107         }
108     }
109 }