Review - II Kevin Gilmore

This commit is contained in:
root 2020-07-25 18:23:39 +00:00
parent 74ebfa24ef
commit c1a9c42194
2 changed files with 0 additions and 43 deletions

View File

@ -1,11 +0,0 @@
package com.baeldung.loadedclasslisting.customLoader;
import java.util.ArrayList;
public class ClassLoaderInfo {
public void printClassLoaders() throws ClassNotFoundException {
System.out.println("Classloader of this class:" + ClassLoaderInfo.class.getClassLoader());
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
}
}

View File

@ -1,32 +0,0 @@
package com.baeldung.loadedclasslisting.customLoader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
public class CustomClassLoader extends ClassLoader {
@Override
public Class<?> findClass(String name) throws ClassNotFoundException {
byte[] b = loadClassFromFile(name);
return defineClass(name, b, 0, b.length);
}
private byte[] loadClassFromFile(String fileName) {
InputStream inputStream = getClass().getClassLoader()
.getResourceAsStream(fileName.replace('.', File.separatorChar) + ".class");
byte[] buffer;
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
int nextValue = 0;
try {
while ((nextValue = inputStream.read()) != -1) {
byteStream.write(nextValue);
}
} catch (IOException e) {
e.printStackTrace();
}
buffer = byteStream.toByteArray();
return buffer;
}
}