ClassUtil { public static <T> Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders) { return classForName(pClassName, true, pClassLoaders); } private  ClassUtil(); static Class<T> classForName(String pClassName, ClassLoader ... pClassLoaders); static Class<T> classForName(String pClassName,boolean pInitialize,ClassLoader ... pClassLoaders); static InputStream getResourceAsStream(String pPath); static boolean checkForClass(String pClassName); static T newInstance(String pClassName, Object ... pArguments); static T newInstance(Class<T> pClass, Object ... pArguments); static Object applyMethod(Object pObject, String pMethod, Object ... pArgs); static Set<String> getResources(String pResource);  }
@Test public void classForName() { assertTrue(ClassUtil.checkForClass("java.lang.String")); assertEquals(ClassUtil.classForName(ClassUtilTest.class.getName()),ClassUtilTest.class); assertNull(ClassUtil.classForName("blablub")); }
@Test public void classForNameFoundInParent() throws ClassNotFoundException { ClassLoader oldCl = Thread.currentThread().getContextClassLoader(); ClassLoader cl = new MyCl(oldCl); Thread.currentThread().setContextClassLoader(cl); Class clazz = ClassUtil.classForName("org.jolokia.util.RequestType"); assertEquals(clazz.getName(),"org.jolokia.util.RequestType"); assertEquals(oldCl.loadClass("org.jolokia.util.RequestType"),clazz); Thread.currentThread().setContextClassLoader(oldCl); }
@Test public void classForNameWithoutContextClassLoader() { Thread current = Thread.currentThread(); ClassLoader origLoader = current.getContextClassLoader(); current.setContextClassLoader(null); try { classForName(); } finally { current.setContextClassLoader(origLoader); } }