Move ClassLoaderUtils
[yangtools.git] / common / concepts / src / main / java / org / opendaylight / yangtools / concepts / util / ClassLoaderUtils.java
1 /*
2  * Copyright (c) 2013 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.concepts.util;
9
10 import java.lang.reflect.ParameterizedType;
11 import java.lang.reflect.Type;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.locks.Lock;
14
15 /**
16  * @deprecated Use {@link org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils} instead.
17  */
18 @Deprecated
19 public final class ClassLoaderUtils {
20
21     private ClassLoaderUtils() {
22         throw new UnsupportedOperationException("Utility class");
23     }
24
25     public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
26         return withClassLoaderAndLock(cls, null, function);
27     }
28
29     public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Lock lock, final Callable<V> function) throws Exception {
30         if (cls == null) {
31             throw new IllegalArgumentException("Classloader should not be null");
32         }
33         if (function == null) {
34             throw new IllegalArgumentException("Function should not be null");
35         }
36
37         if (lock != null) {
38             lock.lock();
39         }
40         ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
41         try {
42             Thread.currentThread().setContextClassLoader(cls);
43             return function.call();
44         } finally {
45             Thread.currentThread().setContextClassLoader(oldCls);
46             if (lock != null) {
47                 lock.unlock();
48             }
49         }
50     }
51
52     public static ParameterizedType findParameterizedType(final Class<?> subclass, final Class<?> genericType) {
53         if(subclass == null || genericType == null) {
54             throw new IllegalArgumentException("Class was not specified.");
55         }
56         for (Type type : subclass.getGenericInterfaces()) {
57             if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) {
58                 return (ParameterizedType) type;
59             }
60         }
61         return null;
62     }
63
64     public static <S,G,P> Class<P> findFirstGenericArgument(final Class<S> scannedClass, final Class<G> genericType) {
65         try {
66             return withClassLoader(scannedClass.getClassLoader(), ClassLoaderUtils.<S,G,P>findFirstGenericArgumentTask(scannedClass, genericType));
67         } catch (Exception e) {
68             return null;
69         }
70     }
71
72     private static <S,G,P> Callable<Class<P>> findFirstGenericArgumentTask(final Class<S> scannedClass, final Class<G> genericType) {
73         return new Callable<Class<P>>() {
74             @Override
75             @SuppressWarnings("unchecked")
76             public Class<P> call() throws Exception {
77                 final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass,
78                         genericType);
79                 if (augmentationGeneric == null) {
80                     return null;
81                 }
82                 return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
83             }
84         };
85     }
86
87     public static Type getFirstGenericParameter(final Type type) {
88         if(type instanceof ParameterizedType) {
89             return ((ParameterizedType) type).getActualTypeArguments()[0];
90         }
91         return null;
92     }
93
94 }