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