Merge pull request #1243 from lzpeng723/v5-dev

V5 dev
This commit is contained in:
Golden Looly
2020-11-28 23:38:12 +08:00
committed by GitHub
14 changed files with 647 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
package cn.hutool.core.compiler;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.core.util.ZipUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.InputStream;
/**
* Java源码编译器测试
*
* @author lzpeng
*/
public class JavaSourceCompilerTest {
/**
* 测试编译Java源码
*/
@Test
public void testCompile() throws ClassNotFoundException {
final File libFile = ZipUtil.zip(FileUtil.file("lib.jar"),
new String[]{"a/A.class", "a/A$1.class", "a/A$InnerClass.class"},
new InputStream[]{
FileUtil.getInputStream("test-compile/a/A.class"),
FileUtil.getInputStream("test-compile/a/A$1.class"),
FileUtil.getInputStream("test-compile/a/A$InnerClass.class")
});
final ClassLoader classLoader = JavaSourceCompiler.create(null)
.addSource(FileUtil.file("test-compile/b/B.java"))
.addSource("c.C", FileUtil.readUtf8String("test-compile/c/C.java"))
.addLibrary(libFile)
.compile();
final Class<?> clazz = classLoader.loadClass("c.C");
Object obj = ReflectUtil.newInstance(clazz);
Assert.assertTrue(String.valueOf(obj).startsWith("c.C@"));
FileUtil.del(libFile);
}
}

Binary file not shown.

View File

@@ -0,0 +1,24 @@
package a;
import cn.hutool.core.lang.ConsoleTable;
import cn.hutool.core.lang.caller.CallerUtil;
public class A {
private class InnerClass {
}
public A() {
new InnerClass() {{
int i = 0;
Class<?> caller = CallerUtil.getCaller(i);
final ConsoleTable t = new ConsoleTable();
t.addHeader("类名", "类加载器");
System.out.println("初始化 " + getClass() + " 的调用链为: ");
while (caller != null) {
t.addBody(caller.toString(), caller.getClassLoader().toString());
caller = CallerUtil.getCaller(++i);
}
t.print();
}};
}
}

Binary file not shown.

View File

@@ -0,0 +1,8 @@
package b;
import a.A;
public class B {
public B() {
new A();
}
}

Binary file not shown.

View File

@@ -0,0 +1,9 @@
package c;
import b.B;
public class C {
public C() {
new B();
}
}