1 package spoon.support.util; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.util.ArrayList ; 7 import java.util.List ; 8 9 import org.eclipse.jdt.internal.compiler.ClassFile; 10 import org.eclipse.jdt.internal.compiler.CompilationResult; 11 import org.eclipse.jdt.internal.compiler.ICompilerRequestor; 12 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; 13 import org.eclipse.jdt.internal.compiler.batch.FileSystem; 14 import org.eclipse.jdt.internal.compiler.classfmt.ClassFileConstants; 15 import org.eclipse.jdt.internal.compiler.env.ICompilationUnit; 16 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; 17 import org.eclipse.jdt.internal.compiler.problem.DefaultProblemFactory; 18 19 public class JDTCompiler implements ICompilerRequestor { 20 21 public static ICompilationUnit getUnit(String name, File file) 22 throws Exception { 23 24 String [] tmp = name.split("[.]"); 25 char[][] pack = new char[tmp.length - 1][]; 26 27 for (int i = 0; i < tmp.length - 1; i++) { 28 pack[i] = tmp[i].toCharArray(); 29 } 30 31 ByteArrayOutputStream out = new ByteArrayOutputStream (); 32 FileInputStream in = new FileInputStream (file); 33 byte[] buffer = new byte[512]; 34 int read; 35 while ((read = in.read(buffer, 0, 512)) >= 0) 36 out.write(buffer, 0, read); 37 38 ICompilationUnit unit = null; 39 40 unit = new BasicCompilationUnit(out.toString().toCharArray(), pack, 41 file.getName()); 42 43 out.close(); 44 in.close(); 45 return unit; 46 } 47 48 CompilerOptions compilerOption; 49 50 List <ClassFile> classFiles = new ArrayList <ClassFile>(); 51 52 public void acceptResult(CompilationResult result) { 53 if (result.hasErrors()) { 54 System.err.println(result); 55 } 56 57 for (ClassFile f : result.getClassFiles()) { 58 classFiles.add(f); 59 } 60 } 61 62 public List <ClassFile> compile(ICompilationUnit[] units) { 63 org.eclipse.jdt.internal.compiler.Compiler compiler = new org.eclipse.jdt.internal.compiler.Compiler( 64 getLibraryAccess(), getHandlingPolicy(), getCompilerOption(), 65 this, new DefaultProblemFactory()); 66 compiler.compile(units); 67 return classFiles; 68 } 69 70 public CompilerOptions getCompilerOption() { 71 if (compilerOption == null) { 72 compilerOption = new CompilerOptions(); 73 compilerOption.sourceLevel = ClassFileConstants.JDK1_5; 74 compilerOption.suppressWarnings = true; 75 } 76 return compilerOption; 77 } 78 79 private IErrorHandlingPolicy getHandlingPolicy() { 80 return new IErrorHandlingPolicy() { 81 public boolean proceedOnErrors() { 82 return true; } 84 85 public boolean stopOnFirstError() { 86 return false; 87 } 88 }; 89 } 90 91 FileSystem getLibraryAccess() { 92 String bootpath = System.getProperty("sun.boot.class.path"); 93 String classpath = System.getProperty("java.class.path"); 94 List <String > lst = new ArrayList <String >(); 95 for (String s : bootpath.split(File.pathSeparator)) { 96 File f = new File (s); 97 if (f.exists()) { 98 lst.add(f.getAbsolutePath()); 99 } 100 } 101 for (String s : classpath.split(File.pathSeparator)) { 102 File f = new File (s); 103 if (f.exists()) { 104 lst.add(f.getAbsolutePath()); 105 } 106 } 107 return new FileSystem(lst.toArray(new String [0]), new String [0], System 108 .getProperty("file.encoding")); 109 } 110 111 public List <ClassFile> getClassFiles() { 112 return classFiles; 113 } 114 115 } 116 | Popular Tags |