1 22 package org.jboss.aop.introduction; 23 24 import java.lang.ref.WeakReference ; 25 import java.util.ArrayList ; 26 import java.io.StringReader ; 27 import org.jboss.aop.Advisor; 28 import org.jboss.aop.pointcut.TypeMatcher; 29 import org.jboss.aop.pointcut.Util; 30 import org.jboss.aop.pointcut.ast.ASTStart; 31 import org.jboss.aop.pointcut.ast.ClassExpression; 32 import org.jboss.aop.pointcut.ast.TypeExpressionParser; 33 import org.jboss.aop.pointcut.ast.ParseException; 34 import javassist.CtClass; 35 36 40 public class InterfaceIntroduction 41 { 42 public static class Mixin 43 { 44 protected String classname; 45 protected String [] interfaces; 46 protected String construction; 47 protected boolean trans; 48 49 public Mixin() {} 50 51 public Mixin(String classname, String [] interfaces, String construction, boolean trans) 52 { 53 this.classname = classname; 54 this.interfaces = interfaces; 55 this.construction = construction; 56 this.trans = trans; 57 } 58 59 public String getClassName() 60 { 61 return classname; 62 } 63 64 public String [] getInterfaces() 65 { 66 return interfaces; 67 } 68 69 public String getConstruction() 70 { 71 return construction; 72 } 73 74 public boolean isTransient() 75 { 76 return trans; 77 } 78 79 public void setClassname(String classname) 80 { 81 this.classname = classname; 82 } 83 84 public void setInterfaces(String [] interfaces) 85 { 86 this.interfaces = interfaces; 87 } 88 89 public void setConstruction(String construction) 90 { 91 this.construction = construction; 92 } 93 94 public void setTrans(boolean trans) 95 { 96 this.trans = trans; 97 } 98 99 100 } 101 102 protected String name; 103 protected ArrayList advisors = new ArrayList (); 104 protected String [] interfaces; 105 protected ArrayList mixins = new ArrayList (); 106 protected ClassExpression classExpr; 107 protected ASTStart ast; 108 109 public InterfaceIntroduction() 110 { 111 112 } 113 public InterfaceIntroduction(String name, String exp, String [] interfaces) 114 { 115 this.name = name; 116 this.interfaces = interfaces; 117 this.classExpr = new ClassExpression(exp); 118 } 119 120 public InterfaceIntroduction(String name, ASTStart ast, String [] interfaces) 121 { 122 this.name = name; 123 this.ast = ast; 124 this.interfaces = interfaces; 125 } 126 127 public void setClassExpression(String exp) 128 { 129 this.classExpr = new ClassExpression(exp); 130 } 131 132 public void setTypeExpression(String exp) 133 { 134 try 135 { 136 ast = new TypeExpressionParser(new StringReader (exp)).Start(); 137 } 138 catch (ParseException e) 139 { 140 throw new RuntimeException (e); 141 } 142 } 143 144 public void setMixins(ArrayList mixins) 145 { 146 this.mixins = mixins; 147 } 148 149 public void addMixin(Mixin mixin) 150 { 151 mixins.add(mixin); 152 } 153 154 public void setInterfaces(String [] interfaces) 155 { 156 this.interfaces = interfaces; 157 } 158 159 public void setName(String name) 160 { 161 this.name = name; 162 } 163 164 public String getName() 165 { 166 return name; 167 } 168 169 public String [] getInterfaces() 170 { 171 return interfaces; 172 } 173 174 public ArrayList getMixins() 175 { 176 return mixins; 177 } 178 179 180 public void addAdvisor(Advisor advisor) 181 { 182 advisors.add(new WeakReference (advisor)); 183 advisor.addInterfaceIntroduction(this); 184 } 185 186 public void clearAdvisors() 187 { 188 for (int i = 0; i < advisors.size(); i++) 189 { 190 WeakReference ref = (WeakReference ) advisors.get(i); 191 Advisor advisor = (Advisor) ref.get(); 192 if (advisor != null) 193 advisor.removeInterfaceIntroduction(this); 194 } 195 } 196 197 public boolean equals(Object obj) 198 { 199 if (obj == this) return true; 200 if (!(obj instanceof InterfaceIntroduction)) return false; 201 return ((InterfaceIntroduction) obj).getName().equals(name); 202 } 203 204 public int hashCode() 205 { 206 return name.hashCode(); 207 } 208 209 public String getClassExpr() 210 { 211 if (classExpr == null) return null; 212 return classExpr.getOriginal(); 213 } 214 215 public ASTStart getAst() 216 { 217 return ast; 218 } 219 220 public boolean matches(Advisor advisor, CtClass clazz) throws Exception 221 { 222 if (classExpr != null) 223 return Util.matchesClassExpr(classExpr, clazz, advisor); 224 else 225 { 226 TypeMatcher matcher = new TypeMatcher(advisor, clazz); 227 return ((Boolean ) ast.jjtAccept(matcher, null)).booleanValue(); 228 } 229 } 230 231 public boolean matches(Advisor advisor, Class clazz) 232 { 233 if (classExpr != null) 234 return Util.matchesClassExpr(classExpr, clazz, advisor); 235 else 236 { 237 TypeMatcher matcher = new TypeMatcher(advisor, clazz); 238 return ((Boolean ) ast.jjtAccept(matcher, null)).booleanValue(); 239 } 240 } 241 } 242 | Popular Tags |