View | Details | Raw Unified | Return to bug 252120
Collapse All | Expand All

(-)compiler/org/eclipse/jdt/internal/compiler/ast/InstanceOfExpression.java (-67 / +57 lines)
Lines 21-40 Link Here
21
	public Expression expression;
21
	public Expression expression;
22
	public TypeReference type;
22
	public TypeReference type;
23
23
24
	public InstanceOfExpression(Expression expression, TypeReference type) {
24
public InstanceOfExpression(Expression expression, TypeReference type) {
25
25
	this.expression = expression;
26
		this.expression = expression;
26
	this.type = type;
27
		this.type = type;
27
	this.bits |= INSTANCEOF << OperatorSHIFT;
28
		type.bits |= IgnoreRawTypeCheck; // no need to worry about raw type usage
28
	this.sourceStart = expression.sourceStart;
29
		this.bits |= INSTANCEOF << OperatorSHIFT;
29
	this.sourceEnd = type.sourceEnd;
30
		this.sourceStart = expression.sourceStart;
30
}
31
		this.sourceEnd = type.sourceEnd;
32
	}
33
31
34
public FlowInfo analyseCode(
32
public FlowInfo analyseCode(BlockScope currentScope, FlowContext flowContext, FlowInfo flowInfo) {
35
		BlockScope currentScope,
36
		FlowContext flowContext,
37
		FlowInfo flowInfo) {
38
	LocalVariableBinding local = this.expression.localVariableBinding();
33
	LocalVariableBinding local = this.expression.localVariableBinding();
39
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
34
	if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) {
40
		flowContext.recordUsingNullReference(currentScope, local,
35
		flowContext.recordUsingNullReference(currentScope, local,
Lines 50-114 Link Here
50
			unconditionalInits();
45
			unconditionalInits();
51
}
46
}
52
47
53
	/**
48
/**
54
	 * Code generation for instanceOfExpression
49
 * Code generation for instanceOfExpression
55
	 *
50
 *
56
	 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
51
 * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
57
	 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
52
 * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
58
	 * @param valueRequired boolean
53
 * @param valueRequired boolean
59
	*/
54
*/
60
	public void generateCode(
55
public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
61
		BlockScope currentScope,
56
	int pc = codeStream.position;
62
		CodeStream codeStream,
57
	this.expression.generateCode(currentScope, codeStream, true);
63
		boolean valueRequired) {
58
	codeStream.instance_of(this.type.resolvedType);
64
59
	if (valueRequired) {
65
		int pc = codeStream.position;
60
		codeStream.generateImplicitConversion(this.implicitConversion);
66
		this.expression.generateCode(currentScope, codeStream, true);
61
	} else {
67
		codeStream.instance_of(this.type.resolvedType);
62
		codeStream.pop();
68
		if (valueRequired) {
69
			codeStream.generateImplicitConversion(this.implicitConversion);
70
		} else {
71
			codeStream.pop();
72
		}
73
		codeStream.recordPositionsFrom(pc, this.sourceStart);
74
	}
63
	}
64
	codeStream.recordPositionsFrom(pc, this.sourceStart);
65
}
75
66
76
	public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
67
public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) {
68
	this.expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
69
	return this.type.print(0, output);
70
}
77
71
78
		this.expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
72
public TypeBinding resolveType(BlockScope scope) {
79
		return this.type.print(0, output);
73
	this.constant = Constant.NotAConstant;
74
	TypeBinding expressionType = this.expression.resolveType(scope);
75
	TypeBinding checkedType = this.type.resolveType(scope, true /* check bounds*/);
76
	if (expressionType == null || checkedType == null)
77
		return null;
78
79
	if (!checkedType.isReifiable()) {
80
		scope.problemReporter().illegalInstanceOfGenericType(checkedType, this);
81
	} else if ((expressionType != TypeBinding.NULL && expressionType.isBaseType()) // disallow autoboxing
82
			|| !checkCastTypesCompatibility(scope, checkedType, expressionType, null)) {
83
		scope.problemReporter().notCompatibleTypesError(this, expressionType, checkedType);
80
	}
84
	}
85
	return this.resolvedType = TypeBinding.BOOLEAN;
86
}
81
87
82
	public TypeBinding resolveType(BlockScope scope) {
88
/**
83
89
 * @see org.eclipse.jdt.internal.compiler.ast.Expression#tagAsUnnecessaryCast(Scope,TypeBinding)
84
		this.constant = Constant.NotAConstant;
90
 */
85
		TypeBinding expressionType = this.expression.resolveType(scope);
91
public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) {
86
		TypeBinding checkedType = this.type.resolveType(scope, true /* check bounds*/);
92
	// null is not instanceof Type, recognize direct scenario
87
		if (expressionType == null || checkedType == null)
93
	if (this.expression.resolvedType != TypeBinding.NULL)
88
			return null;
94
		scope.problemReporter().unnecessaryInstanceof(this, castType);
89
95
}
90
		if (!checkedType.isReifiable()) {
91
			scope.problemReporter().illegalInstanceOfGenericType(checkedType, this);
92
		} else if ((expressionType != TypeBinding.NULL && expressionType.isBaseType()) // disallow autoboxing
93
				|| !checkCastTypesCompatibility(scope, checkedType, expressionType, null)) {
94
			scope.problemReporter().notCompatibleTypesError(this, expressionType, checkedType);
95
		}
96
		return this.resolvedType = TypeBinding.BOOLEAN;
97
	}
98
	/**
99
	 * @see org.eclipse.jdt.internal.compiler.ast.Expression#tagAsUnnecessaryCast(Scope,TypeBinding)
100
	 */
101
	public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) {
102
		// null is not instanceof Type, recognize direct scenario
103
		if (this.expression.resolvedType != TypeBinding.NULL)
104
			scope.problemReporter().unnecessaryInstanceof(this, castType);
105
	}
106
	public void traverse(ASTVisitor visitor, BlockScope scope) {
107
96
108
		if (visitor.visit(this, scope)) {
97
public void traverse(ASTVisitor visitor, BlockScope scope) {
109
			this.expression.traverse(visitor, scope);
98
	if (visitor.visit(this, scope)) {
110
			this.type.traverse(visitor, scope);
99
		this.expression.traverse(visitor, scope);
111
		}
100
		this.type.traverse(visitor, scope);
112
		visitor.endVisit(this, scope);
113
	}
101
	}
102
	visitor.endVisit(this, scope);
103
}
114
}
104
}
(-)src/org/eclipse/jdt/core/tests/compiler/regression/GenericTypeTest.java (-51 / +205 lines)
Lines 5304-5324 Link Here
5304
				"	}\n" +
5304
				"	}\n" +
5305
				"}\n",
5305
				"}\n",
5306
			},
5306
			},
5307
			"----------\n" +
5307
			"----------\n" + 
5308
			"1. ERROR in X.java (at line 4)\n" +
5308
			"1. ERROR in X.java (at line 4)\n" + 
5309
			"	if (t instanceof X<T>) {\n" +
5309
			"	if (t instanceof X<T>) {\n" + 
5310
			"	    ^^^^^^^^^^^^^^\n" +
5310
			"	    ^^^^^^^^^^^^^^\n" + 
5311
			"Cannot perform instanceof check against parameterized type X<T>. Use instead its raw form X since generic type information will be erased at runtime\n" +
5311
			"Cannot perform instanceof check against parameterized type X<T>. Use instead its raw form X since generic type information will be erased at runtime\n" + 
5312
			"----------\n" +
5312
			"----------\n" + 
5313
			"2. ERROR in X.java (at line 6)\n" +
5313
			"2. ERROR in X.java (at line 6)\n" + 
5314
			"	} else if (t instanceof X<String>) {\n" +
5314
			"	} else if (t instanceof X<String>) {\n" + 
5315
			"	           ^^^^^^^^^^^^^^\n" +
5315
			"	           ^^^^^^^^^^^^^^\n" + 
5316
			"Cannot perform instanceof check against parameterized type X<String>. Use instead its raw form X since generic type information will be erased at runtime\n" +
5316
			"Cannot perform instanceof check against parameterized type X<String>. Use instead its raw form X since generic type information will be erased at runtime\n" + 
5317
			"----------\n" +
5317
			"----------\n" + 
5318
			"3. ERROR in X.java (at line 10)\n" +
5318
			"3. ERROR in X.java (at line 10)\n" + 
5319
			"	} else 	if (t instanceof T) {\n" +
5319
			"	} else 	if (t instanceof T) {\n" + 
5320
			"	       	    ^^^^^^^^^^^^^^\n" +
5320
			"	       	    ^^^^^^^^^^^^^^\n" + 
5321
			"Cannot perform instanceof check against type parameter T. Use instead its erasure Object since generic type information will be erased at runtime\n" +
5321
			"Cannot perform instanceof check against type parameter T. Use instead its erasure Object since generic type information will be erased at runtime\n" + 
5322
			"----------\n" + 
5323
			"4. WARNING in X.java (at line 12)\n" + 
5324
			"	} else if (t instanceof X) {\n" + 
5325
			"	                        ^\n" + 
5326
			"X is a raw type. References to generic type X<T> should be parameterized\n" + 
5322
			"----------\n",
5327
			"----------\n",
5323
			null,
5328
			null,
5324
			true,
5329
			true,
Lines 26270-26276 Link Here
26270
		},
26275
		},
26271
		"");
26276
		"");
26272
}
26277
}
26273
// ensure no raw type ref complaint inside instanceof / cast
26278
26274
public void test0830() {
26279
public void test0830() {
26275
	this.runNegativeTest(
26280
	this.runNegativeTest(
26276
		new String[] {
26281
		new String[] {
Lines 26288-26328 Link Here
26288
			"	}\n" +
26293
			"	}\n" +
26289
			"}\n",
26294
			"}\n",
26290
		},
26295
		},
26291
		"----------\n" +
26296
		"----------\n" + 
26292
		"1. WARNING in X.java (at line 5)\n" +
26297
		"1. WARNING in X.java (at line 4)\n" + 
26293
		"	X x = (X) o;\n" +
26298
		"	boolean b = o instanceof X;\n" + 
26294
		"	^\n" +
26299
		"	                         ^\n" + 
26295
		"X is a raw type. References to generic type X<T> should be parameterized\n" +
26300
		"X is a raw type. References to generic type X<T> should be parameterized\n" + 
26296
		"----------\n" +
26301
		"----------\n" + 
26297
		"2. WARNING in X.java (at line 5)\n" +
26302
		"2. WARNING in X.java (at line 5)\n" + 
26298
		"	X x = (X) o;\n" +
26303
		"	X x = (X) o;\n" + 
26299
		"	       ^\n" +
26304
		"	^\n" + 
26300
		"X is a raw type. References to generic type X<T> should be parameterized\n" +
26305
		"X is a raw type. References to generic type X<T> should be parameterized\n" + 
26301
		"----------\n" +
26306
		"----------\n" + 
26302
		"3. WARNING in X.java (at line 6)\n" +
26307
		"3. WARNING in X.java (at line 5)\n" + 
26303
		"	X<String> xs = (X<String>)o;\n" +
26308
		"	X x = (X) o;\n" + 
26304
		"	               ^^^^^^^^^^^^\n" +
26309
		"	       ^\n" + 
26305
		"Type safety: Unchecked cast from Object to X<String>\n" +
26310
		"X is a raw type. References to generic type X<T> should be parameterized\n" + 
26306
		"----------\n" +
26311
		"----------\n" + 
26307
		"4. ERROR in X.java (at line 7)\n" +
26312
		"4. WARNING in X.java (at line 6)\n" + 
26308
		"	Zork z;\n" +
26313
		"	X<String> xs = (X<String>)o;\n" + 
26309
		"	^^^^\n" +
26314
		"	               ^^^^^^^^^^^^\n" + 
26310
		"Zork cannot be resolved to a type\n" +
26315
		"Type safety: Unchecked cast from Object to X<String>\n" + 
26311
		"----------\n" +
26316
		"----------\n" + 
26312
		"5. WARNING in X.java (at line 10)\n" +
26317
		"5. ERROR in X.java (at line 7)\n" + 
26313
		"	List l = (List) al;\n" +
26318
		"	Zork z;\n" + 
26314
		"	^^^^\n" +
26319
		"	^^^^\n" + 
26315
		"List is a raw type. References to generic type List<E> should be parameterized\n" +
26320
		"Zork cannot be resolved to a type\n" + 
26316
		"----------\n" +
26321
		"----------\n" + 
26317
		"6. WARNING in X.java (at line 10)\n" +
26322
		"6. WARNING in X.java (at line 10)\n" + 
26318
		"	List l = (List) al;\n" +
26323
		"	List l = (List) al;\n" + 
26319
		"	         ^^^^^^^^^\n" +
26324
		"	^^^^\n" + 
26320
		"Unnecessary cast from ArrayList<String> to List\n" +
26325
		"List is a raw type. References to generic type List<E> should be parameterized\n" + 
26321
		"----------\n" +
26326
		"----------\n" + 
26322
		"7. WARNING in X.java (at line 10)\n" +
26327
		"7. WARNING in X.java (at line 10)\n" + 
26323
		"	List l = (List) al;\n" +
26328
		"	List l = (List) al;\n" + 
26324
		"	          ^^^^\n" +
26329
		"	         ^^^^^^^^^\n" + 
26325
		"List is a raw type. References to generic type List<E> should be parameterized\n" +
26330
		"Unnecessary cast from ArrayList<String> to List\n" + 
26331
		"----------\n" + 
26332
		"8. WARNING in X.java (at line 10)\n" + 
26333
		"	List l = (List) al;\n" + 
26334
		"	          ^^^^\n" + 
26335
		"List is a raw type. References to generic type List<E> should be parameterized\n" + 
26326
		"----------\n");
26336
		"----------\n");
26327
}
26337
}
26328
//unnecessary cast may be combined with unchecked cast warning
26338
//unnecessary cast may be combined with unchecked cast warning
Lines 47203-47206 Link Here
47203
		},
47213
		},
47204
		"");
47214
		"");
47205
}
47215
}
47216
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120
47217
public void test1398()  throws Exception {
47218
	this.runNegativeTest(
47219
		new String[] {
47220
			"A.java",
47221
			"public class A<T> {\n" + 
47222
			"  void f() {\n" + 
47223
			"    boolean b=null instanceof A; \n" + 
47224
			"	 Zork z;\n" +
47225
			"  }\n" + 
47226
			"}\n",
47227
		},
47228
		"----------\n" + 
47229
		"1. WARNING in A.java (at line 3)\n" + 
47230
		"	boolean b=null instanceof A; \n" + 
47231
		"	                          ^\n" + 
47232
		"A is a raw type. References to generic type A<T> should be parameterized\n" + 
47233
		"----------\n" + 
47234
		"2. ERROR in A.java (at line 4)\n" + 
47235
		"	Zork z;\n" + 
47236
		"	^^^^\n" + 
47237
		"Zork cannot be resolved to a type\n" + 
47238
		"----------\n");
47239
}
47240
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120 - variation
47241
public void test1399()  throws Exception {
47242
	this.runNegativeTest(
47243
		new String[] {
47244
			"A.java",
47245
			"public class A<T> {\n" + 
47246
			"  void f() {\n" + 
47247
			"    boolean b=null instanceof A<?>; \n" + 
47248
			"	 Zork z;\n" +
47249
			"  }\n" + 
47250
			"}\n",
47251
		},
47252
		"----------\n" + 
47253
		"1. ERROR in A.java (at line 4)\n" + 
47254
		"	Zork z;\n" + 
47255
		"	^^^^\n" + 
47256
		"Zork cannot be resolved to a type\n" + 
47257
		"----------\n");
47258
}
47259
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120 - variation
47260
public void test1400()  throws Exception {
47261
	this.runNegativeTest(
47262
		new String[] {
47263
			"A.java",
47264
			"public class A<T> {\n" + 
47265
			"  void f() {\n" + 
47266
			"    Object o = (A)this; \n" + 
47267
			"	 Zork z;\n" +
47268
			"  }\n" + 
47269
			"}\n",
47270
		},
47271
		"----------\n" + 
47272
		"1. WARNING in A.java (at line 3)\n" + 
47273
		"	Object o = (A)this; \n" + 
47274
		"	           ^^^^^^^\n" + 
47275
		"Unnecessary cast from A<T> to A\n" + 
47276
		"----------\n" + 
47277
		"2. WARNING in A.java (at line 3)\n" + 
47278
		"	Object o = (A)this; \n" + 
47279
		"	            ^\n" + 
47280
		"A is a raw type. References to generic type A<T> should be parameterized\n" + 
47281
		"----------\n" + 
47282
		"3. ERROR in A.java (at line 4)\n" + 
47283
		"	Zork z;\n" + 
47284
		"	^^^^\n" + 
47285
		"Zork cannot be resolved to a type\n" + 
47286
		"----------\n");
47287
}
47288
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120 - variation
47289
public void test1401()  throws Exception {
47290
	this.runNegativeTest(
47291
		new String[] {
47292
			"A.java",
47293
			"public class A<T> {\n" + 
47294
			"  void f() {\n" + 
47295
			"    Object o = (A<?>)this; \n" + 
47296
			"	 Zork z;\n" +
47297
			"  }\n" + 
47298
			"}\n",
47299
		},
47300
		"----------\n" + 
47301
		"1. WARNING in A.java (at line 3)\n" + 
47302
		"	Object o = (A<?>)this; \n" + 
47303
		"	           ^^^^^^^^^^\n" + 
47304
		"Unnecessary cast from A<T> to A<?>\n" + 
47305
		"----------\n" + 
47306
		"2. ERROR in A.java (at line 4)\n" + 
47307
		"	Zork z;\n" + 
47308
		"	^^^^\n" + 
47309
		"Zork cannot be resolved to a type\n" + 
47310
		"----------\n");
47311
}
47312
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120 - variation
47313
public void test1402()  throws Exception {
47314
	this.runNegativeTest(
47315
		new String[] {
47316
			"A.java",
47317
			"public class A<T> {\n" + 
47318
			"  void f() {\n" + 
47319
			"    Class<?> c = A.class; \n" + 
47320
			"	 Zork z;\n" +
47321
			"  }\n" + 
47322
			"}\n",
47323
		},
47324
		"----------\n" + 
47325
		"1. ERROR in A.java (at line 4)\n" + 
47326
		"	Zork z;\n" + 
47327
		"	^^^^\n" + 
47328
		"Zork cannot be resolved to a type\n" + 
47329
		"----------\n");
47330
}
47331
//https://bugs.eclipse.org/bugs/show_bug.cgi?id=252120 - variation
47332
public void test1403()  throws Exception {
47333
	this.runNegativeTest(
47334
		new String[] {
47335
			"A.java",
47336
			"public class A<T> {\n" + 
47337
			"  void f() {\n" + 
47338
			"    Class<?> c = A<?>.class; \n" + 
47339
			"	 Zork z;\n" +
47340
			"  }\n" + 
47341
			"}\n",
47342
		},
47343
		"----------\n" + 
47344
		"1. ERROR in A.java (at line 3)\n" + 
47345
		"	Class<?> c = A<?>.class; \n" + 
47346
		"	           ^\n" + 
47347
		"Syntax error on token \"=\", ( expected after this token\n" + 
47348
		"----------\n" + 
47349
		"2. ERROR in A.java (at line 3)\n" + 
47350
		"	Class<?> c = A<?>.class; \n" + 
47351
		"	              ^^^\n" + 
47352
		"Syntax error on token(s), misplaced construct(s)\n" + 
47353
		"----------\n" + 
47354
		"3. ERROR in A.java (at line 3)\n" + 
47355
		"	Class<?> c = A<?>.class; \n" + 
47356
		"	                  ^^^^^\n" + 
47357
		"Syntax error, insert \")\" to complete Expression\n" + 
47358
		"----------\n");
47359
}
47206
}
47360
}
(-)src/org/eclipse/jdt/core/tests/dom/ASTConverter15Test.java (-2 / +4 lines)
Lines 541-547 Link Here
541
		assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
541
		assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
542
		CompilationUnit compilationUnit = (CompilationUnit) result;
542
		CompilationUnit compilationUnit = (CompilationUnit) result;
543
		String expectedProblems =
543
		String expectedProblems =
544
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" +
544
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" + 
545
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" + 
545
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized";
546
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized";
546
		assertProblemsSize(compilationUnit, 2, expectedProblems);
547
		assertProblemsSize(compilationUnit, 2, expectedProblems);
547
		ASTNode node = getASTNode(compilationUnit, 0, 5);
548
		ASTNode node = getASTNode(compilationUnit, 0, 5);
Lines 768-774 Link Here
768
		assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
769
		assertTrue("Not a compilation unit", result.getNodeType() == ASTNode.COMPILATION_UNIT);
769
		CompilationUnit compilationUnit = (CompilationUnit) result;
770
		CompilationUnit compilationUnit = (CompilationUnit) result;
770
		String expectedProblems =
771
		String expectedProblems =
771
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" +
772
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" + 
773
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized\n" + 
772
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized";
774
			"Pair is a raw type. References to generic type Pair<A,B> should be parameterized";
773
		assertProblemsSize(compilationUnit, 2, expectedProblems);
775
		assertProblemsSize(compilationUnit, 2, expectedProblems);
774
		ASTNode node = getASTNode(compilationUnit, 0, 5);
776
		ASTNode node = getASTNode(compilationUnit, 0, 5);

Return to bug 252120