Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: Simplify code #4524

Merged
merged 1 commit into from
Aug 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> interestedKeys =
annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null;
Expand All @@ -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;
}

Expand All @@ -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();
Expand All @@ -204,14 +193,26 @@ private void processApolloJsonValue(Object bean, String beanName, Method method)
if (configUtil.isAutoUpdateInjectedSpringPropertiesEnabled()) {
Set<String> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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<Field> findAllField(Class clazz) {
private List<Field> findAllField(Class<?> clazz) {
final List<Field> 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<Method> findAllMethod(Class clazz) {
private List<Method> findAllMethod(Class<?> clazz) {
final List<Method> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down