Custom class loader example

This commit is contained in:
Raksha Rao 2018-02-19 01:50:24 +05:30
parent f7518c0c05
commit 12a311083c
6 changed files with 88 additions and 37 deletions

View File

@ -0,0 +1,29 @@
package com.baeldung.classloader;
import java.io.*;
public class CustomClassLoader extends ClassLoader {
public Class getClass(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;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.classloader;
import com.sun.javafx.util.Logging;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class PrintClassLoader {
public void printClassLoaders() throws ClassNotFoundException {
System.out.println("Classloader of this class:" + PrintClassLoader.class.getClassLoader());
System.out.println("Classloader of Logging:" + Logging.class.getClassLoader());
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
}
}

View File

@ -1,23 +0,0 @@
package com.baeldung.classloader;
import com.sun.javafx.util.Logging;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
public class SampleClassLoader {
public void loadClass() throws ClassNotFoundException {
System.out.println("Classloader of this class:" + SampleClassLoader.class.getClassLoader());
System.out.println("Classloader of Logging:" + Logging.class.getClassLoader());
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
Class.forName("com.baeldung.classloader.SampleClassLoader", true, SampleClassLoader.class.getClassLoader().getParent());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.classloader;
import org.junit.Test;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class CustomClassLoaderTest {
@Test
public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
CustomClassLoader customClassLoader = new CustomClassLoader();
Class<?> c = customClassLoader.getClass(PrintClassLoader.class.getName());
Object ob = c.newInstance();
Method md = c.getMethod("printClassLoaders");
md.invoke(ob);
}
}

View File

@ -0,0 +1,14 @@
package com.baeldung.classloader;
import org.junit.Test;
import static org.junit.Assert.*;
public class PrintClassLoaderTest {
@Test(expected = ClassNotFoundException.class)
public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception {
PrintClassLoader sampleClassLoader = (PrintClassLoader) Class.forName(PrintClassLoader.class.getName()).newInstance();
sampleClassLoader.printClassLoaders();
Class.forName(PrintClassLoader.class.getName(), true, PrintClassLoader.class.getClassLoader().getParent());
}
}

View File

@ -1,14 +0,0 @@
package com.baeldung.classloader;
import org.junit.Test;
import static org.junit.Assert.*;
public class SampleClassLoaderTest {
@Test(expected = ClassNotFoundException.class)
public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception {
SampleClassLoader sampleClassLoader = (SampleClassLoader) Class.forName(SampleClassLoader.class.getName()).newInstance();
sampleClassLoader.loadClass();
}
}