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 java.lang.reflect.ParameterizedType;
11 import java.lang.reflect.Type;
12 import java.util.concurrent.Callable;
13 import java.util.concurrent.locks.Lock;
16 * @deprecated Use {@link org.opendaylight.yangtools.yang.binding.util.ClassLoaderUtils} instead.
19 public final class ClassLoaderUtils {
21 private ClassLoaderUtils() {
22 throw new UnsupportedOperationException("Utility class");
25 public static <V> V withClassLoader(final ClassLoader cls, final Callable<V> function) throws Exception {
26 return withClassLoaderAndLock(cls, null, function);
29 public static <V> V withClassLoaderAndLock(final ClassLoader cls, final Lock lock, final Callable<V> function) throws Exception {
31 throw new IllegalArgumentException("Classloader should not be null");
33 if (function == null) {
34 throw new IllegalArgumentException("Function should not be null");
40 ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
42 Thread.currentThread().setContextClassLoader(cls);
43 return function.call();
45 Thread.currentThread().setContextClassLoader(oldCls);
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.");
56 for (Type type : subclass.getGenericInterfaces()) {
57 if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) {
58 return (ParameterizedType) type;
64 public static <S,G,P> Class<P> findFirstGenericArgument(final Class<S> scannedClass, final Class<G> genericType) {
66 return withClassLoader(scannedClass.getClassLoader(), ClassLoaderUtils.<S,G,P>findFirstGenericArgumentTask(scannedClass, genericType));
67 } catch (Exception e) {
72 private static <S,G,P> Callable<Class<P>> findFirstGenericArgumentTask(final Class<S> scannedClass, final Class<G> genericType) {
73 return new Callable<Class<P>>() {
75 @SuppressWarnings("unchecked")
76 public Class<P> call() throws Exception {
77 final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass,
79 if (augmentationGeneric == null) {
82 return (Class<P>) augmentationGeneric.getActualTypeArguments()[0];
87 public static Type getFirstGenericParameter(final Type type) {
88 if(type instanceof ParameterizedType) {
89 return ((ParameterizedType) type).getActualTypeArguments()[0];