b1adba6e479b4bc4b58bc6db58264665019f180f
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / 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.util;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11 import com.google.common.base.Joiner;
12 import com.google.common.base.Preconditions;
13 import com.google.common.base.Splitter;
14 import com.google.common.base.Supplier;
15 import java.lang.reflect.Constructor;
16 import java.lang.reflect.InvocationTargetException;
17 import java.lang.reflect.ParameterizedType;
18 import java.lang.reflect.Type;
19 import java.util.List;
20 import java.util.concurrent.Callable;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 public final class ClassLoaderUtils {
25     private static final Logger LOG = LoggerFactory.getLogger(ClassLoaderUtils.class);
26     private static final Splitter DOT_SPLITTER = Splitter.on('.');
27
28     private ClassLoaderUtils() {
29         throw new UnsupportedOperationException("Utility class");
30     }
31
32     /**
33      * Runs {@link Supplier} with provided {@link ClassLoader}.
34      *
35      * Invokes supplies function and makes sure that original {@link ClassLoader}
36      * is context {@link ClassLoader} after execution.
37      *
38      * @param cls {@link ClassLoader} to be used.
39      * @param function Function to be executed.
40      * @return Result of supplier invocation.
41      */
42     public static <V> V withClassLoader(final ClassLoader cls, final Supplier<V> function) {
43         checkNotNull(cls, "Classloader should not be null");
44         checkNotNull(function, "Function should not be null");
45
46         final Thread currentThread = Thread.currentThread();
47         final ClassLoader oldCls = currentThread.getContextClassLoader();
48         try {
49             currentThread.setContextClassLoader(cls);
50             return function.get();
51         } finally {
52             currentThread.setContextClassLoader(oldCls);
53         }
54     }
55
56     /**
57      * Runs {@link Callable} with provided {@link ClassLoader}.
58      *
59      * Invokes supplies function and makes sure that original {@link ClassLoader}
60      * is context {@link ClassLoader} after execution.
61      *
62      * @param cls {@link ClassLoader} to be used.
63      * @param function Function to be executed.
64      * @return Result of callable invocation.
65      */
66     public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
67         checkNotNull(cls, "Classloader should not be null");
68         checkNotNull(function, "Function should not be null");
69
70         final Thread currentThread = Thread.currentThread();
71         final ClassLoader oldCls = currentThread.getContextClassLoader();
72         try {
73             currentThread.setContextClassLoader(cls);
74             return function.call();
75         } finally {
76             currentThread.setContextClassLoader(oldCls);
77         }
78     }
79
80     public static Object construct(final Constructor<?> constructor, final List<Object> objects)
81             throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
82         final Object[] initargs = objects.toArray();
83         return constructor.newInstance(initargs);
84     }
85
86     /**
87      *
88      * Loads class using this supplied classloader.
89      *
90      *
91      * @param cls
92      * @param name String name of class.
93      * @return
94      * @throws ClassNotFoundException
95      */
96     public static Class<?> loadClass(final ClassLoader cls, final String name) throws ClassNotFoundException {
97         if ("byte[]".equals(name)) {
98             return byte[].class;
99         }
100         if ("char[]".equals(name)) {
101             return char[].class;
102         }
103         return loadClass0(cls,name);
104     }
105
106     private static Class<?> loadClass0(final ClassLoader cls, final String name) throws ClassNotFoundException {
107         try {
108             return cls.loadClass(name);
109         } catch (final ClassNotFoundException e) {
110             final List<String> components = DOT_SPLITTER.splitToList(name);
111
112             if (isInnerClass(components)) {
113                 final int length = components.size() - 1;
114                 final String outerName = Joiner.on(".").join(components.subList(0, length));
115                 final String innerName = outerName + "$" + components.get(length);
116                 return cls.loadClass(innerName);
117             } else {
118                 throw e;
119             }
120         }
121     }
122
123     private static boolean isInnerClass(final List<String> components) {
124         final int length = components.size();
125         if (length < 2) {
126             return false;
127         }
128
129         final String potentialOuter = components.get(length - 2);
130         if (potentialOuter == null) {
131             return false;
132         }
133         return Character.isUpperCase(potentialOuter.charAt(0));
134     }
135
136     public static Class<?> loadClassWithTCCL(final String name) throws ClassNotFoundException {
137         return loadClass(Thread.currentThread().getContextClassLoader(), name);
138     }
139
140     public static Class<?> tryToLoadClassWithTCCL(final String fullyQualifiedName) {
141         try {
142             return loadClassWithTCCL(fullyQualifiedName);
143         } catch (final ClassNotFoundException e) {
144             LOG.debug("Failed to load class {}", fullyQualifiedName, e);
145             return null;
146         }
147     }
148
149     public static <S,G,P> Class<P> findFirstGenericArgument(final Class<S> scannedClass, final Class<G> genericType) {
150         return withClassLoader(scannedClass.getClassLoader(), ClassLoaderUtils.findFirstGenericArgumentTask(scannedClass, genericType));
151     }
152
153     private static <S,G,P> Supplier<Class<P>> findFirstGenericArgumentTask(final Class<S> scannedClass, final Class<G> genericType) {
154         return new Supplier<Class<P>>() {
155             @Override
156             @SuppressWarnings("unchecked")
157             public Class<P> get() {
158                 final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass, genericType);
159                 if (augmentationGeneric != null) {
160                     return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
161                 }
162                 return null;
163             }
164         };
165     }
166
167     public static ParameterizedType findParameterizedType(final Class<?> subclass, final Class<?> genericType) {
168         Preconditions.checkNotNull(subclass);
169         Preconditions.checkNotNull(genericType);
170
171         for (final Type type : subclass.getGenericInterfaces()) {
172             if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) {
173                 return (ParameterizedType) type;
174             }
175         }
176
177         LOG.debug("Class {} does not declare interface {}", subclass, genericType);
178         return null;
179     }
180
181     public static Type getFirstGenericParameter(final Type type) {
182         if (type instanceof ParameterizedType) {
183             return ((ParameterizedType) type).getActualTypeArguments()[0];
184         }
185         return null;
186     }
187 }