如何使用eclipse编译器进行独立的动态内存编译?

时间:2013-01-09 00:17:41

标签: java eclipse

eclipse编译器及其API与JDK中包含的一个相比具有一些显着优势(特别有益于我的应用程序),所以我想使用它。我有一个独立的实用程序,我想最小化它的大小和依赖。

访问eclipse编译器的方法是什么(最小的jar文件集和下载位置),并在内存中动态编译生成的代码?

2 个答案:

答案 0 :(得分:5)

starting from this page下载ECJ,点击最新版本,然后找到并下载文件ecj- [version] .jar。为此,我使用的是4.2.1。在类路径中引用此jar。

您使用org.eclipse.jdt.internal.compiler.Compiler。构造函数的大多数内容都有默认值。您只需以ICompilerRequestor的形式为结果提供回调。下面的示例使用简单的字节类加载器来测试结果。要进行级联编译,请创建FileSystem的子类,从INameEnvironment中重写方法。

package test.eclipse.compiler;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.jdt.internal.compiler.ClassFile;
import org.eclipse.jdt.internal.compiler.CompilationResult;
import org.eclipse.jdt.internal.compiler.Compiler;
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
import org.eclipse.jdt.internal.compiler.batch.CompilationUnit;
import org.eclipse.jdt.internal.compiler.batch.FileSystem;
import org.eclipse.jdt.internal.compiler.batch.FileSystem.Classpath;
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory;
import org.eclipse.jdt.internal.compiler.util.Util;


public class TestCompile {
    static class ByteClassLoader extends ClassLoader {
        private Map<String, byte[]> classMap;


        public ByteClassLoader(Map<String, byte[]> classMap) {
            super();
            this.classMap = classMap;
        }

        protected Class<?> findClass(String name) throws ClassNotFoundException {
            byte[] bytes = classMap.get(name);
            if (bytes == null) {
                return super.findClass(name);
            } else {
                return defineClass(name, bytes, 0, bytes.length);
            }
        }
    }


    public static void compile(String code, String filename) {
        ArrayList<Classpath> cp = new ArrayList<FileSystem.Classpath>();
        Util.collectRunningVMBootclasspath(cp);
        INameEnvironment env = new NameEnv(cp.toArray(new FileSystem.Classpath[cp.size()]), null);
        ICompilerRequestor requestor = new ICompilerRequestor() {
            @Override
            public void acceptResult(CompilationResult result) {
                ClassFile[] cf = result.getClassFiles();
                HashMap<String, byte[]> classMap = new HashMap<String, byte[]>();
                classMap.put("Test", cf[0].getBytes());
                ByteClassLoader cl = new ByteClassLoader(classMap);
                try {
                    Class<?> c = cl.loadClass("Test");
                    Method m = c.getMethod("test");
                    m.invoke(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        Compiler compiler = new Compiler(env, DefaultErrorHandlingPolicies.exitAfterAllProblems(),
                new CompilerOptions(), requestor, new DefaultProblemFactory());

        ICompilationUnit[] units = new ICompilationUnit[] { new CompilationUnit(code.toCharArray(), filename, null) };
        compiler.compile(units);
    }

    public static void main(String[] args) {
        compile("public class Test { public static void test() { System.out.println(\"Hello, world.\"); }}",
                "Test.java");
    }
}

this blog post

许可复制

答案 1 :(得分:1)

您可以很容易地在the maven repositoryuse it找到ant找到的内容。