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);
}
}
Aggregations