View | Details | Raw Unified | Return to bug 261534 | Differences between
and this patch

Collapse All | Expand All

(-)codeassist/org/eclipse/jdt/internal/codeassist/complete/CompletionParser.java (-4 / +26 lines)
Lines 80-85 Link Here
80
	protected static final int K_CONTROL_STATEMENT_DELIMITER = COMPLETION_PARSER + 38;
80
	protected static final int K_CONTROL_STATEMENT_DELIMITER = COMPLETION_PARSER + 38;
81
	protected static final int K_INSIDE_ASSERT_EXCEPTION = COMPLETION_PARSER + 39;
81
	protected static final int K_INSIDE_ASSERT_EXCEPTION = COMPLETION_PARSER + 39;
82
	protected static final int K_INSIDE_FOR_CONDITIONAL = COMPLETION_PARSER + 40;
82
	protected static final int K_INSIDE_FOR_CONDITIONAL = COMPLETION_PARSER + 40;
83
	// added for https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
84
	protected static final int K_BETWEEN_INSTANCEOF_AND_RPAREN = COMPLETION_PARSER + 41;
85
83
86
84
	public final static char[] FAKE_TYPE_NAME = new char[]{' '};
87
	public final static char[] FAKE_TYPE_NAME = new char[]{' '};
85
	public final static char[] FAKE_METHOD_NAME = new char[]{' '};
88
	public final static char[] FAKE_METHOD_NAME = new char[]{' '};
Lines 563-569 Link Here
563
					&& ((this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)
566
					&& ((this.expressionPtr > 0 && this.expressionStack[this.expressionPtr - 1] instanceof InstanceOfExpression)
564
						|| (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression)))
567
						|| (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression)))
565
				|| (expression instanceof AllocationExpression
568
				|| (expression instanceof AllocationExpression
566
					&& ((AllocationExpression)expression).type == this.assistNode)){
569
					&& ((AllocationExpression)expression).type == this.assistNode)
570
				|| (expression instanceof AND_AND_Expression
571
						&& (this.elementPtr >= 0 && this.elementObjectInfoStack[this.elementPtr] instanceof InstanceOfExpression))){
567
				buildMoreCompletionContext(expression);
572
				buildMoreCompletionContext(expression);
568
				if (this.assistNodeParent == null
573
				if (this.assistNodeParent == null
569
					&& expression instanceof Assignment) {
574
					&& expression instanceof Assignment) {
Lines 1062-1069 Link Here
1062
1067
1063
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1068
	int blockIndex = lastIndexOfElement(K_BLOCK_DELIMITER);
1064
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1069
	int controlIndex = lastIndexOfElement(K_CONTROL_STATEMENT_DELIMITER);
1065
	int index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex;
1070
	int index;
1066
1071
	if (controlIndex != -1) {
1072
		index = blockIndex != -1 && controlIndex < blockIndex ? blockIndex : controlIndex;
1073
	} else {
1074
		// To handle the case when the completion is requested before enclosing R_PAREN
1075
		// and an instanceof expression is also present
1076
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
1077
		int instanceOfIndex = lastIndexOfElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
1078
		index = blockIndex != -1 && instanceOfIndex < blockIndex ? blockIndex : instanceOfIndex;
1079
	}
1067
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1080
	if (index != -1 && this.elementInfoStack[index] == IF && this.elementObjectInfoStack[index] != null) {
1068
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1081
		Expression condition = (Expression)this.elementObjectInfoStack[index];
1069
1082
Lines 1086-1092 Link Here
1086
1099
1087
			}
1100
			}
1088
		}
1101
		}
1089
1102
		if (statement instanceof AND_AND_Expression && this.assistNode instanceof Statement) {
1103
			statement = (Statement) this.assistNode;
1104
		}
1090
		IfStatement ifStatement =
1105
		IfStatement ifStatement =
1091
			new IfStatement(
1106
			new IfStatement(
1092
					condition,
1107
					condition,
Lines 2606-2611 Link Here
2606
protected void consumeInstanceOfExpression() {
2621
protected void consumeInstanceOfExpression() {
2607
	super.consumeInstanceOfExpression();
2622
	super.consumeInstanceOfExpression();
2608
	popElement(K_BINARY_OPERATOR);
2623
	popElement(K_BINARY_OPERATOR);
2624
	// to handle https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
2625
	if (topKnownElementKind(COMPLETION_OR_ASSIST_PARSER) == K_BETWEEN_IF_AND_RIGHT_PAREN) {
2626
		pushOnElementStack(K_BETWEEN_INSTANCEOF_AND_RPAREN, IF, this.expressionStack[this.expressionPtr]);
2627
	}
2609
2628
2610
	InstanceOfExpression exp = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
2629
	InstanceOfExpression exp = (InstanceOfExpression) this.expressionStack[this.expressionPtr];
2611
	if(this.assistNode != null && exp.type == this.assistNode) {
2630
	if(this.assistNode != null && exp.type == this.assistNode) {
Lines 3444-3449 Link Here
3444
					case K_BETWEEN_CATCH_AND_RIGHT_PAREN :
3463
					case K_BETWEEN_CATCH_AND_RIGHT_PAREN :
3445
						popElement(K_BETWEEN_CATCH_AND_RIGHT_PAREN);
3464
						popElement(K_BETWEEN_CATCH_AND_RIGHT_PAREN);
3446
						break;
3465
						break;
3466
					case K_BETWEEN_INSTANCEOF_AND_RPAREN :
3467
						popElement(K_BETWEEN_INSTANCEOF_AND_RPAREN);
3468
						//$FALL-THROUGH$
3447
					case K_BETWEEN_IF_AND_RIGHT_PAREN :
3469
					case K_BETWEEN_IF_AND_RIGHT_PAREN :
3448
						if(topKnownElementInfo(COMPLETION_OR_ASSIST_PARSER) == this.bracketDepth) {
3470
						if(topKnownElementInfo(COMPLETION_OR_ASSIST_PARSER) == this.bracketDepth) {
3449
							popElement(K_BETWEEN_IF_AND_RIGHT_PAREN);
3471
							popElement(K_BETWEEN_IF_AND_RIGHT_PAREN);
(-)src/org/eclipse/jdt/core/tests/model/CompletionTests.java (+79 lines)
Lines 21186-21189 Link Here
21186
			"AClass[TYPE_REF]{AClass, test, Ltest.AClass;, null, null, 27}",
21186
			"AClass[TYPE_REF]{AClass, test, Ltest.AClass;, null, null, 27}",
21187
			requestor.getResults());
21187
			requestor.getResults());
21188
}
21188
}
21189
21190
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
21191
// To verify that autocast works correctly even when instanceof expression
21192
// and completion node are in the same binary expression, related by &&
21193
public void testBug261534a() throws JavaModelException {
21194
	this.workingCopies = new ICompilationUnit[1];
21195
	this.workingCopies[0] = getWorkingCopy(
21196
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21197
		"package test;" +
21198
		"class MyString {\n" +
21199
		"	public String toWelcome() {\n" +
21200
		"		return \"welcome\";\n" +
21201
		"	}\n" +
21202
		"}\n" +
21203
		"public class CompletionAfterInstanceOf {\n" +
21204
		"	void foo() {\n" +
21205
		"	Object chars= null;\n" +
21206
		"       if (chars instanceof MyString && chars.to) {\n" +
21207
		"		}\n" +
21208
		"	}\n" +
21209
		"}\n");
21210
21211
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, true, true, true);
21212
	requestor.allowAllRequiredProposals();
21213
	String str = this.workingCopies[0].getSource();
21214
	String completeBehind = "chars instanceof MyString && chars.to";
21215
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21216
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21217
21218
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
21219
	int start1 = str.lastIndexOf("to") + "".length();
21220
	int end1 = start1 + "to".length();
21221
	int start2 = str.lastIndexOf("chars.to");
21222
	int end2 = start2 + "chars.to".length();
21223
	int start3 = str.lastIndexOf("chars.");
21224
	int end3 = start3 + "chars".length();
21225
	
21226
	assertResults(
21227
			"toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, null, null, toString, null, replace[" + start1 + ", " + end1 + "], token[" + start1 + ", " + end1 +"], " + relevance1 + "}\n" +
21228
			"toWelcome[METHOD_REF_WITH_CASTED_RECEIVER]{((MyString)chars).toWelcome(), Ltest.MyString;, ()Ljava.lang.String;, Ltest.MyString;, null, null, toWelcome, null, replace[" + start2 +", " + end2 + "], token[" + start1 + ", " + end1 + "], receiver[" + start3 + ", " + end3 + "], " + relevance1 + "}",
21229
			requestor.getResults());
21230
}
21231
21232
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=261534
21233
// Negative test - To verify that proposals from casted receiver are not obtained
21234
// when completion node is in an OR_OR_Expression along with an instanceof exp.
21235
public void testBug261534b() throws JavaModelException {
21236
	this.workingCopies = new ICompilationUnit[1];
21237
	this.workingCopies[0] = getWorkingCopy(
21238
		"/Completion/src/test/CompletionAfterInstanceOf.java",
21239
		"package test;" +
21240
		"class MyString {\n" +
21241
		"	public String toWelcome() {\n" +
21242
		"		return \"welcome\";\n" +
21243
		"	}\n" +
21244
		"}\n" +
21245
		"public class CompletionAfterInstanceOf {\n" +
21246
		"	void foo() {\n" +
21247
		"	Object chars= null;\n" +
21248
		"       if (chars instanceof MyString || chars.to) {\n" +
21249
		"		}\n" +
21250
		"	}\n" +
21251
		"}\n");
21252
21253
	CompletionTestsRequestor2 requestor = new CompletionTestsRequestor2(true, true, true, true, true, true);
21254
	requestor.allowAllRequiredProposals();
21255
	String str = this.workingCopies[0].getSource();
21256
	String completeBehind = "chars instanceof MyString || chars.to";
21257
	int cursorLocation = str.lastIndexOf(completeBehind) + completeBehind.length();
21258
	this.workingCopies[0].codeComplete(cursorLocation, requestor, this.wcOwner);
21259
21260
	int relevance1 = R_DEFAULT + R_RESOLVED + R_INTERESTING + R_CASE + R_NON_STATIC + R_NON_RESTRICTED;
21261
	int start1 = str.lastIndexOf("to") + "".length();
21262
	int end1 = start1 + "to".length();
21263
	
21264
	assertResults(
21265
			"toString[METHOD_REF]{toString(), Ljava.lang.Object;, ()Ljava.lang.String;, null, null, toString, null, replace[" + start1 + ", " + end1 + "], token[" + start1 + ", " + end1 +"], " + relevance1 + "}",
21266
			requestor.getResults());
21267
}
21189
}
21268
}

Return to bug 261534