Skip to content

Commit 83d5f6a

Browse files
Merge pull request eugenp#3589 from raksha-rao/BAEL-1540-classloader
Bael 1540 classloader
2 parents 2aa56c3 + 12a3110 commit 83d5f6a

4 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.classloader;
2+
3+
import java.io.*;
4+
5+
public class CustomClassLoader extends ClassLoader {
6+
7+
8+
public Class getClass(String name) throws ClassNotFoundException {
9+
byte[] b = loadClassFromFile(name);
10+
return defineClass(name, b, 0, b.length);
11+
}
12+
13+
private byte[] loadClassFromFile(String fileName) {
14+
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(
15+
fileName.replace('.', File.separatorChar) + ".class");
16+
byte[] buffer;
17+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
18+
int nextValue = 0;
19+
try {
20+
while ( (nextValue = inputStream.read()) != -1 ) {
21+
byteStream.write(nextValue);
22+
}
23+
} catch (IOException e) {
24+
e.printStackTrace();
25+
}
26+
buffer = byteStream.toByteArray();
27+
return buffer;
28+
}
29+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.classloader;
2+
3+
import com.sun.javafx.util.Logging;
4+
5+
import java.io.ByteArrayOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.net.MalformedURLException;
9+
import java.net.URL;
10+
import java.net.URLConnection;
11+
import java.util.ArrayList;
12+
13+
public class PrintClassLoader {
14+
15+
public void printClassLoaders() throws ClassNotFoundException {
16+
17+
System.out.println("Classloader of this class:" + PrintClassLoader.class.getClassLoader());
18+
System.out.println("Classloader of Logging:" + Logging.class.getClassLoader());
19+
System.out.println("Classloader of ArrayList:" + ArrayList.class.getClassLoader());
20+
21+
}
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.classloader;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.InvocationTargetException;
6+
import java.lang.reflect.Method;
7+
8+
public class CustomClassLoaderTest {
9+
10+
@Test
11+
public void customLoader() throws ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
12+
13+
CustomClassLoader customClassLoader = new CustomClassLoader();
14+
Class<?> c = customClassLoader.getClass(PrintClassLoader.class.getName());
15+
16+
Object ob = c.newInstance();
17+
18+
Method md = c.getMethod("printClassLoaders");
19+
md.invoke(ob);
20+
21+
}
22+
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.baeldung.classloader;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class PrintClassLoaderTest {
8+
@Test(expected = ClassNotFoundException.class)
9+
public void givenAppClassLoader_whenParentClassLoader_thenClassNotFoundException() throws Exception {
10+
PrintClassLoader sampleClassLoader = (PrintClassLoader) Class.forName(PrintClassLoader.class.getName()).newInstance();
11+
sampleClassLoader.printClassLoaders();
12+
Class.forName(PrintClassLoader.class.getName(), true, PrintClassLoader.class.getClassLoader().getParent());
13+
}
14+
}

0 commit comments

Comments
 (0)