< prev index next >

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

Print this page




 219                      if (repl != null) {
 220                          for (ClassSymbol clazz : syms.getAllClasses()) {
 221                              if (clazz.packge() == repl) {
 222                                  syms.removeClass(syms.unnamedModule, clazz.flatName());
 223                              }
 224                          }
 225                          repl.members_field = null;
 226                          repl.completer = ClassFinder.instance(context).getCompleter();
 227                      }
 228                  }
 229             });
 230     }
 231 
 232     interface Worker<T extends BaseTask, Z> {
 233         public Z withTask(T task);
 234     }
 235 
 236     // Parse a snippet and return our parse task handler
 237     <Z> Z parse(final String source, Worker<ParseTask, Z> worker) {
 238         return parse(source, false, pt -> {
 239             if (!pt.units().isEmpty()
 240                     && pt.units().get(0).getKind() == Kind.EXPRESSION_STATEMENT
 241                     && pt.getDiagnostics().hasOtherThanNotStatementErrors()) {
 242                 // It failed, it may be an expression being incorrectly
 243                 // parsed as having a leading type variable, example:   a < b
 244                 // Try forcing interpretation as an expression
 245                 return parse(source, true, ept -> {
 246                     if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) {
 247                         return worker.withTask(ept);
 248                     } else {
 249                         return worker.withTask(pt);
 250                     }
 251                 });
 252             }
 253             return worker.withTask(pt);
 254         });
 255     }














 256 
 257     private interface SourceHandler<T> {
 258 
 259         JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
 260 
 261         Diag diag(Diagnostic<? extends JavaFileObject> d);
 262     }
 263 
 264     private class StringSourceHandler implements SourceHandler<String> {
 265 
 266         @Override
 267         public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) {
 268             return fm.createSourceFileObject(src, "$NeverUsedName$", src);
 269         }
 270 
 271         @Override
 272         public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
 273             return new Diag() {
 274 
 275                 @Override




 219                      if (repl != null) {
 220                          for (ClassSymbol clazz : syms.getAllClasses()) {
 221                              if (clazz.packge() == repl) {
 222                                  syms.removeClass(syms.unnamedModule, clazz.flatName());
 223                              }
 224                          }
 225                          repl.members_field = null;
 226                          repl.completer = ClassFinder.instance(context).getCompleter();
 227                      }
 228                  }
 229             });
 230     }
 231 
 232     interface Worker<T extends BaseTask, Z> {
 233         public Z withTask(T task);
 234     }
 235 
 236     // Parse a snippet and return our parse task handler
 237     <Z> Z parse(final String source, Worker<ParseTask, Z> worker) {
 238         return parse(source, false, pt -> {
 239             if (shouldParseAsExpression(pt)) {


 240                 // It failed, it may be an expression being incorrectly
 241                 // parsed as having a leading type variable, example:   a < b
 242                 // Try forcing interpretation as an expression
 243                 return parse(source, true, ept -> {
 244                     if (!ept.getDiagnostics().hasOtherThanNotStatementErrors()) {
 245                         return worker.withTask(ept);
 246                     } else {
 247                         return worker.withTask(pt);
 248                     }
 249                 });
 250             }
 251             return worker.withTask(pt);
 252         });
 253     }
 254         // where
 255         private boolean shouldParseAsExpression(ParseTask pt) {
 256             if (pt.units().isEmpty() || !pt.getDiagnostics().hasOtherThanNotStatementErrors())
 257                 return false;
 258             Tree tree = pt.units().get(0);
 259             if (tree.getKind() == Kind.EXPRESSION_STATEMENT)
 260                 return true;
 261             if (tree.getKind() == Kind.VARIABLE) {
 262                 JCVariableDecl varDecl = (JCVariableDecl) tree;
 263                 if (varDecl.vartype != null && varDecl.vartype.isQuestioned())
 264                     return true;
 265             }
 266             return false;
 267         }
 268 
 269     private interface SourceHandler<T> {
 270 
 271         JavaFileObject sourceToFileObject(MemoryFileManager fm, T t);
 272 
 273         Diag diag(Diagnostic<? extends JavaFileObject> d);
 274     }
 275 
 276     private class StringSourceHandler implements SourceHandler<String> {
 277 
 278         @Override
 279         public JavaFileObject sourceToFileObject(MemoryFileManager fm, String src) {
 280             return fm.createSourceFileObject(src, "$NeverUsedName$", src);
 281         }
 282 
 283         @Override
 284         public Diag diag(final Diagnostic<? extends JavaFileObject> d) {
 285             return new Diag() {
 286 
 287                 @Override


< prev index next >