This is an automated email from the ASF dual-hosted git repository.
lkishalmi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/netbeans.git
The following commit(s) were added to refs/heads/master by this push:
new 289e483 [NETBEANS-4356] Code Completion must check also
superclass/superinterface methods when determining if cast is required.
289e483 is described below
commit 289e483cda194162d8b8e6ad30894d0b814d6db8
Author: Jan Lahoda <[email protected]>
AuthorDate: Sat May 23 07:42:32 2020 +0200
[NETBEANS-4356] Code Completion must check also superclass/superinterface
methods when determining if cast is required.
---
.../java/completion/JavaCompletionTask.java | 13 ++++-
.../JavaCompletionProviderTest/1.8/testCast1.pass | 5 ++
.../JavaCompletionProviderTest/1.8/testCast2.pass | 5 ++
.../JavaCompletionProviderTest/1.8/testCast3.pass | 13 +++++
.../JavaCompletionProviderTest/1.8/testCast4.pass | 13 +++++
.../java/editor/completion/CompletionTestBase.java | 20 +++++++
.../completion/JavaCompletionItemPerformTest.java | 62 ++++++++++++++++++++++
7 files changed, 130 insertions(+), 1 deletion(-)
diff --git
a/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
b/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
index b70972a..b6bbb7f 100644
---
a/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
+++
b/java/java.completion/src/org/netbeans/modules/java/completion/JavaCompletionTask.java
@@ -3691,7 +3691,7 @@ public final class JavaCompletionTask<T> extends BaseTask
{
et = (ExecutableType) asMemberOf(e, actualType, types);
if (addCast && itemFactory instanceof
TypeCastableItemFactory
&& !types.isSubtype(type,
e.getEnclosingElement().asType())
- && type.getKind() == TypeKind.DECLARED &&
!eu.alreadyDefinedIn(e.getSimpleName(), et,
(TypeElement)((DeclaredType)type).asElement())) {
+ && type.getKind() == TypeKind.DECLARED &&
!hasBaseMethod(elements, (DeclaredType) type, (ExecutableElement) e)) {
results.add(((TypeCastableItemFactory<T>)itemFactory).createTypeCastableExecutableItem(env.getController(),
(ExecutableElement) e, et, actualType, anchorOffset, autoImport ?
env.getReferencesCount() : null, typeElem != e.getEnclosingElement(),
elements.isDeprecated(e), inImport, env.addSemicolon(), isOfSmartType(env,
getCorrectedReturnType(env, et, (ExecutableElement) e, actualType),
smartTypes), env.assignToVarPos(), false));
} else {
results.add(itemFactory.createExecutableItem(env.getController(),
(ExecutableElement) e, et, anchorOffset, autoImport ? env.getReferencesCount()
: null, typeElem != e.getEnclosingElement(), elements.isDeprecated(e),
inImport, env.addSemicolon(), isOfSmartType(env, getCorrectedReturnType(env,
et, (ExecutableElement) e, actualType), smartTypes), env.assignToVarPos(),
false));
@@ -3722,6 +3722,17 @@ public final class JavaCompletionTask<T> extends
BaseTask {
}
}
+ private boolean hasBaseMethod(Elements elements, DeclaredType type,
ExecutableElement invoked) {
+ TypeElement clazz = (TypeElement) type.asElement();
+ for (ExecutableElement existing :
ElementFilter.methodsIn(elements.getAllMembers(clazz))) {
+ if (existing.getSimpleName().equals(invoked.getSimpleName()) &&
+ elements.overrides(invoked, existing, clazz)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
private void addThisOrSuperConstructor(final Env env, final TypeMirror
type, final Element elem, final String name, final ExecutableElement toExclude)
throws IOException {
final CompilationController controller = env.getController();
final Elements elements = controller.getElements();
diff --git
a/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast1.pass
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast1.pass
new file mode 100644
index 0000000..1e18c5e
--- /dev/null
+++
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast1.pass
@@ -0,0 +1,5 @@
+package test;
+public class Test {
+ private t(Object o) {
+ if (o instanceof String) {
+ ((String) o).length()
\ No newline at end of file
diff --git
a/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast2.pass
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast2.pass
new file mode 100644
index 0000000..ad00d55
--- /dev/null
+++
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast2.pass
@@ -0,0 +1,5 @@
+package test;
+public class Test {
+ private t(Number n) {
+ if (n instanceof Integer) {
+ n.intValue()
\ No newline at end of file
diff --git
a/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast3.pass
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast3.pass
new file mode 100644
index 0000000..f405e07
--- /dev/null
+++
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast3.pass
@@ -0,0 +1,13 @@
+package test;
+public class Test {
+ public interface Base {
+ public void test() { }
+ }
+ public interface Int extends Base {
+ }
+ public static class Impl implements Int {
+ public void test() { }
+ }
+ private t(Int b) {
+ if (b instanceof Impl) {
+ b.test();
\ No newline at end of file
diff --git
a/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast4.pass
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast4.pass
new file mode 100644
index 0000000..4b641de
--- /dev/null
+++
b/java/java.editor/test/unit/data/goldenfiles/org/netbeans/modules/java/editor/completion/JavaCompletionProviderTest/1.8/testCast4.pass
@@ -0,0 +1,13 @@
+package test;
+public class Test {
+ public interface Base {
+ public void test() { }
+ }
+ public interface Int extends Base {
+ }
+ public static class Impl implements Int {
+ public void test() { }
+ }
+ private t(Base b) {
+ if (b instanceof Impl) {
+ b.test();
\ No newline at end of file
diff --git
a/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/CompletionTestBase.java
b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/CompletionTestBase.java
index 1505d18..5acd229 100644
---
a/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/CompletionTestBase.java
+++
b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/CompletionTestBase.java
@@ -21,6 +21,7 @@ package org.netbeans.modules.java.editor.completion;
import java.io.File;
import java.io.FileWriter;
+import java.io.IOException;
import java.io.Writer;
import java.util.Collections;
import java.util.List;
@@ -46,6 +47,10 @@ import org.openide.cookies.EditorCookie;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.loaders.DataObject;
+import org.openide.util.lookup.ServiceProvider;
+import org.openide.xml.EntityCatalog;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
/**
*
@@ -118,4 +123,19 @@ public class CompletionTestBase extends
CompletionTestBaseBase {
LifecycleManager.getDefault().saveAll();
}
+ @ServiceProvider(service=EntityCatalog.class)
+ public static final class TestEntityCatalogImpl extends EntityCatalog {
+
+ @Override
+ public InputSource resolveEntity(String publicID, String systemID)
throws SAXException, IOException {
+ switch (publicID) {
+ case "-//NetBeans//DTD Editor Fonts and Colors settings
1.1//EN":
+ return new
InputSource(TestEntityCatalogImpl.class.getResourceAsStream("/org/netbeans/modules/editor/settings/storage/fontscolors/EditorFontsColors-1_1.dtd"));
+ case "-//NetBeans//DTD Editor Code Templates settings 1.0//EN":
+ return new
InputSource(TestEntityCatalogImpl.class.getResourceAsStream("/org/netbeans/lib/editor/codetemplates/storage/EditorCodeTemplates-1_0.dtd"));
+ }
+ return null;
+ }
+
+ }
}
diff --git
a/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/JavaCompletionItemPerformTest.java
b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/JavaCompletionItemPerformTest.java
index 778d4b0..5e23cd6 100644
---
a/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/JavaCompletionItemPerformTest.java
+++
b/java/java.editor/test/unit/src/org/netbeans/modules/java/editor/completion/JavaCompletionItemPerformTest.java
@@ -36,4 +36,66 @@ public class JavaCompletionItemPerformTest extends
CompletionTestBase {
public void testAnnotation() throws Exception {
performTest("Annotation", 27, "Se", "Set", "testAnnotation.pass2");
}
+
+ public void testCast1() throws Exception {
+ performTest("Empty", 0,
+ "package test;\n" +
+ "public class Test {\n" +
+ " private t(Object o) {\n" +
+ " if (o instanceof String) {\n" +
+ " o.len",
+ "length",
+ "testCast1.pass");
+ }
+
+ public void testCast2() throws Exception {
+ performTest("Empty", 0,
+ "package test;\n" +
+ "public class Test {\n" +
+ " private t(Number n) {\n" +
+ " if (n instanceof Integer) {\n" +
+ " n.in",
+ "intValue",
+ "testCast2.pass");
+ }
+
+ public void testCast3() throws Exception {
+ //calling an overridden method
+ performTest("Empty", 0,
+ "package test;\n" +
+ "public class Test {\n" +
+ " public interface Base {\n" +
+ " public void test() { }\n" +
+ " }\n" +
+ " public interface Int extends Base {\n" +
+ " }\n" +
+ " public static class Impl implements Int {\n" +
+ " public void test() { }\n" +
+ " }\n" +
+ " private t(Int b) {\n" +
+ " if (b instanceof Impl) {\n" +
+ " b.tes",
+ "test",
+ "testCast3.pass");
+ }
+
+ public void testCast4() throws Exception {
+ //calling an overridden method
+ performTest("Empty", 0,
+ "package test;\n" +
+ "public class Test {\n" +
+ " public interface Base {\n" +
+ " public void test() { }\n" +
+ " }\n" +
+ " public interface Int extends Base {\n" +
+ " }\n" +
+ " public static class Impl implements Int {\n" +
+ " public void test() { }\n" +
+ " }\n" +
+ " private t(Base b) {\n" +
+ " if (b instanceof Impl) {\n" +
+ " b.tes",
+ "test",
+ "testCast4.pass");
+ }
}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists