1 11 package org.eclipse.jdt.internal.compiler.ast; 12 13 import org.eclipse.jdt.internal.compiler.ASTVisitor; 14 import org.eclipse.jdt.internal.compiler.codegen.*; 15 import org.eclipse.jdt.internal.compiler.flow.*; 16 import org.eclipse.jdt.internal.compiler.impl.Constant; 17 import org.eclipse.jdt.internal.compiler.lookup.*; 18 19 public class InstanceOfExpression extends OperatorExpression { 20 21 public Expression expression; 22 public TypeReference type; 23 24 public InstanceOfExpression(Expression expression, TypeReference type) { 25 26 this.expression = expression; 27 this.type = type; 28 type.bits |= IgnoreRawTypeCheck; this.bits |= INSTANCEOF << OperatorSHIFT; 30 this.sourceStart = expression.sourceStart; 31 this.sourceEnd = type.sourceEnd; 32 } 33 34 public FlowInfo analyseCode( 35 BlockScope currentScope, 36 FlowContext flowContext, 37 FlowInfo flowInfo) { 38 LocalVariableBinding local = this.expression.localVariableBinding(); 39 if (local != null && (local.type.tagBits & TagBits.IsBaseType) == 0) { 40 flowContext.recordUsingNullReference(currentScope, local, 41 this.expression, FlowContext.CAN_ONLY_NULL | FlowContext.IN_INSTANCEOF, flowInfo); 42 flowInfo = expression.analyseCode(currentScope, flowContext, flowInfo). 43 unconditionalInits(); 44 FlowInfo initsWhenTrue = flowInfo.copy(); 45 initsWhenTrue.markAsComparedEqualToNonNull(local); 46 return FlowInfo.conditional(initsWhenTrue, flowInfo.copy()); 48 } 49 return expression.analyseCode(currentScope, flowContext, flowInfo). 50 unconditionalInits(); 51 } 52 53 60 public void generateCode( 61 BlockScope currentScope, 62 CodeStream codeStream, 63 boolean valueRequired) { 64 65 int pc = codeStream.position; 66 expression.generateCode(currentScope, codeStream, true); 67 codeStream.instance_of(type.resolvedType); 68 if (valueRequired) { 69 codeStream.generateImplicitConversion(implicitConversion); 70 } else { 71 codeStream.pop(); 72 } 73 codeStream.recordPositionsFrom(pc, this.sourceStart); 74 } 75 76 public StringBuffer printExpressionNoParenthesis(int indent, StringBuffer output) { 77 78 expression.printExpression(indent, output).append(" instanceof "); return type.print(0, output); 80 } 81 82 public TypeBinding resolveType(BlockScope scope) { 83 84 constant = Constant.NotAConstant; 85 TypeBinding expressionType = expression.resolveType(scope); 86 TypeBinding checkedType = type.resolveType(scope, true ); 87 if (expressionType == null || checkedType == null) 88 return null; 89 90 if (!checkedType.isReifiable()) { 91 scope.problemReporter().illegalInstanceOfGenericType(checkedType, this); 92 } else if ((expressionType != TypeBinding.NULL && expressionType.isBaseType()) || !checkCastTypesCompatibility(scope, checkedType, expressionType, null)) { 94 scope.problemReporter().notCompatibleTypesError(this, expressionType, checkedType); 95 } 96 return this.resolvedType = TypeBinding.BOOLEAN; 97 } 98 101 public void tagAsUnnecessaryCast(Scope scope, TypeBinding castType) { 102 if (expression.resolvedType != TypeBinding.NULL) 104 scope.problemReporter().unnecessaryInstanceof(this, castType); 105 } 106 public void traverse(ASTVisitor visitor, BlockScope scope) { 107 108 if (visitor.visit(this, scope)) { 109 expression.traverse(visitor, scope); 110 type.traverse(visitor, scope); 111 } 112 visitor.endVisit(this, scope); 113 } 114 } 115 | Popular Tags |