optimize:optimize config module code

pull/520/head
wulingxiao 3 years ago
parent e7d5e2a67d
commit f968892eae

@ -27,6 +27,7 @@ import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.Ordered;
import org.springframework.core.PriorityOrdered;
import org.springframework.lang.NonNull;
import org.springframework.util.ReflectionUtils;
/**
@ -37,9 +38,9 @@ import org.springframework.util.ReflectionUtils;
public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrdered {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName)
throws BeansException {
Class clazz = bean.getClass();
Class<?> clazz = bean.getClass();
for (Field field : findAllField(clazz)) {
processField(bean, beanName, field);
}
@ -50,7 +51,7 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
public Object postProcessAfterInitialization(@NonNull Object bean, @NonNull String beanName) throws BeansException {
return bean;
}
@ -77,15 +78,15 @@ public abstract class PolarisProcessor implements BeanPostProcessor, PriorityOrd
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, field -> 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, method -> res.add(method));
ReflectionUtils.doWithMethods(clazz, res::add);
return res;
}
}

@ -87,7 +87,7 @@ public class SpringValueProcessor extends PolarisProcessor implements BeanFactor
}
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
public Object postProcessBeforeInitialization(Object bean, @NonNull String beanName)
throws BeansException {
if (polarisConfigProperties.isAutoRefresh()) {
super.postProcessBeforeInitialization(bean, beanName);

@ -71,7 +71,7 @@ public class PlaceholderHelper {
if (beanFactory.getBeanExpressionResolver() == null) {
return value;
}
Scope scope = (beanDefinition != null ? beanFactory
Scope scope = (beanDefinition != null && beanDefinition.getScope() != null ? beanFactory
.getRegisteredScope(beanDefinition.getScope()) : null);
return beanFactory.getBeanExpressionResolver()
.evaluate(value, new BeanExpressionContext(beanFactory, scope));
@ -92,7 +92,7 @@ public class PlaceholderHelper {
public Set<String> extractPlaceholderKeys(String propertyString) {
Set<String> placeholderKeys = Sets.newHashSet();
if (Strings.isNullOrEmpty(propertyString) || (!isNormalizedPlaceholder(propertyString) && !isExpressionWithPlaceholder(propertyString))) {
if (!isPlaceholder(propertyString)) {
return placeholderKeys;
}
@ -147,6 +147,11 @@ public class PlaceholderHelper {
return placeholderKeys;
}
private boolean isPlaceholder(String propertyString) {
return !Strings.isNullOrEmpty(propertyString) &&
(isNormalizedPlaceholder(propertyString) || isExpressionWithPlaceholder(propertyString));
}
private boolean isNormalizedPlaceholder(String propertyString) {
return propertyString.startsWith(PLACEHOLDER_PREFIX) && propertyString.contains(PLACEHOLDER_SUFFIX);
}

@ -86,7 +86,7 @@ public class SpringValue {
private void injectMethod(Object newVal)
throws InvocationTargetException, IllegalAccessException {
Object bean = beanRef.get();
if (bean == null) {
if (bean == null || methodParameter.getMethod() == null) {
return;
}
methodParameter.getMethod().invoke(bean, newVal);
@ -131,7 +131,12 @@ public class SpringValue {
.format("key: %s, beanName: %s, field: %s.%s", key, beanName, bean.getClass()
.getName(), field.getName());
}
return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(),
methodParameter.getMethod().getName());
if (null != methodParameter.getMethod()) {
return String.format("key: %s, beanName: %s, method: %s.%s", key, beanName, bean.getClass().getName(),
methodParameter.getMethod().getName());
}
else {
return String.format("key: %s, beanName: %s", key, beanName);
}
}
}

Loading…
Cancel
Save