2013-01-09 39 views
6

Trình biên dịch eclipse và API của nó có một số lợi thế đáng kể (đặc biệt có lợi cho ứng dụng của tôi) so với trình bao gồm trong JDK và vì vậy tôi muốn sử dụng nó. Tôi có một tiện ích độc lập và tôi muốn giảm thiểu kích thước và phụ thuộc của nó.Làm thế nào để làm độc lập on-the-fly trong bộ nhớ biên dịch với trình biên dịch eclipse?

Cách để truy cập trình biên dịch nhật thực (tập tối thiểu tệp jar và nơi tải xuống) và biên dịch mã được tạo khi đang chạy trong bộ nhớ?

+0

Tôi không biết nếu điều này là những gì bạn đang tìm kiếm, nhưng nó đã được các hit đầu tiên tôi nhận được từ googling "eclipse trình biên dịch": http: // www. eclipse.org/jdt/core/index.php –

+0

Đó là những gì tôi đang nói đến nhưng chú ý rằng đó không chỉ là trình biên dịch. – mentics

Trả lời

4

Tải xuống ECJ theo starting from this page, nhấp vào bản phát hành mới nhất, sau đó tìm và tải xuống tệp ecj- [version] .jar. Đối với điều này, tôi đang sử dụng 4.2.1. Tham khảo lọ này trong classpath của bạn.

Bạn sử dụng số org.eclipse.jdt.internal.compiler.Compiler. Hầu hết mọi thứ cho constructor có mặc định có sẵn. Bạn chỉ cần cung cấp cho nó một gọi lại cho các kết quả dưới dạng một ICompilerRequestor. Ví dụ dưới đây sử dụng trình nạp lớp byte đơn giản để kiểm tra kết quả. Để thực hiện việc biên dịch tầng, bạn tạo một lớp con của FileSystem, ghi đè các phương thức từ 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"); 
    } 
} 

sao chép với sự cho phép từ this blog post

1

Có vẻ như bạn có thể tìm thấy những gì bạn đang tìm kiếm trong the maven repositoryuse it với ant khá dễ dàng.

+0

Cảm ơn! Có một nơi nào đó cho biết cách sử dụng nó để biên dịch một lớp đơn trong bộ nhớ? Tôi sẽ chỉ ra rõ ràng hơn trong câu hỏi. – mentics

+0

Tôi muốn viết chuỗi đó vào một tệp kết thúc bằng .java và gửi đến ECJ. – hd1

+0

No. Đây là một tình huống hiệu suất quan trọng. Cần phải nhanh nhất có thể. – mentics

Các vấn đề liên quan