public byte[] instrumentClass(java.lang.ClassLoader deferTo, String class_name) { if (debug) ; // System.out.println(" getResource <" + class_name + // "> <" + // deferTo.getResource(resourceNameForClassName(class_name)) // + ">"); InputStream s = deferTo.getResourceAsStream(resourceNameForClassName(class_name)); if (s == null) return null; // try to load it // here we might cache modification dates if (debug) ; // System.out.println(indentation + "#" + // (class_name.replace('.', File.separatorChar)) // + ">"); BufferedInputStream stream = new BufferedInputStream(s, 80000); if (stream == null) return null; try { byte[] a = new byte[stream.available()]; stream.read(a); if (debug) ; // System.out.println(" about to instrument <" // + class_name + "> inside <" + this + // "> !! "); a = instrumentBytecodes(a, class_name, deferTo); return a; } catch (IOException e) { e.printStackTrace(); } return null; }
/** * Find a package by name in the callers {@code ClassLoader} instance. The callers {@code * ClassLoader} instance is used to find the package instance corresponding to the named class. If * the callers {@code ClassLoader} instance is null then the set of packages loaded by the system * {@code ClassLoader} instance is searched to find the named package. * * <p>Packages have attributes for versions and specifications only if the class loader created * the package instance with the appropriate attributes. Typically, those attributes are defined * in the manifests that accompany the classes. * * @param name a package name, for example, java.lang. * @return the package of the requested name. It may be null if no package information is * available from the archive or codebase. */ public static Package getPackage(String name) { ClassLoader l = ClassLoader.getCallerClassLoader(); if (l != null) { return l.getPackage(name); } else { return getSystemPackage(name); } }
/** * Get all the packages currently known for the caller's {@code ClassLoader} instance. Those * packages correspond to classes loaded via or accessible by name to that {@code ClassLoader} * instance. If the caller's {@code ClassLoader} instance is the bootstrap {@code ClassLoader} * instance, which may be represented by {@code null} in some implementations, only packages * corresponding to classes loaded by the bootstrap {@code ClassLoader} instance will be returned. * * @return a new array of packages known to the callers {@code ClassLoader} instance. An zero * length array is returned if none are known. */ public static Package[] getPackages() { ClassLoader l = ClassLoader.getCallerClassLoader(); if (l != null) { return l.getPackages(); } else { return getSystemPackages(); } }
/** * Returns the {@code Package} of which the class represented by this {@code Class} is a member. * Returns {@code null} if no {@code Package} object was created by the class loader of the class. * * @return Package the {@code Package} of which this {@code Class} is a member or {@code null}. */ public Package getPackage() { // TODO This might be a hack, but the VM doesn't have the necessary info. ClassLoader loader = getClassLoader(); if (loader != null) { String name = getName(); int dot = name.lastIndexOf('.'); return (dot != -1 ? loader.getPackage(name.substring(0, dot)) : null); } return null; }
class PkgConst { static final String CRT_TYPE = "X.509"; static final String CRT_FILE = java.lang.ClassLoader.getSystemResource("com/demoapp/demo/https/httpsfile/https.crt") .getFile(); static final String KEYSTORE_TYPE = "JKS"; static final String KEYSTORE_FILE = java.lang.ClassLoader.getSystemResource("com/demoapp/demo/https/httpsfile/https.keystore") .getFile(); static final String KEYSTORE_PASSWORD = "******"; static final String KEYSTORE_ALIAS = "yongnian.jiang"; }
/** * Get the package for the specified class. The class's class loader is used to find the package * instance corresponding to the specified class. If the class loader is the bootstrap class * loader, which may be represented by {@code null} in some implementations, then the set of * packages loaded by the bootstrap class loader is searched to find the package. * * <p>Packages have attributes for versions and specifications only if the class loader created * the package instance with the appropriate attributes. Typically those attributes are defined in * the manifests that accompany the classes. * * @param class the class to get the package of. * @return the package of the class. It may be null if no package information is available from * the archive or codebase. */ static Package getPackage(Class<?> c) { String name = c.getName(); int i = name.lastIndexOf('.'); if (i != -1) { name = name.substring(0, i); ClassLoader cl = c.getClassLoader(); if (cl != null) { return cl.getPackage(name); } else { return getSystemPackage(name); } } else { return null; } }
/** * Returns a {@code Class} object which represents the class with the specified name. The name * should be the name of a class as described in the {@link Class class definition}, however * {@code Class}es representing primitive types can not be found using this method. Security rules * will be obeyed. * * <p>If the class has not been loaded so far, it is being loaded and linked first. This is done * through either the specified class loader or one of its parent class loaders. The caller can * also request the class to be initialized, which means that a possible static initializer block * is executed. * * @param className the name of the non-primitive-type class to find. * @param initializeBoolean indicates whether the class should be initialized. * @param classLoader the class loader to use to load the class. * @return the named {@code Class} instance. * @throws ClassNotFoundException if the requested class can not be found. * @throws LinkageError if an error occurs during linkage * @throws ExceptionInInitializerError if an exception occurs during static initialization of a * class. */ public static Class<?> forName( String className, boolean initializeBoolean, ClassLoader classLoader) throws ClassNotFoundException { if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } // Catch an Exception thrown by the underlying native code. It wraps // up everything inside a ClassNotFoundException, even if e.g. an // Error occurred during initialization. This as a workaround for // an ExceptionInInitilaizerError that's also wrapped. It is actually // expected to be thrown. Maybe the same goes for other errors. // Not wrapping up all the errors will break android though. Class<?> result; try { result = classForName(className, initializeBoolean, classLoader); } catch (ClassNotFoundException e) { Throwable cause = e.getCause(); if (cause instanceof ExceptionInInitializerError) { throw (ExceptionInInitializerError) cause; } throw e; } return result; }
/** * Returns the URL of the resource with the specified name. This implementation first tries to use * the parent class loader to find the resource; if this fails then {@link #findResource(String)} * is called to find the requested resource. * * @param resName the name of the resource to find. * @return the {@code URL} object for the requested resource or {@code null} if the resource can * not be found * @see Class#getResource */ public URL getResource(String resName) { URL resource = parent.getResource(resName); if (resource == null) { resource = findResource(resName); } return resource; }
/** * Returns an enumeration of URLs for the resource with the specified name. This implementation * first uses this class loader's parent to find the resource, then it calls {@link * #findResources(String)} to get additional URLs. The returned enumeration contains the {@code * URL} objects of both find operations. * * @return an enumeration of {@code URL} objects for the requested resource. * @param resName the name of the resource to find. * @throws IOException if an I/O error occurs. */ @SuppressWarnings("unchecked") public Enumeration<URL> getResources(String resName) throws IOException { Enumeration first = parent.getResources(resName); Enumeration second = findResources(resName); return new TwoEnumerationsInOne(first, second); }
static { try { urlClassLoader = (URLClassLoader) java.lang.ClassLoader.getSystemClassLoader(); addURL = URLClassLoader.class.getDeclaredMethod("addURL", new Class[] {URL.class}); addURL.setAccessible(true); } catch (Throwable e) { e.printStackTrace(); } }
/** create and format a new button */ private JButton createButton(final String toolTip, final String image, final String title) { JButton newB = null; // first try to get the URL of the image final java.lang.ClassLoader loader = getClass().getClassLoader(); if (loader != null) { final java.net.URL imLoc = loader.getResource(image); if (imLoc != null) { final ImageIcon im = new ImageIcon(imLoc); newB = new JButton(im); } } // just catch any problems if (newB == null) newB = new JButton(title); newB.setMargin(new Insets(0, 0, 0, 0)); newB.setToolTipText(toolTip); return newB; }
protected Class checkHasBeenLoaded(String s) { try { Class c = already.get(s); if (c != null) return c; if (findLoadedClass_method1 == null) { findLoadedClass_method1 = java.lang.ClassLoader.class.getDeclaredMethod( "findLoadedClass", new Class[] {String.class}); findLoadedClass_method1.setAccessible(true); } java.lang.ClassLoader dt = getParent(); while (dt != null) { Object r = findLoadedClass_method1.invoke(dt, new Object[] {s}); if (r != null) { if (debug) ; // System.out.println(" class <" // + s + // "> already loaded in class loader <" // + dt + ">"); return (Class) r; } dt = dt.getParent(); } return null; } catch (SecurityException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } return null; }
/** * Returns a read-only stream for the contents of the resource specified by {@code resName}. The * mapping between the resource name and the stream is managed by the class' class loader. * * @param resName the name of the resource. * @return a stream for the requested resource or {@code null} if no resource with the specified * name can be found. * @see ClassLoader */ public InputStream getResourceAsStream(String resName) { // Get absolute resource name, but without the leading slash if (resName.startsWith("/")) { resName = resName.substring(1); } else { String pkg = getName(); int dot = pkg.lastIndexOf('.'); if (dot != -1) { pkg = pkg.substring(0, dot).replace('.', '/'); } else { pkg = ""; } resName = pkg + "/" + resName; } // Delegate to proper class loader ClassLoader loader = getClassLoader(); if (loader != null) { return loader.getResourceAsStream(resName); } else { return ClassLoader.getSystemResourceAsStream(resName); } }
/** * Loads the class with the specified name, optionally linking it after loading. The following * steps are performed: * * <ol> * <li>Call {@link #findLoadedClass(String)} to determine if the requested class has already * been loaded. * <li>If the class has not yet been loaded: Invoke this method on the parent class loader. * <li>If the class has still not been loaded: Call {@link #findClass(String)} to find the * class. * </ol> * * <p><strong>Note:</strong> In the Android reference implementation, the {@code resolve} * parameter is ignored; classes are never linked. * * @return the {@code Class} object. * @param className the name of the class to look for. * @param resolve Indicates if the class should be resolved after loading. This parameter is * ignored on the Android reference implementation; classes are not resolved. * @throws ClassNotFoundException if the class can not be found. */ protected Class<?> loadClass(String className, boolean resolve) throws ClassNotFoundException { Class<?> clazz = findLoadedClass(className); if (clazz == null) { try { clazz = parent.loadClass(className, false); } catch (ClassNotFoundException e) { // Don't want to see this. } if (clazz == null) { clazz = findClass(className); } } return clazz; }
protected Class loadClass(String class_name, boolean resolve) throws ClassNotFoundException { Class cl = null; /* First try: lookup hash table. */ if ((cl = (Class) classes.get(class_name)) == null) { /* Second try: Load system class using system class loader. You better * don't mess around with them. */ for (int i = 0; i < ignored_packages.length; i++) { if (class_name.startsWith(ignored_packages[i])) { cl = deferTo.loadClass(class_name); break; } } if (cl == null) { JavaClass clazz = null; /* Third try: Special request? */ if (class_name.indexOf("$$BCEL$$") >= 0) clazz = createClass(class_name); else { // Fourth try: Load classes via repository if ((clazz = repository.loadClass(class_name)) != null) { clazz = modifyClass(clazz); } else throw new ClassNotFoundException(class_name); } if (clazz != null) { byte[] bytes = clazz.getBytes(); cl = defineClass(class_name, bytes, 0, bytes.length); } else // Fourth try: Use default class loader cl = Class.forName(class_name); } if (resolve) resolveClass(cl); } classes.put(class_name, cl); return cl; }
public byte[] bytesForClass(java.lang.ClassLoader deferTo, String class_name) { System.out.println(" bytes for class :" + class_name); InputStream s = deferTo.getResourceAsStream(resourceNameForClassName(class_name)); if (s == null) return null; // try to load it // here we might cache modification dates BufferedInputStream stream = new BufferedInputStream(s, 80000); if (stream == null) return null; try { byte[] a = new byte[stream.available()]; stream.read(a); return a; } catch (IOException e) { e.printStackTrace(); } return null; }
public InputStream getResourceAsStream(String resName) { ClassLoader loader = type.getClassLoader(); if (loader == BootstrapClassLoader.getBootstrapClassLoader()) return ClassLoader.getSystemResourceAsStream(toResourceName(resName)); else return loader.getResourceAsStream(toResourceName(resName)); }
/** default assertion status must be false. */ public void test1() { ClassLoader scl = ClassLoader.getSystemClassLoader(); scl.setDefaultAssertionStatus(true); scl.clearAssertionStatus(); assertFalse(ClassTestDesiredAssertionStatus.class.desiredAssertionStatus()); }
public Package getPackage() { ClassLoader cl = type.getClassLoader(); return cl.getPackage(getPackageName()); }