2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.concepts.util;
10 import static org.opendaylight.yangtools.concepts.util.ClassLoaderUtils.findParameterizedType;
12 import java.lang.reflect.ParameterizedType;
13 import java.lang.reflect.Type;
14 import java.util.concurrent.Callable;
15 import java.util.concurrent.locks.Lock;
18 public final class ClassLoaderUtils {
20 private ClassLoaderUtils() {
21 throw new UnsupportedOperationException("Utility class");
24 public static <V> V withClassLoader(ClassLoader cls, Callable<V> function) throws Exception {
25 return withClassLoaderAndLock(cls, null, function);
28 public static <V> V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable<V> function) throws Exception {
30 throw new IllegalArgumentException("Classloader should not be null");
32 if (function == null) {
33 throw new IllegalArgumentException("Function should not be null");
39 ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
41 Thread.currentThread().setContextClassLoader(cls);
42 return function.call();
44 Thread.currentThread().setContextClassLoader(oldCls);
51 public static ParameterizedType findParameterizedType(Class<?> subclass, Class<?> genericType) {
52 if(subclass == null || genericType == null) {
53 throw new IllegalArgumentException("Class was not specified.");
55 for (Type type : subclass.getGenericInterfaces()) {
56 if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) {
57 return (ParameterizedType) type;
63 public static <S,G,P> Class<P> findFirstGenericArgument(final Class<S> scannedClass, final Class<G> genericType) {
65 return withClassLoader(scannedClass.getClassLoader(), ClassLoaderUtils.<S,G,P>findFirstGenericArgumentTask(scannedClass, genericType));
66 } catch (Exception e) {
71 private static <S,G,P> Callable<Class<P>> findFirstGenericArgumentTask(final Class<S> scannedClass, final Class<G> genericType) {
72 return new Callable<Class<P>>() {
74 @SuppressWarnings("unchecked")
75 public Class<P> call() throws Exception {
76 final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass,
78 if (augmentationGeneric == null) {
81 return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
86 public static Type getFirstGenericParameter(Type type) {
87 if(type instanceof ParameterizedType) {
88 return ((ParameterizedType) type).getActualTypeArguments()[0];