All Downloads are FREE. Search and download functionalities are using the official Maven repository.

panda.aop.AbstractClassAgent Maven / Gradle / Ivy

The newest version!
package panda.aop;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.List;

import panda.lang.Classes;
import panda.lang.reflect.Methods;

/**
 * 提供ClassAgent的基础实现,拦截不可能插入Aop代码的Class
 * 

* 传入的Class对象需要满足的条件 *

  • 不能是final或者abstract的 *
  • 必须有非private的构造函数 *

    * 被拦截的方法需要满足的条件
  • 不能是final或者abstract的
  • 不是private的 * * @author wendal([email protected]) */ public abstract class AbstractClassAgent implements ClassAgent { private ArrayList pairs = new ArrayList(); public ClassAgent addInterceptor(MethodMatcher matcher, MethodInterceptor listener) { if (null != listener) pairs.add(new Pair(matcher, listener)); return this; } public Class define(ClassDefiner cd, Class klass) { if (klass.getName().endsWith(CLASSNAME_SUFFIX)) return klass; String newName = klass.getName() + CLASSNAME_SUFFIX; Class newClass = try2Load(newName, cd); if (newClass != null) return newClass; if (checkClass(klass) == false) return klass; Pair2[] pair2s = findMatchedMethod(klass); if (pair2s.length == 0) return klass; Constructor[] constructors = getEffectiveConstructors(klass); newClass = generate(cd, pair2s, newName, klass, constructors); return newClass; } protected abstract Class generate(ClassDefiner cd, Pair2[] pair2s, String newName, Class klass, Constructor[] constructors); @SuppressWarnings("unchecked") protected Constructor[] getEffectiveConstructors(Class klass) { Constructor[] constructors = (Constructor[])klass.getDeclaredConstructors(); List> cList = new ArrayList>(); for (int i = 0; i < constructors.length; i++) { Constructor constructor = constructors[i]; if (Modifier.isPrivate(constructor.getModifiers())) continue; cList.add(constructor); } if (cList.size() == 0) { throw new RuntimeException("No non-private constructor founded,unable to create sub-class!"); } return cList.toArray(new Constructor[cList.size()]); } protected boolean checkClass(Class klass) { if (klass == null) return false; String klass_name = klass.getName(); if (klass_name.endsWith(CLASSNAME_SUFFIX)) return false; if (klass.isInterface() || klass.isArray() || klass.isEnum() || klass.isPrimitive() || klass.isMemberClass() || klass.isAnnotation() || klass.isAnonymousClass()) throw new RuntimeException(klass_name + " is NOT a Top-Class!Creation FAIL!"); if (Modifier.isFinal(klass.getModifiers()) || Modifier.isAbstract(klass.getModifiers())) throw new RuntimeException(klass_name + " is final or abstract!Creation FAIL!"); return true; } @SuppressWarnings("unchecked") protected Class try2Load(String newName, ClassDefiner cd) { try { return (Class)cd.load(newName); } catch (ClassNotFoundException e) { ClassLoader classLoader = getClass().getClassLoader(); try { return (Class)Class.forName(newName, false, classLoader); } catch (ClassNotFoundException e2) { try { return (Class)Classes.getClass(newName, false); } catch (ClassNotFoundException e1) { try { return (Class)classLoader.loadClass(newName); } catch (ClassNotFoundException e3) { } } } } return null; } private Pair2[] findMatchedMethod(Class cls) { List p2 = new ArrayList(); for (Method m : Methods.getDeclaredMethods(cls)) { int mod = m.getModifiers(); if (mod == 0 || Modifier.isStatic(mod) || Modifier.isPrivate(mod) || Modifier.isFinal(mod) || Modifier.isAbstract(mod)) continue; ArrayList mls = new ArrayList(); for (Pair p : pairs) if (p.matcher.match(m)) mls.add(p.listener); if (mls.size() > 0) p2.add(new Pair2(m, mls)); } return p2.toArray(new Pair2[p2.size()]); } protected static class Pair { Pair(MethodMatcher matcher, MethodInterceptor listener) { this.matcher = matcher; this.listener = listener; } MethodMatcher matcher; MethodInterceptor listener; } protected static class Pair2 { Pair2(Method method, ArrayList listeners) { this.method = method; this.listeners = listeners; } public Method method; public ArrayList listeners; } }




  • © 2015 - 2025 Weber Informatics LLC | Privacy Policy