langtools/src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java
changeset 34857 14d1224cfed3
parent 34092 fd97b0f6abcc
child 35000 952a7b4652f0
equal deleted inserted replaced
34856:5ca50af1b45c 34857:14d1224cfed3
    54 import java.util.Collection;
    54 import java.util.Collection;
    55 import java.util.HashMap;
    55 import java.util.HashMap;
    56 import java.util.LinkedHashMap;
    56 import java.util.LinkedHashMap;
    57 import java.util.Map;
    57 import java.util.Map;
    58 import java.util.stream.Collectors;
    58 import java.util.stream.Collectors;
       
    59 import static java.util.stream.Collectors.toList;
    59 import java.util.stream.Stream;
    60 import java.util.stream.Stream;
    60 import javax.lang.model.util.Elements;
    61 import javax.lang.model.util.Elements;
    61 import javax.tools.FileObject;
    62 import javax.tools.FileObject;
    62 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
    63 import jdk.jshell.MemoryFileManager.SourceMemoryJavaFileObject;
    63 import jdk.jshell.ClassTracker.ClassInfo;
    64 import jdk.jshell.ClassTracker.ClassInfo;
   194      * Parse a snippet of code (as a String) using the parser subclass.  Return
   195      * Parse a snippet of code (as a String) using the parser subclass.  Return
   195      * the parse tree (and errors).
   196      * the parse tree (and errors).
   196      */
   197      */
   197     class ParseTask extends BaseTask {
   198     class ParseTask extends BaseTask {
   198 
   199 
   199         private final CompilationUnitTree cut;
   200         private final Iterable<? extends CompilationUnitTree> cuts;
   200         private final List<? extends Tree> units;
   201         private final List<? extends Tree> units;
   201 
   202 
   202         ParseTask(final String source) {
   203         ParseTask(final String source) {
   203             super(Stream.of(source),
   204             super(Stream.of(source),
   204                     new StringSourceHandler(),
   205                     new StringSourceHandler(),
   205                     "-XDallowStringFolding=false", "-proc:none");
   206                     "-XDallowStringFolding=false", "-proc:none");
   206             ReplParserFactory.instance(getContext());
   207             ReplParserFactory.instance(getContext());
   207             Iterable<? extends CompilationUnitTree> asts = parse();
   208             cuts = parse();
   208             Iterator<? extends CompilationUnitTree> it = asts.iterator();
   209             units = Util.stream(cuts)
   209             if (it.hasNext()) {
   210                     .flatMap(cut -> {
   210                 this.cut = it.next();
   211                         List<? extends ImportTree> imps = cut.getImports();
   211                 List<? extends ImportTree> imps = cut.getImports();
   212                         return (!imps.isEmpty() ? imps : cut.getTypeDecls()).stream();
   212                 this.units = !imps.isEmpty() ? imps : cut.getTypeDecls();
   213                     })
   213             } else {
   214                     .collect(toList());
   214                 this.cut = null;
       
   215                 this.units = Collections.emptyList();
       
   216             }
       
   217         }
   215         }
   218 
   216 
   219         private Iterable<? extends CompilationUnitTree> parse() {
   217         private Iterable<? extends CompilationUnitTree> parse() {
   220             try {
   218             try {
   221                 return task.parse();
   219                 return task.parse();
   227         List<? extends Tree> units() {
   225         List<? extends Tree> units() {
   228             return units;
   226             return units;
   229         }
   227         }
   230 
   228 
   231         @Override
   229         @Override
   232         CompilationUnitTree cuTree() {
   230         Iterable<? extends CompilationUnitTree> cuTrees() {
   233             return cut;
   231             return cuts;
   234         }
   232         }
   235     }
   233     }
   236 
   234 
   237     /**
   235     /**
   238      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
   236      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
   239      */
   237      */
   240     class AnalyzeTask extends BaseTask {
   238     class AnalyzeTask extends BaseTask {
   241 
   239 
   242         private final CompilationUnitTree cut;
   240         private final Iterable<? extends CompilationUnitTree> cuts;
   243 
   241 
   244         AnalyzeTask(final OuterWrap wrap) {
   242         AnalyzeTask(final OuterWrap wrap) {
   245             this(Stream.of(wrap),
   243             this(Stream.of(wrap),
   246                     new WrapSourceHandler(wrap),
   244                     new WrapSourceHandler(wrap),
   247                     "-XDshouldStopPolicy=FLOW", "-proc:none");
   245                     "-XDshouldStopPolicy=FLOW", "-proc:none");
   253         }
   251         }
   254 
   252 
   255         <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
   253         <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
   256                 String... extraOptions) {
   254                 String... extraOptions) {
   257             super(stream, sourceHandler, extraOptions);
   255             super(stream, sourceHandler, extraOptions);
   258             Iterator<? extends CompilationUnitTree> cuts = analyze().iterator();
   256             cuts = analyze();
   259             if (cuts.hasNext()) {
       
   260                 this.cut = cuts.next();
       
   261                 //proc.debug("AnalyzeTask element=%s  cutp=%s  cut=%s\n", e, cutp, cut);
       
   262             } else {
       
   263                 this.cut = null;
       
   264                 //proc.debug("AnalyzeTask -- no elements -- %s\n", getDiagnostics());
       
   265             }
       
   266         }
   257         }
   267 
   258 
   268         private Iterable<? extends CompilationUnitTree> analyze() {
   259         private Iterable<? extends CompilationUnitTree> analyze() {
   269             try {
   260             try {
   270                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
   261                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
   274                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
   265                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
   275             }
   266             }
   276         }
   267         }
   277 
   268 
   278         @Override
   269         @Override
   279         CompilationUnitTree cuTree() {
   270         Iterable<? extends CompilationUnitTree> cuTrees() {
   280             return cut;
   271             return cuts;
   281         }
   272         }
   282 
   273 
   283         Elements getElements() {
   274         Elements getElements() {
   284             return task.getElements();
   275             return task.getElements();
   285         }
   276         }
   330                         .add(jfo);
   321                         .add(jfo);
   331             }
   322             }
   332         }
   323         }
   333 
   324 
   334         @Override
   325         @Override
   335         CompilationUnitTree cuTree() {
   326         Iterable<? extends CompilationUnitTree> cuTrees() {
   336             throw new UnsupportedOperationException("Not supported.");
   327             throw new UnsupportedOperationException("Not supported.");
   337         }
   328         }
   338     }
   329     }
   339 
   330 
   340     abstract class BaseTask {
   331     abstract class BaseTask {
   360             this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null,
   351             this.task = (JavacTaskImpl) ((JavacTool) compiler).getTask(null,
   361                     fileManager, diagnostics, options, null,
   352                     fileManager, diagnostics, options, null,
   362                     compilationUnits, context);
   353                     compilationUnits, context);
   363         }
   354         }
   364 
   355 
   365         abstract CompilationUnitTree cuTree();
   356         abstract Iterable<? extends CompilationUnitTree> cuTrees();
       
   357 
       
   358         CompilationUnitTree firstCuTree() {
       
   359             return cuTrees().iterator().next();
       
   360         }
   366 
   361 
   367         Diag diag(Diagnostic<? extends JavaFileObject> diag) {
   362         Diag diag(Diagnostic<? extends JavaFileObject> diag) {
   368             return sourceHandler.diag(diag);
   363             return sourceHandler.diag(diag);
   369         }
   364         }
   370 
   365