import org.apache.commons.io.IOUtils;
import org.eclipse.jdt.internal.compiler.*;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.HashMap;
import java.util.Locale;
public class TestIt {
public static String code = "package a;" +
"public class Abc {" +
" public static void main(String[] args) {" +
" System.out.println(\"aaabbbccc\");" +
" }" +
"}";
static ICompilerRequestor compilerRequestor = new ICompilerRequestor() {
@Override
public void acceptResult(CompilationResult result) {
if (result.hasErrors()) {
System.out.println("编译出现错误");
System.out.println(result.getErrors()[0].toString());
return;
}
ClassFile[] classFiles = result.getClassFiles();
ClassFile classFile = classFiles[0];
try {
new PrintStream("C:\\Users\\xinxi\\Desktop\\a\\Abc.class").write(classFile.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
};
static INameEnvironment nameEnvironment = new INameEnvironment() {
public NameEnvironmentAnswer findType(final char[][] compoundTypeName) {
return findType(join(compoundTypeName));
}
public NameEnvironmentAnswer findType(final char[] typeName, final char[][] packageName) {
return findType(join(packageName) + "." + new String(typeName));
}
private NameEnvironmentAnswer findType(final String name) {
try {
if (name.equals("a.Abc")) {
ClassFileReader classFileReader = new ClassFileReader(code.getBytes(), name.toCharArray(), true);
return new NameEnvironmentAnswer(classFileReader, null);
}
InputStream input = this.getClass().getClassLoader().getResourceAsStream(name.replace(".", "/") + ".class");
if (input != null) {
byte[] bytes = IOUtils.toByteArray(input);
if (bytes != null) {
ClassFileReader classFileReader = new ClassFileReader(bytes, name.toCharArray(), true);
return new NameEnvironmentAnswer(classFileReader, null);
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
return null;
}
public boolean isPackage(char[][] parentPackageName, char[] packageName) {
String name = new String(packageName);
if (parentPackageName != null) {
name = join(parentPackageName) + "." + name;
}
if (name.equals("a.Abc")) {
return false;
}
return true;
}
public void cleanup() {
}
};
private static class StringCompilationUnit implements ICompilationUnit {
private String fileName;
private char[] typeName;
private char[][] packageName;
private String content;
public StringCompilationUnit(String className, String content) {
this.content = content;
if (className.contains("$")) {
className = className.substring(0, className.indexOf("$"));
}
fileName = className.replace('.', '/') + ".java";
int pos = className.lastIndexOf('.');
if (pos > 0) {
typeName = className.substring(pos + 1).toCharArray();
} else {
typeName = className.toCharArray();
}
String[] names = className.split("\\.");
packageName = new char[names.length - 1][];
for (int i = 0; i < packageName.length; i++) {
packageName[i] = names[i].toCharArray();
}
}
@Override
public char[] getContents() {
return content.toCharArray();
}
@Override
public char[] getMainTypeName() {
return typeName;
}
@Override
public char[][] getPackageName() {
return packageName;
}
@Override
public boolean ignoreOptionalProblems() {
return false;
}
@Override
public char[] getFileName() {
return fileName.toCharArray();
}
}
public static void main(String[] args) {
IProblemFactory problemFactory = new DefaultProblemFactory(Locale.ENGLISH);
IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.exitOnFirstError();
CompilerOptions options = new CompilerOptions(new HashMap() {{
put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_8);
put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_8);
}});
Compiler jdtCompiler = new Compiler(nameEnvironment, policy, options, compilerRequestor, problemFactory);
jdtCompiler.compile(new ICompilationUnit[]{new StringCompilationUnit("a.Abc", code)});
}
private static String join(char[][] chars) {
StringBuilder sb = new StringBuilder();
for (char[] item : chars) {
if (sb.length() > 0) {
sb.append(".");
}
sb.append(item);
}
return sb.toString();
}
}