8326333: jshell <TAB> completion on arrays is incomplete

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2024-03-18 08:25:06 +00:00
parent ece4124f25
commit 48717d63cc
2 changed files with 32 additions and 13 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -1032,14 +1032,7 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
case DECLARED: {
TypeElement element = (TypeElement) at.getTypes().asElement(site);
List<Element> result = new ArrayList<>();
at.getElements().getAllMembers(element).forEach(m -> result.add(
element.equals(m.getEnclosingElement())
? m
: (m instanceof Symbol.MethodSymbol ms)
? ms.clone((Symbol)element)
: (m instanceof Symbol.VarSymbol vs)
? vs.clone((Symbol)element)
: m));
result.addAll(membersOf(at, element));
if (shouldGenerateDotClassItem) {
result.add(createDotClassSymbol(at, site));
}
@ -1076,6 +1069,10 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
}
case ARRAY: {
List<Element> result = new ArrayList<>();
TypeElement jlObject = at.getElements().getTypeElement("java.lang.Object");
if (jlObject != null) {
result.addAll(membersOf(at, jlObject));
}
result.add(createArrayLengthSymbol(at, site));
if (shouldGenerateDotClassItem)
result.add(createDotClassSymbol(at, site));
@ -1086,6 +1083,17 @@ class SourceCodeAnalysisImpl extends SourceCodeAnalysis {
}
}
private List<? extends Element> membersOf(AnalyzeTask at, TypeElement element) {
return at.getElements().getAllMembers(element).stream().map(m ->
element.equals(m.getEnclosingElement())
? m
: (m instanceof Symbol.MethodSymbol ms)
? ms.clone((Symbol)element)
: (m instanceof Symbol.VarSymbol vs)
? vs.clone((Symbol)element)
: m).toList();
}
private List<? extends Element> membersOf(AnalyzeTask at, List<? extends Element> elements) {
return elements.stream()
.flatMap(e -> membersOf(at, e.asType(), true).stream())

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2015, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,7 +23,7 @@
/*
* @test
* @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 8177466 8197439 8221759 8234896 8240658 8278039 8286206 8296789 8314662
* @bug 8131025 8141092 8153761 8145263 8131019 8175886 8176184 8176241 8176110 8177466 8197439 8221759 8234896 8240658 8278039 8286206 8296789 8314662 8326333
* @summary Test Completion and Documentation
* @library /tools/lib
* @modules jdk.compiler/com.sun.tools.javac.api
@ -86,9 +86,9 @@ public class CompletionSuggestionTest extends KullaTesting {
assertCompletion("Object[].|", "class");
assertCompletion("int[].|", "class");
assertEval("Object[] ao = null;");
assertCompletion("int i = ao.|", "length");
assertCompletion("int i = ao.le|", "length");
assertEval("int[] ai = null;");
assertCompletion("int i = ai.|", "length");
assertCompletion("int i = ai.le|", "length");
assertCompletionIncludesExcludes("\"\".|",
new HashSet<>(Collections.emptyList()),
new HashSet<>(Arrays.asList("String(")));
@ -810,4 +810,15 @@ public class CompletionSuggestionTest extends KullaTesting {
ste(m2, RECOVERABLE_DEFINED, VALID, true, MAIN_SNIPPET));
assertSignature("test(|", "void test(String s)", "void test(Integer i)");
}
//JDK-8326333: verify completion returns sensible output for arrays:
public void testArray() {
assertEval("String[] strs = null;");
assertCompletion("strs.to|", "toString()");
assertCompletion("strs.le|", "length");
assertEval("int[] ints = null;");
assertCompletion("ints.no|", "notify()", "notifyAll()");
assertCompletion("ints.le|", "length");
assertCompletion("String[].|", "class");
}
}