KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > eval > CodeSnippetThisReference


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.eval;
12
13 import org.eclipse.jdt.internal.compiler.ast.ThisReference;
14 import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
15 import org.eclipse.jdt.internal.compiler.impl.Constant;
16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
17 import org.eclipse.jdt.internal.compiler.lookup.FieldBinding;
18 import org.eclipse.jdt.internal.compiler.lookup.InvocationSite;
19 import org.eclipse.jdt.internal.compiler.lookup.MethodScope;
20 import org.eclipse.jdt.internal.compiler.lookup.ReferenceBinding;
21 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
22
23 /**
24  * A this reference inside a code snippet denotes a remote
25  * receiver object (that is, the receiver of the context in the stack frame)
26  */

27 public class CodeSnippetThisReference extends ThisReference implements EvaluationConstants, InvocationSite {
28     
29     EvaluationContext evaluationContext;
30     FieldBinding delegateThis;
31     boolean isImplicit;
32     
33     /**
34      * CodeSnippetThisReference constructor comment.
35      * @param s int
36      * @param sourceEnd int
37      */

38     public CodeSnippetThisReference(int s, int sourceEnd, EvaluationContext evaluationContext, boolean isImplicit) {
39         super(s, sourceEnd);
40         this.evaluationContext = evaluationContext;
41         this.isImplicit = isImplicit;
42     }
43     public boolean checkAccess(MethodScope methodScope) {
44         // this/super cannot be used in constructor call
45
if (this.evaluationContext.isConstructorCall) {
46             methodScope.problemReporter().fieldsOrThisBeforeConstructorInvocation(this);
47             return false;
48         }
49     
50         // static may not refer to this/super
51
if (this.evaluationContext.declaringTypeName == null || this.evaluationContext.isStatic) {
52             methodScope.problemReporter().errorThisSuperInStatic(this);
53             return false;
54         }
55         return true;
56     }
57     public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
58         int pc = codeStream.position;
59         if (valueRequired) {
60             codeStream.aload_0();
61             codeStream.getfield(this.delegateThis);
62         }
63         codeStream.recordPositionsFrom(pc, this.sourceStart);
64     }
65     /**
66      * @see org.eclipse.jdt.internal.compiler.lookup.InvocationSite#genericTypeArguments()
67      */

68     public TypeBinding[] genericTypeArguments() {
69         return null;
70     }
71     public boolean isSuperAccess(){
72         return false;
73     }
74     public boolean isTypeAccess(){
75         return false;
76     }
77     public StringBuffer JavaDoc printExpression(int indent, StringBuffer JavaDoc output){
78         
79         char[] declaringType = this.evaluationContext.declaringTypeName;
80         output.append('(');
81         if (declaringType == null)
82             output.append("<NO DECLARING TYPE>"); //$NON-NLS-1$
83
else
84             output.append(declaringType);
85         return output.append(")this"); //$NON-NLS-1$
86
}
87     public TypeBinding resolveType(BlockScope scope) {
88     
89         // implicit this
90
this.constant = Constant.NotAConstant;
91         TypeBinding snippetType = null;
92         MethodScope methodScope = scope.methodScope();
93         if (!this.isImplicit && !checkAccess(methodScope)) {
94             return null;
95         }
96         snippetType = scope.enclosingSourceType();
97         
98         this.delegateThis = scope.getField(snippetType, DELEGATE_THIS, this);
99         if (this.delegateThis == null || !this.delegateThis.isValidBinding()) {
100             // should not happen
101
// if this happen we should report illegal access to this in a static context
102
methodScope.problemReporter().errorThisSuperInStatic(this);
103             return null;
104         }
105         return this.resolvedType = this.delegateThis.type;
106     }
107     public void setActualReceiverType(ReferenceBinding receiverType) {
108         // ignored
109
}
110     public void setDepth(int depth){
111         // ignored
112
}
113     public void setFieldIndex(int index){
114         // ignored
115
}
116 }
117
Popular Tags