From b8a7a399507cac33ccc410528fab4d89fe393204 Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Sat, 12 May 2018 20:44:10 +0800 Subject: [PATCH] Remove unnecessary null check. (#1764) --- .../dubbo/config/spring/AnnotationBean.java | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java index bbb75d9efd1..6da7ba8b901 100644 --- a/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java +++ b/dubbo-config/dubbo-config-spring/src/main/java/com/alibaba/dubbo/config/spring/AnnotationBean.java @@ -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; @@ -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 @@ -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; } @@ -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. @@ -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; } @@ -143,7 +144,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) ServiceBean serviceConfig = new ServiceBean(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 { @@ -152,37 +153,35 @@ 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 registryConfigs = new ArrayList(); 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 protocolConfigs = new ArrayList(); 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); @@ -190,7 +189,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) try { serviceConfig.afterPropertiesSet(); } catch (RuntimeException e) { - throw (RuntimeException) e; + throw e; } catch (Exception e) { throw new IllegalStateException(e.getMessage(), e); } @@ -203,7 +202,7 @@ public Object postProcessAfterInitialization(Object bean, String beanName) @Override public Object postProcessBeforeInitialization(Object bean, String beanName) - throws BeansException { + throws BeansException { if (!isMatchPackage(bean)) { return bean; } @@ -211,15 +210,15 @@ public Object postProcessBeforeInitialization(Object bean, String beanName) 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) { @@ -263,40 +262,40 @@ private Object refer(Reference reference, Class referenceClass) { //method.ge if (referenceConfig == null) { referenceConfig = new ReferenceBean(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 registryConfigs = new ArrayList(); 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); }