.class工具类测试
parent
e4fcf5fadd
commit
4dbe72f50c
|
@ -36,7 +36,7 @@
|
||||||
// // 创建PutObject请求。
|
// // 创建PutObject请求。
|
||||||
// PutObjectResult result = ossClient.putObject(putObjectRequest);
|
// PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||||
// } catch (OSSException oe) {
|
// } catch (OSSException oe) {
|
||||||
// System.out.println("Caught an OSSException, which means your request made it to OSS, "
|
// System.out.pri ntln("Caught an OSSException, which means your request made it to OSS, "
|
||||||
// + "but was rejected with an error response for some reason.");
|
// + "but was rejected with an error response for some reason.");
|
||||||
// System.out.println("Error Message:" + oe.getErrorMessage());
|
// System.out.println("Error Message:" + oe.getErrorMessage());
|
||||||
// System.out.println("Error Code:" + oe.getErrorCode());
|
// System.out.println("Error Code:" + oe.getErrorCode());
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
package com.itranswarp.compiler;
|
package com.muyu.javacomplier;
|
||||||
|
|
||||||
|
import com.muyu.dynamicLoad.DynamicLoader;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
@ -12,7 +14,7 @@ import javax.tools.JavaCompiler.CompilationTask;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In-memory compile Java source code as String.
|
* In-memory compile Java source code as String.
|
||||||
*
|
*
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
public class JavaStringCompiler {
|
public class JavaStringCompiler {
|
||||||
|
@ -27,7 +29,7 @@ public class JavaStringCompiler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Compile a Java source file in memory.
|
* Compile a Java source file in memory.
|
||||||
*
|
*
|
||||||
* @param fileName
|
* @param fileName
|
||||||
* Java file name, e.g. "Test.java"
|
* Java file name, e.g. "Test.java"
|
||||||
* @param source
|
* @param source
|
||||||
|
@ -38,7 +40,7 @@ public class JavaStringCompiler {
|
||||||
* If compile error.
|
* If compile error.
|
||||||
*/
|
*/
|
||||||
public Map<String, byte[]> compile(String fileName, String source) throws IOException {
|
public Map<String, byte[]> compile(String fileName, String source) throws IOException {
|
||||||
try (MemoryJavaFileManager manager = new MemoryJavaFileManager(stdManager)) {
|
try (com.itranswarp.compiler.MemoryJavaFileManager manager = new com.itranswarp.compiler.MemoryJavaFileManager(stdManager)) {
|
||||||
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
JavaFileObject javaFileObject = manager.makeStringSource(fileName, source);
|
||||||
CompilationTask task = compiler.getTask(null, manager, null, null, null, Arrays.asList(javaFileObject));
|
CompilationTask task = compiler.getTask(null, manager, null, null, null, Arrays.asList(javaFileObject));
|
||||||
Boolean result = task.call();
|
Boolean result = task.call();
|
||||||
|
@ -51,7 +53,7 @@ public class JavaStringCompiler {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Load class from compiled classes.
|
* Load class from compiled classes.
|
||||||
*
|
*
|
||||||
* @param name
|
* @param name
|
||||||
* Full class name.
|
* Full class name.
|
||||||
* @param classBytes
|
* @param classBytes
|
||||||
|
@ -63,7 +65,7 @@ public class JavaStringCompiler {
|
||||||
* If load error.
|
* If load error.
|
||||||
*/
|
*/
|
||||||
public Class<?> loadClass(String name, Map<String, byte[]> classBytes) throws ClassNotFoundException, IOException {
|
public Class<?> loadClass(String name, Map<String, byte[]> classBytes) throws ClassNotFoundException, IOException {
|
||||||
try (MemoryClassLoader classLoader = new MemoryClassLoader(classBytes)) {
|
try (DynamicLoader.MemoryClassLoader classLoader = new DynamicLoader.MemoryClassLoader(classBytes)) {
|
||||||
return classLoader.loadClass(name);
|
return classLoader.loadClass(name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,15 +18,15 @@ import javax.tools.SimpleJavaFileObject;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* In-memory java file manager.
|
* In-memory java file manager.
|
||||||
*
|
*
|
||||||
* @author michael
|
* @author michael
|
||||||
*/
|
*/
|
||||||
class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
public class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
||||||
|
|
||||||
// compiled classes in bytes:
|
// compiled classes in bytes:
|
||||||
final Map<String, byte[]> classBytes = new HashMap<String, byte[]>();
|
final Map<String, byte[]> classBytes = new HashMap<String, byte[]>();
|
||||||
|
|
||||||
MemoryJavaFileManager(JavaFileManager fileManager) {
|
public MemoryJavaFileManager(JavaFileManager fileManager) {
|
||||||
super(fileManager);
|
super(fileManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ class MemoryJavaFileManager extends ForwardingJavaFileManager<JavaFileManager> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JavaFileObject makeStringSource(String name, String code) {
|
public JavaFileObject makeStringSource(String name, String code) {
|
||||||
return new MemoryInputJavaFileObject(name, code);
|
return new MemoryInputJavaFileObject(name, code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue