Search in sources :

Example 1 with ClassTransformer

use of org.lanternpowered.launch.transformer.ClassTransformer in project LanternServer by LanternPowered.

the class LanternClassLoader method defineClass.

private Class<?> defineClass(String name, URL url, boolean transform) throws ClassNotFoundException {
    try (InputStream is = url.openStream()) {
        // Get the buffer
        byte[] buffer = this.loadBuffer.get();
        int read;
        int length = 0;
        while ((read = is.read(buffer, length, buffer.length - length)) != -1) {
            length += read;
            // Expand the buffer
            if (length >= buffer.length - 1) {
                final byte[] newBuffer = new byte[buffer.length + BUFFER_SIZE];
                System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
                buffer = newBuffer;
            }
        }
        if (transform) {
            // Write the bytes to a byte array with the proper length,
            // we don't want any trailing bytes when pushing the byte
            // array through the transformers
            byte[] result = new byte[length];
            System.arraycopy(buffer, 0, result, 0, length);
            // Let's start transforming the class
            for (ClassTransformer transformer : this.transformers) {
                try {
                    result = transformer.transform(this, name, result);
                } catch (Exception e) {
                    System.err.print("An error occurred while transforming " + name + ": ");
                    e.printStackTrace();
                }
            }
            buffer = result;
            length = result.length;
        }
        return defineClass(name, buffer, 0, length, url);
    } catch (IOException e) {
        this.invalidClasses.add(name);
        throw new ClassNotFoundException(name, e);
    }
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InputStream(java.io.InputStream) ClassTransformer(org.lanternpowered.launch.transformer.ClassTransformer) IOException(java.io.IOException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SAXException(org.xml.sax.SAXException) ParseException(org.json.simple.parser.ParseException) NoSuchElementException(java.util.NoSuchElementException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Aggregations

IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MalformedURLException (java.net.MalformedURLException)1 NoSuchElementException (java.util.NoSuchElementException)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 ParseException (org.json.simple.parser.ParseException)1 ClassTransformer (org.lanternpowered.launch.transformer.ClassTransformer)1 SAXException (org.xml.sax.SAXException)1