1 package spoon.support.builder; 2 3 import java.io.PrintWriter ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Map ; 7 8 import org.eclipse.jdt.internal.compiler.CompilationResult; 9 import org.eclipse.jdt.internal.compiler.ICompilerRequestor; 10 import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy; 11 import org.eclipse.jdt.internal.compiler.IProblemFactory; 12 import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; 13 import org.eclipse.jdt.internal.compiler.batch.CompilationUnit; 14 import org.eclipse.jdt.internal.compiler.batch.Main; 15 import org.eclipse.jdt.internal.compiler.env.INameEnvironment; 16 import org.eclipse.jdt.internal.compiler.util.Util; 17 18 import spoon.reflect.Factory; 19 20 public class JDTCompiler extends Main implements ICompilerRequestor { 21 22 class Compiler extends org.eclipse.jdt.internal.compiler.Compiler { 23 24 @SuppressWarnings ("deprecation") 25 public Compiler(INameEnvironment environment, 26 IErrorHandlingPolicy policy, Map settings, 27 ICompilerRequestor requestor, IProblemFactory problemFactory, 28 PrintWriter out, boolean statementsRecovery) { 29 super(environment, policy, settings, requestor, problemFactory, 30 statementsRecovery); 31 } 32 33 public CompilationUnitDeclaration[] compileUnits( 34 CompilationUnit[] sourceUnits) { 35 CompilationUnitDeclaration unit = null; 36 int i = 0; 37 beginToCompile(sourceUnits); 39 for (; i < this.totalUnits; i++) { 42 unit = unitsToProcess[i]; 43 this.parser.getMethodBodies(unit); 44 45 if (unit.scope != null) 47 unit.scope.faultInTypes(); 48 if (unit.scope != null) 50 unit.scope 51 .verifyMethods(lookupEnvironment.methodVerifier()); 52 unit.resolve(); 54 unit.analyseCode(); 56 57 requestor.acceptResult(unit.compilationResult.tagAsAccepted()); 58 } 59 return this.unitsToProcess; 60 } 61 } 62 63 public static int javaCompilant = 5; 64 65 public static void compileSrc(Factory f, List <CtFile> files) 66 throws Throwable { 67 List <String > args = new ArrayList <String >(); 69 args.add("-1." + javaCompilant); 70 args.add("-preserveAllLocals"); 71 args.add("-enableJavadoc"); 72 args.add("-nowarn"); 73 args.add("."); 74 75 JDTCompiler compiler = new JDTCompiler(new PrintWriter (System.out), 76 new PrintWriter (System.err)); 77 compiler.configure(args.toArray(new String [0])); 78 79 CompilationUnitDeclaration[] units = compiler.getUnits(files); 80 81 JDTTreeBuilder builder = new JDTTreeBuilder(f); 82 83 for (CompilationUnitDeclaration unit : units) { 84 try { 85 unit.traverse(builder, unit.scope); 86 } catch (Throwable e) { 87 e.printStackTrace(); 88 } 89 } 90 } 91 92 public static void compileTemplate(Factory f, List <CtFile> streams) 93 throws Throwable { 94 List <String > args = new ArrayList <String >(); 96 args.add("-1." + javaCompilant); 97 args.add("-preserveAllLocals"); 98 args.add("-enableJavadoc"); 99 args.add("-nowarn"); 100 args.add("."); 101 102 JDTCompiler compiler = new JDTCompiler(new PrintWriter (System.out), 103 new PrintWriter (System.err)); 104 compiler.configure(args.toArray(new String [0])); 105 106 CompilationUnitDeclaration[] units = compiler.getUnits(streams); 107 108 JDTTreeBuilder builder = new JDTTreeBuilder(f); 109 builder.template = true; 110 for (CompilationUnitDeclaration unit : units) { 111 unit.traverse(builder, unit.scope); 112 } 113 } 114 115 Compiler batchCompiler; 116 117 PrintWriter out; 118 119 public JDTCompiler(PrintWriter outWriter, PrintWriter errWriter) { 120 super(outWriter, errWriter, false); 121 } 122 123 126 public CompilationUnit[] getCompilationUnits(List <CtFile> streams) 127 throws Exception { 128 CompilationUnit[] units = new CompilationUnit[streams.size()]; 129 int i = 0; 130 for (CtFile stream : streams) { 131 units[i] = new CompilationUnit(Util.getInputStreamAsCharArray( 132 stream.getContent(), -1, null), stream.getName(), null); 133 i++; 134 } 135 return units; 136 } 137 138 public CompilationUnitDeclaration[] getUnits(List <CtFile> streams) 139 throws Exception { 140 this.startTime = System.currentTimeMillis(); 141 INameEnvironment environment = getLibraryAccess(); 142 this.batchCompiler = new Compiler (environment, getHandlingPolicy(), 143 this.options, this, getProblemFactory(), this.out, false); 144 return batchCompiler.compileUnits(getCompilationUnits(streams)); 145 } 146 147 public void acceptResult(CompilationResult result) { 148 if (result.hasErrors()) { 149 System.err.println(result); 150 } 151 } 152 } 153 | Popular Tags |