/** * Loads class by its name. If {@link #ignoreException} is set, * no exception is thrown, but <code>null</code> is returned. */ protected Class loadClass(String className) throws ClassNotFoundException { try { return ClassLoaderUtil.loadClass(className); } catch (ClassNotFoundException cnfex) { if (ignoreException) { return null; } throw cnfex; } catch (Error error) { if (ignoreException) { return null; } throw error; } }
/** * Returns default classpath using * {@link #getDefaultClassLoader() default classloader}. */ public static File[] getDefaultClasspath() { return getDefaultClasspath(getDefaultClassLoader()); }
/** * Opens a resource of the specified name for reading. * @see #getResourceAsStream(String, ClassLoader, boolean) */ public static InputStream getResourceAsStream(String resourceName, boolean useCache) throws IOException { return getResourceAsStream(resourceName, null, useCache); }
/** * Opens a class of the specified name for reading. No specific classloader is used * for loading class. * @see #getResourceAsStream(String, ClassLoader) */ public static InputStream getClassAsStream(String className) throws IOException { return getResourceAsStream(getClassFileName(className)); }
/** * Defines a class from byte array into the system class loader. * @see #defineClass(String, byte[], ClassLoader) */ public static Class defineClass(String className, byte[] classData) { return defineClass(className, classData, getDefaultClassLoader()); }
return ClassLoaderUtil.loadClass(targetClassName); } catch (ClassNotFoundException cnfex) { throw new ProxettaException(cnfex); classLoader = ClassLoaderUtil.getDefaultClassLoader();
/** * Retrieves given resource as URL. * @see #getResourceUrl(String, ClassLoader) */ public static URL getResourceUrl(String resourceName) { return getResourceUrl(resourceName, null); }
} else if (targetClassName != null) { try { return ClassLoaderUtil.loadClass(targetClassName); } catch (ClassNotFoundException cnfex) { throw new ProxettaException(cnfex); classLoader = ClassLoaderUtil.getDefaultClassLoader(); return ClassLoaderUtil.defineClass(getProxyClassName(), bytes, classLoader); } catch (Exception ex) { throw new ProxettaException("Class definition failed.", ex);
@Test void testStream() throws IOException { InputStream is = ClassLoaderUtil.getClassAsStream(ClassLoaderUtilTest.class); assertNotNull(is); is.close(); is = ClassLoaderUtil.getClassAsStream(ClassLoaderUtilTest.class); assertNotNull(is); is.close(); URL url; final String resourceName = "jodd/util/Bits.class"; url = ClassLoaderUtil.getResourceUrl(resourceName); assertNotNull(url); is = ClassLoaderUtil.getResourceAsStream(resourceName); assertNotNull(is); is.close(); }
/** * Creates advice's class reader. */ private ClassReader createAdviceClassReader(final Class<? extends ProxyAdvice> advice) { InputStream inputStream = null; try { inputStream = ClassLoaderUtil.getClassAsStream(advice); return new ClassReader(inputStream); } catch (IOException ioex) { throw new ProxettaException(ioex); } finally { StreamUtil.close(inputStream); } }
/** * Defines a class from byte array into the specified class loader. * Warning: this is a <b>hack</b>! * @param className optional class name, may be <code>null</code> * @param classData bytecode data * @param classLoader classloader that will load class */ public static Class of(final String className, final byte[] classData, ClassLoader classLoader) { if (classLoader == null) { classLoader = ClassLoaderUtil.getDefaultClassLoader(); } try { final Method defineClassMethod = ClassLoader.class.getDeclaredMethod("defineClass", String.class, byte[].class, int.class, int.class); defineClassMethod.setAccessible(true); return (Class) defineClassMethod.invoke(classLoader, className, classData, 0, classData.length); } catch (Throwable th) { throw new RuntimeException("Define class failed: " + className, th); } }
/** * Loads a specific jar from a path * @param arg the path to the jar * can be a file, a string path or an URL * @return true if success, false if failed */ public static boolean load_jar(Object arg){ try { if ( arg instanceof String) { ClassLoaderUtil.addFileToClassPath(new File(arg.toString()), ClassLoaderUtil.getDefaultClassLoader()); return true; } if ( arg instanceof File) { ClassLoaderUtil.addFileToClassPath((File)arg, ClassLoaderUtil.getDefaultClassLoader()); return true; } if ( arg instanceof URL){ ClassLoaderUtil.addUrlToClassPath((URL) arg, ClassLoaderUtil.getDefaultClassLoader()); return true; } return false; }catch (Exception e){ System.err.println(e); } return false; }
/** * Scans {@link jodd.util.ClassLoaderUtil#getDefaultClasspath() default class path}. */ public void scanDefaultClasspath() { scan(ClassLoaderUtil.getDefaultClasspath()); }
return ClassLoaderUtil.loadClass(targetClassName); } catch (ClassNotFoundException cnfex) { throw new ProxettaException(cnfex); classLoader = ClassLoaderUtil.getDefaultClassLoader();
/** * Retrieves given resource as URL. * @see #getResourceUrl(String, ClassLoader) */ public static URL getResourceUrl(final String resourceName) { return getResourceUrl(resourceName, null); }
/** * Defines class as a target. */ public T setTarget(final Class target) { assertTargetIsNotDefined(); try { targetInputStream = ClassLoaderUtil.getClassAsStream(target); if (targetInputStream == null) { throw new ProxettaException("Target class not found: " + target.getName()); } targetClass = target; targetClassName = target.getName(); } catch (IOException ioex) { StreamUtil.close(targetInputStream); throw new ProxettaException("Unable to stream class: " + target.getName(), ioex); } return _this(); }
return getBundle(bundleName, locale, ClassLoaderUtil.getDefaultClassLoader()); } catch (MissingResourceException ignore) { return null; ResourceBundle bundle = notmisses.get(key); if (bundle == null) { bundle = getBundle(bundleName, locale, ClassLoaderUtil.getDefaultClassLoader()); notmisses.put(key, bundle);
/** * Opens a class of the specified name for reading using provided class loader. */ public static InputStream getClassAsStream(String className, ClassLoader classLoader) throws IOException { return getResourceAsStream(getClassFileName(className), classLoader); }
/** * Scans {@link jodd.util.ClassLoaderUtil#getDefaultClasspath() default class path}. */ public ClassScanner scanDefaultClasspath() { return scan(ClassLoaderUtil.getDefaultClasspath()); }
/** * Loads class by its name. If {@link #ignoreException} is set, * no exception is thrown, but <code>null</code> is returned. */ public Class loadClass() throws ClassNotFoundException { try { return ClassLoaderUtil.loadClass(name); } catch (ClassNotFoundException | Error cnfex) { if (ignoreException) { return null; } throw cnfex; } }