1 24 25 package org.aspectj.compiler.crosscuts.ast; 26 27 import org.aspectj.compiler.base.ast.*; 28 import org.aspectj.compiler.base.*; 29 import org.aspectj.compiler.crosscuts.joinpoints.*; 30 31 32 import java.util.*; 33 34 import org.aspectj.util.*; 35 36 39 40 public class PerSingleton extends PerClause { 41 FieldDec aspectField; 42 43 public String toShortString() { return "issingleton"; } 44 45 public JpPlanner makeInnerPlanner(PlanData planData) { 46 return new JpPlanner() { 47 public FuzzyBoolean fastMatch(JoinPoint jp) { 48 return FuzzyBoolean.MAYBE; 49 } 50 public JpPlan makePlan(JoinPoint jp) { 51 JpPlan plan = new JpPlan(jp); 52 plan.test = maybeMakeTest(jp); 53 plan.setInstanceExpr(makeAspectInstance()); 54 return plan; 55 } 56 }; 57 } 58 59 public JpPlanner makeInitializerPlanner(PlanData planData) { 60 return JpPlanner.NO_PLANNER; 62 } 63 64 String makeAspectFieldName() { 65 return getAST().makeGeneratedName("aspectInstance"); 66 } 67 68 FieldDec makeAspectField() { 69 final AST ast = getAST(); 70 FieldDec ret = ast.makeField( 71 ast.makeModifiers(Modifiers.PUBLIC | Modifiers.FINAL | Modifiers.STATIC), 72 getAspectType(), makeAspectFieldName(), ast.makeNew(getAspectType())); 73 return ret; 75 } 76 77 Expr maybeMakeTest(JoinPoint point) { 79 final AST ast = getAST(); 80 81 if (getAspectType().isSubtypeOf(point.getBytecodeType())) { 82 return ast.makeCall(hasAspectMethod, ast.makeTypeExpr(getAspectType())); 83 } else { 84 return null; 85 } 86 } 87 88 protected MethodDec makeAspectOfMethod() { 89 93 final AST ast = getAST(); 94 BlockStmt body = ast.makeBlock(ast.makeReturn(makeAspectInstance())); 95 return makeAspectOfMethod(ast.makeFormals(), body); 96 } 97 98 99 protected MethodDec makeHasAspectMethod() { 100 104 final AST ast = getAST(); 105 BlockStmt body = ast.makeBlock(ast.makeReturn( 106 ast.makeBinop("!=", makeAspectInstance(), ast.makeNull()))); 107 return makeHasAspectMethod(ast.makeFormals(), body); 108 } 109 110 111 Expr makeAspectInstance() { 112 return getAST().makeGet(aspectField); 113 } 114 115 public Expr makeAspectOfExpr(Expr fromObject) { 116 return getAST().makeCall(aspectOfMethod, getAST().makeTypeExpr(getAspectType())); 117 } 118 119 120 public void setupAspect() { 121 TypeDec typeDec = getAspectDec(); 122 aspectField = makeAspectField(); 123 typeDec.addToBody(aspectField); 124 super.setupAspect(); 125 } 126 127 129 public PerSingleton(SourceLocation location) { 130 super(location); 131 132 } 133 134 public ASTObject copyWalk(CopyWalker walker) { 135 PerSingleton ret = new PerSingleton(getSourceLocation()); 136 ret.preCopy(walker, this); 137 138 return ret; 139 } 140 141 142 public String getDefaultDisplayName() { 143 return "PerSingleton()"; 144 } 145 146 } 148 149 | Popular Tags |