From c9cb4fbea0ca25d781c399d524c6ed965a6a602d Mon Sep 17 00:00:00 2001 From: John Blum Date: Thu, 2 Apr 2020 00:37:49 -0700 Subject: [PATCH] DATAGEODE-302 - Edit Javadoc. Format source code. Optimize imports. --- ...tFunctionExecutionConfigurationSource.java | 28 +++++------ ...nFunctionExecutionConfigurationSource.java | 49 ++++++++++-------- ...ctionExecutionBeanDefinitionRegistrar.java | 9 ++-- .../FunctionExecutionComponentProvider.java | 50 ++++++++++++------- .../FunctionExecutionConfiguration.java | 2 +- 5 files changed, 76 insertions(+), 62 deletions(-) diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AbstractFunctionExecutionConfigurationSource.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AbstractFunctionExecutionConfigurationSource.java index 1e25bbb2..61ced65f 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AbstractFunctionExecutionConfigurationSource.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AbstractFunctionExecutionConfigurationSource.java @@ -16,8 +16,6 @@ */ package org.springframework.data.gemfire.function.config; -import static org.springframework.data.gemfire.util.CollectionUtils.nullSafeIterable; - import java.lang.annotation.Annotation; import java.util.Collection; import java.util.Collections; @@ -26,9 +24,6 @@ import java.util.Set; import java.util.stream.Collectors; import java.util.stream.StreamSupport; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider; import org.springframework.context.annotation.ScannedGenericBeanDefinition; import org.springframework.core.io.ResourceLoader; @@ -37,15 +32,13 @@ import org.springframework.data.gemfire.function.annotation.OnMembers; import org.springframework.data.gemfire.function.annotation.OnRegion; import org.springframework.data.gemfire.function.annotation.OnServer; import org.springframework.data.gemfire.function.annotation.OnServers; +import org.springframework.data.gemfire.util.CollectionUtils; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** -<<<<<<< Updated upstream - * Annotation based configuration source for function executions - * - * @author David Turanski -======= * Abstract base class and configuration source for Function Executions. ->>>>>>> Stashed changes * * @author David Turanski * @author John Blum @@ -73,7 +66,10 @@ public abstract class AbstractFunctionExecutionConfigurationSource implements Fu } public static Set getFunctionExecutionAnnotationTypeNames() { - return getFunctionExecutionAnnotationTypes().stream().map(Class::getName).collect(Collectors.toSet()); + + return getFunctionExecutionAnnotationTypes().stream() + .map(Class::getName) + .collect(Collectors.toSet()); } protected Logger logger = LoggerFactory.getLogger(getClass()); @@ -85,19 +81,19 @@ public abstract class AbstractFunctionExecutionConfigurationSource implements Fu scanner.setResourceLoader(loader); - StreamSupport.stream(nullSafeIterable(getExcludeFilters()).spliterator(), false) + StreamSupport.stream(CollectionUtils.nullSafeIterable(getExcludeFilters()).spliterator(), false) .forEach(scanner::addExcludeFilter); Set result = new HashSet<>(); for (String basePackage : getBasePackages()) { - if (logger.isDebugEnabled()) { - logger.debug("scanning package " + basePackage); + if (this.logger.isDebugEnabled()) { + this.logger.debug("Scanning Package [{}]", basePackage); } scanner.findCandidateComponents(basePackage).stream() - .map(beanDefinition -> (ScannedGenericBeanDefinition) beanDefinition) + .map(ScannedGenericBeanDefinition.class::cast) .forEach(result::add); } diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AnnotationFunctionExecutionConfigurationSource.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AnnotationFunctionExecutionConfigurationSource.java index 3d83b4a7..8993e8af 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AnnotationFunctionExecutionConfigurationSource.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/AnnotationFunctionExecutionConfigurationSource.java @@ -13,6 +13,7 @@ package org.springframework.data.gemfire.function.config; import static org.springframework.data.gemfire.util.ArrayUtils.nullSafeArray; +import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalArgumentException; import java.lang.annotation.Annotation; import java.util.ArrayList; @@ -48,12 +49,13 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc private final AnnotationMetadata metadata; private final AnnotationAttributes attributes; - /** - * Creates a new {@link AnnotationFunctionExecutionConfigurationSource} from the given {@link AnnotationMetadata} and - * annotation. + * Creates a new instance of {@link AnnotationFunctionExecutionConfigurationSource} from + * the given {@link AnnotationMetadata} and {@link EnableGemfireFunctionExecutions} annotation. * - * @param metadata must not be {@literal null}. + * @param metadata {@link AnnotationMetadata} for the {@link EnableGemfireFunctionExecutions} annotation; + * must not be {@literal null}. + * @see org.springframework.core.type.AnnotationMetadata */ public AnnotationFunctionExecutionConfigurationSource(AnnotationMetadata metadata) { @@ -65,8 +67,6 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc this.metadata = metadata; } - - /* (non-Javadoc) * @see org.springframework.data.gemfire.function.config.FunctionExecutionConfigurationSource#getSource() */ @@ -108,7 +108,7 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc private boolean areAllEmpty(Object[]... arrays) { - for (Object[] array : arrays) { + for (Object[] array : ArrayUtils.nullSafeArray(arrays, Object[].class)) { if (!ArrayUtils.isEmpty(array)) { return false; } @@ -129,12 +129,13 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc private Set parseFilters(String attributeName) { - Set result = new HashSet(); - AnnotationAttributes[] filters = attributes.getAnnotationArray(attributeName); + Set result = new HashSet<>(); - for (AnnotationAttributes filter : filters) { - result.addAll(typeFiltersFor(filter)); - } + AnnotationAttributes[] filters = this.attributes.getAnnotationArray(attributeName); + + Arrays.stream(ArrayUtils.nullSafeArray(filters, AnnotationAttributes.class)) + .map(this::typeFiltersFor) + .forEach(result::addAll); return result; } @@ -142,19 +143,23 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc /** * Copy of {@code ComponentScanAnnotationParser#typeFiltersFor}. * - * @param filterAttributes - * @return + * @param filterAttributes {@link AnnotationAttributes} for the {@literal include} and {@literal exclude} filters. + * @return a {@link List} of {@link TypeFilter TypeFilters} based on the configuration of the {@literal include} + * and {@literal exclude} attributes. + * @see org.springframework.core.annotation.AnnotationAttributes */ + @SuppressWarnings("unchecked") private List typeFiltersFor(AnnotationAttributes filterAttributes) { - List typeFilters = new ArrayList(); + + List typeFilters = new ArrayList<>(); + FilterType filterType = filterAttributes.getEnum("type"); for (Class filterClass : filterAttributes.getClassArray("value")) { switch (filterType) { case ANNOTATION: - Assert.isAssignable(Annotation.class, filterClass, "An error occured when processing a @ComponentScan " - + "ANNOTATION type filter: "); - @SuppressWarnings("unchecked") + String message = "An error occured when processing a @ComponentScan ANNOTATION type filter: "; + Assert.isAssignable(Annotation.class, filterClass, message); Class annoClass = (Class) filterClass; typeFilters.add(new AnnotationTypeFilter(annoClass)); break; @@ -162,15 +167,15 @@ public class AnnotationFunctionExecutionConfigurationSource extends AbstractFunc typeFilters.add(new AssignableTypeFilter(filterClass)); break; case CUSTOM: - Assert.isAssignable(TypeFilter.class, filterClass, "An error occured when processing a @ComponentScan " - + "CUSTOM type filter: "); + message = "An error occurred when processing a @ComponentScan CUSTOM type filter: "; + Assert.isAssignable(TypeFilter.class, filterClass, message); typeFilters.add(BeanUtils.instantiateClass(filterClass, TypeFilter.class)); break; default: - throw new IllegalArgumentException("unknown filter type " + filterType); + throw newIllegalArgumentException("Unknown filter type [%s]", filterType); } } + return typeFilters; } - } diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionBeanDefinitionRegistrar.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionBeanDefinitionRegistrar.java index cd010ccc..b0c3d830 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionBeanDefinitionRegistrar.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionBeanDefinitionRegistrar.java @@ -14,7 +14,6 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ - package org.springframework.data.gemfire.function.config; import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; @@ -22,8 +21,6 @@ import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newI import java.util.Optional; import java.util.Set; -import org.w3c.dom.Element; - import org.springframework.beans.factory.support.BeanDefinitionReaderUtils; import org.springframework.beans.factory.support.BeanDefinitionRegistry; import org.springframework.beans.factory.xml.ParserContext; @@ -40,6 +37,8 @@ import org.springframework.data.gemfire.function.annotation.OnServers; import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import org.w3c.dom.Element; + /** * {@link ImportBeanDefinitionRegistrar} for {@link EnableGemfireFunctionExecutions}, which scans for interfaces * annotated with one of {@link OnRegion}, {@link OnServer}, {@link OnServers}, {@link OnMember}, {@link OnMembers}. @@ -101,8 +100,8 @@ public class FunctionExecutionBeanDefinitionRegistrar implements ImportBeanDefin String functionExecutionAnnotation = Optional.ofNullable(getFunctionExecutionAnnotation(beanDefinition, functionExecutionAnnotationTypeNames)) - .orElseThrow(() -> newIllegalStateException(String.format("No Function Execution Annotation [%1$s] found for type [%2$s]", - functionExecutionAnnotationTypeNames, beanDefinition.getBeanClassName()))); + .orElseThrow(() -> newIllegalStateException("No Function Execution Annotation [%1$s] found for type [%2$s]", + functionExecutionAnnotationTypeNames, beanDefinition.getBeanClassName())); String beanName = Optional.of(beanDefinition.getMetadata()) .map(annotationMetadata -> annotationMetadata.getAnnotationAttributes(functionExecutionAnnotation)) diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionComponentProvider.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionComponentProvider.java index e4b4706a..ff2f5b55 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionComponentProvider.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionComponentProvider.java @@ -26,6 +26,7 @@ import org.springframework.core.type.classreading.MetadataReader; import org.springframework.core.type.classreading.MetadataReaderFactory; import org.springframework.core.type.filter.AbstractTypeHierarchyTraversingFilter; import org.springframework.core.type.filter.TypeFilter; +import org.springframework.data.gemfire.util.CollectionUtils; import org.springframework.util.Assert; /** @@ -36,7 +37,6 @@ import org.springframework.util.Assert; */ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateComponentProvider { - private final Set> functionExecutionAnnotationTypes; /** @@ -46,41 +46,48 @@ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateCompo * @param includeFilters the {@link TypeFilter}s to select function execution interfaces to consider, must not be * {@literal null}. */ - public FunctionExecutionComponentProvider(Iterable includeFilters , Set> functionExecutionAnnotationTypes) { + public FunctionExecutionComponentProvider(Iterable includeFilters , + Set> functionExecutionAnnotationTypes) { super(false); this.functionExecutionAnnotationTypes = functionExecutionAnnotationTypes; - Assert.notNull(includeFilters); - if (includeFilters.iterator().hasNext()) { + if (!CollectionUtils.nullSafeIsEmpty(includeFilters)) { for (TypeFilter filter : includeFilters) { addIncludeFilter(filter); } - } else { - for (Class annotation: this.functionExecutionAnnotationTypes) { + } + else { + for (Class annotation : this.functionExecutionAnnotationTypes) { super.addIncludeFilter(new AnnotationTypeFilter(annotation, true, true)); } } } /** - * Custom extension of {@link #addIncludeFilter(TypeFilter)} to extend the added {@link TypeFilter}. For the - * {@link TypeFilter} handed we'll have two filters registered: one additionally enforcing the - * {@link FunctionExecutionDefinition} annotation, the other one forcing the extension of {@link AbstractFunctionExecution}. + * Custom extension of {@link ClassPathScanningCandidateComponentProvider#addIncludeFilter(TypeFilter)} + * to extend the added {@link TypeFilter}. + * + * For the {@link TypeFilter} handed, we will have two filters registered: one additionally enforcing the + * annotation and the other one forcing the extension of {@literal AbstractFunctionExecution}. * * @see ClassPathScanningCandidateComponentProvider#addIncludeFilter(TypeFilter) */ @Override public void addIncludeFilter(TypeFilter includeFilter) { - List filterPlusInterface = new ArrayList(); + List filterPlusInterface = new ArrayList<>(); + + // TODO: What about the interface? filterPlusInterface.add(includeFilter); super.addIncludeFilter(new AllTypeFilter(filterPlusInterface)); - List filterPlusAnnotation = new ArrayList(); + List filterPlusAnnotation = new ArrayList<>(); + filterPlusAnnotation.add(includeFilter); + for (Class annotation: this.functionExecutionAnnotationTypes) { filterPlusAnnotation.add(new AnnotationTypeFilter(annotation, true, true)); } @@ -150,27 +157,34 @@ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateCompo */ public AnnotationTypeFilter(Class annotationType, boolean considerMetaAnnotations, boolean considerInterfaces) { + super(annotationType.isAnnotationPresent(Inherited.class), considerInterfaces); + this.annotationType = annotationType; this.considerMetaAnnotations = considerMetaAnnotations; } @Override protected boolean matchSelf(MetadataReader metadataReader) { + AnnotationMetadata metadata = metadataReader.getAnnotationMetadata(); + return metadata.hasAnnotation(this.annotationType.getName()) || (this.considerMetaAnnotations && metadata.hasMetaAnnotation(this.annotationType.getName())); } @Override protected Boolean matchSuperClass(String superClassName) { + if (Object.class.getName().equals(superClassName)) { return Boolean.FALSE; - } else if (superClassName.startsWith("java.")) { + } + else if (superClassName.startsWith("java.")) { try { - Class clazz = getClass().getClassLoader().loadClass(superClassName); - return (clazz.getAnnotation(this.annotationType) != null); - } catch (ClassNotFoundException ex) { + Class type = getClass().getClassLoader().loadClass(superClassName); + return type.getAnnotation(this.annotationType) != null; + } + catch (ClassNotFoundException ignore) { // Class not found - can't determine a match that way. } } @@ -194,7 +208,8 @@ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateCompo */ public AllTypeFilter(List delegates) { - Assert.notNull(delegates); + Assert.notNull(delegates, "Delegate TypeFilters must not be null"); + this.delegates = delegates; } @@ -204,7 +219,7 @@ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateCompo */ public boolean match(MetadataReader metadataReader, MetadataReaderFactory metadataReaderFactory) throws IOException { - for (TypeFilter filter : delegates) { + for (TypeFilter filter : this.delegates) { if (!filter.match(metadataReader, metadataReaderFactory)) { return false; } @@ -214,4 +229,3 @@ class FunctionExecutionComponentProvider extends ClassPathScanningCandidateCompo } } } - diff --git a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionConfiguration.java b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionConfiguration.java index 8c6e2afa..9890d1ee 100644 --- a/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionConfiguration.java +++ b/spring-data-geode/src/main/java/org/springframework/data/gemfire/function/config/FunctionExecutionConfiguration.java @@ -35,7 +35,7 @@ class FunctionExecutionConfiguration { private final String annotationType; - /* constructor for testing purposes only */ + /* Constructor used for testing purposes only! */ FunctionExecutionConfiguration() { this.annotationType = null; this.annotationAttributes = null;