### Eclipse Workspace Patch 1.0 #P org.eclipse.jdt.core Index: codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java,v retrieving revision 1.410 diff -u -r1.410 CompletionEngine.java --- codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java 7 Jan 2010 20:18:50 -0000 1.410 +++ codeassist/org/eclipse/jdt/internal/codeassist/CompletionEngine.java 18 Mar 2010 09:11:53 -0000 @@ -6647,134 +6647,143 @@ if (enclosingNode == null || !(enclosingNode instanceof IfStatement)) return; IfStatement ifStatement = (IfStatement)enclosingNode; - - if (!(ifStatement.condition instanceof InstanceOfExpression)) return; - - InstanceOfExpression instanceOfExpression = (InstanceOfExpression) ifStatement.condition; - - TypeReference instanceOfType = instanceOfExpression.type; - - if (instanceOfType.resolvedType == null) return; - - boolean findFromAnotherReceiver = false; - - char[][] receiverName = null; - int receiverStart = -1; - int receiverEnd = -1; - - if (receiver instanceof QualifiedNameReference) { - QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) receiver; - - receiverName = qualifiedNameReference.tokens; - - if (receiverName.length != 1) return; - - receiverStart = (int) (qualifiedNameReference.sourcePositions[0] >>> 32); - receiverEnd = (int) qualifiedNameReference.sourcePositions[qualifiedNameReference.tokens.length - 1] + 1; - - // if (local instanceof X) local.| - // if (field instanceof X) field.| - if (instanceOfExpression.expression instanceof SingleNameReference && - ((SingleNameReference)instanceOfExpression.expression).binding == qualifiedBinding && - (qualifiedBinding instanceof LocalVariableBinding || qualifiedBinding instanceof FieldBinding)) { - findFromAnotherReceiver = true; - } - - // if (this.field instanceof X) field.| - if (instanceOfExpression.expression instanceof FieldReference) { - FieldReference fieldReference = (FieldReference)instanceOfExpression.expression; - - if (fieldReference.receiver instanceof ThisReference && - qualifiedBinding instanceof FieldBinding && - fieldReference.binding == qualifiedBinding) { - findFromAnotherReceiver = true; - } - } - } else if (receiver instanceof FieldReference) { - FieldReference fieldReference1 = (FieldReference) receiver; - - receiverStart = fieldReference1.sourceStart; - receiverEnd = fieldReference1.sourceEnd + 1; - - if (fieldReference1.receiver instanceof ThisReference) { - - receiverName = new char[][] {THIS, fieldReference1.token}; - - // if (field instanceof X) this.field.| + while(true) { + if (!(ifStatement.condition instanceof InstanceOfExpression)) return; + + InstanceOfExpression instanceOfExpression = (InstanceOfExpression) ifStatement.condition; + + TypeReference instanceOfType = instanceOfExpression.type; + + if (instanceOfType.resolvedType == null) return; + + boolean findFromAnotherReceiver = false; + + char[][] receiverName = null; + int receiverStart = -1; + int receiverEnd = -1; + + if (receiver instanceof QualifiedNameReference) { + QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) receiver; + + receiverName = qualifiedNameReference.tokens; + + if (receiverName.length != 1) return; + + receiverStart = (int) (qualifiedNameReference.sourcePositions[0] >>> 32); + receiverEnd = (int) qualifiedNameReference.sourcePositions[qualifiedNameReference.tokens.length - 1] + 1; + + // if (local instanceof X) local.| + // if (field instanceof X) field.| if (instanceOfExpression.expression instanceof SingleNameReference && - ((SingleNameReference)instanceOfExpression.expression).binding == fieldReference1.binding) { + ((SingleNameReference)instanceOfExpression.expression).binding == qualifiedBinding && + (qualifiedBinding instanceof LocalVariableBinding || qualifiedBinding instanceof FieldBinding)) { findFromAnotherReceiver = true; } - - // if (this.field instanceof X) this.field.| + + // if (this.field instanceof X) field.| if (instanceOfExpression.expression instanceof FieldReference) { - FieldReference fieldReference2 = (FieldReference)instanceOfExpression.expression; - - if (fieldReference2.receiver instanceof ThisReference && - fieldReference2.binding == fieldReference1.binding) { + FieldReference fieldReference = (FieldReference)instanceOfExpression.expression; + + if (fieldReference.receiver instanceof ThisReference && + qualifiedBinding instanceof FieldBinding && + fieldReference.binding == qualifiedBinding) { findFromAnotherReceiver = true; } } + } else if (receiver instanceof FieldReference) { + FieldReference fieldReference1 = (FieldReference) receiver; + + receiverStart = fieldReference1.sourceStart; + receiverEnd = fieldReference1.sourceEnd + 1; + + if (fieldReference1.receiver instanceof ThisReference) { + + receiverName = new char[][] {THIS, fieldReference1.token}; + + // if (field instanceof X) this.field.| + if (instanceOfExpression.expression instanceof SingleNameReference && + ((SingleNameReference)instanceOfExpression.expression).binding == fieldReference1.binding) { + findFromAnotherReceiver = true; + } + + // if (this.field instanceof X) this.field.| + if (instanceOfExpression.expression instanceof FieldReference) { + FieldReference fieldReference2 = (FieldReference)instanceOfExpression.expression; + + if (fieldReference2.receiver instanceof ThisReference && + fieldReference2.binding == fieldReference1.binding) { + findFromAnotherReceiver = true; + } + } + } } - } - - if (findFromAnotherReceiver) { - TypeBinding receiverTypeBinding = instanceOfType.resolvedType; - char[] castedReceiver = null; - - char[] castedTypeChars = CharOperation.concatWith(instanceOfType.getTypeName(), '.'); - if(this.source != null) { - int memberRefStart = this.startPosition; - - char[] receiverChars = CharOperation.subarray(this.source, receiverStart, receiverEnd); - char[] dotChars = CharOperation.subarray(this.source, receiverEnd, memberRefStart); - - castedReceiver = - CharOperation.concat( + + if (findFromAnotherReceiver) { + TypeBinding receiverTypeBinding = instanceOfType.resolvedType; + char[] castedReceiver = null; + + char[] castedTypeChars = CharOperation.concatWith(instanceOfType.getTypeName(), '.'); + if(this.source != null) { + int memberRefStart = this.startPosition; + + char[] receiverChars = CharOperation.subarray(this.source, receiverStart, receiverEnd); + char[] dotChars = CharOperation.subarray(this.source, receiverEnd, memberRefStart); + + castedReceiver = CharOperation.concat( - '(', CharOperation.concat( - CharOperation.concat('(', castedTypeChars, ')'), - receiverChars), - ')'), - dotChars); - } else { - castedReceiver = - CharOperation.concat( + '(', + CharOperation.concat( + CharOperation.concat('(', castedTypeChars, ')'), + receiverChars), + ')'), + dotChars); + } else { + castedReceiver = CharOperation.concat( - '(', CharOperation.concat( - CharOperation.concat('(', castedTypeChars, ')'), - CharOperation.concatWith(receiverName, '.')), - ')'), - DOT); + '(', + CharOperation.concat( + CharOperation.concat('(', castedTypeChars, ')'), + CharOperation.concatWith(receiverName, '.')), + ')'), + DOT); + } + + if (castedReceiver == null) return; + + int oldStartPosition = this.startPosition; + this.startPosition = receiverStart; + + findFieldsAndMethods( + this.completionToken, + receiverTypeBinding, + scope, + fieldsFound, + methodsFound, + invocationSite, + invocationScope, + false, + false, + null, + null, + null, + false, + castedReceiver, + receiverStart, + receiverEnd); + + this.startPosition = oldStartPosition; } - - if (castedReceiver == null) return; - - int oldStartPosition = this.startPosition; - this.startPosition = receiverStart; - - findFieldsAndMethods( - this.completionToken, - receiverTypeBinding, - scope, - fieldsFound, - methodsFound, - invocationSite, - invocationScope, - false, - false, - null, - null, - null, - false, - castedReceiver, - receiverStart, - receiverEnd); - - this.startPosition = oldStartPosition; - } + // traverse the enclosing node to find the instanceof expression corresponding + // to the completion node (if any) + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 + if(ifStatement.thenStatement instanceof IfStatement) { + ifStatement = (IfStatement) ifStatement.thenStatement; + } else { + break; + } + } } private void findFieldsAndMethodsFromFavorites( char[] token, Index: codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core/codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java,v retrieving revision 1.210 diff -u -r1.210 CompletionParser.java --- codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java 18 Feb 2010 06:50:57 -0000 1.210 +++ codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java 18 Mar 2010 09:11:58 -0000 @@ -564,12 +564,15 @@ if(expression == this.assistNode || (expression instanceof Assignment // https://bugs.eclipse.org/bugs/show_bug.cgi?id=287939 && ((Assignment)expression).expression == this.assistNode - && (this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)) + && (this.expressionPtr > 0 && expressionStackHasInstanceOfExpression(this.expressionPtr-1))) + || (expression instanceof BinaryExpression // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 + && (((BinaryExpression)expression).left == this.assistNode || ((BinaryExpression)expression).right == this.assistNode) + && (this.expressionPtr > 0 && expressionStackHasInstanceOfExpression(this.expressionPtr-1))) || (expression instanceof AllocationExpression && ((AllocationExpression)expression).type == this.assistNode)){ buildMoreCompletionContext(expression); if (this.assistNodeParent == null - && expression instanceof Assignment) { + && (expression instanceof Assignment || expression instanceof BinaryExpression)) { this.assistNodeParent = detector.getCompletionNodeParent(); } return; @@ -1066,41 +1069,62 @@ int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER); int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER); int index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex; - - if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) { - Expression condition = (Expression)this.elementObjectInfoStack[index]; - - // If currentElement is a RecoveredLocalVariable then it can be contained in the if statement - if (this.currentElement instanceof RecoveredLocalVariable && - this.currentElement.parent instanceof RecoveredBlock) { - RecoveredLocalVariable recoveredLocalVariable = (RecoveredLocalVariable) this.currentElement; - if (recoveredLocalVariable.localDeclaration.initialization == null && - statement instanceof Expression && - condition.sourceStart < recoveredLocalVariable.localDeclaration.sourceStart) { - this.currentElement.add(statement, 0); - - statement = recoveredLocalVariable.updatedStatement(0, new HashSet()); - - // RecoveredLocalVariable must be removed from its parent because the IfStatement will be added instead - RecoveredBlock recoveredBlock = (RecoveredBlock) recoveredLocalVariable.parent; - recoveredBlock.statements[--recoveredBlock.statementCount] = null; - - this.currentElement = recoveredBlock; - + IfStatement ifStatement = null; + while (index >= 0) { + // Try to find an enclosing if statement even if one is not found immediately preceding the completion node. + if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) { + Expression condition = (Expression)this.elementObjectInfoStack[index]; + + // If currentElement is a RecoveredLocalVariable then it can be contained in the if statement + if (this.currentElement instanceof RecoveredLocalVariable && + this.currentElement.parent instanceof RecoveredBlock) { + RecoveredLocalVariable recoveredLocalVariable = (RecoveredLocalVariable) this.currentElement; + if (recoveredLocalVariable.localDeclaration.initialization == null && + statement instanceof Expression && + condition.sourceStart < recoveredLocalVariable.localDeclaration.sourceStart) { + this.currentElement.add(statement, 0); + + statement = recoveredLocalVariable.updatedStatement(0, new HashSet()); + + // RecoveredLocalVariable must be removed from its parent because the IfStatement will be added instead + RecoveredBlock recoveredBlock = (RecoveredBlock) recoveredLocalVariable.parent; + recoveredBlock.statements[--recoveredBlock.statementCount] = null; + + this.currentElement = recoveredBlock; + + } } + + ifStatement = + new IfStatement( + condition, + statement, + condition.sourceStart, + statement.sourceEnd); + index--; + break; } - - IfStatement ifStatement = - new IfStatement( - condition, - statement, - condition.sourceStart, - statement.sourceEnd); - this.enclosingNode = ifStatement; - return ifStatement; + index--; } - - return statement; + if (ifStatement == null) { + return statement; + } + // collect all if statements with instanceof expressions that enclose the completion node + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 + while(index >= 0) { + if (this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] instanceof InstanceOfExpression) { + InstanceOfExpression condition = (InstanceOfExpression)this.elementObjectInfoStack[index]; + ifStatement = + new IfStatement( + condition, + ifStatement, + condition.sourceStart, + statement.sourceEnd); + } + index--; + } + this.enclosingNode = ifStatement; + return ifStatement; } private void buildMoreGenericsCompletionContext(ASTNode node, boolean consumeTypeArguments) { int kind = topKnownElementKind(COMPLETION_OR_ASSIST_PARSER); @@ -4793,4 +4817,20 @@ return field; } } + +/* + * To find out if the expressionStack has an instanceof expression + * at the given startIndex or at one prior to that + * @see https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 + */ +private boolean expressionStackHasInstanceOfExpression(int startIndex) { + int indexInstanceOf = startIndex; + while (indexInstanceOf >= 0) { + if (this.expressionStack[indexInstanceOf] instanceof InstanceOfExpression) { + return true; + } + indexInstanceOf--; + } + return false; +} } \ No newline at end of file #P org.eclipse.jdt.core.tests.model Index: src/org/eclipse/jdt/core/tests/model/CompletionTests.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.jdt.core.tests.model/src/org/eclipse/jdt/core/tests/model/CompletionTests.java,v retrieving revision 1.211 diff -u -r1.211 CompletionTests.java --- src/org/eclipse/jdt/core/tests/model/CompletionTests.java 18 Feb 2010 06:51:07 -0000 1.211 +++ src/org/eclipse/jdt/core/tests/model/CompletionTests.java 18 Mar 2010 09:12:17 -0000 @@ -2369,7 +2369,7 @@ requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 -// unsupported case +// originally unsupported case - fixed with https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 public void testCompletionAfterInstanceof03_05() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( @@ -2394,8 +2394,13 @@ int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; int start1 = str.lastIndexOf("equal") + "".length(); int end1 = start1 + "equal".length(); + int start2 = str.lastIndexOf("a.equal"); + int end2 = start2 + "a.equal".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); assertResults( - "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}", + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" + + "equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 @@ -2504,7 +2509,7 @@ requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 -// unsupported case +// originally unsupported case - fixed with https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 public void testCompletionAfterInstanceof06_01() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( @@ -2529,12 +2534,17 @@ int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; int start1 = str.lastIndexOf("equal") + "".length(); int end1 = start1 + "equal".length(); + int start2 = str.lastIndexOf("a.equal"); + int end2 = start2 + "a.equal".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); assertResults( - "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}", + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" + + "equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 -// unsupported case +// originally unsupported case - fixed with https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 public void testCompletionAfterInstanceof06_02() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( @@ -2559,12 +2569,17 @@ int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; int start1 = str.lastIndexOf("equal") + "".length(); int end1 = start1 + "equal".length(); + int start2 = str.lastIndexOf("a.equal"); + int end2 = start2 + "a.equal".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); assertResults( - "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}", + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" + + "equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 -// unsupported case +// originally unsupported case - fixed with https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 public void testCompletionAfterInstanceof07() throws JavaModelException { this.workingCopies = new ICompilationUnit[1]; this.workingCopies[0] = getWorkingCopy( @@ -2591,8 +2606,13 @@ int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; int start1 = str.lastIndexOf("equal") + "".length(); int end1 = start1 + "equal".length(); + int start2 = str.lastIndexOf("a.equal"); + int end2 = start2 + "a.equal".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); assertResults( - "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}", + "equals[METHOD_REF]{equals(), Ljava.lang.Object;, (Ljava.lang.Object;)Z, equals, (obj), replace["+start1+", "+end1+"], token["+start1+", "+end1+"], " + (relevance1) + "}\n" + + "equalsFoo[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).equalsFoo(), Ltest.CompletionAfterInstanceOf;, ()V, Ltest.CompletionAfterInstanceOf;, equalsFoo, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", requestor.getResults()); } // https://bugs.eclipse.org/bugs/show_bug.cgi?id=193909 @@ -20766,4 +20786,428 @@ "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", requestor.getResults()); } + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly after an instanceof even if there are other expressions in between +// instanceof and the place where code assist is requested. +public void testBug304006a() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == 1)\n" + + " i = a.r\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={I}\n" + + "expectedTypesKeys={I}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006b() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == 1 && a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006c() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == 1 || a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006d() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == 1 & a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006e() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == 1 | a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006f() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1, j;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (j = a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006g() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1, j;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i == a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly in a binary expression after an instanceof +public void testBug304006h() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object a){\n" + + " int i = 1, j;\n" + + " if (a instanceof CompletionAfterInstanceOf) {" + + " if (i != a.r)\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "a.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("a.r"); + int end2 = start2 + "a.r".length(); + int start3 = str.lastIndexOf("a."); + int end3 = start3 + "a".length(); + + assertResults( + "expectedTypesSignatures={Z}\n" + + "expectedTypesKeys={Z}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)a).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly even when the completion node +// is preceeded by a number of unrelated instanceof expressions +public void testBug304006i() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object abc, Object xyz){\n" + + " int i = 1, j;\n" + + " if(i == 1)\n" + + " if (abc instanceof CompletionAfterInstanceOf)\n" + + " if(xyz instanceof CompletionAfterInstanceOf){\n" + + " j = 1;\n" + + " if(j == 1)\n" + + " i = abc.r \n" + + " }\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "abc.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("abc.r"); + int end2 = start2 + "abc.r".length(); + int start3 = str.lastIndexOf("abc."); + int end3 = start3 + "abc".length(); + + assertResults( + "expectedTypesSignatures={I}\n" + + "expectedTypesKeys={I}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)abc).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} + +// https://bugs.eclipse.org/bugs/show_bug.cgi?id=304006 +// To verify that autocast works correctly even when the completion node +// is preceeded by a number of unrelated instanceof expressions +public void testBug304006j() throws JavaModelException { + this.workingCopies = new ICompilationUnit[1]; + this.workingCopies[0] = getWorkingCopy( + "/Completion/src/test/CompletionAfterInstanceOf.java", + "package test;\n" + + "public class CompletionAfterInstanceOf {\n" + + " public int foo() { return 1; }\n" + + " public int returnZero(){ return 0;}\n" + + " void bar(Object abc, Object xyz){\n" + + " int i = 1, j;\n" + + " if(i == 1)\n" + + " if (abc instanceof CompletionAfterInstanceOf)\n" + + " if(xyz instanceof CompletionAfterInstanceOf){\n" + + " j = 1;\n" + + " if(j == 1)\n" + + " i = xyz.r \n" + + " }\n" + + " }\n" + + "}\n"); + + CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, false, true, true, true, true); + requestor.allowAllRequiredProposals(); + String str = this.workingCopies[0].getSource(); + String completeBehind = "xyz.r"; + int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length(); + this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner); + + int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED + R_EXACT_EXPECTED_TYPE; + int start1 = str.lastIndexOf("r") + "".length(); + int end1 = start1 + "r".length(); + int start2 = str.lastIndexOf("xyz.r"); + int end2 = start2 + "xyz.r".length(); + int start3 = str.lastIndexOf("xyz."); + int end3 = start3 + "xyz".length(); + + assertResults( + "expectedTypesSignatures={I}\n" + + "expectedTypesKeys={I}", + requestor.getContext()); + assertResults( + "returnZero[METHOD_REF_WITH_CASTED_RECEIVER]{((CompletionAfterInstanceOf)xyz).returnZero(), Ltest.CompletionAfterInstanceOf;, ()I, Ltest.CompletionAfterInstanceOf;, returnZero, null, replace["+start2+", "+end2+"], token["+start1+", "+end1+"], receiver["+start3+", "+end3+"], " + (relevance1) + "}", + requestor.getResults()); +} }