langtools/src/jdk.jshell/share/classes/jdk/jshell/SourceCodeAnalysisImpl.java
author rfield
Wed, 16 Aug 2017 18:42:11 -0700
changeset 46185 f4c981fc7818
parent 45748 0202b55d8e08
permissions -rw-r--r--
8182270: JShell API: Tools need snippet information without evaluating snippet 8166334: jshell tool: shortcut: expression/statement to method Reviewed-by: jlahoda
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     1
/*
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37939
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     4
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    10
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    15
 * accompanied this code).
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    16
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    20
 *
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    23
 * questions.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    24
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    25
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    26
package jdk.jshell;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    27
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    28
import jdk.jshell.SourceCodeAnalysis.Completeness;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    29
import com.sun.source.tree.AssignmentTree;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
    30
import com.sun.source.tree.ClassTree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    31
import com.sun.source.tree.CompilationUnitTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    32
import com.sun.source.tree.ErroneousTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    33
import com.sun.source.tree.ExpressionTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    34
import com.sun.source.tree.IdentifierTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    35
import com.sun.source.tree.ImportTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    36
import com.sun.source.tree.MemberSelectTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    37
import com.sun.source.tree.MethodInvocationTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    38
import com.sun.source.tree.MethodTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    39
import com.sun.source.tree.NewClassTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    40
import com.sun.source.tree.Scope;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    41
import com.sun.source.tree.Tree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    42
import com.sun.source.tree.Tree.Kind;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
    43
import com.sun.source.tree.TypeParameterTree;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    44
import com.sun.source.tree.VariableTree;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    45
import com.sun.source.util.SourcePositions;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    46
import com.sun.source.util.TreePath;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    47
import com.sun.source.util.TreePathScanner;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    48
import com.sun.tools.javac.api.JavacScope;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    49
import com.sun.tools.javac.code.Flags;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    50
import com.sun.tools.javac.code.Symbol.CompletionFailure;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    51
import com.sun.tools.javac.code.Symbol.VarSymbol;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    52
import com.sun.tools.javac.code.Symtab;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    53
import com.sun.tools.javac.code.Type;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    54
import com.sun.tools.javac.code.Type.ClassType;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
    55
import jdk.internal.shellsupport.doc.JavadocHelper;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    56
import com.sun.tools.javac.util.Name;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    57
import com.sun.tools.javac.util.Names;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    58
import com.sun.tools.javac.util.Pair;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    59
import jdk.jshell.CompletenessAnalyzer.CaInfo;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    60
import jdk.jshell.TaskFactory.AnalyzeTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    61
import jdk.jshell.TaskFactory.ParseTask;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    62
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    63
import java.util.ArrayList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    64
import java.util.Collections;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    65
import java.util.Iterator;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    66
import java.util.List;
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
    67
import java.util.Objects;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    68
import java.util.function.Predicate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    69
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    70
import javax.lang.model.element.Element;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    71
import javax.lang.model.element.ElementKind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    72
import javax.lang.model.element.Modifier;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    73
import javax.lang.model.element.TypeElement;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    74
import javax.lang.model.type.DeclaredType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    75
import javax.lang.model.type.TypeMirror;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    76
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    77
import static jdk.internal.jshell.debug.InternalDebugControl.DBG_COMPA;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    78
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    79
import java.io.IOException;
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    80
import java.net.URI;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    81
import java.nio.file.DirectoryStream;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    82
import java.nio.file.FileSystem;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    83
import java.nio.file.FileSystems;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    84
import java.nio.file.FileVisitResult;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    85
import java.nio.file.FileVisitor;
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    86
import java.nio.file.Files;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    87
import java.nio.file.Path;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
    88
import java.nio.file.Paths;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    89
import java.nio.file.attribute.BasicFileAttributes;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    90
import java.util.Arrays;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    91
import java.util.Collection;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    92
import java.util.Comparator;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
    93
import java.util.EnumSet;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    94
import java.util.HashMap;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    95
import java.util.HashSet;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    96
import java.util.LinkedHashSet;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
    97
import java.util.Map;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    98
import java.util.NoSuchElementException;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
    99
import java.util.Set;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   100
import java.util.concurrent.ExecutorService;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   101
import java.util.concurrent.Executors;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   102
import java.util.function.Function;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   103
import java.util.regex.Matcher;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   104
import java.util.regex.Pattern;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   105
import java.util.stream.Collectors;
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 42258
diff changeset
   106
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   107
import static java.util.stream.Collectors.collectingAndThen;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   108
import static java.util.stream.Collectors.toCollection;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   109
import static java.util.stream.Collectors.toList;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   110
import static java.util.stream.Collectors.toSet;
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 42258
diff changeset
   111
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   112
import java.util.stream.Stream;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   113
import java.util.stream.StreamSupport;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   114
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   115
import javax.lang.model.SourceVersion;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   116
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   117
import javax.lang.model.element.ExecutableElement;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   118
import javax.lang.model.element.PackageElement;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   119
import javax.lang.model.element.QualifiedNameable;
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
   120
import javax.lang.model.element.TypeParameterElement;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   121
import javax.lang.model.element.VariableElement;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   122
import javax.lang.model.type.ArrayType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   123
import javax.lang.model.type.ExecutableType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   124
import javax.lang.model.type.TypeKind;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   125
import javax.lang.model.util.ElementFilter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   126
import javax.lang.model.util.Types;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   127
import javax.tools.JavaFileManager.Location;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   128
import javax.tools.StandardLocation;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   129
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42836
diff changeset
   130
import jdk.jshell.ExpressionToTypeInfo.ExpressionInfo;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   131
import static jdk.jshell.Util.REPL_DOESNOTMATTER_CLASS_NAME;
38835
37280d52d723 8131024: JShell: multi-line comment not detected as incomplete
rfield
parents: 38535
diff changeset
   132
import static jdk.jshell.SourceCodeAnalysis.Completeness.DEFINITELY_INCOMPLETE;
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
   133
import static jdk.jshell.TreeDissector.printType;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   134
42261
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 42258
diff changeset
   135
import static java.util.stream.Collectors.joining;
bb52b5514ad5 8163190: Clarify JavaFileManager use of \"module location\"
jjg
parents: 42258
diff changeset
   136
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   137
/**
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   138
 * The concrete implementation of SourceCodeAnalysis.
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   139
 * @author Robert Field
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   140
 */
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   141
class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   142
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   143
    private static final Map<Path, ClassIndex> PATH_TO_INDEX = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   144
    private static final ExecutorService INDEXER = Executors.newFixedThreadPool(1, r -> {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   145
        Thread t = new Thread(r);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   146
        t.setDaemon(true);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   147
        t.setUncaughtExceptionHandler((thread, ex) -> ex.printStackTrace());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   148
        return t;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   149
    });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   150
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   151
    private final JShell proc;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   152
    private final CompletenessAnalyzer ca;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
   153
    private final List<AutoCloseable> closeables = new ArrayList<>();
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   154
    private final Map<Path, ClassIndex> currentIndexes = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   155
    private int indexVersion;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   156
    private int classpathVersion;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   157
    private final Object suspendLock = new Object();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   158
    private int suspend;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   159
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   160
    SourceCodeAnalysisImpl(JShell proc) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   161
        this.proc = proc;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   162
        this.ca = new CompletenessAnalyzer(proc);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   163
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   164
        int cpVersion = classpathVersion = 1;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   165
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   166
        INDEXER.submit(() -> refreshIndexes(cpVersion));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   167
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   168
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   169
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   170
    public CompletionInfo analyzeCompletion(String srcInput) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   171
        MaskCommentsAndModifiers mcm = new MaskCommentsAndModifiers(srcInput, false);
38835
37280d52d723 8131024: JShell: multi-line comment not detected as incomplete
rfield
parents: 38535
diff changeset
   172
        if (mcm.endsWithOpenComment()) {
37280d52d723 8131024: JShell: multi-line comment not detected as incomplete
rfield
parents: 38535
diff changeset
   173
            proc.debug(DBG_COMPA, "Incomplete (open comment): %s\n", srcInput);
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   174
            return new CompletionInfoImpl(DEFINITELY_INCOMPLETE, null, srcInput + '\n');
38835
37280d52d723 8131024: JShell: multi-line comment not detected as incomplete
rfield
parents: 38535
diff changeset
   175
        }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   176
        String cleared = mcm.cleared();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   177
        String trimmedInput = Util.trimEnd(cleared);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   178
        if (trimmedInput.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   179
            // Just comment or empty
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   180
            return new CompletionInfoImpl(Completeness.EMPTY, srcInput, "");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   181
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   182
        CaInfo info = ca.scan(trimmedInput);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   183
        Completeness status = info.status;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   184
        int unitEndPos = info.unitEndPos;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   185
        if (unitEndPos > srcInput.length()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   186
            unitEndPos = srcInput.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   187
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   188
        int nonCommentNonWhiteLength = trimmedInput.length();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   189
        String src = srcInput.substring(0, unitEndPos);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   190
        switch (status) {
45748
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   191
            case COMPLETE: {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   192
                if (unitEndPos == nonCommentNonWhiteLength) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   193
                    // The unit is the whole non-coment/white input plus semicolon
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   194
                    String compileSource = src
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   195
                            + mcm.mask().substring(nonCommentNonWhiteLength);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   196
                    proc.debug(DBG_COMPA, "Complete: %s\n", compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   197
                    proc.debug(DBG_COMPA, "   nothing remains.\n");
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   198
                    return new CompletionInfoImpl(status, compileSource, "");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   199
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   200
                    String remain = srcInput.substring(unitEndPos);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   201
                    proc.debug(DBG_COMPA, "Complete: %s\n", src);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   202
                    proc.debug(DBG_COMPA, "          remaining: %s\n", remain);
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   203
                    return new CompletionInfoImpl(status, src, remain);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   204
                }
45748
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   205
            }
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   206
            case COMPLETE_WITH_SEMI: {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   207
                // The unit is the whole non-coment/white input plus semicolon
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   208
                String compileSource = src
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   209
                        + ";"
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   210
                        + mcm.mask().substring(nonCommentNonWhiteLength);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   211
                proc.debug(DBG_COMPA, "Complete with semi: %s\n", compileSource);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   212
                proc.debug(DBG_COMPA, "   nothing remains.\n");
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   213
                return new CompletionInfoImpl(status, compileSource, "");
45748
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   214
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   215
            case DEFINITELY_INCOMPLETE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   216
                proc.debug(DBG_COMPA, "Incomplete: %s\n", srcInput);
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   217
                return new CompletionInfoImpl(status, null, srcInput + '\n');
45748
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   218
            case CONSIDERED_INCOMPLETE: {
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   219
                // Since the source is potentually valid, construct the complete source
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   220
                String compileSource = src
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   221
                        + ";"
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   222
                        + mcm.mask().substring(nonCommentNonWhiteLength);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   223
                proc.debug(DBG_COMPA, "Considered incomplete: %s\n", srcInput);
45748
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   224
                return new CompletionInfoImpl(status, compileSource, srcInput + '\n');
0202b55d8e08 8182268: JShell: CompletionInfo.source() for CONSIDERED_INCOMPLETE missing semicolon
rfield
parents: 45747
diff changeset
   225
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   226
            case EMPTY:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   227
                proc.debug(DBG_COMPA, "Detected empty: %s\n", srcInput);
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   228
                return new CompletionInfoImpl(status, srcInput, "");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   229
            case UNKNOWN:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   230
                proc.debug(DBG_COMPA, "Detected error: %s\n", srcInput);
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   231
                return new CompletionInfoImpl(status, srcInput, "");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   232
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   233
        throw new InternalError();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   234
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   235
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   236
    private Tree.Kind guessKind(String code) {
43586
cc7a4eb79b29 8173848: JShell: less-than causes: reached end of file while parsing
rfield
parents: 43134
diff changeset
   237
        ParseTask pt = proc.taskFactory.parse(code);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   238
        List<? extends Tree> units = pt.units();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   239
        if (units.isEmpty()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   240
            return Tree.Kind.BLOCK;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   241
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   242
        Tree unitTree = units.get(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   243
        proc.debug(DBG_COMPA, "Kind: %s -- %s\n", unitTree.getKind(), unitTree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   244
        return unitTree.getKind();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   245
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   246
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   247
    //TODO: would be better handled through a lexer:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   248
    private final Pattern JAVA_IDENTIFIER = Pattern.compile("\\p{javaJavaIdentifierStart}\\p{javaJavaIdentifierPart}*");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   249
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   250
    @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   251
    public List<Suggestion> completionSuggestions(String code, int cursor, int[] anchor) {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   252
        suspendIndexing();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   253
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   254
            return completionSuggestionsImpl(code, cursor, anchor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   255
        } finally {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   256
            resumeIndexing();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   257
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   258
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   259
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   260
    private List<Suggestion> completionSuggestionsImpl(String code, int cursor, int[] anchor) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   261
        code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   262
        Matcher m = JAVA_IDENTIFIER.matcher(code);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   263
        String identifier = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   264
        while (m.find()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   265
            if (m.end() == code.length()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   266
                cursor = m.start();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   267
                code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   268
                identifier = m.group();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   269
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   270
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   271
        code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   272
        if (code.trim().isEmpty()) { //TODO: comment handling
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   273
            code += ";";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   274
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   275
        OuterWrap codeWrap;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   276
        switch (guessKind(code)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   277
            case IMPORT:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
   278
                codeWrap = proc.outerMap.wrapImport(Wrap.simpleWrap(code + "any.any"), null);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   279
                break;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   280
            case CLASS:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   281
            case METHOD:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
   282
                codeWrap = proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   283
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   284
            default:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
   285
                codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   286
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   287
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   288
        String requiredPrefix = identifier;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   289
        return computeSuggestions(codeWrap, cursor, anchor).stream()
38908
f0c186d76c8a 8139829: JShell API: No use of fields to return information from public types
rfield
parents: 38835
diff changeset
   290
                .filter(s -> s.continuation().startsWith(requiredPrefix) && !s.continuation().equals(REPL_DOESNOTMATTER_CLASS_NAME))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
   291
                .sorted(Comparator.comparing(Suggestion::continuation))
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   292
                .collect(collectingAndThen(toList(), Collections::unmodifiableList));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   293
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   294
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   295
    private List<Suggestion> computeSuggestions(OuterWrap code, int cursor, int[] anchor) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   296
        AnalyzeTask at = proc.taskFactory.new AnalyzeTask(code);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   297
        SourcePositions sp = at.trees().getSourcePositions();
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 33715
diff changeset
   298
        CompilationUnitTree topLevel = at.firstCuTree();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   299
        List<Suggestion> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   300
        TreePath tp = pathFor(topLevel, sp, code.snippetIndexToWrapIndex(cursor));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   301
        if (tp != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   302
            Scope scope = at.trees().getScope(tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   303
            Predicate<Element> accessibility = createAccessibilityFilter(at, tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   304
            Predicate<Element> smartTypeFilter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   305
            Predicate<Element> smartFilter;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   306
            Iterable<TypeMirror> targetTypes = findTargetType(at, tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   307
            if (targetTypes != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   308
                smartTypeFilter = el -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   309
                    TypeMirror resultOf = resultTypeOf(el);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   310
                    return Util.stream(targetTypes)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   311
                            .anyMatch(targetType -> at.getTypes().isAssignable(resultOf, targetType));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   312
                };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   313
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   314
                smartFilter = IS_CLASS.negate()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   315
                                      .and(IS_INTERFACE.negate())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   316
                                      .and(IS_PACKAGE.negate())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   317
                                      .and(smartTypeFilter);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   318
            } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   319
                smartFilter = TRUE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   320
                smartTypeFilter = TRUE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   321
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   322
            switch (tp.getLeaf().getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   323
                case MEMBER_SELECT: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   324
                    MemberSelectTree mst = (MemberSelectTree)tp.getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   325
                    if (mst.getIdentifier().contentEquals("*"))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   326
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   327
                    TreePath exprPath = new TreePath(tp, mst.getExpression());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   328
                    TypeMirror site = at.trees().getTypeMirror(exprPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   329
                    boolean staticOnly = isStaticContext(at, exprPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   330
                    ImportTree it = findImport(tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   331
                    boolean isImport = it != null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   332
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   333
                    List<? extends Element> members = membersOf(at, site, staticOnly && !isImport);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   334
                    Predicate<Element> filter = accessibility;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   335
                    Function<Boolean, String> paren = DEFAULT_PAREN;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   336
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   337
                    if (isNewClass(tp)) { // new xxx.|
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   338
                        Predicate<Element> constructorFilter = accessibility.and(IS_CONSTRUCTOR)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   339
                            .and(el -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   340
                                if (el.getEnclosingElement().getEnclosingElement().getKind() == ElementKind.CLASS) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   341
                                    return el.getEnclosingElement().getModifiers().contains(Modifier.STATIC);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   342
                                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   343
                                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   344
                            });
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   345
                        addElements(membersOf(at, members), constructorFilter, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   346
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   347
                        filter = filter.and(IS_PACKAGE);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   348
                    } else if (isThrowsClause(tp)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   349
                        staticOnly = true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   350
                        filter = filter.and(IS_PACKAGE.or(IS_CLASS).or(IS_INTERFACE));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   351
                        smartFilter = IS_PACKAGE.negate().and(smartTypeFilter);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   352
                    } else if (isImport) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   353
                        paren = NO_PAREN;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   354
                        if (!it.isStatic()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   355
                            filter = filter.and(IS_PACKAGE.or(IS_CLASS).or(IS_INTERFACE));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   356
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   357
                    } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   358
                        filter = filter.and(IS_CONSTRUCTOR.negate());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   359
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   360
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   361
                    filter = filter.and(staticOnly ? STATIC_ONLY : INSTANCE_ONLY);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   363
                    addElements(members, filter, smartFilter, paren, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   364
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   365
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   366
                case IDENTIFIER:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   367
                    if (isNewClass(tp)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   368
                        Function<Element, Iterable<? extends Element>> listEnclosed =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   369
                                el -> el.getKind() == ElementKind.PACKAGE ? Collections.singletonList(el)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   370
                                                                          : el.getEnclosedElements();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   371
                        Predicate<Element> filter = accessibility.and(IS_CONSTRUCTOR.or(IS_PACKAGE));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   372
                        NewClassTree newClassTree = (NewClassTree)tp.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   373
                        ExpressionTree enclosingExpression = newClassTree.getEnclosingExpression();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   374
                        if (enclosingExpression != null) { // expr.new IDENT|
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   375
                            TypeMirror site = at.trees().getTypeMirror(new TreePath(tp, enclosingExpression));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   376
                            filter = filter.and(el -> el.getEnclosingElement().getKind() == ElementKind.CLASS && !el.getEnclosingElement().getModifiers().contains(Modifier.STATIC));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   377
                            addElements(membersOf(at, membersOf(at, site, false)), filter, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   378
                        } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   379
                            addScopeElements(at, scope, listEnclosed, filter, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   380
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   381
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   382
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   383
                    if (isThrowsClause(tp)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   384
                        Predicate<Element> accept = accessibility.and(STATIC_ONLY)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   385
                                .and(IS_PACKAGE.or(IS_CLASS).or(IS_INTERFACE));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   386
                        addScopeElements(at, scope, IDENTITY, accept, IS_PACKAGE.negate().and(smartTypeFilter), result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   387
                        break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   388
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   389
                    ImportTree it = findImport(tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   390
                    if (it != null) {
44061
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   391
                        // the context of the identifier is an import, look for
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   392
                        // package names that start with the identifier.
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   393
                        // If and when Java allows imports from the default
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   394
                        // package to the the default package which would allow
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   395
                        // JShell to change to use the default package, and that
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   396
                        // change is done, then this should use some variation
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   397
                        // of membersOf(at, at.getElements().getPackageElement("").asType(), false)
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   398
                        addElements(listPackages(at, ""),
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   399
                                it.isStatic()
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   400
                                        ? STATIC_ONLY.and(accessibility)
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   401
                                        : accessibility,
d9ddf704d193 8175886: JShell: crash on tab-complete with NPE.
rfield
parents: 43586
diff changeset
   402
                                smartFilter, result);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   403
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   404
                    break;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   405
                case CLASS: {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   406
                    Predicate<Element> accept = accessibility.and(IS_TYPE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   407
                    addScopeElements(at, scope, IDENTITY, accept, smartFilter, result);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   408
                    addElements(primitivesOrVoid(at), TRUE, smartFilter, result);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   409
                    break;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   410
                }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   411
                case BLOCK:
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   412
                case EMPTY_STATEMENT:
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   413
                case ERRONEOUS: {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   414
                    boolean staticOnly = ReplResolve.isStatic(((JavacScope)scope).getEnv());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   415
                    Predicate<Element> accept = accessibility.and(staticOnly ? STATIC_ONLY : TRUE);
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   416
                    if (isClass(tp)) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   417
                        ClassTree clazz = (ClassTree) tp.getParentPath().getLeaf();
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   418
                        if (clazz.getExtendsClause() == tp.getLeaf()) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   419
                            accept = accept.and(IS_TYPE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   420
                            smartFilter = smartFilter.and(el -> el.getKind() == ElementKind.CLASS);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   421
                        } else {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   422
                            Predicate<Element> f = smartFilterFromList(at, tp, clazz.getImplementsClause(), tp.getLeaf());
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   423
                            if (f != null) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   424
                                accept = accept.and(IS_TYPE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   425
                                smartFilter = f.and(el -> el.getKind() == ElementKind.INTERFACE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   426
                            }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   427
                        }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   428
                    } else if (isTypeParameter(tp)) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   429
                        TypeParameterTree tpt = (TypeParameterTree) tp.getParentPath().getLeaf();
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   430
                        Predicate<Element> f = smartFilterFromList(at, tp, tpt.getBounds(), tp.getLeaf());
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   431
                        if (f != null) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   432
                            accept = accept.and(IS_TYPE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   433
                            smartFilter = f;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   434
                            if (!tpt.getBounds().isEmpty() && tpt.getBounds().get(0) != tp.getLeaf()) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   435
                                smartFilter = smartFilter.and(el -> el.getKind() == ElementKind.INTERFACE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   436
                            }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   437
                        }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   438
                    } else if (isVariable(tp)) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   439
                        VariableTree var = (VariableTree) tp.getParentPath().getLeaf();
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   440
                        if (var.getType() == tp.getLeaf()) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   441
                            accept = accept.and(IS_TYPE);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   442
                        }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   443
                    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   444
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   445
                    addScopeElements(at, scope, IDENTITY, accept, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   446
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   447
                    Tree parent = tp.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   448
                    switch (parent.getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   449
                        case VARIABLE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   450
                            accept = ((VariableTree)parent).getType() == tp.getLeaf() ?
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   451
                                    IS_VOID.negate() :
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   452
                                    TRUE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   453
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   454
                        case PARAMETERIZED_TYPE: // TODO: JEP 218: Generics over Primitive Types
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   455
                        case TYPE_PARAMETER:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   456
                        case CLASS:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   457
                        case INTERFACE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   458
                        case ENUM:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   459
                            accept = FALSE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   460
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   461
                        default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   462
                            accept = TRUE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   463
                            break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   464
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   465
                    addElements(primitivesOrVoid(at), accept, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   466
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   467
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   468
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   469
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   470
        anchor[0] = cursor;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   471
        return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   472
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   473
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   474
    private static final Set<Kind> CLASS_KINDS = EnumSet.of(
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   475
            Kind.ANNOTATION_TYPE, Kind.CLASS, Kind.ENUM, Kind.INTERFACE
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   476
    );
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   477
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   478
    private Predicate<Element> smartFilterFromList(AnalyzeTask at, TreePath base, Collection<? extends Tree> types, Tree current) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   479
        Set<Element> existingEls = new HashSet<>();
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   480
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   481
        for (Tree type : types) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   482
            if (type == current) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   483
                return el -> !existingEls.contains(el);
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   484
            }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   485
            existingEls.add(at.trees().getElement(new TreePath(base, type)));
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   486
        }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   487
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   488
        return null;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   489
    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   490
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   491
    @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   492
    public SnippetWrapper wrapper(Snippet snippet) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   493
        return new SnippetWrapper() {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   494
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   495
            public String source() {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   496
                return snippet.source();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   497
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   498
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   499
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   500
            public String wrapped() {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   501
                return snippet.outerWrap().wrapped();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   502
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   503
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   504
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   505
            public String fullClassName() {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   506
                return snippet.classFullName();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   507
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   508
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   509
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   510
            public Snippet.Kind kind() {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   511
                return snippet.kind() == Snippet.Kind.ERRONEOUS
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   512
                        ? ((ErroneousSnippet) snippet).probableKind()
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   513
                        : snippet.kind();
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   514
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   515
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   516
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   517
            public int sourceToWrappedPosition(int pos) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   518
                return snippet.outerWrap().snippetIndexToWrapIndex(pos);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   519
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   520
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   521
            @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   522
            public int wrappedToSourcePosition(int pos) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   523
                return snippet.outerWrap().wrapIndexToSnippetIndex(pos);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   524
            }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   525
        };
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   526
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   527
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   528
    @Override
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   529
    public List<SnippetWrapper> wrappers(String input) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   530
        return proc.eval.sourceToSnippetsWithWrappers(input).stream()
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
   531
                .map(this::wrapper)
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   532
                .collect(toList());
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   533
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   534
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   535
    @Override
46185
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   536
    public List<Snippet> sourceToSnippets(String input) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   537
        proc.checkIfAlive();
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   538
        List<Snippet> snl = proc.eval.toScratchSnippets(input);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   539
        for (Snippet sn : snl) {
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   540
            sn.setId(Snippet.UNASSOCIATED_ID);
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   541
        }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   542
        return snl;
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   543
    }
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   544
f4c981fc7818 8182270: JShell API: Tools need snippet information without evaluating snippet
rfield
parents: 45748
diff changeset
   545
    @Override
39370
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   546
    public Collection<Snippet> dependents(Snippet snippet) {
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   547
        return proc.maps.getDependents(snippet);
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   548
    }
437ba9bd2582 8159111: JShell API: Add access to wrappers and dependencies
rfield
parents: 38908
diff changeset
   549
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   550
    private boolean isStaticContext(AnalyzeTask at, TreePath path) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   551
        switch (path.getLeaf().getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   552
            case ARRAY_TYPE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   553
            case PRIMITIVE_TYPE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   554
                return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   555
            default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   556
                Element selectEl = at.trees().getElement(path);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   557
                return selectEl != null && (selectEl.getKind().isClass() || selectEl.getKind().isInterface() || selectEl.getKind() == ElementKind.TYPE_PARAMETER) && selectEl.asType().getKind() != TypeKind.ERROR;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   558
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   559
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   560
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   561
    private TreePath pathFor(CompilationUnitTree topLevel, SourcePositions sp, int pos) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   562
        TreePath[] deepest = new TreePath[1];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   563
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   564
        new TreePathScanner<Void, Void>() {
40606
eb2c81860c86 8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents: 40517
diff changeset
   565
            @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   566
            public Void scan(Tree tree, Void p) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   567
                if (tree == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   568
                    return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   569
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   570
                long start = sp.getStartPosition(topLevel, tree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   571
                long end = sp.getEndPosition(topLevel, tree);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   572
                long prevEnd = deepest[0] != null ? sp.getEndPosition(topLevel, deepest[0].getLeaf()) : -1;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   573
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   574
                if (start <= pos && pos <= end &&
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   575
                    (start != end || prevEnd != end || deepest[0] == null ||
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   576
                     deepest[0].getParentPath().getLeaf() != getCurrentPath().getLeaf())) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   577
                    deepest[0] = new TreePath(getCurrentPath(), tree);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   578
                    return super.scan(tree, p);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   579
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   580
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   581
                return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   582
            }
40606
eb2c81860c86 8164836: TEST_BUG: adjust scope of the DefinedByAnalyzer in tools/all/RunCodingRules.java
jlahoda
parents: 40517
diff changeset
   583
            @Override
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   584
            public Void visitErroneous(ErroneousTree node, Void p) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   585
                return scan(node.getErrorTrees(), null);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   586
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   587
        }.scan(topLevel, null);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   588
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   589
        return deepest[0];
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   590
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   591
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   592
    private boolean isNewClass(TreePath tp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   593
        return tp.getParentPath() != null &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   594
               tp.getParentPath().getLeaf().getKind() == Kind.NEW_CLASS &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   595
               ((NewClassTree) tp.getParentPath().getLeaf()).getIdentifier() == tp.getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   596
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   597
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   598
    private boolean isThrowsClause(TreePath tp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   599
        Tree parent = tp.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   600
        return parent.getKind() == Kind.METHOD &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   601
                ((MethodTree)parent).getThrows().contains(tp.getLeaf());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   602
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   603
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   604
    private boolean isClass(TreePath tp) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   605
        return tp.getParentPath() != null &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   606
               CLASS_KINDS.contains(tp.getParentPath().getLeaf().getKind());
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   607
    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   608
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   609
    private boolean isTypeParameter(TreePath tp) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   610
        return tp.getParentPath() != null &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   611
               tp.getParentPath().getLeaf().getKind() == Kind.TYPE_PARAMETER;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   612
    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   613
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   614
    private boolean isVariable(TreePath tp) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   615
        return tp.getParentPath() != null &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   616
               tp.getParentPath().getLeaf().getKind() == Kind.VARIABLE;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   617
    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   618
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   619
    private ImportTree findImport(TreePath tp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   620
        while (tp != null && tp.getLeaf().getKind() != Kind.IMPORT) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   621
            tp = tp.getParentPath();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   622
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   623
        return tp != null ? (ImportTree)tp.getLeaf() : null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   624
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   625
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   626
    private Predicate<Element> createAccessibilityFilter(AnalyzeTask at, TreePath tp) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   627
        Scope scope = at.trees().getScope(tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   628
        return el -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   629
            switch (el.getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   630
                case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   631
                    return at.trees().isAccessible(scope, (TypeElement) el);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   632
                case PACKAGE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   633
                case EXCEPTION_PARAMETER: case PARAMETER: case LOCAL_VARIABLE: case RESOURCE_VARIABLE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   634
                    return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   635
                default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   636
                    TypeMirror type = el.getEnclosingElement().asType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   637
                    if (type.getKind() == TypeKind.DECLARED)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   638
                        return at.trees().isAccessible(scope, el, (DeclaredType) type);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   639
                    else
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   640
                        return true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   641
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   642
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   643
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   644
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   645
    private final Predicate<Element> TRUE = el -> true;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   646
    private final Predicate<Element> FALSE = TRUE.negate();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   647
    private final Predicate<Element> IS_STATIC = el -> el.getModifiers().contains(Modifier.STATIC);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   648
    private final Predicate<Element> IS_CONSTRUCTOR = el -> el.getKind() == ElementKind.CONSTRUCTOR;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   649
    private final Predicate<Element> IS_METHOD = el -> el.getKind() == ElementKind.METHOD;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   650
    private final Predicate<Element> IS_PACKAGE = el -> el.getKind() == ElementKind.PACKAGE;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   651
    private final Predicate<Element> IS_CLASS = el -> el.getKind().isClass();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   652
    private final Predicate<Element> IS_INTERFACE = el -> el.getKind().isInterface();
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   653
    private final Predicate<Element> IS_TYPE = IS_CLASS.or(IS_INTERFACE).or(el -> el.getKind() == ElementKind.TYPE_PARAMETER);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   654
    private final Predicate<Element> IS_VOID = el -> el.asType().getKind() == TypeKind.VOID;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   655
    private final Predicate<Element> STATIC_ONLY = el -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   656
        ElementKind kind = el.getKind();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   657
        Element encl = el.getEnclosingElement();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   658
        ElementKind enclKind = encl != null ? encl.getKind() : ElementKind.OTHER;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   659
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   660
        return IS_STATIC.or(IS_PACKAGE).or(IS_CLASS).or(IS_INTERFACE).test(el) || IS_PACKAGE.test(encl) ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   661
                (kind == ElementKind.TYPE_PARAMETER && !enclKind.isClass() && !enclKind.isInterface());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   662
    };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   663
    private final Predicate<Element> INSTANCE_ONLY = el -> {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   664
        Element encl = el.getEnclosingElement();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   665
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   666
        return IS_STATIC.or(IS_CLASS).or(IS_INTERFACE).negate().test(el) ||
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   667
                IS_PACKAGE.test(encl);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   668
    };
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
   669
    private final Function<Element, Iterable<? extends Element>> IDENTITY = Collections::singletonList;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   670
    private final Function<Boolean, String> DEFAULT_PAREN = hasParams -> hasParams ? "(" : "()";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   671
    private final Function<Boolean, String> NO_PAREN = hasParams -> "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   672
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   673
    private void addElements(Iterable<? extends Element> elements, Predicate<Element> accept, Predicate<Element> smart, List<Suggestion> result) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   674
        addElements(elements, accept, smart, DEFAULT_PAREN, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   675
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   676
    private void addElements(Iterable<? extends Element> elements, Predicate<Element> accept, Predicate<Element> smart, Function<Boolean, String> paren, List<Suggestion> result) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   677
        Set<String> hasParams = Util.stream(elements)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   678
                .filter(accept)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   679
                .filter(IS_CONSTRUCTOR.or(IS_METHOD))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   680
                .filter(c -> !((ExecutableElement)c).getParameters().isEmpty())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   681
                .map(this::simpleName)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   682
                .collect(toSet());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   683
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   684
        for (Element c : elements) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   685
            if (!accept.test(c))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   686
                continue;
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   687
            if (c.getKind() == ElementKind.METHOD &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   688
                c.getSimpleName().contentEquals(Util.DOIT_METHOD_NAME) &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   689
                ((ExecutableElement) c).getParameters().isEmpty()) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   690
                continue;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   691
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   692
            String simpleName = simpleName(c);
45747
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   693
            switch (c.getKind()) {
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   694
                case CONSTRUCTOR:
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   695
                case METHOD:
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   696
                    // add trailing open or matched parenthesis, as approriate
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   697
                    simpleName += paren.apply(hasParams.contains(simpleName));
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   698
                    break;
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   699
                case PACKAGE:
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   700
                    // add trailing dot to package names
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   701
                    simpleName += ".";
bdf4b1b26697 8176110: JShell: completions with package results should, in most cases, be dot terminated
rfield
parents: 44459
diff changeset
   702
                    break;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   703
            }
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
   704
            result.add(new SuggestionImpl(simpleName, smart.test(c)));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   705
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   706
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   707
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   708
    private String simpleName(Element el) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   709
        return el.getKind() == ElementKind.CONSTRUCTOR ? el.getEnclosingElement().getSimpleName().toString()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   710
                                                       : el.getSimpleName().toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   711
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   712
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   713
    private List<? extends Element> membersOf(AnalyzeTask at, TypeMirror site, boolean shouldGenerateDotClassItem) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   714
        if (site  == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   715
            return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   716
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   717
        switch (site.getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   718
            case DECLARED: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   719
                TypeElement element = (TypeElement) at.getTypes().asElement(site);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   720
                List<Element> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   721
                result.addAll(at.getElements().getAllMembers(element));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   722
                if (shouldGenerateDotClassItem) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   723
                    result.add(createDotClassSymbol(at, site));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   724
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   725
                result.removeIf(el -> el.getKind() == ElementKind.STATIC_INIT);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   726
                return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   727
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   728
            case ERROR: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   729
                //try current qualified name as a package:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   730
                TypeElement typeElement = (TypeElement) at.getTypes().asElement(site);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   731
                Element enclosingElement = typeElement.getEnclosingElement();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   732
                String parentPackageName = enclosingElement instanceof QualifiedNameable ?
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   733
                    ((QualifiedNameable)enclosingElement).getQualifiedName().toString() :
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   734
                    "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   735
                Set<PackageElement> packages = listPackages(at, parentPackageName);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   736
                return packages.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   737
                               .filter(p -> p.getQualifiedName().equals(typeElement.getQualifiedName()))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   738
                               .findAny()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   739
                               .map(p -> membersOf(at, p.asType(), false))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   740
                               .orElse(Collections.emptyList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   741
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   742
            case PACKAGE: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   743
                String packageName = site.toString()/*XXX*/;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   744
                List<Element> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   745
                result.addAll(getEnclosedElements(at.getElements().getPackageElement(packageName)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   746
                result.addAll(listPackages(at, packageName));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   747
                return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   748
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   749
            case BOOLEAN: case BYTE: case SHORT: case CHAR:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   750
            case INT: case FLOAT: case LONG: case DOUBLE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   751
            case VOID: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   752
                return shouldGenerateDotClassItem ?
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   753
                    Collections.singletonList(createDotClassSymbol(at, site)) :
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   754
                    Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   755
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   756
            case ARRAY: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   757
                List<Element> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   758
                result.add(createArrayLengthSymbol(at, site));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   759
                if (shouldGenerateDotClassItem)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   760
                    result.add(createDotClassSymbol(at, site));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   761
                return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   762
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   763
            default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   764
                return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   765
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   766
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   767
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   768
    private List<? extends Element> membersOf(AnalyzeTask at, List<? extends Element> elements) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   769
        return elements.stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   770
                .flatMap(e -> membersOf(at, e.asType(), true).stream())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   771
                .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   772
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   773
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   774
    private List<? extends Element> getEnclosedElements(PackageElement packageEl) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   775
        if (packageEl == null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   776
            return Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   777
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   778
        //workaround for: JDK-8024687
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   779
        while (true) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   780
            try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   781
                return packageEl.getEnclosedElements()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   782
                                .stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   783
                                .filter(el -> el.asType() != null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   784
                                .filter(el -> el.asType().getKind() != TypeKind.ERROR)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   785
                                .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   786
            } catch (CompletionFailure cf) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   787
                //ignore...
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   788
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   789
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   790
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   791
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   792
    private List<? extends Element> primitivesOrVoid(AnalyzeTask at) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   793
        Types types = at.getTypes();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   794
        return Stream.of(
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   795
                TypeKind.BOOLEAN, TypeKind.BYTE, TypeKind.CHAR,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   796
                TypeKind.DOUBLE, TypeKind.FLOAT, TypeKind.INT,
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   797
                TypeKind.LONG, TypeKind.SHORT, TypeKind.VOID)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   798
                .map(tk -> (Type)(tk == TypeKind.VOID ? types.getNoType(tk) : types.getPrimitiveType(tk)))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   799
                .map(Type::asElement)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   800
                .collect(toList());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   801
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   802
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   803
    void classpathChanged() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   804
        synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   805
            int cpVersion = ++classpathVersion;
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   806
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   807
            INDEXER.submit(() -> refreshIndexes(cpVersion));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   808
        }
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   809
    }
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   810
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   811
    private Set<PackageElement> listPackages(AnalyzeTask at, String enclosingPackage) {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   812
        synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   813
            return currentIndexes.values()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   814
                                 .stream()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   815
                                 .flatMap(idx -> idx.packages.stream())
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   816
                                 .filter(p -> enclosingPackage.isEmpty() || p.startsWith(enclosingPackage + "."))
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   817
                                 .map(p -> {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   818
                                     int dot = p.indexOf('.', enclosingPackage.length() + 1);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   819
                                     return dot == (-1) ? p : p.substring(0, dot);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   820
                                 })
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   821
                                 .distinct()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   822
                                 .map(p -> createPackageElement(at, p))
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
   823
                                 .collect(Collectors.toSet());
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   824
        }
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   825
    }
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   826
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   827
    private PackageElement createPackageElement(AnalyzeTask at, String packageName) {
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   828
        Names names = Names.instance(at.getContext());
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   829
        Symtab syms = Symtab.instance(at.getContext());
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36160
diff changeset
   830
        PackageElement existing = syms.enterPackage(syms.unnamedModule, names.fromString(packageName));
33715
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   831
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   832
        return existing;
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   833
    }
74b1bed86932 8141092: JShell: Completion hangs on identifier completion
jlahoda
parents: 33362
diff changeset
   834
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   835
    private Element createArrayLengthSymbol(AnalyzeTask at, TypeMirror site) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   836
        Name length = Names.instance(at.getContext()).length;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   837
        Type intType = Symtab.instance(at.getContext()).intType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   838
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   839
        return new VarSymbol(Flags.PUBLIC | Flags.FINAL, length, intType, ((Type) site).tsym);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   840
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   841
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   842
    private Element createDotClassSymbol(AnalyzeTask at, TypeMirror site) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   843
        Name _class = Names.instance(at.getContext())._class;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   844
        Type classType = Symtab.instance(at.getContext()).classType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   845
        Type erasedSite = (Type)at.getTypes().erasure(site);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   846
        classType = new ClassType(classType.getEnclosingType(), com.sun.tools.javac.util.List.of(erasedSite), classType.asElement());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   847
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   848
        return new VarSymbol(Flags.PUBLIC | Flags.STATIC | Flags.FINAL, _class, classType, erasedSite.tsym);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   849
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   850
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   851
    private Iterable<? extends Element> scopeContent(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   852
        Iterable<Scope> scopeIterable = () -> new Iterator<Scope>() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   853
            private Scope currentScope = scope;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   854
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   855
            public boolean hasNext() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   856
                return currentScope != null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   857
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   858
            @Override
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   859
            public Scope next() {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   860
                if (!hasNext())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   861
                    throw new NoSuchElementException();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   862
                try {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   863
                    return currentScope;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   864
                } finally {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   865
                    currentScope = currentScope.getEnclosingScope();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   866
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   867
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   868
        };
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   869
        @SuppressWarnings("unchecked")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   870
        List<Element> result = Util.stream(scopeIterable)
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
   871
                             .flatMap(this::localElements)
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   872
                             .flatMap(el -> Util.stream((Iterable<Element>)elementConvertor.apply(el)))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   873
                             .collect(toCollection(ArrayList :: new));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   874
        result.addAll(listPackages(at, ""));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   875
        return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   876
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   877
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   878
    private Stream<Element> localElements(Scope scope) {
40837
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   879
        //workaround for: JDK-8024687
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   880
        Iterable<Element> elementsIt = () -> new Iterator<Element>() {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   881
            Iterator<? extends Element> it = scope.getLocalElements().iterator();
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   882
            @Override
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   883
            public boolean hasNext() {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   884
                while (true) {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   885
                    try {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   886
                        return it.hasNext();
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   887
                    } catch (CompletionFailure cf) {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   888
                        //ignore...
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   889
                    }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   890
                }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   891
            }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   892
            @Override
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   893
            public Element next() {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   894
                while (true) {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   895
                    try {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   896
                        return it.next();
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   897
                    } catch (CompletionFailure cf) {
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   898
                        //ignore...
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   899
                    }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   900
                }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   901
            }
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   902
        };
d071bcc8d32e 8131025: JShell: crash on tab-complete reference to bad class file
jlahoda
parents: 40606
diff changeset
   903
        Stream<Element> elements = Util.stream(elementsIt);
40318
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   904
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   905
        if (scope.getEnclosingScope() != null &&
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   906
            scope.getEnclosingClass() != scope.getEnclosingScope().getEnclosingClass()) {
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   907
            elements = Stream.concat(elements, scope.getEnclosingClass().getEnclosedElements().stream());
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   908
        }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   909
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   910
        return elements;
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   911
    }
3f7eb1205cee 8129421: JShell: unacceptable suggestions in 'extends', 'implements' in smart completion
jlahoda
parents: 39587
diff changeset
   912
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   913
    @SuppressWarnings("fallthrough")
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   914
    private Iterable<TypeMirror> findTargetType(AnalyzeTask at, TreePath forPath) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   915
        if (forPath.getParentPath() == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   916
            return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   917
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   918
        Tree current = forPath.getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   919
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   920
        switch (forPath.getParentPath().getLeaf().getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   921
            case ASSIGNMENT: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   922
                AssignmentTree tree = (AssignmentTree) forPath.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   923
                if (tree.getExpression() == current)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   924
                    return Collections.singletonList(at.trees().getTypeMirror(new TreePath(forPath.getParentPath(), tree.getVariable())));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   925
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   926
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   927
            case VARIABLE: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   928
                VariableTree tree = (VariableTree) forPath.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   929
                if (tree.getInitializer()== current)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   930
                    return Collections.singletonList(at.trees().getTypeMirror(forPath.getParentPath()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   931
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   932
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   933
            case ERRONEOUS:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   934
                return findTargetType(at, forPath.getParentPath());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   935
            case NEW_CLASS: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   936
                NewClassTree nct = (NewClassTree) forPath.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   937
                List<TypeMirror> actuals = computeActualInvocationTypes(at, nct.getArguments(), forPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   938
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   939
                if (actuals != null) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   940
                    Iterable<Pair<ExecutableElement, ExecutableType>> candidateConstructors = newClassCandidates(at, forPath.getParentPath());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   941
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   942
                    return computeSmartTypesForExecutableType(at, candidateConstructors, actuals);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   943
                } else {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   944
                    return findTargetType(at, forPath.getParentPath());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   945
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   946
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   947
            case METHOD:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   948
                if (!isThrowsClause(forPath)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   949
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   950
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   951
                // fall through
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   952
            case THROW:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   953
                return Collections.singletonList(at.getElements().getTypeElement("java.lang.Throwable").asType());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   954
            case METHOD_INVOCATION: {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   955
                MethodInvocationTree mit = (MethodInvocationTree) forPath.getParentPath().getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   956
                List<TypeMirror> actuals = computeActualInvocationTypes(at, mit.getArguments(), forPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   957
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   958
                if (actuals == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   959
                    return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   960
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   961
                Iterable<Pair<ExecutableElement, ExecutableType>> candidateMethods = methodCandidates(at, forPath.getParentPath());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   962
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   963
                return computeSmartTypesForExecutableType(at, candidateMethods, actuals);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   964
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   965
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   966
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   967
        return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   968
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   969
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   970
    private List<TypeMirror> computeActualInvocationTypes(AnalyzeTask at, List<? extends ExpressionTree> arguments, TreePath currentArgument) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   971
        if (currentArgument == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   972
            return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   973
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   974
        int paramIndex = arguments.indexOf(currentArgument.getLeaf());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   975
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   976
        if (paramIndex == (-1))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   977
            return null;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   978
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   979
        List<TypeMirror> actuals = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   980
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   981
        for (ExpressionTree arg : arguments.subList(0, paramIndex)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   982
            actuals.add(at.trees().getTypeMirror(new TreePath(currentArgument.getParentPath(), arg)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   983
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   984
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   985
        return actuals;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   986
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   987
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   988
    private List<Pair<ExecutableElement, ExecutableType>> filterExecutableTypesByArguments(AnalyzeTask at, Iterable<Pair<ExecutableElement, ExecutableType>> candidateMethods, List<TypeMirror> precedingActualTypes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   989
        List<Pair<ExecutableElement, ExecutableType>> candidate = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   990
        int paramIndex = precedingActualTypes.size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   991
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   992
        OUTER:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   993
        for (Pair<ExecutableElement, ExecutableType> method : candidateMethods) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   994
            boolean varargInvocation = paramIndex >= method.snd.getParameterTypes().size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   995
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   996
            for (int i = 0; i < paramIndex; i++) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   997
                TypeMirror actual = precedingActualTypes.get(i);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   998
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
   999
                if (this.parameterType(method.fst, method.snd, i, !varargInvocation)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1000
                        .noneMatch(formal -> at.getTypes().isAssignable(actual, formal))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1001
                    continue OUTER;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1002
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1003
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1004
            candidate.add(method);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1005
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1006
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1007
        return candidate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1008
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1009
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1010
    private Stream<TypeMirror> parameterType(ExecutableElement method, ExecutableType methodType, int paramIndex, boolean allowVarArgsArray) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1011
        int paramCount = methodType.getParameterTypes().size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1012
        if (paramIndex >= paramCount && !method.isVarArgs())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1013
            return Stream.empty();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1014
        if (paramIndex < paramCount - 1 || !method.isVarArgs())
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1015
            return Stream.of(methodType.getParameterTypes().get(paramIndex));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1016
        TypeMirror varargType = methodType.getParameterTypes().get(paramCount - 1);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1017
        TypeMirror elemenType = ((ArrayType) varargType).getComponentType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1018
        if (paramIndex >= paramCount || !allowVarArgsArray)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1019
            return Stream.of(elemenType);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1020
        return Stream.of(varargType, elemenType);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1021
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1022
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1023
    private List<TypeMirror> computeSmartTypesForExecutableType(AnalyzeTask at, Iterable<Pair<ExecutableElement, ExecutableType>> candidateMethods, List<TypeMirror> precedingActualTypes) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1024
        List<TypeMirror> candidate = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1025
        int paramIndex = precedingActualTypes.size();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1026
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1027
        this.filterExecutableTypesByArguments(at, candidateMethods, precedingActualTypes)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1028
            .stream()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1029
            .flatMap(method -> parameterType(method.fst, method.snd, paramIndex, true))
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1030
            .forEach(candidate::add);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1031
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1032
        return candidate;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1033
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1034
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1035
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1036
    private TypeMirror resultTypeOf(Element el) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1037
        //TODO: should reflect the type of site!
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1038
        switch (el.getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1039
            case METHOD:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1040
                return ((ExecutableElement) el).getReturnType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1041
            case CONSTRUCTOR:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1042
            case INSTANCE_INIT: case STATIC_INIT: //TODO: should be filtered out
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1043
                return el.getEnclosingElement().asType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1044
            default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1045
                return el.asType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1046
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1047
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1048
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1049
    private void addScopeElements(AnalyzeTask at, Scope scope, Function<Element, Iterable<? extends Element>> elementConvertor, Predicate<Element> filter, Predicate<Element> smartFilter, List<Suggestion> result) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1050
        addElements(scopeContent(at, scope, elementConvertor), filter, smartFilter, result);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1051
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1052
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1053
    private Iterable<Pair<ExecutableElement, ExecutableType>> methodCandidates(AnalyzeTask at, TreePath invocation) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1054
        MethodInvocationTree mit = (MethodInvocationTree) invocation.getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1055
        ExpressionTree select = mit.getMethodSelect();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1056
        List<Pair<ExecutableElement, ExecutableType>> result = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1057
        Predicate<Element> accessibility = createAccessibilityFilter(at, invocation);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1058
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1059
        switch (select.getKind()) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1060
            case MEMBER_SELECT:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1061
                MemberSelectTree mst = (MemberSelectTree) select;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1062
                TreePath tp = new TreePath(new TreePath(invocation, select), mst.getExpression());
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1063
                TypeMirror site = at.trees().getTypeMirror(tp);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1064
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1065
                if (site == null || site.getKind() != TypeKind.DECLARED)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1066
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1067
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1068
                Element siteEl = at.getTypes().asElement(site);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1069
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1070
                if (siteEl == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1071
                    break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1072
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1073
                if (isStaticContext(at, tp)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1074
                    accessibility = accessibility.and(STATIC_ONLY);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1075
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1076
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1077
                for (ExecutableElement ee : ElementFilter.methodsIn(membersOf(at, siteEl.asType(), false))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1078
                    if (ee.getSimpleName().contentEquals(mst.getIdentifier())) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1079
                        if (accessibility.test(ee)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1080
                            result.add(Pair.of(ee, (ExecutableType) at.getTypes().asMemberOf((DeclaredType) site, ee)));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1081
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1082
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1083
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1084
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1085
            case IDENTIFIER:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1086
                IdentifierTree it = (IdentifierTree) select;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1087
                for (ExecutableElement ee : ElementFilter.methodsIn(scopeContent(at, at.trees().getScope(invocation), IDENTITY))) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1088
                    if (ee.getSimpleName().contentEquals(it.getName())) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1089
                        if (accessibility.test(ee)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1090
                            result.add(Pair.of(ee, (ExecutableType) ee.asType())); //XXX: proper site
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1091
                        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1092
                    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1093
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1094
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1095
            default:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1096
                break;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1097
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1098
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1099
        return result;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1100
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1101
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1102
    private Iterable<Pair<ExecutableElement, ExecutableType>> newClassCandidates(AnalyzeTask at, TreePath newClassPath) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1103
        NewClassTree nct = (NewClassTree) newClassPath.getLeaf();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1104
        Element type = at.trees().getElement(new TreePath(newClassPath.getParentPath(), nct.getIdentifier()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1105
        TypeMirror targetType = at.trees().getTypeMirror(newClassPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1106
        if (targetType == null || targetType.getKind() != TypeKind.DECLARED) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1107
            Iterable<TypeMirror> targetTypes = findTargetType(at, newClassPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1108
            if (targetTypes == null)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1109
                targetTypes = Collections.emptyList();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1110
            targetType =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1111
                    StreamSupport.stream(targetTypes.spliterator(), false)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1112
                                 .filter(t -> at.getTypes().asElement(t) == type)
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1113
                                 .findAny()
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1114
                                 .orElse(at.getTypes().erasure(type.asType()));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1115
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1116
        List<Pair<ExecutableElement, ExecutableType>> candidateConstructors = new ArrayList<>();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1117
        Predicate<Element> accessibility = createAccessibilityFilter(at, newClassPath);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1118
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1119
        if (targetType != null &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1120
            targetType.getKind() == TypeKind.DECLARED &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1121
            type != null &&
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1122
            (type.getKind().isClass() || type.getKind().isInterface())) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1123
            for (ExecutableElement constr : ElementFilter.constructorsIn(type.getEnclosedElements())) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1124
                if (accessibility.test(constr)) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1125
                    ExecutableType constrType =
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1126
                            (ExecutableType) at.getTypes().asMemberOf((DeclaredType) targetType, constr);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1127
                    candidateConstructors.add(Pair.of(constr, constrType));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1128
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1129
            }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1130
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1131
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1132
        return candidateConstructors;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1133
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1134
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1135
    @Override
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1136
    public List<Documentation> documentation(String code, int cursor, boolean computeJavadoc) {
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1137
        suspendIndexing();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1138
        try {
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1139
            return documentationImpl(code, cursor, computeJavadoc);
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1140
        } finally {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1141
            resumeIndexing();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1142
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1143
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1144
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1145
    //tweaked by tests to disable reading parameter names from classfiles so that tests using
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1146
    //JDK's classes are stable for both release and fastdebug builds:
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1147
    private final String[] keepParameterNames = new String[] {
39587
76c453568ffa 8160891: javac, remove unused options, step 3
vromero
parents: 39370
diff changeset
  1148
        "-parameters"
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1149
    };
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1150
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1151
    private List<Documentation> documentationImpl(String code, int cursor, boolean computeJavadoc) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1152
        code = code.substring(0, cursor);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1153
        if (code.trim().isEmpty()) { //TODO: comment handling
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1154
            code += ";";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1155
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1156
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1157
        if (guessKind(code) == Kind.IMPORT)
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1158
            return Collections.emptyList();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1159
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
  1160
        OuterWrap codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1161
        AnalyzeTask at = proc.taskFactory.new AnalyzeTask(codeWrap, keepParameterNames);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1162
        SourcePositions sp = at.trees().getSourcePositions();
34857
14d1224cfed3 8145239: JShell: throws AssertionError when replace classes with some methods which depends on these classes
rfield
parents: 33715
diff changeset
  1163
        CompilationUnitTree topLevel = at.firstCuTree();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1164
        TreePath tp = pathFor(topLevel, sp, codeWrap.snippetIndexToWrapIndex(cursor));
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1165
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1166
        if (tp == null)
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1167
            return Collections.emptyList();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1168
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1169
        TreePath prevPath = null;
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1170
        while (tp != null && tp.getLeaf().getKind() != Kind.METHOD_INVOCATION &&
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1171
               tp.getLeaf().getKind() != Kind.NEW_CLASS && tp.getLeaf().getKind() != Kind.IDENTIFIER &&
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1172
               tp.getLeaf().getKind() != Kind.MEMBER_SELECT) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1173
            prevPath = tp;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1174
            tp = tp.getParentPath();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1175
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1176
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1177
        if (tp == null)
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1178
            return Collections.emptyList();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1179
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1180
        Stream<Element> elements;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1181
        Iterable<Pair<ExecutableElement, ExecutableType>> candidates;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1182
        List<? extends ExpressionTree> arguments;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1183
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1184
        if (tp.getLeaf().getKind() == Kind.METHOD_INVOCATION || tp.getLeaf().getKind() == Kind.NEW_CLASS) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1185
            if (tp.getLeaf().getKind() == Kind.METHOD_INVOCATION) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1186
                MethodInvocationTree mit = (MethodInvocationTree) tp.getLeaf();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1187
                candidates = methodCandidates(at, tp);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1188
                arguments = mit.getArguments();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1189
            } else {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1190
                NewClassTree nct = (NewClassTree) tp.getLeaf();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1191
                candidates = newClassCandidates(at, tp);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1192
                arguments = nct.getArguments();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1193
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1194
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1195
            if (!isEmptyArgumentsContext(arguments)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1196
                List<TypeMirror> actuals = computeActualInvocationTypes(at, arguments, prevPath);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1197
                List<TypeMirror> fullActuals = actuals != null ? actuals : Collections.emptyList();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1198
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1199
                candidates =
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1200
                        this.filterExecutableTypesByArguments(at, candidates, fullActuals)
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1201
                            .stream()
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1202
                            .filter(method -> parameterType(method.fst, method.snd, fullActuals.size(), true).findAny().isPresent())
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1203
                            .collect(Collectors.toList());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1204
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1205
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1206
            elements = Util.stream(candidates).map(method -> method.fst);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1207
        } else if (tp.getLeaf().getKind() == Kind.IDENTIFIER || tp.getLeaf().getKind() == Kind.MEMBER_SELECT) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1208
            Element el = at.trees().getElement(tp);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1209
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1210
            if (el == null ||
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1211
                el.asType().getKind() == TypeKind.ERROR ||
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1212
                (el.getKind() == ElementKind.PACKAGE && el.getEnclosedElements().isEmpty())) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1213
                //erroneous element:
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1214
                return Collections.emptyList();
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1215
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1216
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1217
            elements = Stream.of(el);
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1218
        } else {
42828
cce89649f958 8171371: Remove redundant type-arguments from generic method calls
mcimadamore
parents: 42827
diff changeset
  1219
            return Collections.emptyList();
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1220
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1221
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1222
        List<Documentation> result = Collections.emptyList();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1223
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1224
        try (JavadocHelper helper = JavadocHelper.create(at.task, findSources())) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1225
            result = elements.map(el -> constructDocumentation(at, helper, el, computeJavadoc))
42827
36468b5fa7f4 8181370: Convert anonymous inner classes into lambdas/method references
mcimadamore
parents: 42405
diff changeset
  1226
                             .filter(Objects::nonNull)
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1227
                             .collect(Collectors.toList());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1228
        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1229
            proc.debug(ex, "JavadocHelper.close()");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1230
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1231
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1232
        return result;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1233
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1234
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1235
    private Documentation constructDocumentation(AnalyzeTask at, JavadocHelper helper, Element el, boolean computeJavadoc) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1236
        String javadoc = null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1237
        try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1238
            if (hasSyntheticParameterNames(el)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1239
                el = helper.getSourceElement(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1240
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1241
            if (computeJavadoc) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1242
                javadoc = helper.getResolvedDocComment(el);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1243
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1244
        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1245
            proc.debug(ex, "SourceCodeAnalysisImpl.element2String(..., " + el + ")");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1246
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1247
        String signature = Util.expunge(elementHeader(at, el, !hasSyntheticParameterNames(el), true));
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1248
        return new DocumentationImpl(signature,  javadoc);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1249
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1250
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1251
    public void close() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1252
        for (AutoCloseable closeable : closeables) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1253
            try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1254
                closeable.close();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1255
            } catch (Exception ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1256
                proc.debug(ex, "SourceCodeAnalysisImpl.close()");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1257
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1258
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1259
    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1260
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1261
    private static final class DocumentationImpl implements Documentation {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1262
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1263
        private final String signature;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1264
        private final String javadoc;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1265
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1266
        public DocumentationImpl(String signature, String javadoc) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1267
            this.signature = signature;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1268
            this.javadoc = javadoc;
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1269
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1270
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1271
        @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1272
        public String signature() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1273
            return signature;
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1274
        }
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1275
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1276
        @Override
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1277
        public String javadoc() {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1278
            return javadoc;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1279
        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1280
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1281
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1282
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1283
    private boolean isEmptyArgumentsContext(List<? extends ExpressionTree> arguments) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1284
        if (arguments.size() == 1) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1285
            Tree firstArgument = arguments.get(0);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1286
            return firstArgument.getKind() == Kind.ERRONEOUS;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1287
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1288
        return false;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1289
    }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1290
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1291
    private boolean hasSyntheticParameterNames(Element el) {
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1292
        if (el.getKind() != ElementKind.CONSTRUCTOR && el.getKind() != ElementKind.METHOD)
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1293
            return false;
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1294
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1295
        ExecutableElement ee = (ExecutableElement) el;
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1296
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1297
        if (ee.getParameters().isEmpty())
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1298
            return false;
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1299
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1300
        return ee.getParameters()
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1301
                 .stream()
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1302
                 .allMatch(param -> param.getSimpleName().toString().startsWith("arg"));
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1303
    }
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1304
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44061
diff changeset
  1305
    private static List<Path> availableSourcesOverride; //for tests
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37939
diff changeset
  1306
    private List<Path> availableSources;
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1307
37944
1153fab98d25 8149843: StandardJavaFileManager should provide a way to get paths from strings
jjg
parents: 37939
diff changeset
  1308
    private List<Path> findSources() {
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1309
        if (availableSources != null) {
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1310
            return availableSources;
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1311
        }
44459
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44061
diff changeset
  1312
        if (availableSourcesOverride != null) {
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44061
diff changeset
  1313
            return availableSources = availableSourcesOverride;
5224425af378 8177076: jshell tool: usability of completion
jlahoda
parents: 44061
diff changeset
  1314
        }
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1315
        List<Path> result = new ArrayList<>();
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1316
        Path home = Paths.get(System.getProperty("java.home"));
42832
be7a035526ab 8170428: Move src.zip to JDK/lib/src.zip
mchung
parents: 42405
diff changeset
  1317
        Path srcZip = home.resolve("lib").resolve("src.zip");
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1318
        if (!Files.isReadable(srcZip))
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1319
            srcZip = home.getParent().resolve("src.zip");
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1320
        if (Files.isReadable(srcZip)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1321
            boolean keepOpen = false;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1322
            FileSystem zipFO = null;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1323
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1324
            try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1325
                URI uri = URI.create("jar:" + srcZip.toUri());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1326
                zipFO = FileSystems.newFileSystem(uri, Collections.emptyMap());
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1327
                Path root = zipFO.getRootDirectories().iterator().next();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1328
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1329
                if (Files.exists(root.resolve("java/lang/Object.java".replace("/", zipFO.getSeparator())))) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1330
                    //non-modular format:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1331
                    result.add(srcZip);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1332
                } else if (Files.exists(root.resolve("java.base/java/lang/Object.java".replace("/", zipFO.getSeparator())))) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1333
                    //modular format:
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1334
                    try (DirectoryStream<Path> ds = Files.newDirectoryStream(root)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1335
                        for (Path p : ds) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1336
                            if (Files.isDirectory(p)) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1337
                                result.add(p);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1338
                            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1339
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1340
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1341
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1342
                    keepOpen = true;
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1343
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1344
            } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1345
                proc.debug(ex, "SourceCodeAnalysisImpl.findSources()");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1346
            } finally {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1347
                if (zipFO != null) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1348
                    if (keepOpen) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1349
                        closeables.add(zipFO);
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1350
                    } else {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1351
                        try {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1352
                            zipFO.close();
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1353
                        } catch (IOException ex) {
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1354
                            proc.debug(ex, "SourceCodeAnalysisImpl.findSources()");
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1355
                        }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1356
                    }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1357
                }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1358
            }
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1359
        }
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1360
        return availableSources = result;
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1361
    }
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1362
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1363
    private String elementHeader(AnalyzeTask at, Element el, boolean includeParameterNames, boolean useFQN) {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1364
        switch (el.getKind()) {
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1365
            case ANNOTATION_TYPE: case CLASS: case ENUM: case INTERFACE: {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1366
                TypeElement type = (TypeElement)el;
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1367
                String fullname = type.getQualifiedName().toString();
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1368
                Element pkg = at.getElements().getPackageOf(el);
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1369
                String name = pkg == null || useFQN ? fullname :
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1370
                        proc.maps.fullClassNameAndPackageToClass(fullname, ((PackageElement)pkg).getQualifiedName().toString());
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1371
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1372
                return name + typeParametersOpt(at, type.getTypeParameters(), includeParameterNames);
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1373
            }
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1374
            case TYPE_PARAMETER: {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1375
                TypeParameterElement tp = (TypeParameterElement)el;
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1376
                String name = tp.getSimpleName().toString();
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1377
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1378
                List<? extends TypeMirror> bounds = tp.getBounds();
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1379
                boolean boundIsObject = bounds.isEmpty() ||
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1380
                        bounds.size() == 1 && at.getTypes().isSameType(bounds.get(0), Symtab.instance(at.getContext()).objectType);
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1381
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1382
                return boundIsObject
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1383
                        ? name
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1384
                        : name + " extends " + bounds.stream()
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1385
                                .map(bound -> printType(at, proc, bound))
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1386
                                .collect(joining(" & "));
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1387
            }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1388
            case FIELD:
42258
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41865
diff changeset
  1389
                return appendDot(elementHeader(at, el.getEnclosingElement(), includeParameterNames, false)) + el.getSimpleName() + ":" + el.asType();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1390
            case ENUM_CONSTANT:
42258
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41865
diff changeset
  1391
                return appendDot(elementHeader(at, el.getEnclosingElement(), includeParameterNames, false)) + el.getSimpleName();
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1392
            case EXCEPTION_PARAMETER: case LOCAL_VARIABLE: case PARAMETER: case RESOURCE_VARIABLE:
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1393
                return el.getSimpleName() + ":" + el.asType();
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1394
            case CONSTRUCTOR: case METHOD: {
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1395
                StringBuilder header = new StringBuilder();
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1396
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1397
                boolean isMethod = el.getKind() == ElementKind.METHOD;
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1398
                ExecutableElement method = (ExecutableElement) el;
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1399
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1400
                if (isMethod) {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1401
                    // return type
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1402
                    header.append(printType(at, proc, method.getReturnType())).append(" ");
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1403
                } else {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1404
                    // type parameters for the constructor
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1405
                    String typeParameters = typeParametersOpt(at, method.getTypeParameters(), includeParameterNames);
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1406
                    if (!typeParameters.isEmpty()) {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1407
                        header.append(typeParameters).append(" ");
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1408
                    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1409
                }
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1410
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1411
                // receiver type
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1412
                String clazz = elementHeader(at, el.getEnclosingElement(), includeParameterNames, false);
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1413
                header.append(clazz);
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1414
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1415
                if (isMethod) {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1416
                    //method name with type parameters
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1417
                    (clazz.isEmpty() ? header : header.append("."))
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1418
                            .append(typeParametersOpt(at, method.getTypeParameters(), includeParameterNames))
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1419
                            .append(el.getSimpleName());
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1420
                }
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1421
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1422
                // arguments
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1423
                header.append("(");
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1424
                String sep = "";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1425
                for (Iterator<? extends VariableElement> i = method.getParameters().iterator(); i.hasNext();) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1426
                    VariableElement p = i.next();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1427
                    header.append(sep);
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1428
                    if (!i.hasNext() && method.isVarArgs()) {
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1429
                        header.append(printType(at, proc, unwrapArrayType(p.asType()))).append("...");
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1430
                    } else {
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1431
                        header.append(printType(at, proc, p.asType()));
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1432
                    }
37939
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1433
                    if (includeParameterNames) {
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1434
                        header.append(" ");
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1435
                        header.append(p.getSimpleName());
3eb8c2a89b77 8153761: JShell: Completion -- Show parameter names if possible
jlahoda
parents: 37644
diff changeset
  1436
                    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1437
                    sep = ", ";
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1438
                }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1439
                header.append(")");
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1440
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1441
                // throws
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1442
                List<? extends TypeMirror> thrownTypes = method.getThrownTypes();
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1443
                if (!thrownTypes.isEmpty()) {
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1444
                    header.append(" throws ")
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1445
                            .append(thrownTypes.stream()
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1446
                                    .map(type -> printType(at, proc, type))
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1447
                                    .collect(joining(", ")));
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1448
                }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1449
                return header.toString();
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1450
            }
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1451
            default:
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1452
                return el.toString();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1453
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1454
    }
42258
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41865
diff changeset
  1455
    private String appendDot(String fqn) {
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41865
diff changeset
  1456
        return fqn.isEmpty() ? fqn : fqn + ".";
a1aafd5ea6ec 8169561: jshell tool: double shift-tab on variable crashes tool
jlahoda
parents: 41865
diff changeset
  1457
    }
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1458
    private TypeMirror unwrapArrayType(TypeMirror arrayType) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1459
        if (arrayType.getKind() == TypeKind.ARRAY) {
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1460
            return ((ArrayType)arrayType).getComponentType();
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1461
        }
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1462
        return arrayType;
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1463
    }
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1464
    private String typeParametersOpt(AnalyzeTask at, List<? extends TypeParameterElement> typeParameters, boolean includeParameterNames) {
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1465
        return typeParameters.isEmpty() ? ""
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1466
                : typeParameters.stream()
41865
3ef02797070d 8131019: jshell tool: access javadoc from tool
jlahoda
parents: 41527
diff changeset
  1467
                        .map(tp -> elementHeader(at, tp, includeParameterNames, false))
41527
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1468
                        .collect(joining(", ", "<", ">"));
e8f487b79e24 8145263: JShell: Fix the format of SourceCodeAnalysis#documentation
shinyafox
parents: 40837
diff changeset
  1469
    }
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1470
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1471
    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1472
    public String analyzeType(String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1473
        switch (guessKind(code)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1474
            case IMPORT: case METHOD: case CLASS: case ENUM:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1475
            case INTERFACE: case ANNOTATION_TYPE: case VARIABLE:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1476
                return null;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1477
            default:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1478
                break;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1479
        }
43134
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42836
diff changeset
  1480
        ExpressionInfo ei = ExpressionToTypeInfo.expressionInfo(code, proc);
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42836
diff changeset
  1481
        return (ei == null || !ei.isNonVoid)
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42836
diff changeset
  1482
                ? null
006808ae5f6e 8171981: JShell: Fails compilation: new Object().getClass().getSuperclass()
rfield
parents: 42836
diff changeset
  1483
                : ei.typeName;
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1484
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1485
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1486
    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1487
    public QualifiedNames listQualifiedNames(String code, int cursor) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1488
        code = code.substring(0, cursor);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1489
        if (code.trim().isEmpty()) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1490
            return new QualifiedNames(Collections.emptyList(), -1, true, false);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1491
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1492
        OuterWrap codeWrap;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1493
        switch (guessKind(code)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1494
            case IMPORT:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1495
                return new QualifiedNames(Collections.emptyList(), -1, true, false);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1496
            case METHOD:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
  1497
                codeWrap = proc.outerMap.wrapInTrialClass(Wrap.classMemberWrap(code));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1498
                break;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1499
            default:
37644
33cf53901cac 8154485: JShell: infrastructure for multi-Snippet class wrappers
rfield
parents: 36780
diff changeset
  1500
                codeWrap = proc.outerMap.wrapInTrialClass(Wrap.methodWrap(code));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1501
                break;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1502
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1503
        AnalyzeTask at = proc.taskFactory.new AnalyzeTask(codeWrap);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1504
        SourcePositions sp = at.trees().getSourcePositions();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1505
        CompilationUnitTree topLevel = at.firstCuTree();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1506
        TreePath tp = pathFor(topLevel, sp, codeWrap.snippetIndexToWrapIndex(code.length()));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1507
        if (tp.getLeaf().getKind() != Kind.IDENTIFIER) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1508
            return new QualifiedNames(Collections.emptyList(), -1, true, false);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1509
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1510
        Scope scope = at.trees().getScope(tp);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1511
        TypeMirror type = at.trees().getTypeMirror(tp);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1512
        Element el = at.trees().getElement(tp);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1513
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1514
        boolean erroneous = (type.getKind() == TypeKind.ERROR && el.getKind() == ElementKind.CLASS) ||
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1515
                            (el.getKind() == ElementKind.PACKAGE && el.getEnclosedElements().isEmpty());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1516
        String simpleName = ((IdentifierTree) tp.getLeaf()).getName().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1517
        boolean upToDate;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1518
        List<String> result;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1519
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1520
        synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1521
            upToDate = classpathVersion == indexVersion;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1522
            result = currentIndexes.values()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1523
                                   .stream()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1524
                                   .flatMap(idx -> idx.classSimpleName2FQN.getOrDefault(simpleName,
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1525
                                                                                        Collections.emptyList()).stream())
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1526
                                   .distinct()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1527
                                   .filter(fqn -> isAccessible(at, scope, fqn))
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1528
                                   .sorted()
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1529
                                   .collect(Collectors.toList());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1530
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1531
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1532
        return new QualifiedNames(result, simpleName.length(), upToDate, !erroneous);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1533
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1534
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1535
    private boolean isAccessible(AnalyzeTask at, Scope scope, String fqn) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1536
        TypeElement type = at.getElements().getTypeElement(fqn);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1537
        if (type == null)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1538
            return false;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1539
        return at.trees().isAccessible(scope, type);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1540
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1541
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1542
    //--------------------
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1543
    // classpath indexing:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1544
    //--------------------
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1545
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1546
    //the indexing can be suspended when a more important task is running:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1547
    private void waitIndexingNotSuspended() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1548
        boolean suspendedNotified = false;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1549
        synchronized (suspendLock) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1550
            while (suspend > 0) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1551
                if (!suspendedNotified) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1552
                    suspendedNotified = true;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1553
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1554
                try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1555
                    suspendLock.wait();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1556
                } catch (InterruptedException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1557
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1558
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1559
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1560
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1561
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1562
    public void suspendIndexing() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1563
        synchronized (suspendLock) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1564
            suspend++;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1565
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1566
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1567
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1568
    public void resumeIndexing() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1569
        synchronized (suspendLock) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1570
            if (--suspend == 0) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1571
                suspendLock.notifyAll();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1572
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1573
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1574
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1575
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1576
    //update indexes, either initially or after a classpath change:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1577
    private void refreshIndexes(int version) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1578
        try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1579
            Collection<Path> paths = new ArrayList<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1580
            MemoryFileManager fm = proc.taskFactory.fileManager();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1581
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1582
            appendPaths(fm, StandardLocation.PLATFORM_CLASS_PATH, paths);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1583
            appendPaths(fm, StandardLocation.CLASS_PATH, paths);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1584
            appendPaths(fm, StandardLocation.SOURCE_PATH, paths);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1585
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1586
            Map<Path, ClassIndex> newIndexes = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1587
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1588
            //setup existing/last known data:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1589
            for (Path p : paths) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1590
                ClassIndex index = PATH_TO_INDEX.get(p);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1591
                if (index != null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1592
                    newIndexes.put(p, index);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1593
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1594
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1595
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1596
            synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1597
                //temporary setting old data:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1598
                currentIndexes.clear();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1599
                currentIndexes.putAll(newIndexes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1600
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1601
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1602
            //update/compute the indexes if needed:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1603
            for (Path p : paths) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1604
                waitIndexingNotSuspended();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1605
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1606
                ClassIndex index = indexForPath(p);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1607
                newIndexes.put(p, index);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1608
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1609
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1610
            synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1611
                currentIndexes.clear();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1612
                currentIndexes.putAll(newIndexes);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1613
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1614
        } catch (Exception ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1615
            proc.debug(ex, "SourceCodeAnalysisImpl.refreshIndexes(" + version + ")");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1616
        } finally {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1617
            synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1618
                indexVersion = version;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1619
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1620
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1621
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1622
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1623
    private void appendPaths(MemoryFileManager fm, Location loc, Collection<Path> paths) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1624
        Iterable<? extends Path> locationPaths = fm.getLocationAsPaths(loc);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1625
        if (locationPaths == null)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1626
            return ;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1627
        for (Path path : locationPaths) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1628
            if (".".equals(path.toString())) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1629
                //skip CWD
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1630
                continue;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1631
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1632
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1633
            paths.add(path);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1634
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1635
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1636
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1637
    //create/update index a given JavaFileManager entry (which may be a JDK installation, a jar/zip file or a directory):
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1638
    //if an index exists for the given entry, the existing index is kept unless the timestamp is modified
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1639
    private ClassIndex indexForPath(Path path) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1640
        if (isJRTMarkerFile(path)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1641
            FileSystem jrtfs = FileSystems.getFileSystem(URI.create("jrt:/"));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1642
            Path modules = jrtfs.getPath("modules");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1643
            return PATH_TO_INDEX.compute(path, (p, index) -> {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1644
                try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1645
                    long lastModified = Files.getLastModifiedTime(modules).toMillis();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1646
                    if (index == null || index.timestamp != lastModified) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1647
                        try (DirectoryStream<Path> stream = Files.newDirectoryStream(modules)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1648
                            index = doIndex(lastModified, path, stream);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1649
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1650
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1651
                    return index;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1652
                } catch (IOException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1653
                    proc.debug(ex, "SourceCodeAnalysisImpl.indexesForPath(" + path.toString() + ")");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1654
                    return new ClassIndex(-1, path, Collections.emptySet(), Collections.emptyMap());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1655
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1656
            });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1657
        } else if (!Files.isDirectory(path)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1658
            if (Files.exists(path)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1659
                return PATH_TO_INDEX.compute(path, (p, index) -> {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1660
                    try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1661
                        long lastModified = Files.getLastModifiedTime(p).toMillis();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1662
                        if (index == null || index.timestamp != lastModified) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1663
                            ClassLoader cl = SourceCodeAnalysisImpl.class.getClassLoader();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1664
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1665
                            try (FileSystem zip = FileSystems.newFileSystem(path, cl)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1666
                                index = doIndex(lastModified, path, zip.getRootDirectories());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1667
                            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1668
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1669
                        return index;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1670
                    } catch (IOException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1671
                        proc.debug(ex, "SourceCodeAnalysisImpl.indexesForPath(" + path.toString() + ")");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1672
                        return new ClassIndex(-1, path, Collections.emptySet(), Collections.emptyMap());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1673
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1674
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1675
            } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1676
                return new ClassIndex(-1, path, Collections.emptySet(), Collections.emptyMap());
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1677
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1678
        } else {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1679
            return PATH_TO_INDEX.compute(path, (p, index) -> {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1680
                //no persistence for directories, as we cannot check timestamps:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1681
                if (index == null) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1682
                    index = doIndex(-1, path, Arrays.asList(p));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1683
                }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1684
                return index;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1685
            });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1686
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1687
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1688
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1689
    static boolean isJRTMarkerFile(Path path) {
36526
3b41f1c69604 8142968: Module System implementation
alanb
parents: 36160
diff changeset
  1690
        return path.equals(Paths.get(System.getProperty("java.home"), "lib", "modules"));
36160
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1691
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1692
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1693
    //create an index based on the content of the given dirs; the original JavaFileManager entry is originalPath.
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1694
    private ClassIndex doIndex(long timestamp, Path originalPath, Iterable<? extends Path> dirs) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1695
        Set<String> packages = new HashSet<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1696
        Map<String, Collection<String>> classSimpleName2FQN = new HashMap<>();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1697
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1698
        for (Path d : dirs) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1699
            try {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1700
                Files.walkFileTree(d, new FileVisitor<Path>() {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1701
                    int depth;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1702
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1703
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1704
                        waitIndexingNotSuspended();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1705
                        if (depth++ == 0)
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1706
                            return FileVisitResult.CONTINUE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1707
                        String dirName = dir.getFileName().toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1708
                        String sep = dir.getFileSystem().getSeparator();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1709
                        dirName = dirName.endsWith(sep) ? dirName.substring(0, dirName.length() - sep.length())
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1710
                                                        : dirName;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1711
                        if (SourceVersion.isIdentifier(dirName))
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1712
                            return FileVisitResult.CONTINUE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1713
                        return FileVisitResult.SKIP_SUBTREE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1714
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1715
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1716
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1717
                        waitIndexingNotSuspended();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1718
                        if (file.getFileName().toString().endsWith(".class")) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1719
                            String relativePath = d.relativize(file).toString();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1720
                            String binaryName = relativePath.substring(0, relativePath.length() - 6).replace('/', '.');
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1721
                            int packageDot = binaryName.lastIndexOf('.');
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1722
                            if (packageDot > (-1)) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1723
                                packages.add(binaryName.substring(0, packageDot));
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1724
                            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1725
                            String typeName = binaryName.replace('$', '.');
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1726
                            addClassName2Map(classSimpleName2FQN, typeName);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1727
                        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1728
                        return FileVisitResult.CONTINUE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1729
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1730
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1731
                    public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1732
                        return FileVisitResult.CONTINUE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1733
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1734
                    @Override
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1735
                    public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1736
                        depth--;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1737
                        return FileVisitResult.CONTINUE;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1738
                    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1739
                });
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1740
            } catch (IOException ex) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1741
                proc.debug(ex, "doIndex(" + d.toString() + ")");
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1742
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1743
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1744
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1745
        return new ClassIndex(timestamp, originalPath, packages, classSimpleName2FQN);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1746
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1747
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1748
    private static void addClassName2Map(Map<String, Collection<String>> classSimpleName2FQN, String typeName) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1749
        int simpleNameDot = typeName.lastIndexOf('.');
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1750
        classSimpleName2FQN.computeIfAbsent(typeName.substring(simpleNameDot + 1), n -> new LinkedHashSet<>())
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1751
                           .add(typeName);
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1752
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1753
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1754
    //holder for indexed data about a given path
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1755
    public static final class ClassIndex {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1756
        public final long timestamp;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1757
        public final Path forPath;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1758
        public final Set<String> packages;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1759
        public final Map<String, Collection<String>> classSimpleName2FQN;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1760
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1761
        public ClassIndex(long timestamp, Path forPath, Set<String> packages, Map<String, Collection<String>> classSimpleName2FQN) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1762
            this.timestamp = timestamp;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1763
            this.forPath = forPath;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1764
            this.packages = packages;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1765
            this.classSimpleName2FQN = classSimpleName2FQN;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1766
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1767
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1768
    }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1769
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1770
    //for tests, to be able to wait until the indexing finishes:
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1771
    public void waitBackgroundTaskFinished() throws Exception {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1772
        boolean upToDate;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1773
        synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1774
            upToDate = classpathVersion == indexVersion;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1775
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1776
        while (!upToDate) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1777
            INDEXER.submit(() -> {}).get();
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1778
            synchronized (currentIndexes) {
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1779
                upToDate = classpathVersion == indexVersion;
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1780
            }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1781
        }
f42d362d0d17 8131027: JShell API/tool: suggest imports for a class
jlahoda
parents: 34857
diff changeset
  1782
    }
40498
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1783
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1784
    /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1785
     * A candidate for continuation of the given user's input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1786
     */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1787
    private static class SuggestionImpl implements Suggestion {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1788
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1789
        private final String continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1790
        private final boolean matchesType;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1791
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1792
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1793
         * Create a {@code Suggestion} instance.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1794
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1795
         * @param continuation a candidate continuation of the user's input
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1796
         * @param matchesType does the candidate match the target type
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1797
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1798
        public SuggestionImpl(String continuation, boolean matchesType) {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1799
            this.continuation = continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1800
            this.matchesType = matchesType;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1801
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1802
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1803
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1804
         * The candidate continuation of the given user's input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1805
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1806
         * @return the continuation string
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1807
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1808
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1809
        public String continuation() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1810
            return continuation;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1811
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1812
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1813
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1814
         * Indicates whether input continuation matches the target type and is thus
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1815
         * more likely to be the desired continuation. A matching continuation is
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1816
         * preferred.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1817
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1818
         * @return {@code true} if this suggested continuation matches the
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1819
         * target type; otherwise {@code false}
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1820
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1821
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1822
        public boolean matchesType() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1823
            return matchesType;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1824
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1825
    }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1826
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1827
    /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1828
     * The result of {@code analyzeCompletion(String input)}.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1829
     * Describes the completeness and position of the first snippet in the given input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1830
     */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1831
    private static class CompletionInfoImpl implements CompletionInfo {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1832
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1833
        private final Completeness completeness;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1834
        private final String source;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1835
        private final String remaining;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1836
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1837
        CompletionInfoImpl(Completeness completeness, String source, String remaining) {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1838
            this.completeness = completeness;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1839
            this.source = source;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1840
            this.remaining = remaining;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1841
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1842
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1843
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1844
         * The analyzed completeness of the input.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1845
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1846
         * @return an enum describing the completeness of the input string.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1847
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1848
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1849
        public Completeness completeness() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1850
            return completeness;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1851
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1852
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1853
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1854
         * Input remaining after the complete part of the source.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1855
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1856
         * @return the portion of the input string that remains after the
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1857
         * complete Snippet
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1858
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1859
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1860
        public String remaining() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1861
            return remaining;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1862
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1863
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1864
        /**
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1865
         * Source code for the first Snippet of code input. For example, first
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1866
         * statement, or first method declaration. Trailing semicolons will be
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1867
         * added, as needed.
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1868
         *
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1869
         * @return the source of the first encountered Snippet
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1870
         */
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1871
        @Override
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1872
        public String source() {
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1873
            return source;
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1874
        }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1875
    }
f54048be4a57 8159027: JShell API: SourceCodeAnalysis.Suggestion has constructor, ...
rfield
parents: 40318
diff changeset
  1876
33362
65ec6de1d6b4 8134254: JShell API/tool: REPL for Java into JDK9
jlahoda
parents:
diff changeset
  1877
}