Skip to content

Commit

Permalink
fix: change compiler to arthas-memorycompiler;
Browse files Browse the repository at this point in the history
release to 1.0.0
  • Loading branch information
zhengzq committed Jul 1, 2021
1 parent 5fae951 commit 40758b2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<groupId>cn.zzq0324</groupId>
<artifactId>feature-flag</artifactId>
<packaging>jar</packaging>
<version>1.0.0-SNAPSHOT</version>
<version>1.0.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand All @@ -31,9 +31,9 @@
</dependency>

<dependency>
<groupId>com.itranswarp</groupId>
<artifactId>compiler</artifactId>
<version>1.0</version>
<groupId>com.taobao.arthas</groupId>
<artifactId>arthas-memorycompiler</artifactId>
<version>3.5.2</version>
</dependency>

<dependency>
Expand Down
29 changes: 23 additions & 6 deletions src/main/java/cn/zzq0324/feature/flag/support/JdkCompiler.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package cn.zzq0324.feature.flag.support;

import com.itranswarp.compiler.JavaStringCompiler;
import com.taobao.arthas.compiler.DynamicCompiler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.CollectionUtils;

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

/**
Expand All @@ -13,6 +17,8 @@
*/
public class JdkCompiler {

private static final Logger logger = LoggerFactory.getLogger(JdkCompiler.class);

public static final String JAVA_FILE_SUFFIX = ".java";

/**
Expand All @@ -25,11 +31,22 @@ public class JdkCompiler {
* @throws IOException
* @throws ClassNotFoundException
*/
public static <T> Class<? extends T> compile(String classPackage, String classSimpleName, String source)
throws IOException, ClassNotFoundException {
JavaStringCompiler compiler = new JavaStringCompiler();
Map<String, byte[]> compileResult = compiler.compile(classSimpleName + JAVA_FILE_SUFFIX, source);
public static <T> Class<? extends T> compile(String classPackage, String classSimpleName, String source) {
DynamicCompiler dynamicCompiler = new DynamicCompiler(Thread.currentThread().getContextClassLoader());

String className = classPackage + "." + classSimpleName;
dynamicCompiler.addSource(className, source);

Map<String, Class<?>> classMap = dynamicCompiler.build();

// 成功编译,未报错
if (CollectionUtils.isEmpty(dynamicCompiler.getErrors())) {
return (Class<? extends T>)classMap.get(className);
}

logger.error("Compile class: {} error, error info: {}", className,
Arrays.toString(dynamicCompiler.getErrors().toArray()));

return (Class<T>)compiler.loadClass(classPackage + "." + classSimpleName, compileResult);
throw new IllegalStateException("Compile error");
}
}

0 comments on commit 40758b2

Please sign in to comment.