langtools/src/jdk.jshell/share/classes/jdk/jshell/Eval.java
changeset 36780 6bf2bef08a91
parent 36499 9d823cc0fe98
child 36990 ec0b843a7af5
equal deleted inserted replaced
36779:9a030c4d2591 36780:6bf2bef08a91
    48 import java.io.IOException;
    48 import java.io.IOException;
    49 import java.io.StringWriter;
    49 import java.io.StringWriter;
    50 import java.io.Writer;
    50 import java.io.Writer;
    51 import java.util.LinkedHashSet;
    51 import java.util.LinkedHashSet;
    52 import java.util.Set;
    52 import java.util.Set;
       
    53 import com.sun.tools.javac.util.Context;
    53 import jdk.jshell.ClassTracker.ClassInfo;
    54 import jdk.jshell.ClassTracker.ClassInfo;
    54 import jdk.jshell.Key.ErroneousKey;
    55 import jdk.jshell.Key.ErroneousKey;
    55 import jdk.jshell.Key.MethodKey;
    56 import jdk.jshell.Key.MethodKey;
    56 import jdk.jshell.Key.TypeDeclKey;
    57 import jdk.jshell.Key.TypeDeclKey;
    57 import jdk.jshell.Snippet.SubKind;
    58 import jdk.jshell.Snippet.SubKind;
   131                 return processStatement(userSource, compileSource);
   132                 return processStatement(userSource, compileSource);
   132         }
   133         }
   133     }
   134     }
   134 
   135 
   135     private List<SnippetEvent> processImport(String userSource, String compileSource) {
   136     private List<SnippetEvent> processImport(String userSource, String compileSource) {
   136         Wrap guts = Wrap.importWrap(compileSource);
   137         Wrap guts = Wrap.simpleWrap(compileSource);
   137         Matcher mat = IMPORT_PATTERN.matcher(compileSource);
   138         Matcher mat = IMPORT_PATTERN.matcher(compileSource);
   138         String fullname;
   139         String fullname;
   139         String name;
   140         String name;
   140         boolean isStatic;
   141         boolean isStatic;
   141         if (mat.find()) {
   142         if (mat.find()) {
   298 
   299 
   299         TreeDissector dis = TreeDissector.createByFirstClass(pt);
   300         TreeDissector dis = TreeDissector.createByFirstClass(pt);
   300 
   301 
   301         ClassTree klassTree = (ClassTree) unitTree;
   302         ClassTree klassTree = (ClassTree) unitTree;
   302         String name = klassTree.getSimpleName().toString();
   303         String name = klassTree.getSimpleName().toString();
       
   304         DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
       
   305         TypeDeclKey key = state.keyMap.keyForClass(name);
       
   306         // Corralling mutates.  Must be last use of pt, unitTree, klassTree
       
   307         Wrap corralled = new Corraller(key.index(), pt.getContext()).corralType(klassTree);
       
   308 
   303         Wrap guts = Wrap.classMemberWrap(compileSource);
   309         Wrap guts = Wrap.classMemberWrap(compileSource);
   304         TypeDeclKey key = state.keyMap.keyForClass(name);
   310         Snippet snip = new TypeDeclSnippet(key, userSource, guts,
   305         Wrap corralled = new Corraller(key.index(), compileSource, dis).corralType(klassTree, 1);
       
   306         Snippet snip = new TypeDeclSnippet(state.keyMap.keyForClass(name), userSource, guts,
       
   307                 name, snippetKind,
   311                 name, snippetKind,
   308                 corralled, tds.declareReferences(), tds.bodyReferences());
   312                 corralled, tds.declareReferences(), tds.bodyReferences());
   309         DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
       
   310         return declare(snip, modDiag);
   313         return declare(snip, modDiag);
   311     }
   314     }
   312 
   315 
   313     private List<SnippetEvent> processStatement(String userSource, String compileSource) {
   316     private List<SnippetEvent> processStatement(String userSource, String compileSource) {
   314         Wrap guts = Wrap.methodWrap(compileSource);
   317         Wrap guts = Wrap.methodWrap(compileSource);
   352     }
   355     }
   353 
   356 
   354     private List<SnippetEvent> processMethod(String userSource, Tree unitTree, String compileSource, ParseTask pt) {
   357     private List<SnippetEvent> processMethod(String userSource, Tree unitTree, String compileSource, ParseTask pt) {
   355         TreeDependencyScanner tds = new TreeDependencyScanner();
   358         TreeDependencyScanner tds = new TreeDependencyScanner();
   356         tds.scan(unitTree);
   359         tds.scan(unitTree);
       
   360         TreeDissector dis = TreeDissector.createByFirstClass(pt);
   357 
   361 
   358         MethodTree mt = (MethodTree) unitTree;
   362         MethodTree mt = (MethodTree) unitTree;
   359         TreeDissector dis = TreeDissector.createByFirstClass(pt);
       
   360         DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
       
   361         if (modDiag.hasErrors()) {
       
   362             return compileFailResult(modDiag, userSource);
       
   363         }
       
   364         String unitName = mt.getName().toString();
       
   365         Wrap guts = Wrap.classMemberWrap(compileSource);
       
   366 
       
   367         Range typeRange = dis.treeToRange(mt.getReturnType());
       
   368         String name = mt.getName().toString();
   363         String name = mt.getName().toString();
   369 
       
   370         String parameterTypes
   364         String parameterTypes
   371                 = mt.getParameters()
   365                 = mt.getParameters()
   372                 .stream()
   366                 .stream()
   373                 .map(param -> dis.treeToRange(param.getType()).part(compileSource))
   367                 .map(param -> dis.treeToRange(param.getType()).part(compileSource))
   374                 .collect(Collectors.joining(","));
   368                 .collect(Collectors.joining(","));
       
   369         Tree returnType = mt.getReturnType();
       
   370         DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
       
   371         MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
       
   372         // Corralling mutates.  Must be last use of pt, unitTree, mt
       
   373         Wrap corralled = new Corraller(key.index(), pt.getContext()).corralMethod(mt);
       
   374 
       
   375         if (modDiag.hasErrors()) {
       
   376             return compileFailResult(modDiag, userSource);
       
   377         }
       
   378         Wrap guts = Wrap.classMemberWrap(compileSource);
       
   379         Range typeRange = dis.treeToRange(returnType);
   375         String signature = "(" + parameterTypes + ")" + typeRange.part(compileSource);
   380         String signature = "(" + parameterTypes + ")" + typeRange.part(compileSource);
   376 
   381 
   377         MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
       
   378         // rewrap with correct Key index
       
   379         Wrap corralled = new Corraller(key.index(), compileSource, dis).corralMethod(mt);
       
   380         Snippet snip = new MethodSnippet(key, userSource, guts,
   382         Snippet snip = new MethodSnippet(key, userSource, guts,
   381                 unitName, signature,
   383                 name, signature,
   382                 corralled, tds.declareReferences(), tds.bodyReferences());
   384                 corralled, tds.declareReferences(), tds.bodyReferences());
   383         return declare(snip, modDiag);
   385         return declare(snip, modDiag);
   384     }
   386     }
   385 
   387 
   386     /**
   388     /**