/* * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.yangtools.concepts.util; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.util.concurrent.Callable; import java.util.concurrent.locks.Lock; public final class ClassLoaderUtils { private ClassLoaderUtils() { throw new UnsupportedOperationException("Utility class"); } public static V withClassLoader(ClassLoader cls, Callable function) throws Exception { return withClassLoaderAndLock(cls, null, function); } public static V withClassLoaderAndLock(ClassLoader cls, Lock lock, Callable function) throws Exception { if (cls == null) { throw new IllegalArgumentException("Classloader should not be null"); } if (function == null) { throw new IllegalArgumentException("Function should not be null"); } if (lock != null) { lock.lock(); } ClassLoader oldCls = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(cls); return function.call(); } finally { Thread.currentThread().setContextClassLoader(oldCls); if (lock != null) { lock.unlock(); } } } public static ParameterizedType findParameterizedType(Class subclass, Class genericType) { if(subclass == null || genericType == null) { throw new IllegalArgumentException("Class was not specified."); } for (Type type : subclass.getGenericInterfaces()) { if (type instanceof ParameterizedType && genericType.equals(((ParameterizedType) type).getRawType())) { return (ParameterizedType) type; } } return null; } public static Class

findFirstGenericArgument(final Class scannedClass, final Class genericType) { try { return withClassLoader(scannedClass.getClassLoader(), ClassLoaderUtils.findFirstGenericArgumentTask(scannedClass, genericType)); } catch (Exception e) { return null; } } private static Callable> findFirstGenericArgumentTask(final Class scannedClass, final Class genericType) { return new Callable>() { @Override @SuppressWarnings("unchecked") public Class

call() throws Exception { final ParameterizedType augmentationGeneric = findParameterizedType(scannedClass, genericType); if (augmentationGeneric == null) { return null; } return (Class

) augmentationGeneric.getActualTypeArguments()[0]; } }; } public static Type getFirstGenericParameter(Type type) { if(type instanceof ParameterizedType) { return ((ParameterizedType) type).getActualTypeArguments()[0]; } return null; } }