KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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 QualifiedThisReference extends ThisReference {
20     
21     public TypeReference qualification;
22     ReferenceBinding currentCompatibleType;
23     
24     public QualifiedThisReference(TypeReference name, int sourceStart, int sourceEnd) {
25         super(sourceStart, sourceEnd);
26         qualification = name;
27         name.bits |= IgnoreRawTypeCheck; // no need to worry about raw type usage
28
this.sourceStart = name.sourceStart;
29     }
30
31     public FlowInfo analyseCode(
32         BlockScope currentScope,
33         FlowContext flowContext,
34         FlowInfo flowInfo) {
35
36         return flowInfo;
37     }
38
39     public FlowInfo analyseCode(
40         BlockScope currentScope,
41         FlowContext flowContext,
42         FlowInfo flowInfo,
43         boolean valueRequired) {
44
45         return flowInfo;
46     }
47
48     /**
49      * Code generation for QualifiedThisReference
50      *
51      * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
52      * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
53      * @param valueRequired boolean
54      */

55     public void generateCode(
56         BlockScope currentScope,
57         CodeStream codeStream,
58         boolean valueRequired) {
59
60         int pc = codeStream.position;
61         if (valueRequired) {
62             if ((bits & DepthMASK) != 0) {
63                 Object JavaDoc[] emulationPath =
64                     currentScope.getEmulationPath(this.currentCompatibleType, true /*only exact match*/, false/*consider enclosing arg*/);
65                 codeStream.generateOuterAccess(emulationPath, this, this.currentCompatibleType, currentScope);
66             } else {
67                 // nothing particular after all
68
codeStream.aload_0();
69             }
70         }
71         codeStream.recordPositionsFrom(pc, this.sourceStart);
72     }
73
74     public TypeBinding resolveType(BlockScope scope) {
75
76         constant = Constant.NotAConstant;
77         // X.this is not a param/raw type as denoting enclosing instance
78
TypeBinding type = this.qualification.resolveType(scope, true /* check bounds*/);
79         if (type == null) return null;
80         // X.this is not a param/raw type as denoting enclosing instance
81
type = type.erasure();
82         
83         // resolvedType needs to be converted to parameterized
84
if (type instanceof ReferenceBinding) {
85             this.resolvedType = scope.environment().convertToParameterizedType((ReferenceBinding) type);
86         } else {
87             // error case
88
this.resolvedType = type;
89         }
90         
91         // the qualification MUST exactly match some enclosing type name
92
// It is possible to qualify 'this' by the name of the current class
93
int depth = 0;
94         this.currentCompatibleType = scope.referenceType().binding;
95         while (this.currentCompatibleType != null && this.currentCompatibleType != type) {
96             depth++;
97             this.currentCompatibleType = this.currentCompatibleType.isStatic() ? null : this.currentCompatibleType.enclosingType();
98         }
99         bits &= ~DepthMASK; // flush previous depth if any
100
bits |= (depth & 0xFF) << DepthSHIFT; // encoded depth into 8 bits
101

102         if (this.currentCompatibleType == null) {
103             scope.problemReporter().noSuchEnclosingInstance(type, this, false);
104             return this.resolvedType;
105         }
106
107         // Ensure one cannot write code like: B() { super(B.this); }
108
if (depth == 0) {
109             checkAccess(scope.methodScope());
110         } // if depth>0, path emulation will diagnose bad scenarii
111

112         return this.resolvedType;
113     }
114
115     public StringBuffer JavaDoc printExpression(int indent, StringBuffer JavaDoc output) {
116
117         return qualification.print(0, output).append(".this"); //$NON-NLS-1$
118
}
119
120     public void traverse(
121         ASTVisitor visitor,
122         BlockScope blockScope) {
123
124         if (visitor.visit(this, blockScope)) {
125             qualification.traverse(visitor, blockScope);
126         }
127         visitor.endVisit(this, blockScope);
128     }
129     
130     public void traverse(
131             ASTVisitor visitor,
132             ClassScope blockScope) {
133
134         if (visitor.visit(this, blockScope)) {
135             qualification.traverse(visitor, blockScope);
136         }
137         visitor.endVisit(this, blockScope);
138     }
139 }
140
Popular Tags