代码优化
This commit is contained in:
@@ -22,7 +22,6 @@
|
||||
package fun.asgc.neutrino.core.aop.compiler;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.sun.tools.javac.resources.compiler;
|
||||
import fun.asgc.neutrino.core.base.GlobalConfig;
|
||||
import fun.asgc.neutrino.core.util.CollectionUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -51,6 +50,9 @@ public class AsgcCompiler {
|
||||
private String generatorCodeSavePath;
|
||||
private DynamicClassLoader dynamicClassLoader;
|
||||
|
||||
private final List<Diagnostic<? extends JavaFileObject>> errors = new ArrayList<Diagnostic<? extends JavaFileObject>>();
|
||||
private final List<Diagnostic<? extends JavaFileObject>> warnings = new ArrayList<Diagnostic<? extends JavaFileObject>>();
|
||||
|
||||
public AsgcCompiler() {
|
||||
this(ClassLoader.getSystemClassLoader());
|
||||
}
|
||||
@@ -117,17 +119,54 @@ public class AsgcCompiler {
|
||||
* @param className 类名
|
||||
* @param sourceCode 源代码
|
||||
*/
|
||||
public Class<?> compile(String className, String sourceCode) throws ClassNotFoundException {
|
||||
public Class<?> compile(String pkg, String className, String sourceCode) throws ClassNotFoundException {
|
||||
log.info("options:" + getOptions());
|
||||
JavaFileManager javaFileManager = new DynamicJavaFileManager(standardJavaFileManager, dynamicClassLoader);
|
||||
Boolean result = javaCompiler.getTask(null, javaFileManager, collector, getOptions(), null, Lists.newArrayList(new StringSource(className, sourceCode))).call();
|
||||
if (!result) {
|
||||
collector.getDiagnostics().forEach(item -> log.error(item.toString()));
|
||||
if (!result || collector.getDiagnostics().size() > 0) {
|
||||
// collector.getDiagnostics().forEach(item -> log.error(item.toString()));
|
||||
if (!result || collector.getDiagnostics().size() > 0) {
|
||||
for (Diagnostic<? extends JavaFileObject> diagnostic : collector.getDiagnostics()) {
|
||||
switch (diagnostic.getKind()) {
|
||||
case NOTE:
|
||||
case MANDATORY_WARNING:
|
||||
case WARNING:
|
||||
warnings.add(diagnostic);
|
||||
break;
|
||||
case OTHER:
|
||||
case ERROR:
|
||||
default:
|
||||
errors.add(diagnostic);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
log.error("warring: {}", getWarnings());
|
||||
log.error("error: {}", getErrors());
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Class<?>> map = dynamicClassLoader.getClasses();
|
||||
if (CollectionUtil.isEmpty(map)) {
|
||||
return null;
|
||||
return dynamicClassLoader.findClass(pkg + "." + className);
|
||||
}
|
||||
|
||||
private List<String> diagnosticToString(List<Diagnostic<? extends JavaFileObject>> diagnostics) {
|
||||
|
||||
List<String> diagnosticMessages = new ArrayList<String>();
|
||||
|
||||
for (Diagnostic<? extends JavaFileObject> diagnostic : diagnostics) {
|
||||
diagnosticMessages.add(
|
||||
"line: " + diagnostic.getLineNumber() + ", message: " + diagnostic.getMessage(Locale.US));
|
||||
}
|
||||
return map.values().stream().filter(c -> c.getSimpleName().equals(className)).findFirst().orElseGet(null);
|
||||
|
||||
return diagnosticMessages;
|
||||
|
||||
}
|
||||
|
||||
public List<String> getErrors() {
|
||||
return diagnosticToString(errors);
|
||||
}
|
||||
|
||||
public List<String> getWarnings() {
|
||||
return diagnosticToString(warnings);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -43,7 +43,7 @@ public class DynamicClassLoader extends ClassLoader {
|
||||
@Override
|
||||
protected Class<?> findClass(String name) throws ClassNotFoundException {
|
||||
MemoryByteCode byteCode = byteCodes.get(name);
|
||||
if (byteCode == null) {
|
||||
if (null == byteCode) {
|
||||
return super.findClass(name);
|
||||
}
|
||||
|
||||
@@ -51,7 +51,7 @@ public class DynamicClassLoader extends ClassLoader {
|
||||
}
|
||||
|
||||
public Map<String, Class<?>> getClasses() throws ClassNotFoundException {
|
||||
Map<String, Class<?>> classes = new HashMap<String, Class<?>>();
|
||||
Map<String, Class<?>> classes = new HashMap<>();
|
||||
for (MemoryByteCode byteCode : byteCodes.values()) {
|
||||
classes.put(byteCode.getClassName(), findClass(byteCode.getClassName()));
|
||||
}
|
||||
@@ -59,7 +59,7 @@ public class DynamicClassLoader extends ClassLoader {
|
||||
}
|
||||
|
||||
public Map<String, byte[]> getByteCodes() {
|
||||
Map<String, byte[]> result = new HashMap<String, byte[]>(byteCodes.size());
|
||||
Map<String, byte[]> result = new HashMap<>(byteCodes.size());
|
||||
for (Map.Entry<String, MemoryByteCode> entry : byteCodes.entrySet()) {
|
||||
result.put(entry.getKey(), entry.getValue().getByteCode());
|
||||
}
|
||||
|
||||
+1
-1
@@ -92,7 +92,7 @@ public class DynamicJavaFileManager extends ForwardingJavaFileManager<JavaFileMa
|
||||
|
||||
// merge JavaFileObjects from specified ClassLoader
|
||||
if (location == StandardLocation.CLASS_PATH && kinds.contains(JavaFileObject.Kind.CLASS)) {
|
||||
return new IterableJoin<JavaFileObject>(super.list(location, packageName, kinds, recurse),
|
||||
return new IterableJoin<>(super.list(location, packageName, kinds, recurse),
|
||||
finder.find(packageName));
|
||||
}
|
||||
|
||||
|
||||
+2
@@ -21,6 +21,8 @@
|
||||
*/
|
||||
package fun.asgc.neutrino.core.aop.compiler;
|
||||
|
||||
import fun.asgc.neutrino.core.util.CollectionUtil;
|
||||
|
||||
import javax.tools.JavaFileObject;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
+7
-7
@@ -43,7 +43,7 @@ public class AsgcCompilerTest {
|
||||
"\t\tSystem.out.println(\"hello\");\n" +
|
||||
"\t}\n" +
|
||||
"}\n";
|
||||
Class clazz = compiler.compile("Hello", code);
|
||||
Class clazz = compiler.compile("a.b","Hello", code);
|
||||
Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("hello")).findFirst().get();
|
||||
Object instance = clazz.newInstance();
|
||||
method.invoke(instance);
|
||||
@@ -59,7 +59,7 @@ public class AsgcCompilerTest {
|
||||
"\t\tSystem.out.println(\"熊猫正在吃\" + food);\n" +
|
||||
"\t}\n" +
|
||||
"}\n";
|
||||
Class clazz = compiler.compile("Panda", code);
|
||||
Class clazz = compiler.compile("a.b","Panda", code);
|
||||
Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("eat")).findFirst().get();
|
||||
Object instance = clazz.newInstance();
|
||||
method.invoke(instance, "竹子");
|
||||
@@ -70,13 +70,13 @@ public class AsgcCompilerTest {
|
||||
AsgcCompiler compiler = new AsgcCompiler();
|
||||
compiler.addClasspath("/Users/yangwen/my/tmp/java");
|
||||
String code = "package a.b;\n" +
|
||||
"import fun.asgc.Player;\n" +
|
||||
"import fun.asgc.cptest.Player;\n" +
|
||||
"public class RadioPlayer implements Player {\n" +
|
||||
"\tpublic void play() {\n" +
|
||||
"\t\tSystem.out.println(\"收音机播放\");\n" +
|
||||
"\t}\n" +
|
||||
"}\n";
|
||||
Class clazz = compiler.compile("RadioPlayer", code);
|
||||
Class clazz = compiler.compile("a.b","RadioPlayer", code);
|
||||
Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("play")).findFirst().get();
|
||||
Object instance = clazz.newInstance();
|
||||
method.invoke(instance);
|
||||
@@ -89,7 +89,7 @@ public class AsgcCompilerTest {
|
||||
compiler.addClasspath("/Users/yangwen/my/tmp/java/asgc-package-lab1-1.0-SNAPSHOT.jar");
|
||||
String code = "package a.b;\n" +
|
||||
"import fun.asgc.lab.pkg.lab1.Dog1;\n" +
|
||||
"import fun.asgc.Player;\n" +
|
||||
"import fun.asgc.cptest.Player;\n" +
|
||||
"public class RadioPlayer implements Player {\n" +
|
||||
"\tpublic void play() {\n" +
|
||||
// "\t\tSystem.out.println(Dog.class);\n" +
|
||||
@@ -97,7 +97,7 @@ public class AsgcCompilerTest {
|
||||
"\t\tSystem.out.println(\"收音机播放\");\n" +
|
||||
"\t}\n" +
|
||||
"}\n";
|
||||
Class clazz = compiler.compile("RadioPlayer", code);
|
||||
Class clazz = compiler.compile("a.b","RadioPlayer", code);
|
||||
Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("play")).findFirst().get();
|
||||
Object instance = clazz.newInstance();
|
||||
method.invoke(instance);
|
||||
@@ -113,7 +113,7 @@ public class AsgcCompilerTest {
|
||||
"}\n";
|
||||
AsgcCompiler compiler = new AsgcCompiler();
|
||||
try {
|
||||
Class clazz = compiler.compile( "Calc", code);
|
||||
Class clazz = compiler.compile( "fun.asgc.test","Calc", code);
|
||||
Method method = ReflectUtil.getMethods(clazz).stream().filter(m -> m.getName().equals("invoke")).findFirst().get();
|
||||
return method.invoke(null);
|
||||
} catch (ClassNotFoundException|IllegalAccessException|InvocationTargetException e) {
|
||||
|
||||
Reference in New Issue
Block a user