From e05baf69d37153a1fcaa91f94240fe61eab3cddc Mon Sep 17 00:00:00 2001 From: mghio Date: Tue, 16 Aug 2022 23:52:08 +0800 Subject: [PATCH] refactor:Simplify code --- .../annotation/ApolloAnnotationProcessor.java | 47 ++++++++++--------- .../spring/annotation/ApolloProcessor.java | 23 +++------ .../annotation/SpringValueProcessor.java | 1 - 3 files changed, 31 insertions(+), 40 deletions(-) diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java index 3311daa8798..45cae764781 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java @@ -27,13 +27,13 @@ import com.ctrip.framework.apollo.spring.util.SpringInjector; import com.ctrip.framework.apollo.util.ConfigUtil; import com.google.common.base.Preconditions; +import com.google.common.collect.Sets; import com.google.gson.Gson; import java.lang.reflect.Field; import java.lang.reflect.Method; import java.lang.reflect.Type; import java.util.Set; - -import com.google.common.collect.Sets; +import javax.annotation.Nullable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; @@ -120,12 +120,7 @@ private void processApolloConfigChangeListener(final Object bean, final Method m String[] namespaces = annotation.value(); String[] annotatedInterestedKeys = annotation.interestedKeys(); String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes(); - ConfigChangeListener configChangeListener = new ConfigChangeListener() { - @Override - public void onChange(ConfigChangeEvent changeEvent) { - ReflectionUtils.invokeMethod(method, bean, changeEvent); - } - }; + ConfigChangeListener configChangeListener = changeEvent -> ReflectionUtils.invokeMethod(method, bean, changeEvent); Set interestedKeys = annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null; @@ -145,18 +140,15 @@ public void onChange(ConfigChangeEvent changeEvent) { } } - private void processApolloJsonValue(Object bean, String beanName, Field field) { ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(field, ApolloJsonValue.class); if (apolloJsonValue == null) { return; } - String placeholder = apolloJsonValue.value(); - Object propertyValue = placeholderHelper - .resolvePropertyValue(this.configurableBeanFactory, beanName, placeholder); - // propertyValue will never be null, as @ApolloJsonValue will not allow that - if (!(propertyValue instanceof String)) { + String placeholder = apolloJsonValue.value(); + Object propertyValue = this.resolvePropertyValue(beanName, placeholder); + if (propertyValue == null) { return; } @@ -181,19 +173,16 @@ private void processApolloJsonValue(Object bean, String beanName, Method method) if (apolloJsonValue == null) { return; } - String placeHolder = apolloJsonValue.value(); - - Object propertyValue = placeholderHelper - .resolvePropertyValue(this.configurableBeanFactory, beanName, placeHolder); - // propertyValue will never be null, as @ApolloJsonValue will not allow that - if (!(propertyValue instanceof String)) { + String placeHolder = apolloJsonValue.value(); + Object propertyValue = this.resolvePropertyValue(beanName, placeHolder); + if (propertyValue == null) { return; } Type[] types = method.getGenericParameterTypes(); Preconditions.checkArgument(types.length == 1, - "Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters", + "Ignore @ApolloJsonValue setter {}.{}, expecting 1 parameter, actual {} parameters", bean.getClass().getName(), method.getName(), method.getParameterTypes().length); boolean accessible = method.isAccessible(); @@ -204,14 +193,26 @@ private void processApolloJsonValue(Object bean, String beanName, Method method) if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) { Set keys = placeholderHelper.extractPlaceholderKeys(placeHolder); for (String key : keys) { - SpringValue springValue = new SpringValue(key, apolloJsonValue.value(), bean, beanName, - method, true); + SpringValue springValue = new SpringValue(key, placeHolder, bean, beanName, method, true); springValueRegistry.register(this.configurableBeanFactory, key, springValue); logger.debug("Monitoring {}", springValue); } } } + @Nullable + private Object resolvePropertyValue(String beanName, String placeHolder) { + Object propertyValue = placeholderHelper + .resolvePropertyValue(this.configurableBeanFactory, beanName, placeHolder); + + // propertyValue will never be null, as @ApolloJsonValue will not allow that + if (!(propertyValue instanceof String)) { + return null; + } + + return propertyValue; + } + private Object parseJsonValue(String json, Type targetType) { try { return GSON.fromJson(json, targetType); diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloProcessor.java index c745ffb5a80..ce5bbf9d8f7 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloProcessor.java @@ -34,10 +34,12 @@ public abstract class ApolloProcessor implements BeanPostProcessor, PriorityOrde @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { - Class clazz = bean.getClass(); + Class clazz = bean.getClass(); + for (Field field : findAllField(clazz)) { processField(bean, beanName, field); } + for (Method method : findAllMethod(clazz)) { processMethod(bean, beanName, method); } @@ -59,32 +61,21 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw */ protected abstract void processMethod(Object bean, String beanName, Method method); - @Override public int getOrder() { //make it as late as possible return Ordered.LOWEST_PRECEDENCE; } - private List findAllField(Class clazz) { + private List findAllField(Class clazz) { final List res = new LinkedList<>(); - ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() { - @Override - public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { - res.add(field); - } - }); + ReflectionUtils.doWithFields(clazz, res::add); return res; } - private List findAllMethod(Class clazz) { + private List findAllMethod(Class clazz) { final List res = new LinkedList<>(); - ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() { - @Override - public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { - res.add(method); - } - }); + ReflectionUtils.doWithMethods(clazz, res::add); return res; } } diff --git a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java index 860505423f3..3265f55568a 100644 --- a/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java +++ b/apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java @@ -87,7 +87,6 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) return bean; } - @Override protected void processField(Object bean, String beanName, Field field) { // register @Value on field