/edu.illinois.dpjizer.region.core/src/edu/illinois/dpjizer/region/core/compile/CompilerInvoker.java

https://github.com/dpj/DPJizer · Java · 138 lines · 101 code · 26 blank · 11 comment · 13 complexity · fcd56c566328bf7845fd1d78b8be598d MD5 · raw file

  1. /**
  2. * This file is licensed under the University of Illinois/NCSA Open Source License. See LICENSE.TXT for details.
  3. */
  4. package edu.illinois.dpjizer.region.core.compile;
  5. import java.io.File;
  6. import java.io.IOException;
  7. import javax.tools.JavaFileManager;
  8. import javax.tools.JavaFileObject;
  9. import com.google.inject.Inject;
  10. import com.sun.tools.javac.code.RPL;
  11. import com.sun.tools.javac.code.Source;
  12. import com.sun.tools.javac.code.dpjizer.constraints.RPLElementContainmentConstraint;
  13. import com.sun.tools.javac.comp.AttrContext;
  14. import com.sun.tools.javac.comp.Env;
  15. import com.sun.tools.javac.jvm.Target;
  16. import com.sun.tools.javac.main.JavaCompiler;
  17. import com.sun.tools.javac.main.OptionName;
  18. import com.sun.tools.javac.tree.JCTree.JCClassDecl;
  19. import com.sun.tools.javac.tree.JCTree.JCCompilationUnit;
  20. import com.sun.tools.javac.util.Context;
  21. import com.sun.tools.javac.util.JavacFileManager;
  22. import com.sun.tools.javac.util.List;
  23. import com.sun.tools.javac.util.Log;
  24. import com.sun.tools.javac.util.Options;
  25. import com.sun.tools.javac.util.Pair;
  26. import edu.illinois.dpjizer.region.core.change.Changes;
  27. /**
  28. *
  29. * @author Mohsen Vakilian
  30. *
  31. */
  32. public class CompilerInvoker {
  33. public enum EraseDPJ {
  34. YES, NO
  35. }
  36. public Context context;
  37. ConstraintCollectionPhase constraintCollectionPhase;
  38. @Inject
  39. public CompilerInvoker(Context context, ConstraintCollectionPhase constraintCollectionPhase) {
  40. this.context = context;
  41. RPLElementContainmentConstraint.context= context;
  42. this.constraintCollectionPhase = constraintCollectionPhase;
  43. }
  44. protected List<JCCompilationUnit> parseFiles(String[] filepaths) {
  45. // Force Java 1.5 parsing and code generation
  46. Options.instance(context).put(OptionName.SOURCE, Source.JDK1_5.name);
  47. Options.instance(context).put(OptionName.TARGET, Target.JDK1_5.name);
  48. JavaCompiler comp = JavaCompiler.instance(context);
  49. List<JavaFileObject> javaFileObjects = javaFileObjectsOf(filepaths);
  50. List<JCCompilationUnit> astList;
  51. try {
  52. astList = comp.parseFiles(javaFileObjects);
  53. } catch (IOException e) {
  54. throw new RuntimeException(e);
  55. }
  56. if (astList == null)
  57. throw new RuntimeException("The ast list is null.");
  58. for (JCCompilationUnit compilationUnit : astList) {
  59. if (compilationUnit == null)
  60. throw new RuntimeException("A returned AST is null.");
  61. }
  62. return astList;
  63. }
  64. private List<JavaFileObject> javaFileObjectsOf(String[] filepaths) {
  65. JavacFileManager fileManager = (JavacFileManager) context.get(JavaFileManager.class);
  66. JavaFileObject[] javaFileObjects = new JavaFileObject[filepaths.length];
  67. for (int i = 0; i < filepaths.length; ++i) {
  68. javaFileObjects[i] = fileManager.getRegularFile(new File(filepaths[i]));
  69. }
  70. return List.from(javaFileObjects);
  71. }
  72. protected List<Pair<Env<AttrContext>, JCClassDecl>> analyzeASTs(List<JCCompilationUnit> astList, Phase lastPhase) {
  73. return analyzeASTs(astList, false, lastPhase);
  74. }
  75. private List<Pair<Env<AttrContext>, JCClassDecl>> analyzeASTs(List<JCCompilationUnit> astList, boolean expectErrors, Phase additionalPhase) {
  76. if (context == null) {
  77. throw new RuntimeException("Must call #parse first.");
  78. }
  79. JavaCompiler comp = JavaCompiler.instance(context);
  80. comp.eraseDPJ = false;
  81. comp.enterTrees(astList);
  82. List<Pair<Env<AttrContext>, JCClassDecl>> result = null;
  83. RPL.resultOfIsIncludedIn = true;
  84. List<Env<AttrContext>> attributed = comp.attribute(comp.todo);
  85. RPL.resultOfIsIncludedIn = false;
  86. result = comp.desugar(comp.flow(additionalPhase.analyze(comp.checkEffects(attributed), context)));
  87. if (!expectErrors)
  88. if (result.isEmpty())
  89. // throw new RuntimeException("Result is empty.");
  90. System.out.println("Result is empty.\n");
  91. try {
  92. context.get(JavaFileManager.class).close();
  93. } catch (IOException e) {
  94. throw new RuntimeException("Error in closing the context.", e);
  95. }
  96. if (expectErrors != (Log.instance(context).nerrors != 0)) {
  97. // new RuntimeException("Errors were (not) reported as expected.");
  98. System.out.println("Errors were (not) reported as expected.\n");
  99. }
  100. return result;
  101. }
  102. public void compile(String[] filepaths) {
  103. analyzeFiles(filepaths, new IdentitiyPhase());
  104. }
  105. private Changes analyzeFiles(String[] filepaths, Phase additionalPhase) {
  106. analyzeASTs(parseFiles(filepaths), additionalPhase);
  107. return additionalPhase.getChanges();
  108. }
  109. public Changes collectConstraints(String[] filepaths) {
  110. return analyzeFiles(filepaths, constraintCollectionPhase);
  111. }
  112. }