Skip to content

Commit

Permalink
Remove unnecessary null check. (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
refeccd authored and ralf0131 committed May 12, 2018
1 parent a3b5d25 commit b8a7a39
Showing 1 changed file with 41 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.alibaba.dubbo.config.ServiceConfig;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
Expand Down Expand Up @@ -75,7 +76,7 @@ public String getPackage() {
public void setPackage(String annotationPackage) {
this.annotationPackage = annotationPackage;
this.annotationPackages = (annotationPackage == null || annotationPackage.length() == 0) ? null
: Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);
: Constants.COMMA_SPLIT_PATTERN.split(annotationPackage);
}

@Override
Expand All @@ -85,7 +86,7 @@ public void setApplicationContext(ApplicationContext applicationContext) throws

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
throws BeansException {
throws BeansException {
if (annotationPackage == null || annotationPackage.length() == 0) {
return;
}
Expand All @@ -110,7 +111,7 @@ public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)
}

@Override
public void destroy() throws Exception {
public void destroy() {

// This will only be called for singleton scope bean, and expected to be called by spring shutdown hook when BeanFactory/ApplicationContext destroys.
// We will guarantee dubbo related resources being released with dubbo shutdown hook.
Expand All @@ -134,7 +135,7 @@ public void destroy() throws Exception {

@Override
public Object postProcessAfterInitialization(Object bean, String beanName)
throws BeansException {
throws BeansException {
if (!isMatchPackage(bean)) {
return bean;
}
Expand All @@ -143,7 +144,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
ServiceBean<Object> serviceConfig = new ServiceBean<Object>(service);
serviceConfig.setRef(bean);
if (void.class.equals(service.interfaceClass())
&& "".equals(service.interfaceName())) {
&& "".equals(service.interfaceName())) {
if (bean.getClass().getInterfaces().length > 0) {
serviceConfig.setInterface(bean.getClass().getInterfaces()[0]);
} else {
Expand All @@ -152,45 +153,43 @@ public Object postProcessAfterInitialization(Object bean, String beanName)
}
if (applicationContext != null) {
serviceConfig.setApplicationContext(applicationContext);
if (service.registry() != null && service.registry().length > 0) {
if (service.registry().length > 0) {
List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
for (String registryId : service.registry()) {
if (registryId != null && registryId.length() > 0) {
registryConfigs.add((RegistryConfig) applicationContext.getBean(registryId, RegistryConfig.class));
registryConfigs.add(applicationContext.getBean(registryId, RegistryConfig.class));
}
}
serviceConfig.setRegistries(registryConfigs);
}
if (service.provider() != null && service.provider().length() > 0) {
serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
if (service.provider().length() > 0) {
serviceConfig.setProvider(applicationContext.getBean(service.provider(), ProviderConfig.class));
}
if (service.monitor() != null && service.monitor().length() > 0) {
serviceConfig.setMonitor((MonitorConfig) applicationContext.getBean(service.monitor(), MonitorConfig.class));
if (service.monitor().length() > 0) {
serviceConfig.setMonitor(applicationContext.getBean(service.monitor(), MonitorConfig.class));
}
if (service.application() != null && service.application().length() > 0) {
serviceConfig.setApplication((ApplicationConfig) applicationContext.getBean(service.application(), ApplicationConfig.class));
if (service.application().length() > 0) {
serviceConfig.setApplication(applicationContext.getBean(service.application(), ApplicationConfig.class));
}
if (service.module() != null && service.module().length() > 0) {
serviceConfig.setModule((ModuleConfig) applicationContext.getBean(service.module(), ModuleConfig.class));
if (service.module().length() > 0) {
serviceConfig.setModule(applicationContext.getBean(service.module(), ModuleConfig.class));
}
if (service.provider() != null && service.provider().length() > 0) {
serviceConfig.setProvider((ProviderConfig) applicationContext.getBean(service.provider(), ProviderConfig.class));
} else {

if (service.provider().length() > 0) {
serviceConfig.setProvider(applicationContext.getBean(service.provider(), ProviderConfig.class));
}
if (service.protocol() != null && service.protocol().length > 0) {
if (service.protocol().length > 0) {
List<ProtocolConfig> protocolConfigs = new ArrayList<ProtocolConfig>();
for (String protocolId : service.protocol()) {
if (protocolId != null && protocolId.length() > 0) {
protocolConfigs.add((ProtocolConfig) applicationContext.getBean(protocolId, ProtocolConfig.class));
protocolConfigs.add(applicationContext.getBean(protocolId, ProtocolConfig.class));
}
}
serviceConfig.setProtocols(protocolConfigs);
}
try {
serviceConfig.afterPropertiesSet();
} catch (RuntimeException e) {
throw (RuntimeException) e;
throw e;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
Expand All @@ -203,23 +202,23 @@ public Object postProcessAfterInitialization(Object bean, String beanName)

@Override
public Object postProcessBeforeInitialization(Object bean, String beanName)
throws BeansException {
throws BeansException {
if (!isMatchPackage(bean)) {
return bean;
}
Method[] methods = bean.getClass().getMethods();
for (Method method : methods) {
String name = method.getName();
if (name.length() > 3 && name.startsWith("set")
&& method.getParameterTypes().length == 1
&& Modifier.isPublic(method.getModifiers())
&& !Modifier.isStatic(method.getModifiers())) {
&& method.getParameterTypes().length == 1
&& Modifier.isPublic(method.getModifiers())
&& !Modifier.isStatic(method.getModifiers())) {
try {
Reference reference = method.getAnnotation(Reference.class);
if (reference != null) {
Object value = refer(reference, method.getParameterTypes()[0]);
if (value != null) {
method.invoke(bean, new Object[]{value});
method.invoke(bean, value);
}
}
} catch (Throwable e) {
Expand Down Expand Up @@ -263,40 +262,40 @@ private Object refer(Reference reference, Class<?> referenceClass) { //method.ge
if (referenceConfig == null) {
referenceConfig = new ReferenceBean<Object>(reference);
if (void.class.equals(reference.interfaceClass())
&& "".equals(reference.interfaceName())
&& referenceClass.isInterface()) {
&& "".equals(reference.interfaceName())
&& referenceClass.isInterface()) {
referenceConfig.setInterface(referenceClass);
}
if (applicationContext != null) {
referenceConfig.setApplicationContext(applicationContext);
if (reference.registry() != null && reference.registry().length > 0) {
if (reference.registry().length > 0) {
List<RegistryConfig> registryConfigs = new ArrayList<RegistryConfig>();
for (String registryId : reference.registry()) {
if (registryId != null && registryId.length() > 0) {
registryConfigs.add((RegistryConfig) applicationContext.getBean(registryId, RegistryConfig.class));
registryConfigs.add(applicationContext.getBean(registryId, RegistryConfig.class));
}
}
referenceConfig.setRegistries(registryConfigs);
}
if (reference.consumer() != null && reference.consumer().length() > 0) {
referenceConfig.setConsumer((ConsumerConfig) applicationContext.getBean(reference.consumer(), ConsumerConfig.class));
if (reference.consumer().length() > 0) {
referenceConfig.setConsumer(applicationContext.getBean(reference.consumer(), ConsumerConfig.class));
}
if (reference.monitor() != null && reference.monitor().length() > 0) {
referenceConfig.setMonitor((MonitorConfig) applicationContext.getBean(reference.monitor(), MonitorConfig.class));
if (reference.monitor().length() > 0) {
referenceConfig.setMonitor(applicationContext.getBean(reference.monitor(), MonitorConfig.class));
}
if (reference.application() != null && reference.application().length() > 0) {
referenceConfig.setApplication((ApplicationConfig) applicationContext.getBean(reference.application(), ApplicationConfig.class));
if (reference.application().length() > 0) {
referenceConfig.setApplication(applicationContext.getBean(reference.application(), ApplicationConfig.class));
}
if (reference.module() != null && reference.module().length() > 0) {
referenceConfig.setModule((ModuleConfig) applicationContext.getBean(reference.module(), ModuleConfig.class));
if (reference.module().length() > 0) {
referenceConfig.setModule(applicationContext.getBean(reference.module(), ModuleConfig.class));
}
if (reference.consumer() != null && reference.consumer().length() > 0) {
referenceConfig.setConsumer((ConsumerConfig) applicationContext.getBean(reference.consumer(), ConsumerConfig.class));
if (reference.consumer().length() > 0) {
referenceConfig.setConsumer(applicationContext.getBean(reference.consumer(), ConsumerConfig.class));
}
try {
referenceConfig.afterPropertiesSet();
} catch (RuntimeException e) {
throw (RuntimeException) e;
throw e;
} catch (Exception e) {
throw new IllegalStateException(e.getMessage(), e);
}
Expand Down

0 comments on commit b8a7a39

Please sign in to comment.