List of usage examples for java.lang ClassLoader loadClass
public Class<?> loadClass(String name) throws ClassNotFoundException
From source file:com.hurence.logisland.classloading.PluginLoader.java
/** * Load a plugin by autoproxying between current caller classloader and plugin own classloader. * * @param className the name of plugin class to load * @param <U> the return type. * @return an instance of the requested plugin * @throws ClassNotFoundException/*w w w. j a v a 2 s . co m*/ * @throws InstantiationException * @throws IllegalAccessException */ public static <U> U loadPlugin(String className) throws ClassNotFoundException, InstantiationException, IllegalAccessException { ClassLoader cl = registry.get(className); if (cl == null) { throw new ClassNotFoundException( "Unable to find component with class " + className + ". Please check your classpath"); } ClassLoader thiz = Thread.currentThread().getContextClassLoader(); Object o; try { Class<?> cls = cl.loadClass(className); Thread.currentThread().setContextClassLoader(cl); o = cls.newInstance(); } finally { Thread.currentThread().setContextClassLoader(thiz); } return (U) PluginProxy.create(o); }
From source file:SocketFetcher.java
/** * Return a socket factory of the specified class. *///from w w w . j av a2 s . c o m private static SocketFactory getSocketFactory(String sfClass) throws ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException { if (sfClass == null || sfClass.length() == 0) return null; // dynamically load the class ClassLoader cl = getContextClassLoader(); Class clsSockFact = null; if (cl != null) { try { clsSockFact = cl.loadClass(sfClass); } catch (ClassNotFoundException cex) { } } if (clsSockFact == null) clsSockFact = Class.forName(sfClass); // get & invoke the getDefault() method Method mthGetDefault = clsSockFact.getMethod("getDefault", new Class[] {}); SocketFactory sf = (SocketFactory) mthGetDefault.invoke(new Object(), new Object[] {}); return sf; }
From source file:core.sample.pool.WorkerThread.java
/** * getClass//from w w w .j a va 2s .co m * @param cls * @return Class * @throws ClassNotFoundException */ private static Class getClass(String cls) throws ClassNotFoundException { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = WorkerThread.class.getClassLoader(); } return classLoader.loadClass(cls); }
From source file:com.acrutiapps.browser.ui.components.CustomWebView.java
private static void loadMethods() { try {//from www.j a va 2 s .com // 15 is ICS 2nd release. if (android.os.Build.VERSION.SDK_INT > 15) { // WebSettings became abstract in JB, and "setProperty" moved to the concrete class, WebSettingsClassic, // not present in the SDK. So we must look for the class first, then for the methods. ClassLoader classLoader = CustomWebView.class.getClassLoader(); Class<?> webSettingsClassicClass = classLoader.loadClass("android.webkit.WebSettingsClassic"); sWebSettingsSetProperty = webSettingsClassicClass.getMethod("setProperty", new Class[] { String.class, String.class }); } else { sWebSettingsSetProperty = WebSettings.class.getMethod("setProperty", new Class[] { String.class, String.class }); } } catch (NoSuchMethodException e) { Log.e("CustomWebView", "loadMethods(): " + e.getMessage()); sWebSettingsSetProperty = null; } catch (ClassNotFoundException e) { Log.e("CustomWebView", "loadMethods(): " + e.getMessage()); sWebSettingsSetProperty = null; } sMethodsLoaded = true; }
From source file:com.google.gdt.eclipse.designer.util.GwtInvocationEvaluatorInterceptor.java
/** * @return the instance of <code>TextColumn</code> for displaying static text; *//*from w ww .j a va 2 s .co m*/ public static Object createTextColumn(ClassLoader classLoader, final String columnText) throws ClassNotFoundException { Class<?> classTextColumn = classLoader.loadClass("com.google.gwt.user.cellview.client.TextColumn"); // prepare Enhancer Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(classTextColumn); enhancer.setCallback(new MethodInterceptor() { public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { if (ReflectionUtils.getMethodSignature(method).equals("getValue(java.lang.Object)")) { return columnText; } return proxy.invokeSuper(obj, args); } }); // create instance return enhancer.create(); }
From source file:com.wavemaker.tools.project.LauncherHelper.java
/** * Invoke a method. It's expected that the ClassLoader cl will be the same as the context classloader, or else the * Spring init will likely fail (or otherwise be bad). *///from w w w .j a v a 2s .c o m public static Object invoke(ClassLoader cl, String fqClass, String methodName, Class<?>[] argTypes, Object[] args, boolean isStatic) throws ClassNotFoundException, SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { if (ResourceManager.getInstance() == null) { SpringUtils.initSpringConfig(); } Class<?> klass = cl.loadClass(fqClass); Method m = klass.getMethod(methodName, argTypes); Object o; if (isStatic) { o = null; } else { o = klass.newInstance(); } return m.invoke(o, args); }
From source file:Main.java
/** * Convert a given String into the appropriate Class. * /*w w w. ja va 2 s . com*/ * @param name * Name of class * @param cl * ClassLoader to use * * @return The class for the given name * * @throws ClassNotFoundException * When the class could not be found by the specified ClassLoader */ private final static Class convertToJavaClass(String name, ClassLoader cl) throws ClassNotFoundException { int arraySize = 0; while (name.endsWith("[]")) { name = name.substring(0, name.length() - 2); arraySize++; } // Check for a primitive type Class c = (Class) PRIMITIVE_NAME_TYPE_MAP.get(name); if (c == null) { // No primitive, try to load it from the given ClassLoader try { c = cl.loadClass(name); } catch (ClassNotFoundException cnfe) { throw new ClassNotFoundException("Parameter class not found: " + name); } } // if we have an array get the array class if (arraySize > 0) { int[] dims = new int[arraySize]; for (int i = 0; i < arraySize; i++) { dims[i] = 1; } c = Array.newInstance(c, dims).getClass(); } return c; }
From source file:edu.stanford.epad.common.plugins.impl.ClassFinderTestUtils.java
public static void dynamicClassLoader() { try {//from ww w. j ava 2 s . c o m ClassLoader myClassLoader = ClassLoader.getSystemClassLoader(); // Step 2: Define a class to be loaded. String classNameToBeLoaded = "edu.stanford.epad.plugins.first.FirstHandler"; // Step 3: Load the class Class<?> myClass = myClassLoader.loadClass(classNameToBeLoaded); if (myClass != null) { logger.info("dynamicClassLoader found FirstHandler."); // Step 4: create a new instance of that class @SuppressWarnings("unused") Object whatInstance = myClass.newInstance(); } else { logger.info("FirstHandler was not found"); } } catch (Exception e) { logger.warning(e.getMessage(), e); } catch (IncompatibleClassChangeError err) { logger.warning(err.getMessage(), err); } }
From source file:illab.nabal.util.Util.java
/** * Get how many APIs given network delegate has. * //from w w w .j a v a 2 s.c om * @param networkDelegate * @return how many APIs given network delegate has */ public static int getHowManyApis(Delegate networkDelegate) { // method names to be exculded when counting String[] excludedMethodNames = { // inherited from JDK "equals", "getClass", "hashCode", "notify", "notifyAll", "toString", "wait" // inherited from Delegate , "isSessionValid", "fetchSession", "purgeSession", "hasNoJobs", "getBitmapFromUrl" }; // determine which socail network this delegate class represents Delegate delegate = networkDelegate; Class<? extends Delegate> thisClass = null; if (networkDelegate instanceof FacebookDelegate) { thisClass = ((FacebookDelegate) delegate).getClass(); } else if (networkDelegate instanceof TwitterDelegate) { thisClass = ((TwitterDelegate) delegate).getClass(); } else if (networkDelegate instanceof WeiboDelegate) { thisClass = ((WeiboDelegate) delegate).getClass(); } // get max of method index int maxMethodIndex = 0; String thisClassName = thisClass.getName(); ClassLoader classLoader = thisClass.getClassLoader(); try { for (Method method : classLoader.loadClass(thisClassName).getMethods()) { if (Arrays.asList(excludedMethodNames).contains(method.getName()) == false) { //Log.v(TAG, "indexing... " + method.getName() + " : added"); maxMethodIndex++; } else { //Log.v(TAG, "indexing... " + method.getName() + " : pass"); } } } catch (ClassNotFoundException e) { // if error occurrs let's set 100 to prevent the shortage of the index maxMethodIndex = 100; } return maxMethodIndex; }
From source file:azkaban.common.utils.Utils.java
/** * Load the class/*from ww w . j ava2 s. com*/ * * @param className The name of the class to load * @return The loaded class */ public static Class<?> loadClass(String className, ClassLoader loader) { try { return loader.loadClass(className); } catch (ClassNotFoundException e) { throw new IllegalStateException(e); } }