注解部分答案
public static <T> T newInstance(Class<T> clazz) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, InstantiationException {
var inst = clazz.getConstructor().newInstance();
var annotations = clazz.getAnnotations();
var aspects = new LinkedList<IAspect>();
System.out.println(clazz.getAnnotations().length);
System.out.println(clazz.getAnnotation(Aspect.class));
for(var annotation : annotations) {
if(annotation instanceof Aspect) {
var type = ((Aspect) annotation).type();
var aspect= (IAspect)(type.getConstructor().newInstance());
aspects.push(aspect);
}
}
return (T) Proxy.newProxyInstance(
clazz.getClassLoader(),
clazz.getInterfaces(),
new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
aspects.forEach(aspect -> {
aspect.before();
});
var result = method.invoke(inst);
aspects.forEach(aspect -> {
aspect.after();
});
return result;
}
}
);
}