Skip to content

Commit

Permalink
refactor: AOP相关调整
Browse files Browse the repository at this point in the history
  • Loading branch information
livk-cloud committed Sep 19, 2024
1 parent db8b26f commit b15dd6f
Show file tree
Hide file tree
Showing 7 changed files with 60 additions and 99 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
*
* @param <A> the type parameter
* @author livk
* @see AnnotationPointcutType
* @see AnnotationAbstractPointcutAdvisor
*/
public abstract class AnnotationAbstractPointcutTypeAdvisor<A extends Annotation>
Expand All @@ -35,17 +34,17 @@ public abstract class AnnotationAbstractPointcutTypeAdvisor<A extends Annotation
@NonNull
@Override
public Pointcut getPointcut() {
return pointcutType().getPointcut(annotationType);
return autoPointcut().getPointcut(annotationType);
}

/**
* <p>
* 用于指定不同的切点类型,默认为{@link AnnotationPointcutType#AUTO}
* 用于指定不同的切点类型,默认为{@link AnnotationAutoPointcut#auto()}
* </p>
* @return the annotation pointcut type
*/
protected AnnotationPointcutType pointcutType() {
return AnnotationPointcutType.AUTO;
protected AnnotationAutoPointcut autoPointcut() {
return AnnotationAutoPointcut.auto();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.livk.commons.aop;

import org.springframework.aop.Pointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

import java.lang.annotation.Annotation;

Expand All @@ -26,7 +27,7 @@
* @author livk
*/
@FunctionalInterface
interface AnnotationAutoPointcut {
public interface AnnotationAutoPointcut {

/**
* 根据注解获取到切点
Expand All @@ -35,4 +36,20 @@ interface AnnotationAutoPointcut {
*/
Pointcut getPointcut(Class<? extends Annotation> annotationType);

static AnnotationAutoPointcut type() {
return AnnotationMatchingPointcut::forClassAnnotation;
}

static AnnotationAutoPointcut method() {
return AnnotationMatchingPointcut::forMethodAnnotation;
}

static AnnotationAutoPointcut typeOrMethod() {
return AnnotationClassOrMethodPointcut::new;
}

static AnnotationAutoPointcut auto() {
return AnnotationTarget.POINTCUT;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*
* @author livk
*/
public class AnnotationClassOrMethodPointcut extends StaticMethodMatcherPointcut {
final class AnnotationClassOrMethodPointcut extends StaticMethodMatcherPointcut {

private final MethodMatcher methodResolver;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
package com.livk.commons.aop;

import com.google.common.collect.Sets;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.springframework.aop.Pointcut;
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut;

import java.lang.annotation.Annotation;
import java.lang.annotation.ElementType;
Expand All @@ -27,6 +31,8 @@
*/
final class AnnotationTarget<A extends Annotation> {

public static final AnnotationAutoPointcut POINTCUT = new AnnotationTargetPointcut();

private static final ConcurrentMap<Class<? extends Annotation>, AnnotationTarget<?>> CACHE = new ConcurrentHashMap<>();

@SuppressWarnings("unchecked")
Expand All @@ -49,4 +55,27 @@ public boolean supportType() {
return elementTypes.contains(ElementType.TYPE);
}

@NoArgsConstructor(access = AccessLevel.PRIVATE)
static final class AnnotationTargetPointcut implements AnnotationAutoPointcut {

@Override
public Pointcut getPointcut(Class<? extends Annotation> annotationType) {
AnnotationTarget<?> target = AnnotationTarget.of(annotationType);
if (target.supportType() && target.supportMethod()) {
return new AnnotationClassOrMethodPointcut(annotationType);
}
else if (target.supportType()) {
return AnnotationMatchingPointcut.forClassAnnotation(annotationType);
}
else if (target.supportMethod()) {
return AnnotationMatchingPointcut.forMethodAnnotation(annotationType);
}
else {
throw new IllegalArgumentException(
"annotation:" + annotationType + " Missing " + Target.class + " TYPE or METHOD information");
}
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ protected Object invoke(MethodInvocation invocation, MyAnnotation annotation) th
}

@Override
protected AnnotationPointcutType pointcutType() {
return AnnotationPointcutType.TYPE;
protected AnnotationAutoPointcut autoPointcut() {
return AnnotationAutoPointcut.type();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,43 +28,43 @@

/**
* <p>
* AnnotationPointcutTypeTest
* AnnotationAutoPointcutTest
* </p>
*
* @author livk
*/
class AnnotationPointcutTypeTest {
class AnnotationAutoPointcutTest {

@Test
void test() throws NoSuchMethodException {
Class<? extends Annotation> annotationType = MyAnnotation.class;
Class<?> type = AopProxyClass.class;
Method method = AopProxyClass.class.getDeclaredMethod("testAop");
{
AnnotationAutoPointcut pointcut = AnnotationPointcutType.AUTO;
AnnotationAutoPointcut pointcut = AnnotationAutoPointcut.auto();
assertEquals(new AnnotationClassOrMethodPointcut(annotationType), pointcut.getPointcut(annotationType));
assertTrue(pointcut.getPointcut(annotationType).getClassFilter().matches(type));
assertTrue(pointcut.getPointcut(annotationType).getMethodMatcher().matches(method, type));
}

{
AnnotationAutoPointcut pointcut = AnnotationPointcutType.TYPE;
AnnotationAutoPointcut pointcut = AnnotationAutoPointcut.type();
assertEquals(AnnotationMatchingPointcut.forClassAnnotation(annotationType),
pointcut.getPointcut(annotationType));
assertTrue(pointcut.getPointcut(annotationType).getClassFilter().matches(type));
assertTrue(pointcut.getPointcut(annotationType).getMethodMatcher().matches(method, type));
}

{
AnnotationAutoPointcut pointcut = AnnotationPointcutType.METHOD;
AnnotationAutoPointcut pointcut = AnnotationAutoPointcut.method();
assertEquals(AnnotationMatchingPointcut.forMethodAnnotation(annotationType),
pointcut.getPointcut(annotationType));
assertTrue(pointcut.getPointcut(annotationType).getClassFilter().matches(type));
assertTrue(pointcut.getPointcut(annotationType).getMethodMatcher().matches(method, type));
}

{
AnnotationAutoPointcut pointcut = AnnotationPointcutType.TYPE_OR_METHOD;
AnnotationAutoPointcut pointcut = AnnotationAutoPointcut.typeOrMethod();
assertEquals(new AnnotationClassOrMethodPointcut(annotationType), pointcut.getPointcut(annotationType));
assertTrue(pointcut.getPointcut(annotationType).getClassFilter().matches(type));
assertTrue(pointcut.getPointcut(annotationType).getMethodMatcher().matches(method, type));
Expand Down

0 comments on commit b15dd6f

Please sign in to comment.