|
@@ -0,0 +1,127 @@
|
|
|
+package cn.nosum.framework.aop.support;
|
|
|
+
|
|
|
+
|
|
|
+import cn.nosum.framework.aop.aspect.Advice;
|
|
|
+import cn.nosum.framework.aop.config.AopConfig;
|
|
|
+import cn.nosum.util.StringUtils;
|
|
|
+
|
|
|
+import java.lang.reflect.Method;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+
|
|
|
+public class AdvisedSupport {
|
|
|
+
|
|
|
+
|
|
|
+ private AopConfig config;
|
|
|
+ private Object target;
|
|
|
+ private Class targetClass;
|
|
|
+ private Pattern pointCutClassPattern;
|
|
|
+
|
|
|
+ private Map<Method,Map<String,Advice>> methodCache;
|
|
|
+
|
|
|
+ public AdvisedSupport(AopConfig config) {
|
|
|
+ this.config = config;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private void parse() {
|
|
|
+
|
|
|
+ String pointCut = config.getPointCut()
|
|
|
+ .replaceAll("\\.", "\\\\.")
|
|
|
+ .replaceAll("\\\\.\\*", ".*")
|
|
|
+ .replaceAll("\\(", "\\\\(")
|
|
|
+ .replaceAll("\\)", "\\\\)");
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ String pointCutForClassRegex = pointCut.substring(0, pointCut.lastIndexOf("\\(") - 4);
|
|
|
+ pointCutClassPattern = Pattern.compile("class " + pointCutForClassRegex.substring(pointCutForClassRegex.lastIndexOf(" ") + 1));
|
|
|
+ if (pointCutMath()){
|
|
|
+
|
|
|
+ this.methodCache = new HashMap<Method, Map<String, Advice>>();
|
|
|
+
|
|
|
+ Pattern pointCutPattern = Pattern.compile(pointCut);
|
|
|
+ try{
|
|
|
+
|
|
|
+ Class aspectClass = Class.forName(this.config.getAspectClass());
|
|
|
+ Map<String,Method> aspectMethods = new HashMap<String, Method>();
|
|
|
+
|
|
|
+ for (Method method : aspectClass.getMethods()) {
|
|
|
+ aspectMethods.put(method.getName(),method);
|
|
|
+ }
|
|
|
+
|
|
|
+ for (Method method : this.targetClass.getMethods()) {
|
|
|
+ String methodString = method.toString();
|
|
|
+
|
|
|
+ if(methodString.contains("throws")){
|
|
|
+ methodString = methodString.substring(0,methodString.lastIndexOf("throws")).trim();
|
|
|
+ }
|
|
|
+
|
|
|
+ Matcher matcher = pointCutPattern.matcher(methodString);
|
|
|
+ if(matcher.matches()){
|
|
|
+ Map<String,Advice> advices = new HashMap<String, Advice>();
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(config.getAspectBefore())){
|
|
|
+ advices.put("before",new Advice(aspectClass.newInstance(),aspectMethods.get(config.getAspectBefore())));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(config.getAspectAfter())){
|
|
|
+ advices.put("after",new Advice(aspectClass.newInstance(),aspectMethods.get(config.getAspectAfter())));
|
|
|
+ }
|
|
|
+
|
|
|
+ if(StringUtils.isNotEmpty(config.getAspectAfterThrow())){
|
|
|
+ Advice advice = new Advice(aspectClass.newInstance(),aspectMethods.get(config.getAspectAfterThrow()));
|
|
|
+
|
|
|
+ advice.setThrowName(config.getAspectAfterThrowingName());
|
|
|
+ advices.put("afterThrow",advice);
|
|
|
+ }
|
|
|
+
|
|
|
+ this.methodCache.put(method,advices);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch(Exception e){
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ public Map<String,Advice> getAdvices(Method method) throws NoSuchMethodException {
|
|
|
+
|
|
|
+ Map<String,Advice> cache = methodCache.get(method);
|
|
|
+ if(null == cache){
|
|
|
+
|
|
|
+ Method m = targetClass.getMethod(method.getName(),method.getParameterTypes());
|
|
|
+ cache = methodCache.get(m);
|
|
|
+
|
|
|
+ this.methodCache.put(method,cache);
|
|
|
+ }
|
|
|
+ return cache;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public boolean pointCutMath() {
|
|
|
+ return pointCutClassPattern.matcher(this.targetClass.toString()).matches();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTargetClass(Class<?> targetClass) {
|
|
|
+ this.targetClass = targetClass;
|
|
|
+ parse();
|
|
|
+ }
|
|
|
+
|
|
|
+ public void setTarget(Object target) {
|
|
|
+ this.target = target;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Class getTargetClass() {
|
|
|
+ return targetClass;
|
|
|
+ }
|
|
|
+
|
|
|
+ public Object getTarget() {
|
|
|
+ return target;
|
|
|
+ }
|
|
|
+}
|