Skip to content

Commit bd735c0

Browse files
mghionobodyiam
authored andcommitted
refactor:Simplify code
1 parent dfbffe5 commit bd735c0

File tree

3 files changed

+31
-40
lines changed

3 files changed

+31
-40
lines changed

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloAnnotationProcessor.java

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,13 @@
2727
import com.ctrip.framework.apollo.spring.util.SpringInjector;
2828
import com.ctrip.framework.apollo.util.ConfigUtil;
2929
import com.google.common.base.Preconditions;
30+
import com.google.common.collect.Sets;
3031
import com.google.gson.Gson;
3132
import java.lang.reflect.Field;
3233
import java.lang.reflect.Method;
3334
import java.lang.reflect.Type;
3435
import java.util.Set;
35-
36-
import com.google.common.collect.Sets;
36+
import javax.annotation.Nullable;
3737
import org.slf4j.Logger;
3838
import org.slf4j.LoggerFactory;
3939
import org.springframework.beans.BeansException;
@@ -120,12 +120,7 @@ private void processApolloConfigChangeListener(final Object bean, final Method m
120120
String[] namespaces = annotation.value();
121121
String[] annotatedInterestedKeys = annotation.interestedKeys();
122122
String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes();
123-
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
124-
@Override
125-
public void onChange(ConfigChangeEvent changeEvent) {
126-
ReflectionUtils.invokeMethod(method, bean, changeEvent);
127-
}
128-
};
123+
ConfigChangeListener configChangeListener = changeEvent -> ReflectionUtils.invokeMethod(method, bean, changeEvent);
129124

130125
Set<String> interestedKeys =
131126
annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null;
@@ -145,18 +140,15 @@ public void onChange(ConfigChangeEvent changeEvent) {
145140
}
146141
}
147142

148-
149143
private void processApolloJsonValue(Object bean, String beanName, Field field) {
150144
ApolloJsonValue apolloJsonValue = AnnotationUtils.getAnnotation(field, ApolloJsonValue.class);
151145
if (apolloJsonValue == null) {
152146
return;
153147
}
154-
String placeholder = apolloJsonValue.value();
155-
Object propertyValue = placeholderHelper
156-
.resolvePropertyValue(this.configurableBeanFactory, beanName, placeholder);
157148

158-
// propertyValue will never be null, as @ApolloJsonValue will not allow that
159-
if (!(propertyValue instanceof String)) {
149+
String placeholder = apolloJsonValue.value();
150+
Object propertyValue = this.resolvePropertyValue(beanName, placeholder);
151+
if (propertyValue == null) {
160152
return;
161153
}
162154

@@ -181,19 +173,16 @@ private void processApolloJsonValue(Object bean, String beanName, Method method)
181173
if (apolloJsonValue == null) {
182174
return;
183175
}
184-
String placeHolder = apolloJsonValue.value();
185-
186-
Object propertyValue = placeholderHelper
187-
.resolvePropertyValue(this.configurableBeanFactory, beanName, placeHolder);
188176

189-
// propertyValue will never be null, as @ApolloJsonValue will not allow that
190-
if (!(propertyValue instanceof String)) {
177+
String placeHolder = apolloJsonValue.value();
178+
Object propertyValue = this.resolvePropertyValue(beanName, placeHolder);
179+
if (propertyValue == null) {
191180
return;
192181
}
193182

194183
Type[] types = method.getGenericParameterTypes();
195184
Preconditions.checkArgument(types.length == 1,
196-
"Ignore @Value setter {}.{}, expecting 1 parameter, actual {} parameters",
185+
"Ignore @ApolloJsonValue setter {}.{}, expecting 1 parameter, actual {} parameters",
197186
bean.getClass().getName(), method.getName(), method.getParameterTypes().length);
198187

199188
boolean accessible = method.isAccessible();
@@ -204,14 +193,26 @@ private void processApolloJsonValue(Object bean, String beanName, Method method)
204193
if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) {
205194
Set<String> keys = placeholderHelper.extractPlaceholderKeys(placeHolder);
206195
for (String key : keys) {
207-
SpringValue springValue = new SpringValue(key, apolloJsonValue.value(), bean, beanName,
208-
method, true);
196+
SpringValue springValue = new SpringValue(key, placeHolder, bean, beanName, method, true);
209197
springValueRegistry.register(this.configurableBeanFactory, key, springValue);
210198
logger.debug("Monitoring {}", springValue);
211199
}
212200
}
213201
}
214202

203+
@Nullable
204+
private Object resolvePropertyValue(String beanName, String placeHolder) {
205+
Object propertyValue = placeholderHelper
206+
.resolvePropertyValue(this.configurableBeanFactory, beanName, placeHolder);
207+
208+
// propertyValue will never be null, as @ApolloJsonValue will not allow that
209+
if (!(propertyValue instanceof String)) {
210+
return null;
211+
}
212+
213+
return propertyValue;
214+
}
215+
215216
private Object parseJsonValue(String json, Type targetType) {
216217
try {
217218
return GSON.fromJson(json, targetType);

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/ApolloProcessor.java

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ public abstract class ApolloProcessor implements BeanPostProcessor, PriorityOrde
3434
@Override
3535
public Object postProcessBeforeInitialization(Object bean, String beanName)
3636
throws BeansException {
37-
Class clazz = bean.getClass();
37+
Class<?> clazz = bean.getClass();
38+
3839
for (Field field : findAllField(clazz)) {
3940
processField(bean, beanName, field);
4041
}
42+
4143
for (Method method : findAllMethod(clazz)) {
4244
processMethod(bean, beanName, method);
4345
}
@@ -59,32 +61,21 @@ public Object postProcessAfterInitialization(Object bean, String beanName) throw
5961
*/
6062
protected abstract void processMethod(Object bean, String beanName, Method method);
6163

62-
6364
@Override
6465
public int getOrder() {
6566
//make it as late as possible
6667
return Ordered.LOWEST_PRECEDENCE;
6768
}
6869

69-
private List<Field> findAllField(Class clazz) {
70+
private List<Field> findAllField(Class<?> clazz) {
7071
final List<Field> res = new LinkedList<>();
71-
ReflectionUtils.doWithFields(clazz, new ReflectionUtils.FieldCallback() {
72-
@Override
73-
public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException {
74-
res.add(field);
75-
}
76-
});
72+
ReflectionUtils.doWithFields(clazz, res::add);
7773
return res;
7874
}
7975

80-
private List<Method> findAllMethod(Class clazz) {
76+
private List<Method> findAllMethod(Class<?> clazz) {
8177
final List<Method> res = new LinkedList<>();
82-
ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
83-
@Override
84-
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
85-
res.add(method);
86-
}
87-
});
78+
ReflectionUtils.doWithMethods(clazz, res::add);
8879
return res;
8980
}
9081
}

apollo-client/src/main/java/com/ctrip/framework/apollo/spring/annotation/SpringValueProcessor.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public Object postProcessBeforeInitialization(Object bean, String beanName)
8787
return bean;
8888
}
8989

90-
9190
@Override
9291
protected void processField(Object bean, String beanName, Field field) {
9392
// register @Value on field

0 commit comments

Comments
 (0)