KickJava   Java API By Example, From Geeks To Geeks.

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


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.base.JavaCompiler;
29 import org.aspectj.compiler.base.CodeWriter;
30
31 /**
32   * @grammar assert test [:message]
33   * @child Expr test
34   * @child Expr message
35   */

36
37 public class AssertStmt extends TestStmt {
38
39     public void unparse(CodeWriter writer) {
40         writer.writeKeyword("assert");
41         writer.requiredSpace();
42         writer.write(test);
43         if (getMessage() != null) {
44             writer.optionalSpace();
45             writer.write(":");
46             writer.optionalSpace();
47             writer.write(getMessage());
48         }
49         writer.closeStmt();
50     }
51
52     /* Should this only be used in bcg mode???
53      * (no, because of the next point)
54      * This doesn't work correctly with "moved" code???
55      * (yes, it does, because we use the lexical type to get the field)
56      */

57     public ASTObject postFixAST(final ASTFixerPass fixer) {
58         if (! getCompiler().willGenerateSourceCode()) {
59             return deSugar();
60         }
61
62         if (getOutermostLexicalType() == getOutermostBytecodeType()) {
63             return this;
64         }
65
66         if (getLexicalType().isEffectivelyStatic()) { // may be "effectively"
67
return deSugar();
68         } else {
69             showWarning("compiler limitation: this assert will behave as if inside " + getOutermostBytecodeType().toShortString());
70             return this;
71         }
72     }
73
74     /** can't be called in non-bcg mode if my immediately enclosing
75      * lexical type is non-static
76      */

77     private ASTObject deSugar() {
78         final AST ast = getAST();
79         Field adf =
80             getLexicalType().getTypeDec().getAssertionsDisabledField();
81
82         Type assertionErrorType =
83             getTypeManager().getType("java.lang", "AssertionError");
84         Exprs args = ast.makeExprs();
85         if (message != null) {
86             Expr e = message;
87             if (e.getType().isReferenceType()) {
88                 e = ast.forceCast(getTypeManager().getObjectType(), e);
89             }
90             args.add(e);
91         }
92         Stmt ret = ast.makeIf(ast.makeUnop("!", ast.makeParen(test)),
93                    ast.makeThrow(ast.makeNew(assertionErrorType, args)));
94
95         ret = ast.makeIf(ast.makeUnop("!", ast.makeGet(adf)), ret);
96         return ret;
97
98     }
99
100
101     // ------------------------------
102
// flow
103
public void walkFlow(FlowCheckerPass w) {
104         FlowCheckerPass.Vars v0 = w.getVars();
105
106         w.process(getTest());
107         if (getMessage() != null) w.process(getMessage());
108
109         FlowCheckerPass.Vars v1 = w.getVars();
110         w.setVars(v0);
111     }
112
113     //BEGIN: Generated from @child and @property
114
protected Expr test;
115     public Expr getTest() { return test; }
116     public void setTest(Expr _test) {
117         if (_test != null) _test.setParent(this);
118         test = _test;
119     }
120
121     protected Expr message;
122     public Expr getMessage() { return message; }
123     public void setMessage(Expr _message) {
124         if (_message != null) _message.setParent(this);
125         message = _message;
126     }
127
128     public AssertStmt(SourceLocation location, Expr _test, Expr _message) {
129         super(location);
130         setTest(_test);
131         setMessage(_message);
132     }
133     protected AssertStmt(SourceLocation source) {
134         super(source);
135     }
136
137     public ASTObject copyWalk(CopyWalker walker) {
138         AssertStmt ret = new AssertStmt(getSourceLocation());
139         ret.preCopy(walker, this);
140         if (test != null) ret.setTest( (Expr)walker.process(test) );
141         if (message != null) ret.setMessage( (Expr)walker.process(message) );
142         return ret;
143     }
144
145     public ASTObject getChildAt(int childIndex) {
146         switch(childIndex) {
147         case 0: return test;
148         case 1: return message;
149         default: return super.getChildAt(childIndex);
150         }
151     }
152      public String JavaDoc getChildNameAt(int childIndex) {
153         switch(childIndex) {
154         case 0: return "test";
155         case 1: return "message";
156         default: return super.getChildNameAt(childIndex);
157         }
158     }
159      public void setChildAt(int childIndex, ASTObject child) {
160         switch(childIndex) {
161         case 0: setTest((Expr)child); return;
162         case 1: setMessage((Expr)child); return;
163         default: super.setChildAt(childIndex, child); return;
164         }
165     }
166      public int getChildCount() {
167         return 2;
168     }
169
170     public String JavaDoc getDefaultDisplayName() {
171         return "AssertStmt()";
172     }
173
174     //END: Generated from @child and @property
175
}
176
177
178
Popular Tags