Search in sources :

Example 1 with UnsafeUtil

use of primal.jdk.UnsafeUtil in project suite by stupidsing.

the class JdkUtil method load.

private <T> Class<? extends T> load(Class<T> interfaceClazz, String canonicalName, Path path) {
    Log_.info("Loading class " + canonicalName);
    var bytes = ReadFile.from(path).readBytes();
    return new UnsafeUtil().defineClass(interfaceClazz, canonicalName, bytes);
}
Also used : UnsafeUtil(primal.jdk.UnsafeUtil)

Example 2 with UnsafeUtil

use of primal.jdk.UnsafeUtil in project suite by stupidsing.

the class JdkUtilTest method compile.

private <I> Class<? extends I> compile(Class<I> clazz, String className, String source) {
    var filename = className.replace('.', '/') + ".java";
    var baos_ = new ByteArrayOutputStream();
    try (var baos = baos_) {
        var sjfo = new SimpleJavaFileObject(URI.create(filename), Kind.SOURCE) {

            public CharSequence getCharContent(boolean ignoreEncodingErrors) {
                return source;
            }

            public OutputStream openOutputStream() {
                return baos;
            }
        };
        var sfm = ToolProvider.getSystemJavaCompiler().getStandardFileManager(null, null, null);
        var jfm = new ForwardingJavaFileManager<>(sfm) {

            public JavaFileObject getJavaFileForOutput(Location loc, String clazz, Kind kind, FileObject sibling) {
                return sjfo;
            }
        };
        ToolProvider.getSystemJavaCompiler().getTask(null, jfm, null, null, null, List.of(sjfo)).call();
    } catch (IOException ex) {
        fail(ex);
    }
    return new UnsafeUtil().defineClass(clazz, filename, baos_.toByteArray());
}
Also used : SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) ForwardingJavaFileManager(javax.tools.ForwardingJavaFileManager) Kind(javax.tools.JavaFileObject.Kind) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SimpleJavaFileObject(javax.tools.SimpleJavaFileObject) FileObject(javax.tools.FileObject) JavaFileObject(javax.tools.JavaFileObject) IOException(java.io.IOException) UnsafeUtil(primal.jdk.UnsafeUtil)

Aggregations

UnsafeUtil (primal.jdk.UnsafeUtil)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 FileObject (javax.tools.FileObject)1 ForwardingJavaFileManager (javax.tools.ForwardingJavaFileManager)1 JavaFileObject (javax.tools.JavaFileObject)1 Kind (javax.tools.JavaFileObject.Kind)1 SimpleJavaFileObject (javax.tools.SimpleJavaFileObject)1