src/jdk.jshell/share/classes/jdk/jshell/TaskFactory.java

Print this page




 206             return cuts;
 207         }
 208     }
 209 
 210     /**
 211      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
 212      */
 213     class AnalyzeTask extends BaseTask {
 214 
 215         private final Iterable<? extends CompilationUnitTree> cuts;
 216 
 217         AnalyzeTask(final OuterWrap wrap, String... extraArgs) {
 218             this(Collections.singletonList(wrap), extraArgs);
 219         }
 220 
 221         AnalyzeTask(final Collection<OuterWrap> wraps, String... extraArgs) {
 222             this(wraps.stream(),
 223                     new WrapSourceHandler(),
 224                     Util.join(new String[] {
 225                         "-Xshouldstop:at=FLOW", "-Xlint:unchecked",
 226                         "-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED",
 227                         "-proc:none"
 228                     }, extraArgs));
 229         }
 230 
 231         private <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
 232                 String... extraOptions) {
 233             super(stream, sourceHandler, extraOptions);
 234             cuts = analyze();
 235         }
 236 
 237         private Iterable<? extends CompilationUnitTree> analyze() {
 238             try {
 239                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
 240                 task.analyze();
 241                 return cuts;
 242             } catch (Exception ex) {
 243                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
 244             }
 245         }
 246 


 250         }
 251 
 252         Elements getElements() {
 253             return task.getElements();
 254         }
 255 
 256         javax.lang.model.util.Types getTypes() {
 257             return task.getTypes();
 258         }
 259     }
 260 
 261     /**
 262      * Unit the wrapped snippet to class files.
 263      */
 264     class CompileTask extends BaseTask {
 265 
 266         private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>();
 267 
 268         CompileTask(final Collection<OuterWrap> wraps) {
 269             super(wraps.stream(), new WrapSourceHandler(),
 270                     "-Xlint:unchecked", "-XaddExports:jdk.jshell/jdk.internal.jshell.remote=ALL-UNNAMED", "-proc:none", "-parameters");
 271         }
 272 
 273         boolean compile() {
 274             fileManager.registerClassFileCreationListener(this::listenForNewClassFile);
 275             boolean result = task.call();
 276             fileManager.registerClassFileCreationListener(null);
 277             return result;
 278         }
 279 
 280         // Returns the list of classes generated during this compile.
 281         // Stores the mapping between class name and current compiled bytes.
 282         List<String> classList(OuterWrap w) {
 283             List<OutputMemoryJavaFileObject> l = classObjs.get(w);
 284             if (l == null) {
 285                 return Collections.emptyList();
 286             }
 287             List<String> list = new ArrayList<>();
 288             for (OutputMemoryJavaFileObject fo : l) {
 289                 state.setClassnameToBytes(fo.getName(), fo.getBytes());
 290                 list.add(fo.getName());
 291             }
 292             return list;
 293         }
 294 
 295         private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
 296                 String className, JavaFileObject.Kind kind, FileObject sibling) {
 297             //debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
 298             if (location == CLASS_OUTPUT) {
 299                 state.debug(DBG_GEN, "Compiler generating class %s\n", className);
 300                 OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject)
 301                         && (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap))
 302                         ? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin()
 303                         : null;
 304                 classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v)
 305                         .add(jfo);
 306             }
 307         }
 308 
 309         @Override




 206             return cuts;
 207         }
 208     }
 209 
 210     /**
 211      * Run the normal "analyze()" pass of the compiler over the wrapped snippet.
 212      */
 213     class AnalyzeTask extends BaseTask {
 214 
 215         private final Iterable<? extends CompilationUnitTree> cuts;
 216 
 217         AnalyzeTask(final OuterWrap wrap, String... extraArgs) {
 218             this(Collections.singletonList(wrap), extraArgs);
 219         }
 220 
 221         AnalyzeTask(final Collection<OuterWrap> wraps, String... extraArgs) {
 222             this(wraps.stream(),
 223                     new WrapSourceHandler(),
 224                     Util.join(new String[] {
 225                         "-Xshouldstop:at=FLOW", "-Xlint:unchecked",

 226                         "-proc:none"
 227                     }, extraArgs));
 228         }
 229 
 230         private <T>AnalyzeTask(final Stream<T> stream, SourceHandler<T> sourceHandler,
 231                 String... extraOptions) {
 232             super(stream, sourceHandler, extraOptions);
 233             cuts = analyze();
 234         }
 235 
 236         private Iterable<? extends CompilationUnitTree> analyze() {
 237             try {
 238                 Iterable<? extends CompilationUnitTree> cuts = task.parse();
 239                 task.analyze();
 240                 return cuts;
 241             } catch (Exception ex) {
 242                 throw new InternalError("Exception during analyze - " + ex.getMessage(), ex);
 243             }
 244         }
 245 


 249         }
 250 
 251         Elements getElements() {
 252             return task.getElements();
 253         }
 254 
 255         javax.lang.model.util.Types getTypes() {
 256             return task.getTypes();
 257         }
 258     }
 259 
 260     /**
 261      * Unit the wrapped snippet to class files.
 262      */
 263     class CompileTask extends BaseTask {
 264 
 265         private final Map<OuterWrap, List<OutputMemoryJavaFileObject>> classObjs = new HashMap<>();
 266 
 267         CompileTask(final Collection<OuterWrap> wraps) {
 268             super(wraps.stream(), new WrapSourceHandler(),
 269                     "-Xlint:unchecked", "-proc:none", "-parameters");
 270         }
 271 
 272         boolean compile() {
 273             fileManager.registerClassFileCreationListener(this::listenForNewClassFile);
 274             boolean result = task.call();
 275             fileManager.registerClassFileCreationListener(null);
 276             return result;
 277         }
 278 
 279         // Returns the list of classes generated during this compile.
 280         // Stores the mapping between class name and current compiled bytes.
 281         List<String> classList(OuterWrap w) {
 282             List<OutputMemoryJavaFileObject> l = classObjs.get(w);
 283             if (l == null) {
 284                 return Collections.emptyList();
 285             }
 286             List<String> list = new ArrayList<>();
 287             for (OutputMemoryJavaFileObject fo : l) {
 288                 state.classTracker.setCurrentBytes(fo.getName(), fo.getBytes());
 289                 list.add(fo.getName());
 290             }
 291             return list;
 292         }
 293 
 294         private void listenForNewClassFile(OutputMemoryJavaFileObject jfo, JavaFileManager.Location location,
 295                 String className, JavaFileObject.Kind kind, FileObject sibling) {
 296             //debug("listenForNewClassFile %s loc=%s kind=%s\n", className, location, kind);
 297             if (location == CLASS_OUTPUT) {
 298                 state.debug(DBG_GEN, "Compiler generating class %s\n", className);
 299                 OuterWrap w = ((sibling instanceof SourceMemoryJavaFileObject)
 300                         && (((SourceMemoryJavaFileObject) sibling).getOrigin() instanceof OuterWrap))
 301                         ? (OuterWrap) ((SourceMemoryJavaFileObject) sibling).getOrigin()
 302                         : null;
 303                 classObjs.compute(w, (k, v) -> (v == null)? new ArrayList<>() : v)
 304                         .add(jfo);
 305             }
 306         }
 307 
 308         @Override