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