org.voovan.tools.compiler.JavaMemClass Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of voovan-framework Show documentation
Show all versions of voovan-framework Show documentation
Voovan is a java framwork and it not depends on any third-party framework.
package org.voovan.tools.compiler;
import org.voovan.tools.log.Logger;
import org.voovan.tools.reflect.TReflect;
import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
/**
* java 内存 Class
* @author helyho
*
* Voovan Framework.
* WebSite: https://github.com/helyho/Voovan
* Licence: Apache v2 License
*/
public class JavaMemClass extends SimpleJavaFileObject {
private ClassLoader classLoader;
protected final ByteArrayOutputStream classByteArrayOutputStream =
new ByteArrayOutputStream();
public JavaMemClass(String name, Kind kind, ClassLoader classLoader) {
super(URI.create("string:///" + name.replace('.', '/')
+ kind.extension), kind);
this.classLoader = classLoader;
}
public byte[] getBytes() {
return classByteArrayOutputStream.toByteArray();
}
@Override
public OutputStream openOutputStream() throws IOException {
return classByteArrayOutputStream;
}
public Class loadThisClass(){
try {
byte[] classBytes = this.getBytes();
return (Class)TReflect.invokeMethod(classLoader, "defineClass", new Object[]{null, classBytes, 0, classBytes.length});
} catch (ReflectiveOperationException e) {
Logger.error(e);
}
return null;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy