com.shijingsh.core.utility.ClassUtility Maven / Gradle / Ivy
The newest version!
package com.shijingsh.core.utility;
import java.io.DataInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import com.shijingsh.core.common.compilation.MemoryClassLoader;
import org.apache.commons.lang3.ClassUtils;
/**
* 类型工具
*
* @author Birdy
*
*/
public class ClassUtility extends ClassUtils {
public static Map classes2Bytes(Class>... classes) {
HashMap bytes = new HashMap<>();
for (Class> clazz : classes) {
String name = clazz.getName();
ClassLoader container = clazz.getClassLoader();
if (container instanceof MemoryClassLoader) {
bytes.put(name, MemoryClassLoader.class.cast(container).getBytes(name));
} else {
String path = name.replace('.', '/') + ".class";
try (InputStream stream = container.getResourceAsStream(path); DataInputStream buffer = new DataInputStream(stream)) {
byte[] data = new byte[buffer.available()];
buffer.readFully(data);
bytes.put(name, data);
} catch (Exception exception) {
throw new IllegalArgumentException(exception);
}
}
}
return bytes;
}
public static Class>[] bytes2Classes(Map bytes) {
try (MemoryClassLoader classLoader = new MemoryClassLoader(bytes)) {
Class>[] classes = new Class>[bytes.size()];
int index = 0;
for (String name : bytes.keySet()) {
classes[index++] = classLoader.loadClass(name);
}
return classes;
} catch (Exception exception) {
throw new IllegalArgumentException(exception);
}
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy