1 package org.opendaylight.controller.sal.binding.impl.util;
5 import java.util.concurrent.Callable;
7 import static com.google.common.base.Preconditions.*;
9 import java.lang.reflect.Constructor;
10 import java.lang.reflect.InvocationTargetException;
11 import java.util.ArrayList;
13 import org.opendaylight.yangtools.yang.binding.Identifier;
15 public class ClassLoaderUtils {
17 public static <V> V withClassLoader(ClassLoader cls,Callable<V> function) throws Exception {
19 checkNotNull(function);
20 ClassLoader oldCls = Thread.currentThread().getContextClassLoader();
22 Thread.currentThread().setContextClassLoader(cls);
23 V result = function.call();
24 Thread.currentThread().setContextClassLoader(oldCls);
26 } catch (Exception e) {
27 Thread.currentThread().setContextClassLoader(oldCls);
28 throw new Exception(e);
32 public static Object construct(Constructor<? extends Object> constructor, ArrayList<Object> objects) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
33 Object[] initargs = objects.toArray(new Object[]{});
34 return constructor.newInstance(initargs);