KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > BangExpr


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base.ast;
26
27 import org.aspectj.compiler.base.*;
28 import org.aspectj.compiler.crosscuts.AccessFixer;
29
30 import java.util.*;
31
32 /**
33  *
34  * @grammar
35  * @child AssignableExpr lhs the value to be mutated
36  */

37 public abstract class BangExpr extends Expr {
38     public Type discoverType() {
39         return lhs.getType();
40     }
41
42     public boolean isLegalStmt() {
43         return true;
44     }
45
46     //INTRO from AccessFixer
47
public ASTObject fixAccessPost(AccessFixer fixer) {
48         if (!(getLhs() instanceof FieldAccessExpr)) return this;
49
50         FieldAccessExpr fieldAccessExpr = (FieldAccessExpr)getLhs();
51         Field field = fieldAccessExpr.getField();
52
53         if (field == null) return this; //XXX should be an error
54

55         if (field.getDeclaringType().isInterface() && !field.isStatic()) {
56             return fixAccessToFieldSet(fieldAccessExpr).setSource(this);
57         }
58         //TypeDec fromTypeDec = getBytecodeTypeDec(); //fixer.fromTypeDec;
59
if (field.isAccessible(this, true)) return this;
60
61         getCompiler().showMessage(" fixing privileged set: " + field.toShortString());
62 // System.out.println(field.getBytecodeTypeDec() + " not accessible from " +
63
// fixer.fromTypeDec);
64

65     // !!! these aren't used right now
66
if (!fixer.apply) {
67             field.getBackdoorGetterMethod();
68             field.getBackdoorSetterMethod();
69             return this;
70         }
71
72         return fixAccessToFieldSet(fieldAccessExpr).setSource(this);
73     }
74
75 // //INTRO from ASTFixerPass
76
// public ASTObject postFixAST(ASTFixerPass fixer) {
77
// if (!(getLhs() instanceof FieldAccessExpr)) return this;
78
//
79
// FieldAccessExpr fieldAccessExpr = (FieldAccessExpr)getLhs();
80
// Field field = fieldAccessExpr.getField();
81
//
82
// if (field == null) return this; //XXX should be an error
83
//
84
// if (field.getDeclaringType().isInterface() && !field.isStatic()) {
85
// return fixAccessToFieldSet(fieldAccessExpr).setSource(this);
86
// }
87
//
88
// return this;
89
// }
90

91     //INTRO from AccessFixer
92
public abstract ASTObject fixAccessToFieldSet(FieldAccessExpr fieldAccessExpr);
93
94     //INTRO from AccessFixer
95
final Expr makeInPlaceSet(FieldAccessExpr expr, String JavaDoc op, Expr rhs) {
96         Expr lhs = expr.getExpr();
97         Field field = expr.getField();
98
99         // this needs to be evaluated before any references to it
100
// there might be computation here
101
Expr newExpr = lhs.makeReference();
102
103         Method getMethod = field.getBackdoorGetterMethod();
104         Method setMethod = field.getBackdoorSetterMethod();
105         final AST ast = getAST();
106
107         return ast.makeCall(setMethod, newExpr,
108                             ast.makeBinop(op,
109                                           ast.makeCall(getMethod, newExpr.makeReference()),
110                                           rhs));
111     }
112
113     // ------------------------------
114
// INTRO from FlowCheckerPass
115

116     // this is overridden in AssignExpr, but is used for both ++x and x++.
117
public void walkFlow(FlowCheckerPass w) {
118         w.process(getLhs());
119         if (getLhs() instanceof VarExpr) {
120             VarExpr lhs = (VarExpr)getLhs();
121             VarDec dec = lhs.getVarDec();
122             if (dec.isFinal()
123                 && ((! dec.isBlank())
124                     || ! w.getVars().isDefinitelyUnassigned(dec))) {
125                 w.showVarError(this, dec, "Final variable " + dec.getId() +
126                                " already has a value");
127             }
128         } else if (getLhs() instanceof FieldAccessExpr) {
129             FieldAccessExpr fa = (FieldAccessExpr) getLhs();
130             FieldDec dec = fa.getFieldDec();
131             if (dec.isFinal()
132                 && ((! dec.isBlank())
133                     || ! w.getVars().isDefinitelyUnassigned(dec))) {
134                 w.showVarError(this, dec, "Final field " + dec.getId() +
135                                " already has a value");
136             }
137         }
138     }
139
140     //BEGIN: Generated from @child and @property
141
protected AssignableExpr lhs;
142     public AssignableExpr getLhs() { return lhs; }
143     public void setLhs(AssignableExpr _lhs) {
144         if (_lhs != null) _lhs.setParent(this);
145         lhs = _lhs;
146     }
147
148     public BangExpr(SourceLocation location, AssignableExpr _lhs) {
149         super(location);
150         setLhs(_lhs);
151     }
152     protected BangExpr(SourceLocation source) {
153         super(source);
154     }
155
156     public ASTObject getChildAt(int childIndex) {
157         switch(childIndex) {
158         case 0: return lhs;
159         default: return super.getChildAt(childIndex);
160         }
161     }
162      public String JavaDoc getChildNameAt(int childIndex) {
163         switch(childIndex) {
164         case 0: return "lhs";
165         default: return super.getChildNameAt(childIndex);
166         }
167     }
168      public void setChildAt(int childIndex, ASTObject child) {
169         switch(childIndex) {
170         case 0: setLhs((AssignableExpr)child); return;
171         default: super.setChildAt(childIndex, child); return;
172         }
173     }
174      public int getChildCount() {
175         return 1;
176     }
177
178     public String JavaDoc getDefaultDisplayName() {
179         return "BangExpr()";
180     }
181
182     //END: Generated from @child and @property
183
}
184
Popular Tags