KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > crosscuts > ast > SoftThrowableDec


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.crosscuts.ast;
26
27 import org.aspectj.compiler.crosscuts.joinpoints.*;
28
29 import org.aspectj.compiler.base.ast.*;
30 import org.aspectj.compiler.base.cst.*;
31
32 import org.aspectj.compiler.base.*;
33 import org.aspectj.compiler.crosscuts.AspectJCompiler;
34
35 import java.util.*;
36
37 /**
38  * @grammar declare soft: getTypeName: pointcut;
39  * @child GenTypeName genTypeName
40  * @child Pcd pcd
41  */

42
43 public class SoftThrowableDec extends Dec implements JpPlannerMaker {
44
45     public Modifiers getModifiers() { return getAST().makeModifiers(0); }
46
47     public String JavaDoc getId() { return "declare"; }
48     public String JavaDoc toShortString() {
49         return "declare soft: " + genTypeName.toShortString();
50     }
51     public String JavaDoc getKind() { return "declare soft"; }
52     
53     public void checkSpec() {
54         pcd.checkStatic();
55     }
56     
57     public JpPlanner makePlanner(PlanData planData) {
58         return new WrappedJpPlanner(pcd.makePlanner(planData)) {
59                 public JpPlan makePlan(JoinPoint jp) {
60                     JpPlan plan = super.makePlan(jp);
61                     // these aren't in the static model so they're not affected by this
62
if (jp.getSourceLocation() == null) return JpPlan.NO_PLAN;
63                     if (!plan.isPossible()) return plan;
64                     
65                     ASTObject target = jp.getTargetNode();
66                     if (target instanceof PossibleSoftThrowable) {
67                         ((PossibleSoftThrowable)target).setSoftThrowable();
68                     }
69                     
70                     return new SoftThrowablePlan(getCompiler(), plan);
71                 }
72                 
73                 public boolean isStaticPlanner() { return true; }
74             };
75     }
76
77     private class SoftThrowablePlan extends JpPlan {
78         JpPlan innerPlan;
79         
80         public SoftThrowablePlan(JavaCompiler compiler, JpPlan innerPlan) {
81             super(innerPlan.joinPoint);
82             
83             this.innerPlan = innerPlan;
84         }
85                 
86         public int getPreSortOrder() { return SOFT_THROWABLE; }
87         
88         public String JavaDoc toString() {
89             return "makesoft" + super.toString();
90         }
91         public void wrapJoinPoint(JoinPoint joinPoint) {
92             joinPoint.setStmts(wrapSoftThrowable(joinPoint.getStmts(), joinPoint));
93         }
94     }
95     
96
97     CatchClause makeCatchClause(Type excType, JoinPoint jp) {
98         final AST ast = getAST();
99         
100         FormalDec formalDec = ast.makeFormal(excType, "exc");
101         Expr softThrowable = ast.makeNew(
102                 getTypeManager().getType("org.aspectj.lang", "SoftException"),
103                 ast.makeVar(formalDec));
104         Stmt softThrow = ast.makeBlock(ast.makeThrow(softThrowable));
105         JpPlan plan = genTypeName.makePlan(jp, ast.makeVar(formalDec));
106         
107         Expr test = plan.getDynamicTest();
108
109         BlockStmt body;
110     if (test == null) {
111         body = ast.makeBlock(softThrow);
112     } else {
113         body = ast.makeBlock(ast.makeIf(test,
114                         ast.makeBlock(softThrow),
115                         ast.makeBlock(ast.makeThrow(ast.makeVar(formalDec)))));
116     }
117         return ast.makeCatch(formalDec, body);
118     }
119     
120     Stmts wrapSoftThrowable(Stmts stmts, JoinPoint jp) {
121         final AST ast = getAST();
122         
123         CatchClauses clauses = new CatchClauses(ast.getSourceLocation());
124         
125         Set excTypes = ExceptionFinder.getPossibleExceptions(stmts, true);
126         // we are throwing away some information here that COULD lead to more optimized
127
// but much more complicated code below
128
// instead of filtering we could sort
129
excTypes = Type.filterTopTypes(excTypes);
130         
131         for (Iterator i = excTypes.iterator(); i.hasNext(); ) {
132             Type excType = (Type)i.next();
133             if (genTypeName.matchesInstance(excType).alwaysFalse()) continue;
134             clauses.add(makeCatchClause(excType, jp));
135         }
136         
137         return ast.makeStmts(new TryCatchStmt(ast.getSourceLocation(), ast.makeBlock(stmts), clauses));
138     }
139     
140     //BEGIN: Generated from @child and @property
141
protected GenTypeName genTypeName;
142     public GenTypeName getGenTypeName() { return genTypeName; }
143     public void setGenTypeName(GenTypeName _genTypeName) {
144         if (_genTypeName != null) _genTypeName.setParent(this);
145         genTypeName = _genTypeName;
146     }
147     
148     protected Pcd pcd;
149     public Pcd getPcd() { return pcd; }
150     public void setPcd(Pcd _pcd) {
151         if (_pcd != null) _pcd.setParent(this);
152         pcd = _pcd;
153     }
154     
155     public SoftThrowableDec(SourceLocation location, GenTypeName _genTypeName, Pcd _pcd) {
156         super(location);
157         setGenTypeName(_genTypeName);
158         setPcd(_pcd);
159     }
160     protected SoftThrowableDec(SourceLocation source) {
161         super(source);
162     }
163     
164     public ASTObject copyWalk(CopyWalker walker) {
165         SoftThrowableDec ret = new SoftThrowableDec(getSourceLocation());
166         ret.preCopy(walker, this);
167         if (genTypeName != null) ret.setGenTypeName( (GenTypeName)walker.process(genTypeName) );
168         if (pcd != null) ret.setPcd( (Pcd)walker.process(pcd) );
169         return ret;
170     }
171     
172     public ASTObject getChildAt(int childIndex) {
173         switch(childIndex) {
174         case 0: return genTypeName;
175         case 1: return pcd;
176         default: return super.getChildAt(childIndex);
177         }
178     }
179      public String JavaDoc getChildNameAt(int childIndex) {
180         switch(childIndex) {
181         case 0: return "genTypeName";
182         case 1: return "pcd";
183         default: return super.getChildNameAt(childIndex);
184         }
185     }
186      public void setChildAt(int childIndex, ASTObject child) {
187         switch(childIndex) {
188         case 0: setGenTypeName((GenTypeName)child); return;
189         case 1: setPcd((Pcd)child); return;
190         default: super.setChildAt(childIndex, child); return;
191         }
192     }
193      public int getChildCount() {
194         return 2;
195     }
196     
197     public String JavaDoc getDefaultDisplayName() {
198         return "SoftThrowableDec()";
199     }
200     
201     //END: Generated from @child and @property
202
}
203
204
205
206
Popular Tags