1 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 42 43 public class SoftThrowableDec extends Dec implements JpPlannerMaker { 44 45 public Modifiers getModifiers() { return getAST().makeModifiers(0); } 46 47 public String getId() { return "declare"; } 48 public String toShortString() { 49 return "declare soft: " + genTypeName.toShortString(); 50 } 51 public String 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 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 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 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 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 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 getDefaultDisplayName() { 198 return "SoftThrowableDec()"; 199 } 200 201 } 203 204 205 206 | Popular Tags |