src/jdk.jshell/share/classes/jdk/jshell/Eval.java
changeset 55397 5eeee2cc94f5
parent 55156 1c291d1c272c
child 58679 9c3209ff7550
equal deleted inserted replaced
55396:49ed5e31fe1e 55397:5eeee2cc94f5
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    42 import com.sun.source.tree.MethodTree;
    42 import com.sun.source.tree.MethodTree;
    43 import com.sun.source.tree.ModifiersTree;
    43 import com.sun.source.tree.ModifiersTree;
    44 import com.sun.source.tree.NewClassTree;
    44 import com.sun.source.tree.NewClassTree;
    45 import com.sun.source.tree.Tree;
    45 import com.sun.source.tree.Tree;
    46 import com.sun.source.tree.VariableTree;
    46 import com.sun.source.tree.VariableTree;
    47 import com.sun.source.util.TreeScanner;
       
    48 import com.sun.tools.javac.tree.JCTree;
    47 import com.sun.tools.javac.tree.JCTree;
    49 import com.sun.tools.javac.tree.Pretty;
    48 import com.sun.tools.javac.tree.Pretty;
    50 import java.io.IOException;
    49 import java.io.IOException;
    51 import java.io.StringWriter;
    50 import java.io.StringWriter;
    52 import java.io.Writer;
    51 import java.io.Writer;
   620                     }
   619                     }
   621                     while (name == null || state.keyMap.doesVariableNameExist(name)) {
   620                     while (name == null || state.keyMap.doesVariableNameExist(name)) {
   622                         name = "$" + ++varNumber;
   621                         name = "$" + ++varNumber;
   623                     }
   622                     }
   624                 }
   623                 }
   625                 TreeDissector dis = TreeDissector.createByFirstClass(pt);
       
   626                 ExpressionInfo varEI =
   624                 ExpressionInfo varEI =
   627                         ExpressionToTypeInfo.localVariableTypeForInitializer(compileSource, state, true);
   625                         ExpressionToTypeInfo.localVariableTypeForInitializer(compileSource, state, true);
   628                 String declareTypeName;
   626                 String declareTypeName;
   629                 String fullTypeName;
   627                 String fullTypeName;
   630                 String displayTypeName;
   628                 String displayTypeName;
   632                 if (varEI != null) {
   630                 if (varEI != null) {
   633                     declareTypeName = varEI.declareTypeName;
   631                     declareTypeName = varEI.declareTypeName;
   634                     fullTypeName = varEI.fullTypeName;
   632                     fullTypeName = varEI.fullTypeName;
   635                     displayTypeName = varEI.displayTypeName;
   633                     displayTypeName = varEI.displayTypeName;
   636 
   634 
       
   635                     TreeDissector dis = TreeDissector.createByFirstClass(pt);
   637                     Pair<Wrap, Wrap> anonymous2Member =
   636                     Pair<Wrap, Wrap> anonymous2Member =
   638                             anonymous2Member(varEI, compileSource, new Range(0, compileSource.length()), dis, expr.getExpression());
   637                             anonymous2Member(varEI, compileSource, new Range(0, compileSource.length()), dis, expr.getExpression());
   639                     guts = Wrap.tempVarWrap(anonymous2Member.second.wrapped(), declareTypeName, name, anonymous2Member.first);
   638                     guts = Wrap.tempVarWrap(anonymous2Member.second.wrapped(), declareTypeName, name, anonymous2Member.first);
   640                     anonymousClasses = varEI.anonymousClasses.stream().map(ad -> ad.declareTypeName).collect(Collectors.toSet());
   639                     anonymousClasses = varEI.anonymousClasses.stream().map(ad -> ad.declareTypeName).collect(Collectors.toSet());
   641                 } else {
   640                 } else {
   678 
   677 
   679         ClassTree klassTree = (ClassTree) unitTree;
   678         ClassTree klassTree = (ClassTree) unitTree;
   680         String name = klassTree.getSimpleName().toString();
   679         String name = klassTree.getSimpleName().toString();
   681         DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
   680         DiagList modDiag = modifierDiagnostics(klassTree.getModifiers(), dis, false);
   682         TypeDeclKey key = state.keyMap.keyForClass(name);
   681         TypeDeclKey key = state.keyMap.keyForClass(name);
   683         // Corralling mutates.  Must be last use of pt, unitTree, klassTree
   682         // Corralling
   684         Wrap corralled = new Corraller(key.index(), pt.getContext()).corralType(klassTree);
   683         Wrap corralled = new Corraller(dis, key.index(), compileSource).corralType(klassTree);
   685 
   684 
   686         Wrap guts = Wrap.classMemberWrap(compileSource);
   685         Wrap guts = Wrap.classMemberWrap(compileSource);
   687         Snippet snip = new TypeDeclSnippet(key, userSource, guts,
   686         Snippet snip = new TypeDeclSnippet(key, userSource, guts,
   688                 name, snippetKind,
   687                 name, snippetKind,
   689                 corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
   688                 corralled, tds.declareReferences(), tds.bodyReferences(), modDiag);
   750                 .map(param -> dis.treeToRange(param.getType()).part(compileSource))
   749                 .map(param -> dis.treeToRange(param.getType()).part(compileSource))
   751                 .collect(Collectors.joining(","));
   750                 .collect(Collectors.joining(","));
   752         Tree returnType = mt.getReturnType();
   751         Tree returnType = mt.getReturnType();
   753         DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
   752         DiagList modDiag = modifierDiagnostics(mt.getModifiers(), dis, true);
   754         MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
   753         MethodKey key = state.keyMap.keyForMethod(name, parameterTypes);
   755         // Corralling mutates.  Must be last use of pt, unitTree, mt
   754         // Corralling
   756         Wrap corralled = new Corraller(key.index(), pt.getContext()).corralMethod(mt);
   755         Wrap corralled = new Corraller(dis, key.index(), compileSource).corralMethod(mt);
   757 
   756 
   758         if (modDiag.hasErrors()) {
   757         if (modDiag.hasErrors()) {
   759             return compileFailResult(modDiag, userSource, Kind.METHOD);
   758             return compileFailResult(modDiag, userSource, Kind.METHOD);
   760         }
   759         }
   761         Wrap guts = Wrap.classMemberWrap(compileSource);
   760         Wrap guts = Wrap.classMemberWrap(compileSource);