org.zodiac.autoconfigure.operator.GenericOperatorAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.operator;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.aop.ClassFilter;
import org.springframework.aop.MethodMatcher;
import org.springframework.aop.Pointcut;
import org.springframework.aop.PointcutAdvisor;
import org.springframework.aop.support.AbstractGenericPointcutAdvisor;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.operator.EmptyOperator;
import org.zodiac.commons.operator.GenericOperatorAdapter;
import org.zodiac.commons.operator.Operator;
import org.zodiac.commons.operator.aopalliance.OperatorAutoHandlerInterceptor;
@SpringBootConfiguration
@ConditionalOnProperty(name = SystemPropertiesConstants.Zodiac.SPRING_MAIN_GENERIC_OPERATOR, havingValue = "true", matchIfMissing = true)
public class GenericOperatorAutoConfiguration {
public GenericOperatorAutoConfiguration() {
super();
}
@Bean
@ConditionalOnMissingBean(value = {Operator.class})
protected Operator> emptyOperator() {
return new EmptyOperator();
}
@Bean
@ConditionalOnBean(value = {Operator.class})
protected OperatorAutoHandlerInterceptor operatorAutoHandlerInterceptor() {
return new OperatorAutoHandlerInterceptor();
}
@Bean
@ConditionalOnBean(value = {OperatorAutoHandlerInterceptor.class})
protected PointcutAdvisor compositeOperatorAspectJExpressionPointcutAdvisor(OperatorAutoHandlerInterceptor advice) {
AbstractGenericPointcutAdvisor advisor = new AbstractGenericPointcutAdvisor() {
private static final long serialVersionUID = 8814415936637159417L;
@Override
public Pointcut getPointcut() {
return new Pointcut() {
private final List EXCLUDE_METHODS = new ArrayList(4) {
private static final long serialVersionUID = -2120922370443621931L;
{
addAll(Arrays.asList(Operator.class.getDeclaredMethods()).stream().map(m -> m.getName())
.collect(Collectors.toList()));
addAll(Arrays.asList(GenericOperatorAdapter.class.getDeclaredMethods()).stream()
.map(m -> m.getName()).collect(Collectors.toList()));
addAll(Arrays.asList(Object.class.getDeclaredMethods()).stream().map(m -> m.getName())
.collect(Collectors.toList()));
}
};
@Override
public MethodMatcher getMethodMatcher() {
return new MethodMatcher() {
@Override
public boolean matches(Method method, Class> targetClass) {
Class> declareClass = method.getDeclaringClass();
int mod = method.getModifiers();
String name = method.getName();
return !Modifier.isAbstract(mod) && Modifier.isPublic(mod)
&& !Modifier.isInterface(declareClass.getModifiers())
&& !EXCLUDE_METHODS.contains(name);
}
@Override
public boolean isRuntime() {
return false;
}
@Override
public boolean matches(Method method, Class> targetClass, Object... args) {
throw new Error("Shouldn't be here");
}
};
}
@Override
public ClassFilter getClassFilter() {
return clazz -> {
return Operator.class.isAssignableFrom(clazz)
&& !GenericOperatorAdapter.class.isAssignableFrom(clazz)
&& !Modifier.isAbstract(clazz.getModifiers())
&& !Modifier.isInterface(clazz.getModifiers());
};
}
};
}
};
advisor.setAdvice(advice);
return advisor;
}
}