KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > InstanceOfExpression


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

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; // no need to worry about raw type usage
29
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         // no impact upon enclosing try context
47
return FlowInfo.conditional(initsWhenTrue, flowInfo.copy());
48     }
49     return expression.analyseCode(currentScope, flowContext, flowInfo).
50             unconditionalInits();
51 }
52
53     /**
54      * Code generation for instanceOfExpression
55      *
56      * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
57      * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
58      * @param valueRequired boolean
59     */

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 JavaDoc printExpressionNoParenthesis(int indent, StringBuffer JavaDoc output) {
77
78         expression.printExpression(indent, output).append(" instanceof "); //$NON-NLS-1$
79
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 /* check bounds*/);
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()) // 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 (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