Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
Fixed class loader arch.
Browse files Browse the repository at this point in the history
  • Loading branch information
ParkJeongHwan committed Sep 23, 2019
1 parent 11545f2 commit 464f223
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -185,19 +185,10 @@ public static Class<?>[] getAllDependenciesClass(Class<?> clazz) throws IOExcept

recursiveSearch(clazz, classDynamicLoader, resultClasses);

Class<?>[] resultClassArray = resultClasses.toArray(new Class[resultClasses.size()]);

for (Class<?> resultClass : resultClassArray) {
recursiveSort(resultClass, resultClass, resultClasses);
}

for (Class<?> dependencyClass : resultClasses) {
ClassFile[] classFiles = ClassFileReader.getClassBytes(dependencyClass);
for (ClassFile classFile : classFiles) {
try {
classDynamicLoader.loadClassByBytes(classFile.getBytes(), classFile.getName());
} catch (Exception e) {
}
classDynamicLoader.addClassFile(classFile);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,50 @@
package com.realtimetech.reflection.classloader;

import java.util.HashMap;

import com.realtimetech.reflection.classfile.classfile.ClassFile;

public class ClassDynamicLoader extends ClassLoader {
public ClassDynamicLoader() {
super(null);
}
private HashMap<String, byte[]> classMap;

public ClassDynamicLoader(Class<?> baseClass) {
super(baseClass.getClassLoader());
public ClassDynamicLoader() {
this(null);
}

public ClassDynamicLoader(ClassLoader classLoader) {
super(classLoader);
this.classMap = new HashMap<String, byte[]>();
}

public void addClass(String className, byte[] bytes) {
this.classMap.put(className, bytes);
}

public void addClassFile(ClassFile className) {
this.classMap.put(className.getName(), className.getBytes());
}

@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> result = null;
try {
result = super.findClass(name);
} catch (ClassNotFoundException e) {
if (this.classMap.containsKey(name)) {
byte[] bytes = this.classMap.get(name);
this.classMap.remove(name);
result = defineClass(name, bytes, 0, bytes.length);
}
}

if (result == null) {
throw new ClassNotFoundException();
}

return result;
}

public Class<?> loadClassByBytes(byte[] bytes, String className) {
public Class<?> loadClassByBytes(String className, byte[] bytes) {
return defineClass(className, bytes, 0, bytes.length);
}
}

0 comments on commit 464f223

Please sign in to comment.