Example usage for java.net URLStreamHandlerFactory getClass

List of usage examples for java.net URLStreamHandlerFactory getClass

Introduction

In this page you can find the example usage for java.net URLStreamHandlerFactory getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:org.allcolor.yahp.converter.CClassLoader.java

/**
 * Install a custom URLStreamHandlerFactory which handle nested jar loading,
 * and memory url. The installation is done only if necessary.
 * //www.ja va2s. c o m
 */
private static void installURLStreamHandlerFactory() {
    synchronized (URL.class) {
        try {
            try {
                new URL("yahpjarloader://test/test");
                return;
            } catch (MalformedURLException e) {
            }
            if (CClassLoader.class.getClassLoader() != ClassLoader.getSystemClassLoader()) {
                try {
                    Class c = ClassLoader.getSystemClassLoader()
                            .loadClass("org.allcolor.yahp.converter.CYaHPURLStreamHandlerFactory");
                    if (c != null) {
                        return;
                    }
                } catch (Throwable ignore) {
                }
            }
            final Field factory = URL.class.getDeclaredField("factory");
            factory.setAccessible(true);
            final URLStreamHandlerFactory oldFactory = (URLStreamHandlerFactory) factory.get(null);
            if ((oldFactory == null)
                    || (oldFactory.getClass().getName().indexOf("CYaHPURLStreamHandlerFactory") == -1)) {
                synchronized (factory) {
                    System.out.println("Installing new URLStreamHandlerFactory...");
                    final Method defineClass = ClassLoader.class.getDeclaredMethod("defineClass",
                            new Class[] { String.class, byte[].class, int.class, int.class });
                    defineClass.setAccessible(true);
                    final byte[] bauc = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CByteArrayUrlConnection.class"));
                    final byte[] crypt = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CCryptoUtils.class"));
                    final byte[] b64c = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CBASE64Codec.class"));
                    final byte[] muh = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CMemoryURLHandler.class"));
                    final byte[] juhgc = CClassLoader
                            .loadByteArray(CClassLoader.class.getClassLoader().getResource(
                                    "org/allcolor/yahp/converter/CJarLoaderURLStreamHandler$CGCCleaner.class"));
                    final byte[] juh = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CJarLoaderURLStreamHandler.class"));
                    final byte[] hf = CClassLoader.loadByteArray(CClassLoader.class.getClassLoader()
                            .getResource("org/allcolor/yahp/converter/CYaHPURLStreamHandlerFactory.class"));
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CByteArrayUrlConnection", bauc,
                                    new Integer(0), new Integer(bauc.length) });
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CCryptoUtils", crypt, new Integer(0),
                                    new Integer(crypt.length) });
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CBASE64Codec", b64c, new Integer(0),
                                    new Integer(b64c.length) });
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CMemoryURLHandler", muh, new Integer(0),
                                    new Integer(muh.length) });
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CJarLoaderURLStreamHandler$CGCCleaner",
                                    juhgc, new Integer(0), new Integer(juhgc.length) });
                    defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CJarLoaderURLStreamHandler", juh,
                                    new Integer(0), new Integer(juh.length) });
                    final Class c = (Class) defineClass.invoke(ClassLoader.getSystemClassLoader(),
                            new Object[] { "org.allcolor.yahp.converter.CYaHPURLStreamHandlerFactory", hf,
                                    new Integer(0), new Integer(hf.length) });
                    final Constructor cons = c.getConstructor(new Class[] { URLStreamHandlerFactory.class });
                    final URLStreamHandlerFactory fact = (URLStreamHandlerFactory) cons
                            .newInstance(new Object[] { oldFactory });
                    factory.set(null, fact);
                    System.out.println("Installing new URLStreamHandlerFactory DONE.");
                }
            }
        } catch (final Throwable ignore) {
            ignore.printStackTrace();
        }
    }
}