Refactor framework infrastructure classes.

This commit is contained in:
John Blum
2021-04-15 13:33:13 -07:00
parent 1d1270f8a9
commit ba2e73e70c
3 changed files with 116 additions and 102 deletions

View File

@@ -54,6 +54,8 @@ import org.springframework.expression.EvaluationContext;
import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.expression.spel.support.StandardTypeConverter; import org.springframework.expression.spel.support.StandardTypeConverter;
import org.springframework.expression.spel.support.StandardTypeLocator; import org.springframework.expression.spel.support.StandardTypeLocator;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@@ -76,8 +78,6 @@ import org.slf4j.LoggerFactory;
* @see org.springframework.beans.factory.support.AbstractBeanDefinition * @see org.springframework.beans.factory.support.AbstractBeanDefinition
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry * @see org.springframework.beans.factory.support.BeanDefinitionRegistry
* @see org.springframework.context.EnvironmentAware * @see org.springframework.context.EnvironmentAware
* @see org.springframework.context.expression.BeanFactoryAccessor
* @see org.springframework.context.expression.EnvironmentAccessor
* @see org.springframework.core.annotation.AnnotationAttributes * @see org.springframework.core.annotation.AnnotationAttributes
* @see org.springframework.core.env.Environment * @see org.springframework.core.env.Environment
* @see org.springframework.core.type.AnnotationMetadata * @see org.springframework.core.type.AnnotationMetadata
@@ -113,7 +113,7 @@ public abstract class AbstractAnnotationConfigSupport
* @param value {@link Number} to evaluate. * @param value {@link Number} to evaluate.
* @return a boolean value indicating whether the given {@link Number} has value. * @return a boolean value indicating whether the given {@link Number} has value.
*/ */
protected static boolean hasValue(Number value) { protected static boolean hasValue(@Nullable Number value) {
return Optional.ofNullable(value).filter(it -> it.doubleValue() != 0.0d).isPresent(); return Optional.ofNullable(value).filter(it -> it.doubleValue() != 0.0d).isPresent();
} }
@@ -124,7 +124,7 @@ public abstract class AbstractAnnotationConfigSupport
* @param value {@link Object} to evaluate. * @param value {@link Object} to evaluate.
* @return a boolean value indicating whether the given {@link Object} has value. * @return a boolean value indicating whether the given {@link Object} has value.
*/ */
protected static boolean hasValue(Object value) { protected static boolean hasValue(@Nullable Object value) {
return value != null; return value != null;
} }
@@ -135,7 +135,7 @@ public abstract class AbstractAnnotationConfigSupport
* @param value {@link String} to evaluate. * @param value {@link String} to evaluate.
* @return a boolean value indicating whether the given {@link String} is valuable. * @return a boolean value indicating whether the given {@link String} is valuable.
*/ */
protected static boolean hasValue(String value) { protected static boolean hasValue(@Nullable String value) {
return StringUtils.hasText(value); return StringUtils.hasText(value);
} }
@@ -155,7 +155,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.BeanFactory
* @see #newEvaluationContext(BeanFactory) * @see #newEvaluationContext(BeanFactory)
*/ */
public AbstractAnnotationConfigSupport(BeanFactory beanFactory) { public AbstractAnnotationConfigSupport(@Nullable BeanFactory beanFactory) {
this.evaluationContext = newEvaluationContext(beanFactory); this.evaluationContext = newEvaluationContext(beanFactory);
this.log = newLog(); this.log = newLog();
@@ -170,7 +170,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.expression.EvaluationContext * @see org.springframework.expression.EvaluationContext
* @see #getBeanFactory() * @see #getBeanFactory()
*/ */
protected EvaluationContext newEvaluationContext(BeanFactory beanFactory) { protected EvaluationContext newEvaluationContext(@Nullable BeanFactory beanFactory) {
StandardEvaluationContext evaluationContext = new StandardEvaluationContext(); StandardEvaluationContext evaluationContext = new StandardEvaluationContext();
@@ -184,18 +184,19 @@ public abstract class AbstractAnnotationConfigSupport
return evaluationContext; return evaluationContext;
} }
private void configureTypeConverter(EvaluationContext evaluationContext, BeanFactory beanFactory) { private void configureTypeConverter(@Nullable EvaluationContext evaluationContext,
@Nullable BeanFactory beanFactory) {
Optional.ofNullable(evaluationContext) Optional.ofNullable(evaluationContext)
.filter(evalContext -> evalContext instanceof StandardEvaluationContext) .filter(StandardEvaluationContext.class::isInstance)
.map(StandardEvaluationContext.class::cast)
.ifPresent(evalContext -> .ifPresent(evalContext ->
Optional.ofNullable(beanFactory) Optional.ofNullable(beanFactory)
.filter(it -> it instanceof ConfigurableBeanFactory) .filter(ConfigurableBeanFactory.class::isInstance)
.map(it -> ((ConfigurableBeanFactory) it).getConversionService()) .map(ConfigurableBeanFactory.class::cast)
.map(ConfigurableBeanFactory::getConversionService)
.ifPresent(conversionService -> .ifPresent(conversionService ->
((StandardEvaluationContext) evalContext).setTypeConverter( evalContext.setTypeConverter(new StandardTypeConverter(conversionService))));
new StandardTypeConverter(conversionService)))
);
} }
/** /**
@@ -220,7 +221,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see #getAnnotationTypeName() * @see #getAnnotationTypeName()
* @see org.springframework.core.type.AnnotationMetadata * @see org.springframework.core.type.AnnotationMetadata
*/ */
protected boolean isAnnotationPresent(AnnotationMetadata importingClassMetadata) { protected boolean isAnnotationPresent(@NonNull AnnotationMetadata importingClassMetadata) {
return isAnnotationPresent(importingClassMetadata, getAnnotationTypeName()); return isAnnotationPresent(importingClassMetadata, getAnnotationTypeName());
} }
@@ -234,7 +235,9 @@ public abstract class AbstractAnnotationConfigSupport
* the given {@link Annotation} defined by {@link String name}. * the given {@link Annotation} defined by {@link String name}.
* @see org.springframework.core.type.AnnotationMetadata * @see org.springframework.core.type.AnnotationMetadata
*/ */
protected boolean isAnnotationPresent(AnnotationMetadata importingClassMetadata, String annotationName) { protected boolean isAnnotationPresent(@NonNull AnnotationMetadata importingClassMetadata,
@NonNull String annotationName) {
return importingClassMetadata.hasAnnotation(annotationName); return importingClassMetadata.hasAnnotation(annotationName);
} }
@@ -246,7 +249,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.core.annotation.AnnotationAttributes * @see org.springframework.core.annotation.AnnotationAttributes
* @see java.lang.annotation.Annotation * @see java.lang.annotation.Annotation
*/ */
protected AnnotationAttributes getAnnotationAttributes(Annotation annotation) { protected @NonNull AnnotationAttributes getAnnotationAttributes(@NonNull Annotation annotation) {
return AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(annotation)); return AnnotationAttributes.fromMap(AnnotationUtils.getAnnotationAttributes(annotation));
} }
@@ -260,7 +263,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see #getAnnotationAttributes(AnnotationMetadata, String) * @see #getAnnotationAttributes(AnnotationMetadata, String)
* @see #getAnnotationTypeName() * @see #getAnnotationTypeName()
*/ */
protected AnnotationAttributes getAnnotationAttributes(AnnotationMetadata importingClassMetadata) { protected @NonNull AnnotationAttributes getAnnotationAttributes(@NonNull AnnotationMetadata importingClassMetadata) {
return getAnnotationAttributes(importingClassMetadata, getAnnotationTypeName()); return getAnnotationAttributes(importingClassMetadata, getAnnotationTypeName());
} }
@@ -274,8 +277,8 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.core.annotation.AnnotationAttributes * @see org.springframework.core.annotation.AnnotationAttributes
* @see org.springframework.core.type.AnnotationMetadata * @see org.springframework.core.type.AnnotationMetadata
*/ */
protected AnnotationAttributes getAnnotationAttributes(AnnotationMetadata importingClassMetadata, protected @NonNull AnnotationAttributes getAnnotationAttributes(@NonNull AnnotationMetadata importingClassMetadata,
String annotationName) { @NonNull String annotationName) {
return AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(annotationName)); return AnnotationAttributes.fromMap(importingClassMetadata.getAnnotationAttributes(annotationName));
} }
@@ -285,7 +288,7 @@ public abstract class AbstractAnnotationConfigSupport
* *
* @return the cache application {@link java.lang.annotation.Annotation} type used by this application. * @return the cache application {@link java.lang.annotation.Annotation} type used by this application.
*/ */
protected abstract Class<? extends Annotation> getAnnotationType(); protected abstract @NonNull Class<? extends Annotation> getAnnotationType();
/** /**
* Returns the fully-qualified {@link Class#getName() class name} of the cache application * Returns the fully-qualified {@link Class#getName() class name} of the cache application
@@ -296,7 +299,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see java.lang.Class#getName() * @see java.lang.Class#getName()
* @see #getAnnotationType() * @see #getAnnotationType()
*/ */
protected String getAnnotationTypeName() { protected @NonNull String getAnnotationTypeName() {
return getAnnotationType().getName(); return getAnnotationType().getName();
} }
@@ -309,7 +312,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see java.lang.Class#getSimpleName() * @see java.lang.Class#getSimpleName()
* @see #getAnnotationType() * @see #getAnnotationType()
*/ */
protected String getAnnotationTypeSimpleName() { protected @NonNull String getAnnotationTypeSimpleName() {
return getAnnotationType().getSimpleName(); return getAnnotationType().getSimpleName();
} }
@@ -321,12 +324,13 @@ public abstract class AbstractAnnotationConfigSupport
* @return {@literal true} iff the {@link Object bean} is not a Spring container infrastructure bean. * @return {@literal true} iff the {@link Object bean} is not a Spring container infrastructure bean.
* @see #isNotInfrastructureClass(String) * @see #isNotInfrastructureClass(String)
*/ */
protected boolean isNotInfrastructureBean(Object bean) { protected boolean isNotInfrastructureBean(@Nullable Object bean) {
return Optional.ofNullable(bean) return Optional.ofNullable(bean)
.map(Object::getClass) .map(Object::getClass)
.map(Class::getName) .map(Class::getName)
.filter(this::isNotInfrastructureClass).isPresent(); .filter(this::isNotInfrastructureClass)
.isPresent();
} }
/** /**
@@ -340,8 +344,8 @@ public abstract class AbstractAnnotationConfigSupport
* @see #isNotInfrastructureClass(BeanDefinition) * @see #isNotInfrastructureClass(BeanDefinition)
* @see #isNotInfrastructureRole(BeanDefinition) * @see #isNotInfrastructureRole(BeanDefinition)
*/ */
protected boolean isNotInfrastructureBean(BeanDefinition beanDefinition) { protected boolean isNotInfrastructureBean(@Nullable BeanDefinition beanDefinition) {
return (isNotInfrastructureRole(beanDefinition) && isNotInfrastructureClass(beanDefinition)); return isNotInfrastructureRole(beanDefinition) && isNotInfrastructureClass(beanDefinition);
} }
/** /**
@@ -355,8 +359,11 @@ public abstract class AbstractAnnotationConfigSupport
* @see #resolveBeanClassName(BeanDefinition) * @see #resolveBeanClassName(BeanDefinition)
* @see #isNotInfrastructureClass(String) * @see #isNotInfrastructureClass(String)
*/ */
protected boolean isNotInfrastructureClass(BeanDefinition beanDefinition) { protected boolean isNotInfrastructureClass(@Nullable BeanDefinition beanDefinition) {
return resolveBeanClassName(beanDefinition).filter(this::isNotInfrastructureClass).isPresent();
return resolveBeanClassName(beanDefinition)
.filter(this::isNotInfrastructureClass)
.isPresent();
} }
/** /**
@@ -370,9 +377,9 @@ public abstract class AbstractAnnotationConfigSupport
* @return {@literal true} iff the given {@link Class#getName() class type name} is not considered a * @return {@literal true} iff the given {@link Class#getName() class type name} is not considered a
* Spring container infrastructure type. * Spring container infrastructure type.
*/ */
protected boolean isNotInfrastructureClass(String className) { protected boolean isNotInfrastructureClass(@NonNull String className) {
return (className.startsWith(ORG_SPRINGFRAMEWORK_DATA_GEMFIRE_PACKAGE) return className.startsWith(ORG_SPRINGFRAMEWORK_DATA_GEMFIRE_PACKAGE)
|| !className.startsWith(ORG_SPRINGFRAMEWORK_PACKAGE)); || !className.startsWith(ORG_SPRINGFRAMEWORK_PACKAGE);
} }
/** /**
@@ -384,7 +391,7 @@ public abstract class AbstractAnnotationConfigSupport
* infrastructure bean. * infrastructure bean.
* @see org.springframework.beans.factory.config.BeanDefinition * @see org.springframework.beans.factory.config.BeanDefinition
*/ */
protected boolean isNotInfrastructureRole(BeanDefinition beanDefinition) { protected boolean isNotInfrastructureRole(@Nullable BeanDefinition beanDefinition) {
return Optional.ofNullable(beanDefinition) return Optional.ofNullable(beanDefinition)
.map(BeanDefinition::getRole) .map(BeanDefinition::getRole)
@@ -403,7 +410,7 @@ public abstract class AbstractAnnotationConfigSupport
* @return a boolean value indicating whether the {@link Method} was declared/defined by the user. * @return a boolean value indicating whether the {@link Method} was declared/defined by the user.
* @see java.lang.reflect.Method * @see java.lang.reflect.Method
*/ */
protected boolean isUserLevelMethod(Method method) { protected boolean isUserLevelMethod(@Nullable Method method) {
return Optional.ofNullable(method) return Optional.ofNullable(method)
.filter(ClassUtils::isUserLevelMethod) .filter(ClassUtils::isUserLevelMethod)
@@ -415,7 +422,7 @@ public abstract class AbstractAnnotationConfigSupport
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setBeanClassLoader(ClassLoader beanClassLoader) { public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) {
this.beanClassLoader = beanClassLoader; this.beanClassLoader = beanClassLoader;
} }
@@ -426,7 +433,7 @@ public abstract class AbstractAnnotationConfigSupport
* @return the {@link ClassLoader} used by the Spring {@link BeanFactory} to load classes for bean definitions. * @return the {@link ClassLoader} used by the Spring {@link BeanFactory} to load classes for bean definitions.
* @see #setBeanClassLoader(ClassLoader) * @see #setBeanClassLoader(ClassLoader)
*/ */
protected ClassLoader getBeanClassLoader() { protected @Nullable ClassLoader getBeanClassLoader() {
return this.beanClassLoader; return this.beanClassLoader;
} }
@@ -439,7 +446,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see java.lang.Thread#getContextClassLoader() * @see java.lang.Thread#getContextClassLoader()
* @see #getBeanClassLoader() * @see #getBeanClassLoader()
*/ */
protected ClassLoader resolveBeanClassLoader() { protected @NonNull ClassLoader resolveBeanClassLoader() {
return Optional.ofNullable(getBeanClassLoader()) return Optional.ofNullable(getBeanClassLoader())
.orElseGet(() -> Thread.currentThread().getContextClassLoader()); .orElseGet(() -> Thread.currentThread().getContextClassLoader());
} }
@@ -448,7 +455,7 @@ public abstract class AbstractAnnotationConfigSupport
* {@inheritDoc} * {@inheritDoc}
*/ */
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(@Nullable BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
configureTypeConverter(getEvaluationContext(), beanFactory); configureTypeConverter(getEvaluationContext(), beanFactory);
} }
@@ -460,7 +467,7 @@ public abstract class AbstractAnnotationConfigSupport
* @throws IllegalStateException if the Spring {@link BeanFactory} was not properly configured. * @throws IllegalStateException if the Spring {@link BeanFactory} was not properly configured.
* @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.BeanFactory
*/ */
protected BeanFactory getBeanFactory() { protected @NonNull BeanFactory getBeanFactory() {
return Optional.ofNullable(this.beanFactory) return Optional.ofNullable(this.beanFactory)
.orElseThrow(() -> newIllegalStateException("BeanFactory is required")); .orElseThrow(() -> newIllegalStateException("BeanFactory is required"));
} }
@@ -473,7 +480,7 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.core.env.Environment * @see org.springframework.core.env.Environment
*/ */
@Override @Override
public void setEnvironment(Environment environment) { public void setEnvironment(@Nullable Environment environment) {
this.environment = environment; this.environment = environment;
} }
@@ -483,7 +490,7 @@ public abstract class AbstractAnnotationConfigSupport
* @return a reference to the Spring {@link Environment}. * @return a reference to the Spring {@link Environment}.
* @see org.springframework.core.env.Environment * @see org.springframework.core.env.Environment
*/ */
protected Environment getEnvironment() { protected @Nullable Environment getEnvironment() {
return this.environment; return this.environment;
} }
@@ -493,7 +500,7 @@ public abstract class AbstractAnnotationConfigSupport
* @return a reference to the {@link EvaluationContext} used to evaluate SpEL expressions. * @return a reference to the {@link EvaluationContext} used to evaluate SpEL expressions.
* @see org.springframework.expression.EvaluationContext * @see org.springframework.expression.EvaluationContext
*/ */
protected EvaluationContext getEvaluationContext() { protected @NonNull EvaluationContext getEvaluationContext() {
return this.evaluationContext; return this.evaluationContext;
} }
@@ -503,7 +510,7 @@ public abstract class AbstractAnnotationConfigSupport
* @return a reference to the {@link Logger} used by this class to log {@link String messages}. * @return a reference to the {@link Logger} used by this class to log {@link String messages}.
* @see org.apache.commons.logging.Log * @see org.apache.commons.logging.Log
*/ */
protected Logger getLog() { protected @NonNull Logger getLog() {
return this.log; return this.log;
} }
@@ -603,7 +610,7 @@ public abstract class AbstractAnnotationConfigSupport
*/ */
protected void logError(Supplier<String> message) { protected void logError(Supplier<String> message) {
Optional.ofNullable(getLog()) Optional.ofNullable(getLog())
.filter(Logger::isWarnEnabled) .filter(Logger::isErrorEnabled)
.ifPresent(log -> log.info(message.get())); .ifPresent(log -> log.info(message.get()));
} }
@@ -618,13 +625,13 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry) * @see org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry)
* @see #getBeanFactory() * @see #getBeanFactory()
*/ */
protected AbstractBeanDefinition register(AbstractBeanDefinition beanDefinition) { protected @NonNull AbstractBeanDefinition register(@NonNull AbstractBeanDefinition beanDefinition) {
BeanFactory beanFactory = getBeanFactory(); BeanFactory beanFactory = getBeanFactory();
return (beanFactory instanceof BeanDefinitionRegistry return beanFactory instanceof BeanDefinitionRegistry
? register(beanDefinition, (BeanDefinitionRegistry) beanFactory) ? register(beanDefinition, (BeanDefinitionRegistry) beanFactory)
: beanDefinition); : beanDefinition;
} }
/** /**
@@ -637,7 +644,8 @@ public abstract class AbstractAnnotationConfigSupport
* @see org.springframework.beans.factory.support.BeanDefinitionRegistry * @see org.springframework.beans.factory.support.BeanDefinitionRegistry
* @see org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry) * @see org.springframework.beans.factory.support.BeanDefinitionReaderUtils#registerWithGeneratedName(AbstractBeanDefinition, BeanDefinitionRegistry)
*/ */
protected AbstractBeanDefinition register(AbstractBeanDefinition beanDefinition, BeanDefinitionRegistry registry) { protected @NonNull AbstractBeanDefinition register(@NonNull AbstractBeanDefinition beanDefinition,
@Nullable BeanDefinitionRegistry registry) {
Optional.ofNullable(registry).ifPresent(it -> Optional.ofNullable(registry).ifPresent(it ->
BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, it) BeanDefinitionReaderUtils.registerWithGeneratedName(beanDefinition, it)
@@ -929,7 +937,7 @@ public abstract class AbstractAnnotationConfigSupport
* by the given {@link BeanDefinition}. * by the given {@link BeanDefinition}.
* @see org.springframework.beans.factory.config.BeanDefinition#getBeanClassName() * @see org.springframework.beans.factory.config.BeanDefinition#getBeanClassName()
*/ */
protected Optional<String> resolveBeanClassName(BeanDefinition beanDefinition) { protected Optional<String> resolveBeanClassName(@Nullable BeanDefinition beanDefinition) {
Optional<String> beanClassName = Optional.ofNullable(beanDefinition) Optional<String> beanClassName = Optional.ofNullable(beanDefinition)
.map(BeanDefinition::getBeanClassName) .map(BeanDefinition::getBeanClassName)
@@ -937,7 +945,7 @@ public abstract class AbstractAnnotationConfigSupport
if (!beanClassName.isPresent()) { if (!beanClassName.isPresent()) {
beanClassName = Optional.ofNullable(beanDefinition) beanClassName = Optional.ofNullable(beanDefinition)
.filter(it -> it instanceof AnnotatedBeanDefinition) .filter(AnnotatedBeanDefinition.class::isInstance)
.filter(it -> StringUtils.hasText(it.getFactoryMethodName())) .filter(it -> StringUtils.hasText(it.getFactoryMethodName()))
.map(it -> ((AnnotatedBeanDefinition) it).getFactoryMethodMetadata()) .map(it -> ((AnnotatedBeanDefinition) it).getFactoryMethodMetadata())
.map(MethodMetadata::getReturnTypeName); .map(MethodMetadata::getReturnTypeName);
@@ -1090,11 +1098,11 @@ public abstract class AbstractAnnotationConfigSupport
} }
/** /**
* {@link TypeResolver} is a {@link FunctionalInterface} defining a contract to encapsulated the logic * {@link TypeResolver} is a {@link FunctionalInterface} defining a contract to encapsulate logic
* to resolve a particular {@link Class type}. * used to resolve a particular {@link Class type}.
* *
* Implementations are free to decide on how a {@link Class type} gets resolved, such as * Implementations are free to decide on how a {@link Class type} gets resolved, such as
* with {@link Class#forName(String)} or perhaps using {@link ClassLoader#defineClass(String, byte[], int, int)}. * with {@link Class#forName(String)} or perhaps by using {@link ClassLoader#defineClass(String, byte[], int, int)}.
* *
* @param <T> {@link Class} of the type to resolve. * @param <T> {@link Class} of the type to resolve.
*/ */

View File

@@ -43,6 +43,8 @@ import org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer;
import org.springframework.data.gemfire.config.annotation.LocatorConfigurer; import org.springframework.data.gemfire.config.annotation.LocatorConfigurer;
import org.springframework.data.gemfire.config.annotation.PeerCacheConfigurer; import org.springframework.data.gemfire.config.annotation.PeerCacheConfigurer;
import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.data.gemfire.util.CollectionUtils;
import org.springframework.lang.NonNull;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
@@ -87,15 +89,15 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
* @see org.springframework.data.gemfire.config.annotation.AbstractCacheConfiguration * @see org.springframework.data.gemfire.config.annotation.AbstractCacheConfiguration
*/ */
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
protected <T extends AbstractCacheConfiguration> T getCacheConfiguration() { protected @NonNull <T extends AbstractCacheConfiguration> T getCacheConfiguration() {
return Optional.ofNullable((T) this.cacheConfiguration) return Optional.ofNullable((T) this.cacheConfiguration)
.orElseThrow(() -> newIllegalStateException("AbstractCacheConfiguration is required")); .orElseThrow(() -> newIllegalStateException("AbstractCacheConfiguration is required"));
} }
@Override @Override
public final void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, public final void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata,
BeanDefinitionRegistry registry) { @NonNull BeanDefinitionRegistry registry) {
if (isAnnotationPresent(importingClassMetadata)) { if (isAnnotationPresent(importingClassMetadata)) {
@@ -106,14 +108,19 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
} }
} }
@SuppressWarnings("unused") protected void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata,
protected void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, @NonNull AnnotationAttributes annotationAttributes, @NonNull BeanDefinitionRegistry registry) {
Map<String, Object> annotationAttributes, BeanDefinitionRegistry registry) {
registerBeanDefinitions(importingClassMetadata, (Map<String, Object>) annotationAttributes, registry);
}
protected void registerBeanDefinitions(@NonNull AnnotationMetadata importingClassMetadata,
@NonNull Map<String, Object> annotationAttributes, @NonNull BeanDefinitionRegistry registry) {
} }
protected void setGemFireProperties(AnnotationMetadata importingClassMetadata, protected void setGemFireProperties(@NonNull AnnotationMetadata importingClassMetadata,
AnnotationAttributes annotationAttributes, BeanDefinitionRegistry registry) { @NonNull AnnotationAttributes annotationAttributes, @NonNull BeanDefinitionRegistry registry) {
Properties gemfireProperties = toGemFireProperties(annotationAttributes); Properties gemfireProperties = toGemFireProperties(annotationAttributes);
@@ -129,27 +136,28 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
} }
} }
protected abstract Properties toGemFireProperties(Map<String, Object> annotationAttributes); protected abstract @Nullable Properties toGemFireProperties(@NonNull Map<String, Object> annotationAttributes);
protected boolean hasProperties(Properties properties) { protected boolean hasProperties(@Nullable Properties properties) {
return !CollectionUtils.isEmpty(properties); return !CollectionUtils.isEmpty(properties);
} }
protected void registerGemFirePropertiesBeanPostProcessor(BeanDefinitionRegistry registry, protected void registerGemFirePropertiesBeanPostProcessor(@NonNull BeanDefinitionRegistry registry,
Properties gemFireProperties) { Properties gemFireProperties) {
registerBeanDefinition(registry, GemFirePropertiesBeanPostProcessor.class, gemFireProperties); registerBeanDefinition(registry, GemFirePropertiesBeanPostProcessor.class, gemFireProperties);
} }
protected void registerGemFirePropertiesConfigurer(BeanDefinitionRegistry registry, Properties gemfireProperties) { protected void registerGemFirePropertiesConfigurer(@NonNull BeanDefinitionRegistry registry,
Properties gemfireProperties) {
registerClientGemFirePropertiesConfigurer(registry, gemfireProperties); registerClientGemFirePropertiesConfigurer(registry, gemfireProperties);
registerLocatorGemFirePropertiesConfigurer(registry, gemfireProperties); registerLocatorGemFirePropertiesConfigurer(registry, gemfireProperties);
registerPeerGemFirePropertiesConfigurer(registry, gemfireProperties); registerPeerGemFirePropertiesConfigurer(registry, gemfireProperties);
} }
private void registerBeanDefinition(BeanDefinitionRegistry registry, Class<?> beanType, private void registerBeanDefinition(@NonNull BeanDefinitionRegistry registry, @NonNull Class<?> beanType,
Properties gemfireProperties) { @NonNull Properties gemfireProperties) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder builder =
BeanDefinitionBuilder.genericBeanDefinition(beanType); BeanDefinitionBuilder.genericBeanDefinition(beanType);
@@ -231,39 +239,34 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
} }
} }
protected String resolveHost(String hostname) { protected @Nullable String resolveHost(@Nullable String hostname) {
return resolveHost(hostname, DEFAULT_HOST); return resolveHost(hostname, DEFAULT_HOST);
} }
protected String resolveHost(String hostname, String defaultHostname) { protected @Nullable String resolveHost(@Nullable String hostname, @Nullable String defaultHostname) {
return StringUtils.hasText(hostname) ? hostname : defaultHostname;
return Optional.ofNullable(hostname)
.filter(StringUtils::hasText)
.orElse(defaultHostname);
} }
protected Integer resolvePort(Integer port) { protected @Nullable Integer resolvePort(@Nullable Integer port) {
return resolvePort(port, DEFAULT_PORT); return resolvePort(port, DEFAULT_PORT);
} }
protected Integer resolvePort(Integer port, Integer defaultPort) { protected @Nullable Integer resolvePort(@Nullable Integer port, @Nullable Integer defaultPort) {
return port != null ? port : defaultPort;
return Optional.ofNullable(port)
.orElse(defaultPort);
} }
protected static class AbstractGemFirePropertiesConfigurer { protected static class AbstractGemFirePropertiesConfigurer {
private final Properties gemfireProperties; private final Properties gemfireProperties;
protected AbstractGemFirePropertiesConfigurer(Properties gemfireProperties) { protected AbstractGemFirePropertiesConfigurer(@NonNull Properties gemfireProperties) {
Assert.notEmpty(gemfireProperties, "GemFire Properties are required"); Assert.notEmpty(gemfireProperties, "GemFire Properties must not be null");
this.gemfireProperties = gemfireProperties; this.gemfireProperties = gemfireProperties;
} }
protected void configureGemFireProperties(CacheFactoryBean bean) { protected void configureGemFireProperties(@NonNull CacheFactoryBean bean) {
bean.getProperties().putAll(this.gemfireProperties); bean.getProperties().putAll(this.gemfireProperties);
} }
} }
@@ -271,12 +274,12 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
protected static class ClientGemFirePropertiesConfigurer extends AbstractGemFirePropertiesConfigurer protected static class ClientGemFirePropertiesConfigurer extends AbstractGemFirePropertiesConfigurer
implements ClientCacheConfigurer { implements ClientCacheConfigurer {
protected ClientGemFirePropertiesConfigurer(Properties gemfireProperties) { protected ClientGemFirePropertiesConfigurer(@NonNull Properties gemfireProperties) {
super(gemfireProperties); super(gemfireProperties);
} }
@Override @Override
public void configure(String beanName, ClientCacheFactoryBean bean) { public void configure(@Nullable String beanName, @NonNull ClientCacheFactoryBean bean) {
configureGemFireProperties(bean); configureGemFireProperties(bean);
} }
} }
@@ -285,15 +288,15 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
private final Properties gemfireProperties; private final Properties gemfireProperties;
public LocatorGemFirePropertiesConfigurer(Properties gemfireProperties) { public LocatorGemFirePropertiesConfigurer(@NonNull Properties gemfireProperties) {
Assert.notEmpty(gemfireProperties, "GemFire Properties are required"); Assert.notEmpty(gemfireProperties, "GemFire Properties must not be null");
this.gemfireProperties = gemfireProperties; this.gemfireProperties = gemfireProperties;
} }
@Override @Override
public void configure(String beanName, LocatorFactoryBean bean) { public void configure(@Nullable String beanName, @NonNull LocatorFactoryBean bean) {
Properties gemfireProperties = bean.getGemFireProperties(); Properties gemfireProperties = bean.getGemFireProperties();
@@ -306,12 +309,12 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
protected static class PeerGemFirePropertiesConfigurer extends AbstractGemFirePropertiesConfigurer protected static class PeerGemFirePropertiesConfigurer extends AbstractGemFirePropertiesConfigurer
implements PeerCacheConfigurer { implements PeerCacheConfigurer {
protected PeerGemFirePropertiesConfigurer(Properties gemfireProperties) { protected PeerGemFirePropertiesConfigurer(@NonNull Properties gemfireProperties) {
super(gemfireProperties); super(gemfireProperties);
} }
@Override @Override
public void configure(String beanName, CacheFactoryBean bean) { public void configure(@Nullable String beanName, @NonNull CacheFactoryBean bean) {
configureGemFireProperties(bean); configureGemFireProperties(bean);
} }
} }
@@ -336,9 +339,9 @@ public abstract class EmbeddedServiceConfigurationSupport extends AbstractAnnota
* @throws IllegalArgumentException if {@link Properties} are {@literal null} or empty. * @throws IllegalArgumentException if {@link Properties} are {@literal null} or empty.
* @see java.util.Properties * @see java.util.Properties
*/ */
protected GemFirePropertiesBeanPostProcessor(Properties gemfireProperties) { protected GemFirePropertiesBeanPostProcessor(@NonNull Properties gemfireProperties) {
Assert.notEmpty(gemfireProperties, "GemFire Properties are required"); Assert.notEmpty(gemfireProperties, "GemFire Properties must not be null");
this.gemfireProperties = gemfireProperties; this.gemfireProperties = gemfireProperties;
} }

View File

@@ -24,6 +24,7 @@ import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.FactoryBean;
import org.springframework.lang.Nullable;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -57,7 +58,7 @@ public abstract class AbstractFactoryBeanSupport<T>
private String beanName; private String beanName;
/** /**
* Constructs a new instance of {@link AbstractFactoryBeanSupport} and initialized the logger. * Constructs a new instance of {@link AbstractFactoryBeanSupport} initializing an object instance {@link Logger}.
* *
* @see #newLog() * @see #newLog()
*/ */
@@ -77,25 +78,27 @@ public abstract class AbstractFactoryBeanSupport<T>
} }
/** /**
* Sets a reference to the {@link ClassLoader} used by the Spring container to load and create bean classes. * Sets a reference to the {@link ClassLoader} used by the Spring container to load bean {@link Class classes}.
* *
* @param classLoader {@link ClassLoader} used by the Spring container to load and create bean classes. * @param classLoader {@link ClassLoader} used by the Spring container to load bean {@link Class classes}.
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(ClassLoader) * @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(ClassLoader)
* @see java.lang.ClassLoader * @see java.lang.ClassLoader
* @see java.lang.Class
*/ */
@Override @Override
public void setBeanClassLoader(ClassLoader classLoader) { public void setBeanClassLoader(@Nullable ClassLoader classLoader) {
this.beanClassLoader = classLoader; this.beanClassLoader = classLoader;
} }
/** /**
* Returns a reference to the {@link ClassLoader} used by the Spring container to load and create bean classes. * Returns a reference to the {@link ClassLoader} used by the Spring container to load bean {@link Class classes}.
* *
* @return the {@link ClassLoader} used by the Spring container to load and create bean classes. * @return the {@link ClassLoader} used by the Spring container to load bean {@link Class classes}.
* @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(ClassLoader) * @see org.springframework.beans.factory.BeanClassLoaderAware#setBeanClassLoader(ClassLoader)
* @see java.lang.ClassLoader * @see java.lang.ClassLoader
* @see java.lang.Class
*/ */
public ClassLoader getBeanClassLoader() { public @Nullable ClassLoader getBeanClassLoader() {
return this.beanClassLoader; return this.beanClassLoader;
} }
@@ -107,7 +110,7 @@ public abstract class AbstractFactoryBeanSupport<T>
* @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.BeanFactory
*/ */
@Override @Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { public void setBeanFactory(@Nullable BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory; this.beanFactory = beanFactory;
} }
@@ -118,7 +121,7 @@ public abstract class AbstractFactoryBeanSupport<T>
* @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(BeanFactory) * @see org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(BeanFactory)
* @see org.springframework.beans.factory.BeanFactory * @see org.springframework.beans.factory.BeanFactory
*/ */
public BeanFactory getBeanFactory() { public @Nullable BeanFactory getBeanFactory() {
return this.beanFactory; return this.beanFactory;
} }
@@ -130,7 +133,7 @@ public abstract class AbstractFactoryBeanSupport<T>
* @see java.lang.String * @see java.lang.String
*/ */
@Override @Override
public void setBeanName(String name) { public void setBeanName(@Nullable String name) {
this.beanName = name; this.beanName = name;
} }
@@ -141,7 +144,7 @@ public abstract class AbstractFactoryBeanSupport<T>
* @see org.springframework.beans.factory.BeanNameAware#setBeanName(String) * @see org.springframework.beans.factory.BeanNameAware#setBeanName(String)
* @see java.lang.String * @see java.lang.String
*/ */
public String getBeanName() { public @Nullable String getBeanName() {
return this.beanName; return this.beanName;
} }
@@ -151,7 +154,7 @@ public abstract class AbstractFactoryBeanSupport<T>
* @return a reference to the {@link Logger} used by this {@link FactoryBean} to log {@link String messages}. * @return a reference to the {@link Logger} used by this {@link FactoryBean} to log {@link String messages}.
* @see org.apache.commons.logging.Log * @see org.apache.commons.logging.Log
*/ */
protected Logger getLog() { protected @Nullable Logger getLog() {
return this.log; return this.log;
} }