1 16 17 package org.springframework.aop.aspectj.annotation; 18 19 import org.aspectj.lang.annotation.Aspect; 20 import org.aspectj.lang.reflect.AjType; 21 import org.aspectj.lang.reflect.AjTypeSystem; 22 import org.aspectj.lang.reflect.PerClauseKind; 23 24 import org.springframework.aop.Pointcut; 25 import org.springframework.aop.aspectj.AspectJExpressionPointcut; 26 import org.springframework.aop.aspectj.TypePatternClassFilter; 27 import org.springframework.aop.framework.AopConfigException; 28 import org.springframework.aop.support.ComposablePointcut; 29 30 43 public class AspectMetadata { 44 45 48 private final AjType ajType; 49 50 55 private final Pointcut perClausePointcut; 56 57 62 private String aspectName; 63 64 65 70 public AspectMetadata(Class aspectClass, String aspectName) { 71 this.aspectName = aspectName; 72 this.ajType = AjTypeSystem.getAjType(aspectClass); 73 74 if (!this.ajType.isAspect()) { 75 throw new IllegalArgumentException ("Class '" + aspectClass.getName() + "' is not an @AspectJ aspect"); 76 } 77 if (this.ajType.getDeclarePrecedence().length > 0) { 78 throw new IllegalArgumentException ("DeclarePrecendence not presently supported in Spring AOP"); 79 } 80 81 switch (this.ajType.getPerClause().getKind()) { 82 case SINGLETON : 83 this.perClausePointcut = Pointcut.TRUE; 84 return; 85 case PERTARGET : case PERTHIS : 86 AspectJExpressionPointcut ajexp = new AspectJExpressionPointcut(); 87 ajexp.setLocation("@Aspect annotation on " + aspectClass.getName()); 88 ajexp.setExpression(findPerClause(aspectClass)); 89 this.perClausePointcut = ajexp; 90 return; 91 case PERTYPEWITHIN : 92 this.perClausePointcut = new ComposablePointcut(new TypePatternClassFilter(findPerClause(aspectClass))); 94 return; 95 default : 96 throw new AopConfigException( 97 "PerClause " + ajType.getPerClause().getKind() + " not supported by Spring AOP for " + aspectClass); 98 } 99 } 100 101 104 private String findPerClause(Class <?> aspectClass) { 105 String str = aspectClass.getAnnotation(Aspect.class).value(); 108 str = str.substring(str.indexOf("(") + 1); 109 str = str.substring(0, str.length() - 1); 110 return str; 111 } 112 113 114 117 public AjType getAjType() { 118 return this.ajType; 119 } 120 121 124 public Class getAspectClass() { 125 return this.ajType.getJavaClass(); 126 } 127 128 131 public String getAspectName() { 132 return this.aspectName; 133 } 134 135 139 public Pointcut getPerClausePointcut() { 140 return this.perClausePointcut; 141 } 142 143 146 public boolean isPerThisOrPerTarget() { 147 PerClauseKind kind = getAjType().getPerClause().getKind(); 148 return (kind == PerClauseKind.PERTARGET || kind == PerClauseKind.PERTHIS); 149 } 150 151 154 public boolean isPerTypeWithin() { 155 PerClauseKind kind = getAjType().getPerClause().getKind(); 156 return (kind == PerClauseKind.PERTYPEWITHIN); 157 } 158 159 162 public boolean isLazilyInstantiated() { 163 return (isPerThisOrPerTarget() || isPerTypeWithin()); 164 } 165 166 } 167 | Popular Tags |