KickJava   Java API By Example, From Geeks To Geeks.

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


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.JavaCompiler;
28 import org.aspectj.compiler.base.CompilerObject;
29
30 public class SyntheticGetSet extends CompilerObject {
31     public BangExpr bangExpr;
32
33     public SyntheticGetSet(JavaCompiler compiler, BangExpr bangExpr) {
34         super(compiler);
35         this.bangExpr = bangExpr;
36     }
37
38     public FieldAccessExpr makeGetExpr() {
39         makeInnerExprs();
40         return getExpr;
41     }
42
43     public AssignExpr makeSetExpr() {
44         makeInnerExprs();
45         return setExpr;
46     }
47
48     public BangExpr getBangExpr() { return bangExpr; }
49
50     boolean isConcrete = false;
51     public void makeConcrete() {
52         if (isConcrete) return;
53         //bangExpr.showWarning(" making concrete");
54
isConcrete = true;
55         makeInnerExprs();
56         bangExpr.replaceWith(finishSetExpr(setExpr));
57     }
58
59     Expr finishSetExpr(Expr setExpr) {
60         if (bangExpr instanceof PostfixExpr) {
61             if (bangExpr.isInExprStmt()) return setExpr;
62
63             PostfixExpr postfixExpr = (PostfixExpr)bangExpr;
64             String JavaDoc op = "-";
65             if (postfixExpr.getOp().equals("--")) op = "+";
66             final AST ast = getAST();
67             return ast.makeParen(ast.makeBinop(op, setExpr, ast.makeLiteral(1)));
68         }
69         return setExpr;
70     }
71
72     FieldAccessExpr getExpr;
73     AssignExpr setExpr;
74
75     AssignExpr makeAssignExpr(BangExpr bangExpr) {
76         if (bangExpr instanceof AssignExpr) return (AssignExpr)bangExpr;
77         if (bangExpr instanceof PrefixExpr) {
78             PrefixExpr prefixExpr = (PrefixExpr)bangExpr;
79             String JavaDoc op = prefixExpr.getOp().substring(1);
80             final AST ast = getAST();
81             return ast.makeSet(bangExpr.getLhs(), op, ast.makeLiteral(1));
82         } else if (bangExpr instanceof PostfixExpr) {
83             PostfixExpr postfixExpr = (PostfixExpr)bangExpr;
84             String JavaDoc op = postfixExpr.getOp().substring(1);
85             final AST ast = getAST();
86             return ast.makeSet(bangExpr.getLhs(), op, ast.makeLiteral(1));
87         }
88         return null;
89     }
90
91
92     void makeInnerExprs() {
93         if (getExpr != null) return;
94
95         AST ast = getAST();
96
97         AssignExpr assignExpr = makeAssignExpr(bangExpr);
98         assignExpr.setParent(bangExpr.getParent()); //XXX this call indicates a design flaw
99

100         FieldAccessExpr fieldAccessExpr = (FieldAccessExpr)bangExpr.getLhs();
101         FieldDec fieldDec = fieldAccessExpr.getFieldDec();
102
103         Expr baseExpr = fieldAccessExpr.getExpr();
104         BlockStmt body = bangExpr.getEnclosingCodeDec().getBody();
105
106         String JavaDoc op = assignExpr.getOp();
107
108         Expr newExpr = null;
109         if (baseExpr != null) newExpr = baseExpr.makeReference();
110         getExpr = ast.makeGet(newExpr, fieldDec);
111
112         Expr newValue =
113           ast.makeCast(fieldDec.getType(),
114             ast.makeParen(ast.makeBinop(op, getExpr, assignExpr.getRhs())));
115
116         Expr firstBaseExpr = fieldAccessExpr.getExpr();
117         FieldAccessExpr lhs = ast.makeGet(firstBaseExpr, fieldDec);
118
119         setExpr = ast.makeSet(lhs, newValue);
120     }
121 }
122
Popular Tags