- return result;
- } catch (Exception e) {
- Thread.currentThread().setContextClassLoader(oldCls);
- throw new Exception(e);
+ if (lock.isPresent()) {
+ lock.get().unlock();
+ }
+ }
+ }
+
+ public static Object construct(Constructor<? extends Object> constructor, List<Object> objects)
+ throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
+ Object[] initargs = objects.toArray(new Object[] {});
+ return constructor.newInstance(initargs);
+ }
+
+ public static Class<?> loadClassWithTCCL(String name) throws ClassNotFoundException {
+ if ("byte[]".equals(name)) {
+ return byte[].class;
+ } else if("char[]".equals(name)) {
+ return char[].class;
+ }
+ try {
+ return Thread.currentThread().getContextClassLoader().loadClass(name);
+ } catch (ClassNotFoundException e) {
+ String[] components = name.split("\\.");
+ String potentialOuter;
+ int length = components.length;
+ if (length > 2 && (potentialOuter = components[length - 2]) != null && Character.isUpperCase(potentialOuter.charAt(0))) {
+
+ String outerName = Joiner.on(".").join(Arrays.asList(components).subList(0, length - 1));
+ String innerName = outerName + "$" + components[length-1];
+ return Thread.currentThread().getContextClassLoader().loadClass(innerName);
+ } else {
+ throw e;
+ }