Skip to content

Commit ff87a68

Browse files
committed
[J17] Add manual class loading example (one module)
Java 8 style.
1 parent d410f06 commit ff87a68

File tree

6 files changed

+140
-1
lines changed

6 files changed

+140
-1
lines changed

.idea/runConfigurations/java_17_manual_one_module_java_8_style___run_.xml

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
plugins {
2+
id 'java-library'
3+
id 'application'
4+
}
5+
6+
group = 'com.habr.j17.manual.onemodule.j8style'
7+
version = '1.0'
8+
9+
compileJava {
10+
java {
11+
toolchain {
12+
languageVersion = JavaLanguageVersion.of(17)
13+
}
14+
}
15+
}
16+
17+
application {
18+
mainClassName = 'com.habr.j17.manual.onemodule.j8style.Main'
19+
}
20+
21+
sourceSets {
22+
main.java.srcDirs = ['src']
23+
}
24+
25+
jar {
26+
manifest {
27+
attributes(
28+
"Main-Class": application.mainClass
29+
)
30+
}
31+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.habr.j17.manual.onemodule.j8style;
2+
3+
public class Cat {
4+
public void talk() {
5+
System.out.println("Meow");
6+
}
7+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.habr.j17.manual.onemodule.j8style;
2+
3+
import java.io.ByteArrayOutputStream;
4+
import java.io.File;
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
8+
public class CustomClassLoader extends ClassLoader {
9+
@Override
10+
public Class<?> loadClass(String name) throws ClassNotFoundException {
11+
synchronized (getClassLoadingLock(name)) {
12+
Class<?> c = findLoadedClass(name);
13+
if (c != null)
14+
return c;
15+
16+
String APP_GROUP = "com.habr";
17+
if (name.startsWith(APP_GROUP)) {
18+
System.out.println("CCL: Loading " + name);
19+
c = loadClassFromFile(name);
20+
21+
if (c != null)
22+
return c;
23+
}
24+
25+
System.out.println("CCL: Delegating " + name);
26+
return super.loadClass(name);
27+
}
28+
}
29+
30+
private Class<?> loadClassFromFile(String name) {
31+
String classFile = name.replace('.', File.separatorChar) + ".class";
32+
try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream(classFile)) {
33+
if (inputStream == null)
34+
throw new RuntimeException();
35+
36+
byte[] buffer;
37+
ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
38+
39+
int nextValue;
40+
while ((nextValue = inputStream.read()) != -1) {
41+
byteStream.write(nextValue);
42+
}
43+
44+
buffer = byteStream.toByteArray();
45+
return defineClass(name, buffer, 0, buffer.length);
46+
} catch (RuntimeException | IOException e) {
47+
throw new RuntimeException("Failed to read from input stream", e);
48+
}
49+
}
50+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.habr.j17.manual.onemodule.j8style;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
6+
public class Main {
7+
public static void main(String[] args) {
8+
ClassLoader customClassLoader = new CustomClassLoader();
9+
try {
10+
Class<?> catClass = customClassLoader.loadClass("com.habr.j17.manual.onemodule.j8style.Cat");
11+
Object cat = catClass.getDeclaredConstructor().newInstance();
12+
13+
System.out.println("System ClassLoader is " + ClassLoader.getSystemClassLoader());
14+
System.out.println("Cat Class ClassLoader is " + catClass.getClassLoader());
15+
16+
System.out.println("Main Class Module is " + Main.class.getModule());
17+
System.out.println("Cat Class Module is " + catClass.getModule());
18+
19+
Method talkMethod = catClass.getMethod("talk");
20+
talkMethod.invoke(cat);
21+
} catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException
22+
| InstantiationException | IllegalAccessException e) {
23+
throw new RuntimeException(e);
24+
}
25+
}
26+
}

settings.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
rootProject.name = 'java-classloading'
22

3-
include 'java-8'
43
include 'java-8:manual'
54
include 'java-8:auto'
5+
6+
include 'java-17:manual:one-module:java8style'

0 commit comments

Comments
 (0)