From e62391f825c73167e44252813a923e41e4e10695 Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sat, 9 Aug 2014 11:55:46 +0200 Subject: [PATCH] DATACMNS-558 - Some code cleanups according to the Sonar report. --- .../auditing/AuditableBeanWrapperFactory.java | 2 +- .../data/convert/CollectionFactory.java | 2 + .../data/domain/SliceImpl.java | 2 +- .../mapping/model/BasicPersistentEntity.java | 6 +-- .../model/PreferredConstructorDiscoverer.java | 6 +-- .../data/mapping/model/SimpleTypeHolder.java | 6 ++- .../support/RepositoryFactorySupport.java | 8 ++-- .../util/QueryExecutionConverters.java | 4 +- .../support/CachingIsNewStrategyFactory.java | 6 +-- .../data/util/ReflectionUtils.java | 2 + .../data/web/SpringDataAnnotationUtils.java | 2 + .../config/EnableSpringDataWebSupport.java | 42 +++++++++---------- 12 files changed, 48 insertions(+), 40 deletions(-) diff --git a/src/main/java/org/springframework/data/auditing/AuditableBeanWrapperFactory.java b/src/main/java/org/springframework/data/auditing/AuditableBeanWrapperFactory.java index 54b1bb506..7fea361e4 100644 --- a/src/main/java/org/springframework/data/auditing/AuditableBeanWrapperFactory.java +++ b/src/main/java/org/springframework/data/auditing/AuditableBeanWrapperFactory.java @@ -117,7 +117,7 @@ class AuditableBeanWrapperFactory { */ static abstract class DateConvertingAuditableBeanWrapper implements AuditableBeanWrapper { - private static boolean IS_JODA_TIME_PRESENT = ClassUtils.isPresent("org.joda.time.DateTime", + private static final boolean IS_JODA_TIME_PRESENT = ClassUtils.isPresent("org.joda.time.DateTime", ReflectionAuditingBeanWrapper.class.getClassLoader()); private final ConversionService conversionService; diff --git a/src/main/java/org/springframework/data/convert/CollectionFactory.java b/src/main/java/org/springframework/data/convert/CollectionFactory.java index 67fcce6e8..28591a04a 100644 --- a/src/main/java/org/springframework/data/convert/CollectionFactory.java +++ b/src/main/java/org/springframework/data/convert/CollectionFactory.java @@ -30,6 +30,8 @@ import org.springframework.util.Assert; */ public class CollectionFactory { + private CollectionFactory() {} + /** * Creates a new collection instance for the given collection type. Might also inspect the element type in case * special collections are requested (e.g. {@link EnumSet}). diff --git a/src/main/java/org/springframework/data/domain/SliceImpl.java b/src/main/java/org/springframework/data/domain/SliceImpl.java index d66a9229a..2b957fe59 100644 --- a/src/main/java/org/springframework/data/domain/SliceImpl.java +++ b/src/main/java/org/springframework/data/domain/SliceImpl.java @@ -74,7 +74,7 @@ public class SliceImpl extends Chunk { contentType = content.get(0).getClass().getName(); } - return String.format("Slice %s of %d containing %s instances", getNumber(), contentType); + return String.format("Slice %d containing %s instances", getNumber(), contentType); } /* diff --git a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java index e0e4a4244..5db6a9a65 100644 --- a/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java +++ b/src/main/java/org/springframework/data/mapping/model/BasicPersistentEntity.java @@ -175,10 +175,10 @@ public class BasicPersistentEntity> implement propertyCache.put(property.getName(), property); } - P idProperty = returnPropertyIfBetterIdPropertyCandidateOrNull(property); + P candidate = returnPropertyIfBetterIdPropertyCandidateOrNull(property); - if (idProperty != null) { - this.idProperty = idProperty; + if (candidate != null) { + this.idProperty = candidate; } if (property.isVersionProperty()) { diff --git a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java index 4648733b3..43e564cde 100644 --- a/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java +++ b/src/main/java/org/springframework/data/mapping/model/PreferredConstructorDiscoverer.java @@ -73,9 +73,9 @@ public class PreferredConstructorDiscoverer> int numberOfArgConstructors = 0; Class rawOwningType = type.getType(); - for (Constructor constructor : rawOwningType.getDeclaredConstructors()) { + for (Constructor candidate : rawOwningType.getDeclaredConstructors()) { - PreferredConstructor preferredConstructor = buildPreferredConstructor(constructor, type, entity); + PreferredConstructor preferredConstructor = buildPreferredConstructor(candidate, type, entity); // Explicitly defined constructor trumps all if (preferredConstructor.isExplicitlyAnnotated()) { @@ -135,4 +135,4 @@ public class PreferredConstructorDiscoverer> public PreferredConstructor getConstructor() { return constructor; } -} \ No newline at end of file +} diff --git a/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java b/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java index 7a4e24a77..dab3f9ece 100644 --- a/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java +++ b/src/main/java/org/springframework/data/mapping/model/SimpleTypeHolder.java @@ -115,15 +115,19 @@ public class SimpleTypeHolder { * @return */ public boolean isSimpleType(Class type) { + Assert.notNull(type); + if (Object.class.equals(type)) { return true; } + for (Class clazz : simpleTypes) { - if (type == clazz || clazz.isAssignableFrom(type)) { + if (clazz.isAssignableFrom(type)) { return true; } } + return false; } } diff --git a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java index 157da489b..145da0b51 100644 --- a/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java +++ b/src/main/java/org/springframework/data/repository/core/support/RepositoryFactorySupport.java @@ -60,9 +60,9 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { private static final TypeDescriptor WRAPPER_TYPE = TypeDescriptor.valueOf(NullableWrapper.class); - private final Map REPOSITORY_INFORMATION_CACHE = new HashMap(); - + private final Map repositoryInformationCache = new HashMap(); private final List postProcessors = new ArrayList(); + private QueryLookupStrategy.Key queryLookupStrategyKey; private List> queryPostProcessors = new ArrayList>(); private NamedQueries namedQueries = PropertiesBasedNamedQueries.EMPTY; @@ -193,7 +193,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { Class customImplementationClass) { RepositoryInformationCacheKey cacheKey = new RepositoryInformationCacheKey(metadata, customImplementationClass); - RepositoryInformation repositoryInformation = REPOSITORY_INFORMATION_CACHE.get(cacheKey); + RepositoryInformation repositoryInformation = repositoryInformationCache.get(cacheKey); if (repositoryInformation != null) { return repositoryInformation; @@ -201,7 +201,7 @@ public abstract class RepositoryFactorySupport implements BeanClassLoaderAware { repositoryInformation = new DefaultRepositoryInformation(metadata, getRepositoryBaseClass(metadata), customImplementationClass); - REPOSITORY_INFORMATION_CACHE.put(cacheKey, repositoryInformation); + repositoryInformationCache.put(cacheKey, repositoryInformation); return repositoryInformation; } diff --git a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java index 4b21eee09..f0104e236 100644 --- a/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java +++ b/src/main/java/org/springframework/data/repository/util/QueryExecutionConverters.java @@ -60,6 +60,8 @@ public class QueryExecutionConverters { } } + private QueryExecutionConverters() {} + /** * Returns whether the given type is a supported wrapper type. * @@ -243,7 +245,7 @@ public class QueryExecutionConverters { */ private static class NullableWrapperToFutureConverter extends AbstractWrapperTypeConverter { - private final AsyncResult NULL_OBJECT = new AsyncResult(null); + private static final AsyncResult NULL_OBJECT = new AsyncResult(null); /** * Creates a new {@link NullableWrapperToFutureConverter} using the given {@link ConversionService}. diff --git a/src/main/java/org/springframework/data/support/CachingIsNewStrategyFactory.java b/src/main/java/org/springframework/data/support/CachingIsNewStrategyFactory.java index cc7db2c96..6ee9949a1 100644 --- a/src/main/java/org/springframework/data/support/CachingIsNewStrategyFactory.java +++ b/src/main/java/org/springframework/data/support/CachingIsNewStrategyFactory.java @@ -28,7 +28,7 @@ import org.springframework.util.Assert; */ public class CachingIsNewStrategyFactory implements IsNewStrategyFactory { - private final Map, IsNewStrategy> CACHE = new ConcurrentHashMap, IsNewStrategy>(); + private final Map, IsNewStrategy> cache = new ConcurrentHashMap, IsNewStrategy>(); private final IsNewStrategyFactory delegate; /** @@ -48,7 +48,7 @@ public class CachingIsNewStrategyFactory implements IsNewStrategyFactory { */ public IsNewStrategy getIsNewStrategy(Class type) { - IsNewStrategy strategy = CACHE.get(type); + IsNewStrategy strategy = cache.get(type); if (strategy != null) { return strategy; @@ -56,7 +56,7 @@ public class CachingIsNewStrategyFactory implements IsNewStrategyFactory { IsNewStrategy isNewStrategy = delegate.getIsNewStrategy(type); - CACHE.put(type, isNewStrategy); + cache.put(type, isNewStrategy); return isNewStrategy; } } diff --git a/src/main/java/org/springframework/data/util/ReflectionUtils.java b/src/main/java/org/springframework/data/util/ReflectionUtils.java index 9ebd35634..7bfc68640 100644 --- a/src/main/java/org/springframework/data/util/ReflectionUtils.java +++ b/src/main/java/org/springframework/data/util/ReflectionUtils.java @@ -32,6 +32,8 @@ import org.springframework.util.ReflectionUtils.FieldFilter; */ public class ReflectionUtils { + private ReflectionUtils() {} + /** * Creates an instance of the class with the given fully qualified name or returns the given default instance if the * class cannot be loaded or instantiated. diff --git a/src/main/java/org/springframework/data/web/SpringDataAnnotationUtils.java b/src/main/java/org/springframework/data/web/SpringDataAnnotationUtils.java index 552676b88..1bcbc4552 100644 --- a/src/main/java/org/springframework/data/web/SpringDataAnnotationUtils.java +++ b/src/main/java/org/springframework/data/web/SpringDataAnnotationUtils.java @@ -34,6 +34,8 @@ import org.springframework.util.ObjectUtils; */ class SpringDataAnnotationUtils { + private SpringDataAnnotationUtils() {} + /** * Asserts uniqueness of all {@link Pageable} parameters of the method of the given {@link MethodParameter}. * diff --git a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java index 0c9e7e28b..0a8e3e27e 100644 --- a/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java +++ b/src/main/java/org/springframework/data/web/config/EnableSpringDataWebSupport.java @@ -26,38 +26,33 @@ import java.util.List; import org.springframework.context.annotation.Import; import org.springframework.context.annotation.ImportSelector; import org.springframework.core.type.AnnotationMetadata; -import org.springframework.data.domain.Pageable; -import org.springframework.data.domain.Sort; -import org.springframework.data.repository.support.DomainClassConverter; -import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver; -import org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver; import org.springframework.data.web.PageableHandlerMethodArgumentResolver; -import org.springframework.data.web.PagedResourcesAssembler; -import org.springframework.data.web.PagedResourcesAssemblerArgumentResolver; -import org.springframework.data.web.SortHandlerMethodArgumentResolver; import org.springframework.util.ClassUtils; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * Annotation to automatically register the following beans for usage with Spring MVC. Note that using this annotation * will require Spring 3.2. *
    - *
  • {@link DomainClassConverter} - to allow usage of domain types managed by Spring Data repositories as controller - * method arguments bound with {@link PathVariable} or {@link RequestParam}.
  • - *
  • {@link PageableHandlerMethodArgumentResolver} - to allow injection of {@link Pageable} instances into controller - * methods automatically created from request parameters.
  • - *
  • {@link SortHandlerMethodArgumentResolver} - to allow injection of {@link Sort} instances into controller methods - * automatically created from request parameters.
  • + *
  • {@link org.springframework.data.repository.support.DomainClassConverter} - to allow usage of domain types managed + * by Spring Data repositories as controller method arguments bound with + * {@link org.springframework.web.bind.annotation.PathVariable} or + * {@link org.springframework.web.bind.annotation.RequestParam}.
  • + *
  • {@link PageableHandlerMethodArgumentResolver} - to allow injection of + * {@link org.springframework.data.domain.Pageable} instances into controller methods automatically created from request + * parameters.
  • + *
  • {@link org.springframework.data.web.SortHandlerMethodArgumentResolver} - to allow injection of + * {@link org.springframework.data.domain.Sort} instances into controller methods automatically created from request + * parameters.
  • *
* If Spring HATEOAS is present on the classpath we will register the following beans: *
    - *
  • {@link HateoasPageableHandlerMethodArgumentResolver} - instead of {@link PageableHandlerMethodArgumentResolver}
  • - *
  • {@link HateoasSortHandlerMethodArgumentResolver} - instead of {@link SortHandlerMethodArgumentResolver}
  • - *
  • {@link PagedResourcesAssembler} - for injection into web components
  • - *
  • {@link PagedResourcesAssemblerArgumentResolver} - for injection of {@link PagedResourcesAssembler} into - * controller methods
  • + *
  • {@link org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver} - instead of + * {@link PageableHandlerMethodArgumentResolver}
  • + *
  • {@link org.springframework.data.web.HateoasSortHandlerMethodArgumentResolver} - instead of + * {@link org.springframework.data.web.SortHandlerMethodArgumentResolver}
  • + *
  • {@link org.springframework.data.web.PagedResourcesAssembler} - for injection into web components
  • + *
  • {@link org.springframework.data.web.SortHandlerMethodArgumentResolver} - for injection of + * {@link org.springframework.data.web.PagedResourcesAssembler} into controller methods
  • *
      * * @since 1.6 @@ -74,7 +69,8 @@ public @interface EnableSpringDataWebSupport { /** * Import selector to import the appropriate configuration class depending on whether Spring HATEOAS is present on the * classpath. We need to register the HATEOAS specific class first as apparently only the first class implementing - * {@link WebMvcConfigurationSupport} gets callbacks invoked (see https://jira.springsource.org/browse/SPR-10565). + * {@link org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport} gets callbacks invoked (see + * https://jira.springsource.org/browse/SPR-10565). * * @author Oliver Gierke */