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