diff --git a/src/main/java/com/realtimetech/reflection/classfile/DependencyFinder.java b/src/main/java/com/realtimetech/reflection/classfile/DependencyFinder.java index adfe7f8..cc8eb9e 100644 --- a/src/main/java/com/realtimetech/reflection/classfile/DependencyFinder.java +++ b/src/main/java/com/realtimetech/reflection/classfile/DependencyFinder.java @@ -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); } } diff --git a/src/main/java/com/realtimetech/reflection/classloader/ClassDynamicLoader.java b/src/main/java/com/realtimetech/reflection/classloader/ClassDynamicLoader.java index 385b430..da8b45c 100644 --- a/src/main/java/com/realtimetech/reflection/classloader/ClassDynamicLoader.java +++ b/src/main/java/com/realtimetech/reflection/classloader/ClassDynamicLoader.java @@ -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 classMap; - public ClassDynamicLoader(Class baseClass) { - super(baseClass.getClassLoader()); + public ClassDynamicLoader() { + this(null); } public ClassDynamicLoader(ClassLoader classLoader) { super(classLoader); + this.classMap = new HashMap(); + } + + 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); } }