diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests2.java index a067924..8cf9c88 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests2.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2010 IBM Corporation and others. + * Copyright (c) 2000, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -38,6 +38,7 @@ import org.eclipse.jdt.core.IJavaProject; import org.eclipse.jdt.core.IType; import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; import org.eclipse.jdt.core.tests.util.Util; import org.eclipse.jdt.internal.codeassist.RelevanceConstants; import org.eclipse.jdt.internal.compiler.impl.CompilerOptions; @@ -45,6 +46,10 @@ import org.eclipse.jdt.internal.core.search.indexing.IndexManager; public class CompletionTests2 extends ModifyingResourceTests implements RelevanceConstants { + + static { +// TESTS_NAMES = new String[]{"testBug340945"}; + } public static class CompletionContainerInitializer implements ContainerInitializer.ITestInitializer { @@ -5591,4 +5596,260 @@ deleteProject(project); } } + +//https://bugs.eclipse.org/bugs/show_bug.cgi?id=292087 +public void testBug340945() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/Try.java", + "package test;\n" + + "public class Try extends Thread{\n" + + " int inty = 1;\n" + + " void foo() {\n" + + " int i = 1;\n" + + " Object o = new Object() {\n" + + " void running() {\n" + + " int j = 1;\n" + + " int k = " + + " }\n" + + " }\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.setRequireExtendedContext(true); + requestor.allowAllRequiredProposals(); + requestor.setComputeVisibleElements(true); +// requestor.setAssignableType("I"); + String str = this.workingCopies[0].getSource(); + String completeBehind = "int k ="; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "", + requestor.getCompletionNode()); + assertResults( + "int k = ;", + requestor.getCompletionNodeParent()); + assertResults( + "int j[pos: unused][id:2]\n" + + "int i[pos: unused][id:0]\n" + + "java.lang.Object o[pos: unused][id:1]\n", + requestor.getVisibleLocalVariables()); + assertResults( + "int inty\n", + requestor.getVisibleFields()); + assertResults( + "void running() \n" + + "public final void wait(long, int) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait(long) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait() throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public java.lang.String toString() \n" + + "public final void notifyAll() throws java.lang.IllegalMonitorStateException\n" + + "public final void notify() throws java.lang.IllegalMonitorStateException\n" + + "public int hashCode() \n" + + "public final java.lang.Class getClass() \n" + + "protected void finalize() throws java.lang.Throwable\n" + + "public boolean equals(java.lang.Object) \n" + + "protected java.lang.Object clone() throws java.lang.CloneNotSupportedException\n" + + "void foo() \n", + requestor.getVisibleMethods()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=292087 +public void testBug340945a() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/Try.java", + "package test;\n" + + "public class Try extends Thread{\n" + + " int int1 = 1;\n" + + " int int2 = 2;\n" + + " int int3 = " + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.setRequireExtendedContext(true); + requestor.allowAllRequiredProposals(); + requestor.setComputeVisibleElements(true); + String str = this.workingCopies[0].getSource(); + String completeBehind = "int int3 ="; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "", + requestor.getCompletionNode()); + assertResults( + "int int3 = ;", + requestor.getCompletionNodeParent()); + assertResults( + "", + requestor.getVisibleLocalVariables()); + assertResults( + "int int2\n" + + "int int1\n", + requestor.getVisibleFields()); + assertResults( + "public final void wait(long, int) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait(long) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait() throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public java.lang.String toString() \n" + + "public final void notifyAll() throws java.lang.IllegalMonitorStateException\n" + + "public final void notify() throws java.lang.IllegalMonitorStateException\n" + + "public int hashCode() \n" + + "public final java.lang.Class getClass() \n" + + "protected void finalize() throws java.lang.Throwable\n" + + "public boolean equals(java.lang.Object) \n" + + "protected java.lang.Object clone() throws java.lang.CloneNotSupportedException\n", + requestor.getVisibleMethods()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=292087 +public void testBug340945b() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/Try.java", + "package test;\n" + + "public class Try extends Thread{\n" + + "Object field;\n" + + "void foo() {\n" + + " int int1 = 1;\n" + + " int int2 = 2;\n" + + " int int3 = " + + "}\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.setRequireExtendedContext(true); + requestor.allowAllRequiredProposals(); + requestor.setComputeVisibleElements(true); + String str = this.workingCopies[0].getSource(); + String completeBehind = "int int3 ="; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "", + requestor.getCompletionNode()); + assertResults( + "int int3 = ;", + requestor.getCompletionNodeParent()); + assertResults( + "int int1[pos: unused][id:0]\n" + + "int int2[pos: unused][id:1]\n", + requestor.getVisibleLocalVariables()); + assertResults( + "java.lang.Object field\n", + requestor.getVisibleFields()); + assertResults( + "void foo() \n" + + "public final void wait(long, int) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait(long) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait() throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public java.lang.String toString() \n" + + "public final void notifyAll() throws java.lang.IllegalMonitorStateException\n" + + "public final void notify() throws java.lang.IllegalMonitorStateException\n" + + "public int hashCode() \n" + + "public final java.lang.Class getClass() \n" + + "protected void finalize() throws java.lang.Throwable\n" + + "public boolean equals(java.lang.Object) \n" + + "protected java.lang.Object clone() throws java.lang.CloneNotSupportedException\n", + requestor.getVisibleMethods()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=292087 +public void testBug340945c() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/Try.java", + "package test;\n" + + "public class Try extends Thread{\n" + + "Object field;\n" + + "void foo() {\n" + + " int int1 = 1;\n" + + " int int2 = 2;\n" + + " Object o1 = new Object();\n" + + " o1." + + "}\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.setRequireExtendedContext(true); + requestor.allowAllRequiredProposals(); + requestor.setComputeVisibleElements(true); + String str = this.workingCopies[0].getSource(); + String completeBehind = "o1."; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "", + requestor.getCompletionNode()); + assertNull( + "should be null", + requestor.getCompletionNodeParent()); + assertResults( + "int int1[pos: unused][id:0]\n" + + "int int2[pos: unused][id:1]\n" + + "java.lang.Object o1[pos: unused][id:2]\n", + requestor.getVisibleLocalVariables()); + assertResults( + "java.lang.Object field\n", + requestor.getVisibleFields()); + assertResults( + "void foo() \n" + + "public final void wait(long, int) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait(long) throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public final void wait() throws java.lang.IllegalMonitorStateException, java.lang.InterruptedException\n" + + "public java.lang.String toString() \n" + + "public final void notifyAll() throws java.lang.IllegalMonitorStateException\n" + + "public final void notify() throws java.lang.IllegalMonitorStateException\n" + + "public int hashCode() \n" + + "public final java.lang.Class getClass() \n" + + "protected void finalize() throws java.lang.Throwable\n" + + "public boolean equals(java.lang.Object) \n" + + "protected java.lang.Object clone() throws java.lang.CloneNotSupportedException\n", + requestor.getVisibleMethods()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=292087 +public void testBug340945d() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/Try.java", + "package test;\n" + + "public class Try extends Thread{\n" + + "Object field;\n" + + "static void foo() {\n" + // field should not be visible here + " int int1 = 1;\n" + + " int int2 = 2;\n" + + " Object o1 = new Object();\n" + + " o1." + + "}\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true); + requestor.setRequireExtendedContext(true); + requestor.allowAllRequiredProposals(); + requestor.setComputeVisibleElements(true); + String str = this.workingCopies[0].getSource(); + String completeBehind = "o1."; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + assertResults( + "", + requestor.getCompletionNode()); + assertNull( + "should be null", + requestor.getCompletionNodeParent()); + assertResults( + "int int1[pos: unused][id:0]\n" + + "int int2[pos: unused][id:1]\n" + + "java.lang.Object o1[pos: unused][id:2]\n", + requestor.getVisibleLocalVariables()); + assertResults( + "", + requestor.getVisibleFields()); + assertResults( + "static void foo() \n", + requestor.getVisibleMethods()); +} } diff --git a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTestsRequestor2.java b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTestsRequestor2.java index c3102c4..38e7374 100644 --- a/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTestsRequestor2.java +++ b/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTestsRequestor2.java @@ -20,6 +20,9 @@ import org.eclipse.jdt.core.IJavaElement; import org.eclipse.jdt.core.Signature; import org.eclipse.jdt.core.compiler.IProblem; +import org.eclipse.jdt.internal.codeassist.InternalCompletionContext; +import org.eclipse.jdt.internal.compiler.ast.ASTNode; +import org.eclipse.jdt.internal.compiler.util.ObjectVector; import org.eclipse.jdt.internal.core.JavaElement; public class CompletionTestsRequestor2 extends CompletionRequestor { @@ -605,4 +608,54 @@ } return false; } + + public String getCompletionNode() { + if (this.context instanceof InternalCompletionContext) { + InternalCompletionContext internalCompletionContext = (InternalCompletionContext) this.context; + ASTNode astNode = internalCompletionContext.getCompletionNode(); + if (astNode != null) return astNode.toString(); + + } + return null; + } + + public String getCompletionNodeParent() { + if (this.context instanceof InternalCompletionContext) { + InternalCompletionContext internalCompletionContext = (InternalCompletionContext) this.context; + ASTNode astNode = internalCompletionContext.getCompletionNodeParent(); + if (astNode != null) return astNode.toString(); + + } + return null; + } + + public String getVisibleLocalVariables() { + if (this.context instanceof InternalCompletionContext) { + InternalCompletionContext internalCompletionContext = (InternalCompletionContext) this.context; + ObjectVector locals = internalCompletionContext.getVisibleLocalVariables(); + if (locals != null) return locals.toString(); + + } + return null; + } + + public String getVisibleFields() { + if (this.context instanceof InternalCompletionContext) { + InternalCompletionContext internalCompletionContext = (InternalCompletionContext) this.context; + ObjectVector fields = internalCompletionContext.getVisibleFields(); + if (fields != null) return fields.toString(); + + } + return null; + } + + public String getVisibleMethods() { + if (this.context instanceof InternalCompletionContext) { + InternalCompletionContext internalCompletionContext = (InternalCompletionContext) this.context; + ObjectVector methods = internalCompletionContext.getVisibleMethods(); + if (methods != null) return methods.toString(); + + } + return null; + } } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java index 8b7c565..989c11c 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java @@ -1502,6 +1502,7 @@ this.lookupEnvironment, scope, astNode, + astNodeParent, this.owner, this.parser); } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalCompletionContext.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalCompletionContext.java index ba33ea1..bba8d78 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalCompletionContext.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalCompletionContext.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2005, 2008 IBM Corporation and others. + * Copyright (c) 2005, 2011 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -24,8 +24,12 @@ import org.eclipse.jdt.internal.codeassist.complete.CompletionParser; import org.eclipse.jdt.internal.compiler.ast.ASTNode; import org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration; +import org.eclipse.jdt.internal.compiler.lookup.FieldBinding; +import org.eclipse.jdt.internal.compiler.lookup.LocalVariableBinding; import org.eclipse.jdt.internal.compiler.lookup.LookupEnvironment; +import org.eclipse.jdt.internal.compiler.lookup.MethodBinding; import org.eclipse.jdt.internal.compiler.lookup.Scope; +import org.eclipse.jdt.internal.compiler.util.ObjectVector; /** @@ -65,6 +69,7 @@ LookupEnvironment lookupEnvironment, Scope scope, ASTNode astNode, + ASTNode astNodeParent, WorkingCopyOwner owner, CompletionParser parser) { this.isExtended = true; @@ -76,6 +81,7 @@ lookupEnvironment, scope, astNode, + astNodeParent, owner, parser); } @@ -368,4 +374,87 @@ public boolean isInJavadocText() { return (this.javadoc & CompletionOnJavadoc.TEXT) != 0; } + + /** + * Return the completion node associated with the current completion. + * + * @return completion AST node, or null if the extendedContext is null. + * @exception UnsupportedOperationException if the context is not an extended context + * + * @see #isExtended() + */ + public ASTNode getCompletionNode() { + if (!this.isExtended) throw new UnsupportedOperationException("Operation only supported in extended context"); //$NON-NLS-1$ + + if (this.extendedContext == null) return null; + + return this.extendedContext.getCompletionNode(); + } + + /** + * Return the parent AST node of the completion node associated with the current completion. + * + * @return completion parent AST node, or null if the extendedContext is null. + * @exception UnsupportedOperationException if the context is not an extended context + * + * @see #isExtended() + */ + public ASTNode getCompletionNodeParent() { + if (!this.isExtended) throw new UnsupportedOperationException("Operation only supported in extended context"); //$NON-NLS-1$ + + if (this.extendedContext == null) return null; + + return this.extendedContext.getCompletionNodeParent(); + } + + /** + * Return the bindings of all visible local variables in the current completion context. + * + * @return bindings of all visible local variables, or null if the extendedContext is null. Returned bindings are instances of + * {@link LocalVariableBinding} + * @exception UnsupportedOperationException if the context is not an extended context + * + * @see #isExtended() + */ + public ObjectVector getVisibleLocalVariables() { + if (!this.isExtended) throw new UnsupportedOperationException("Operation only supported in extended context"); //$NON-NLS-1$ + + if (this.extendedContext == null) return null; + + return this.extendedContext.getVisibleLocalVariables(); + } + + /** + * Return the bindings of all visible fields in the current completion context. + * + * @return bindings of all visible fields, or null if the extendedContext is null. Returned bindings are instances of + * {@link FieldBinding} + * @exception UnsupportedOperationException if the context is not an extended context + * + * @see #isExtended() + */ + public ObjectVector getVisibleFields() { + if (!this.isExtended) throw new UnsupportedOperationException("Operation only supported in extended context"); //$NON-NLS-1$ + + if (this.extendedContext == null) return null; + + return this.extendedContext.getVisibleFields(); + } + + /** + * Return the bindings of all visible methods in the current completion context. + * + * @return bindings of all visible methods, or null if the extendedContext is null. Returned bindings are instances of + * {@link MethodBinding} + * @exception UnsupportedOperationException if the context is not an extended context + * + * @see #isExtended() + */ + public ObjectVector getVisibleMethods() { + if (!this.isExtended) throw new UnsupportedOperationException("Operation only supported in extended context"); //$NON-NLS-1$ + + if (this.extendedContext == null) return null; + + return this.extendedContext.getVisibleMethods(); + } } diff --git a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java index 152977a..19f2546 100644 --- a/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java +++ b/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/InternalExtendedCompletionContext.java @@ -76,6 +76,7 @@ private LookupEnvironment lookupEnvironment; private Scope assistScope; private ASTNode assistNode; + private ASTNode assistNodeParent; private WorkingCopyOwner owner; private CompletionParser parser; @@ -98,6 +99,7 @@ LookupEnvironment lookupEnvironment, Scope assistScope, ASTNode assistNode, + ASTNode assistNodeParent, WorkingCopyOwner owner, CompletionParser parser) { this.completionContext = completionContext; @@ -106,6 +108,7 @@ this.lookupEnvironment = lookupEnvironment; this.assistScope = assistScope; this.assistNode = assistNode; + this.assistNodeParent = assistNodeParent; this.owner = owner; this.parser = parser; } @@ -929,4 +932,40 @@ } return false; } + + /** + * @see InternalCompletionContext#getCompletionNode() + */ + public ASTNode getCompletionNode() { + return this.assistNode; + } + + /** + * @see InternalCompletionContext#getCompletionNodeParent() + */ + public ASTNode getCompletionNodeParent() { + // TODO Auto-generated method stub + return this.assistNodeParent; + } + + public ObjectVector getVisibleLocalVariables() { + if (!this.hasComputedVisibleElementBindings) { + computeVisibleElementBindings(); + } + return this.visibleLocalVariables; + } + + public ObjectVector getVisibleFields() { + if (!this.hasComputedVisibleElementBindings) { + computeVisibleElementBindings(); + } + return this.visibleFields; + } + + public ObjectVector getVisibleMethods() { + if (!this.hasComputedVisibleElementBindings) { + computeVisibleElementBindings(); + } + return this.visibleMethods; + } }