1 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 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 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()) { return deSugar(); 68 } else { 69 showWarning("compiler limitation: this assert will behave as if inside " + getOutermostBytecodeType().toShortString()); 70 return this; 71 } 72 } 73 74 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 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 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 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 getDefaultDisplayName() { 171 return "AssertStmt()"; 172 } 173 174 } 176 177 178 | Popular Tags |