From e67e35ac8d4e18d48fe0351d2028d1b93779ab08 Mon Sep 17 00:00:00 2001 From: Jon Brisbin Date: Tue, 10 May 2011 10:50:31 -0500 Subject: [PATCH] Reformatting source code to use tabs instead of spaces. --- spring-data-commons-core/pom.xml | 230 ++--- .../data/annotation/Persistent.java | 8 +- .../data/annotation/Reference.java | 4 +- .../data/domain/Auditable.java | 102 +-- .../data/domain/AuditorAware.java | 16 +- .../org/springframework/data/domain/Page.java | 166 ++-- .../springframework/data/domain/PageImpl.java | 442 +++++----- .../data/domain/PageRequest.java | 228 ++--- .../springframework/data/domain/Pageable.java | 52 +- .../data/domain/Persistable.java | 28 +- .../org/springframework/data/domain/Sort.java | 624 +++++++------- .../data/domain/package-info.java | 2 +- .../data/mapping/AbstractMappingContext.java | 373 ++++----- .../mapping/AbstractPersistentProperty.java | 22 +- .../AnnotationBasedPersistentProperty.java | 136 +-- .../data/mapping/AssociationHandler.java | 2 +- .../data/mapping/BasicPersistentEntity.java | 154 ++-- .../data/mapping/MappingBeanHelper.java | 26 +- .../data/mapping/MutablePersistentEntity.java | 52 +- .../PreferredConstructorDiscoverer.java | 136 +-- .../data/mapping/PropertyHandler.java | 4 +- .../mapping/context/MappingContextAware.java | 12 +- .../MappingContextAwareBeanPostProcessor.java | 2 +- .../mapping/event/MappingContextEvent.java | 26 +- .../data/mapping/model/Association.java | 24 +- .../model/IllegalMappingException.java | 18 +- .../model/MappingConfigurationException.java | 16 +- .../data/mapping/model/MappingContext.java | 34 +- .../data/mapping/model/MappingException.java | 14 +- .../model/MappingInstantiationException.java | 10 +- .../data/mapping/model/PersistentEntity.java | 100 +-- .../mapping/model/PreferredConstructor.java | 50 +- ...AbstractConstructorEntityInstantiator.java | 283 +++---- .../data/persistence/ChangeSet.java | 47 +- .../data/persistence/ChangeSetBacked.java | 27 +- .../persistence/ChangeSetConfiguration.java | 5 +- .../data/persistence/ChangeSetPersister.java | 26 +- .../persistence/ChangeSetSynchronizer.java | 57 +- .../data/persistence/EntityInstantiator.java | 64 +- .../data/persistence/HashMapChangeSet.java | 93 +- .../data/persistence/StateBackedCreator.java | 12 +- .../data/persistence/StateProvider.java | 25 +- .../data/querydsl/EntityPathResolver.java | 4 +- .../data/querydsl/QueryDslUtils.java | 5 +- .../querydsl/SimpleEntityPathResolver.java | 165 ++-- .../data/repository/NoRepositoryBean.java | 4 +- .../PagingAndSortingRepository.java | 36 +- .../data/repository/Repository.java | 122 +-- ...tractRepositoryConfigDefinitionParser.java | 792 +++++++++--------- .../AutomaticRepositoryConfigInformation.java | 70 +- .../CommonRepositoryConfigInformation.java | 80 +- .../GlobalRepositoryConfigInformation.java | 58 +- .../ManualRepositoryConfigInformation.java | 240 +++--- ...DelegatingRepositoryConfigInformation.java | 246 +++--- .../repository/config/RepositoryConfig.java | 282 +++---- .../SingleRepositoryConfigInformation.java | 80 +- .../repository/config/TypeFilterParser.java | 369 ++++---- .../data/repository/query/Param.java | 4 +- .../data/repository/query/Parameter.java | 290 +++---- .../repository/query/ParameterAccessor.java | 72 +- .../query/ParameterOutOfBoundsException.java | 22 +- .../data/repository/query/Parameters.java | 512 +++++------ .../query/ParametersParameterAccessor.java | 192 ++--- .../query/QueryCreationException.java | 96 +-- .../repository/query/QueryLookupStrategy.java | 50 +- .../data/repository/query/QueryMethod.java | 222 ++--- .../repository/query/RepositoryQuery.java | 31 +- .../query/parser/AbstractQueryCreator.java | 164 ++-- .../query/parser/OrderBySource.java | 86 +- .../data/repository/query/parser/Part.java | 372 ++++---- .../repository/query/parser/PartTree.java | 342 ++++---- .../repository/query/parser/Property.java | 436 +++++----- .../support/AbstractEntityInformation.java | 66 +- .../support/DefaultRepositoryInformation.java | 396 ++++----- .../support/DefaultRepositoryMetadata.java | 91 +- .../support/DomainClassConverter.java | 176 ++-- .../support/DomainClassPropertyEditor.java | 225 +++-- .../DomainClassPropertyEditorRegistrar.java | 104 ++- .../repository/support/EntityInformation.java | 44 +- .../repository/support/EntityMetadata.java | 14 +- .../support/PersistableEntityInformation.java | 84 +- .../support/QueryCreationListener.java | 14 +- .../support/RepositoryFactoryBeanSupport.java | 194 ++--- .../support/RepositoryFactoryInformation.java | 26 +- .../support/RepositoryFactorySupport.java | 554 ++++++------ .../support/RepositoryInformation.java | 80 +- ...sitoryInterfaceAwareBeanPostProcessor.java | 126 +-- .../support/RepositoryMetadata.java | 11 +- .../support/RepositoryProxyPostProcessor.java | 16 +- ...sactionalRepositoryFactoryBeanSupport.java | 106 +-- ...sactionalRepositoryProxyPostProcessor.java | 66 +- .../data/repository/util/ClassUtils.java | 262 +++--- .../data/repository/util/TxUtils.java | 10 +- ...geSetBackedTransactionSynchronization.java | 7 +- .../NaiveDoubleTransactionManager.java | 148 ++-- .../data/util/ArrayTypeDiscoverer.java | 60 +- .../data/util/ClassTypeInformation.java | 139 ++- .../data/util/GenericTypeResolver.java | 578 +++++++------ .../data/util/TypeDiscoverer.java | 406 ++++----- .../data/util/TypeInformation.java | 94 +-- .../util/TypeVariableTypeInformation.java | 68 +- .../data/web/PageableArgumentResolver.java | 427 +++++----- .../data/web/PageableDefaults.java | 26 +- .../config/spring-repository-1.0.xsd | 50 +- .../data/domain/DirectionUnitTests.java | 20 +- .../data/domain/PageImplUnitTests.java | 68 +- .../data/domain/PageRequestUnitTests.java | 82 +- .../data/domain/SortUnitTests.java | 102 +-- .../data/domain/UnitTestUtils.java | 52 +- .../springframework/data/mapping/Child.java | 6 +- .../data/mapping/Document.java | 8 +- .../data/mapping/MappingMetadataTests.java | 104 +-- .../springframework/data/mapping/Person.java | 54 +- .../data/mapping/PersonDocument.java | 6 +- .../data/mapping/PersonNoId.java | 6 +- .../data/mapping/PersonPersistent.java | 6 +- .../data/mapping/PersonWithChildren.java | 32 +- .../data/mapping/PersonWithId.java | 16 +- ...eferredConstructorDiscovererUnitTests.java | 150 ++-- .../SimpleEntityPathResolverUnitTests.java | 56 +- .../config/TypeFilterParserUnitTests.java | 85 +- .../repository/query/ParametersUnitTests.java | 156 ++-- .../SimpleParameterAccessorUnitTests.java | 146 ++-- .../query/parser/OrderBySourceUnitTests.java | 70 +- .../query/parser/PartTreeUnitTests.java | 208 ++--- .../query/parser/PropertyUnitTests.java | 194 ++--- .../AbstractEntityInformationUnitTests.java | 72 +- ...DefaultRepositoryInformationUnitTests.java | 63 +- .../DefaultRepositoryMetadataUnitTests.java | 156 ++-- .../DomainClassConverterUnitTests.java | 199 +++-- ...ClassPropertyEditorRegistrarUnitTests.java | 143 ++-- .../DomainClassPropertyEditorUnitTests.java | 189 +++-- ...PersistableEntityInformationUnitTests.java | 74 +- ...eryExecuterMethodInterceptorUnitTests.java | 16 +- .../RepositoryFactorySupportUnitTests.java | 116 +-- ...erfaceAwareBeanPostProcessorUnitTests.java | 110 +-- ...RepositoryProxyPostProcessorUnitTests.java | 70 +- .../repository/util/ClassUtilsUnitTests.java | 108 +-- .../util/ClassTypeInformationUnitTests.java | 256 +++--- .../data/util/TypeDiscovererUnitTests.java | 8 +- .../PageableArgumentResolverUnitTests.java | 202 ++--- .../src/test/resources/log4j.xml | 22 +- .../src/test/resources/mapping.xml | 6 +- .../repository/config/type-filter-test.xml | 14 +- 144 files changed, 8643 insertions(+), 8672 deletions(-) diff --git a/spring-data-commons-core/pom.xml b/spring-data-commons-core/pom.xml index 6038e3242..9ce8a9565 100644 --- a/spring-data-commons-core/pom.xml +++ b/spring-data-commons-core/pom.xml @@ -1,124 +1,124 @@ - 4.0.0 - - org.springframework.data - spring-data-commons-parent - 1.1.0.BUILD-SNAPSHOT - ../spring-data-commons-parent/pom.xml - - spring-data-commons-core - jar - Spring Data Commons Core - - - 2.2.0-beta4 - - - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + 4.0.0 + + org.springframework.data + spring-data-commons-parent + 1.1.0.BUILD-SNAPSHOT + ../spring-data-commons-parent/pom.xml + + spring-data-commons-core + jar + Spring Data Commons Core - - - org.springframework - spring-beans - - - org.springframework - spring-tx - - - - org.springframework - spring-web - true - - - javax.servlet - servlet-api - 2.5 - provided - + + 2.2.0-beta4 + - - - log4j - log4j - test - + - - javax.annotation - jsr250-api - true - + + + org.springframework + spring-beans + + + org.springframework + spring-tx + - - org.mockito - mockito-all - test - + + org.springframework + spring-web + true + + + javax.servlet + servlet-api + 2.5 + provided + - - junit - junit - - - org.springframework - spring-test - test - + + + log4j + log4j + test + - - joda-time - joda-time - 1.6 - true - - - - com.mysema.querydsl - querydsl-mongodb - ${querydsl.version} - true - - - com.google.code.morphia - morphia - - - - - - com.mysema.querydsl - querydsl-apt - ${querydsl.version} - provided - + + javax.annotation + jsr250-api + true + - - - - - com.springsource.bundlor - com.springsource.bundlor.maven - - - com.mysema.maven - maven-apt-plugin - 1.0.1 - - - generate-test-sources - - test-process - - - target/generated-sources/test-annotations - com.mysema.query.apt.QuerydslAnnotationProcessor - - - - - - + + org.mockito + mockito-all + test + + + + junit + junit + + + org.springframework + spring-test + test + + + + joda-time + joda-time + 1.6 + true + + + + com.mysema.querydsl + querydsl-mongodb + ${querydsl.version} + true + + + com.google.code.morphia + morphia + + + + + + com.mysema.querydsl + querydsl-apt + ${querydsl.version} + provided + + + + + + + com.springsource.bundlor + com.springsource.bundlor.maven + + + com.mysema.maven + maven-apt-plugin + 1.0.1 + + + generate-test-sources + + test-process + + + target/generated-sources/test-annotations + com.mysema.query.apt.QuerydslAnnotationProcessor + + + + + + diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Persistent.java b/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Persistent.java index c613148d7..8c94efb85 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Persistent.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Persistent.java @@ -26,10 +26,10 @@ import java.lang.annotation.Target; */ @Retention(RetentionPolicy.RUNTIME) @Target(value = { - ElementType.TYPE, - ElementType.ANNOTATION_TYPE, - ElementType.FIELD, - ElementType.PARAMETER + ElementType.TYPE, + ElementType.ANNOTATION_TYPE, + ElementType.FIELD, + ElementType.PARAMETER }) public @interface Persistent { } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Reference.java b/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Reference.java index e1adb0bfa..9ac33e012 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Reference.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/annotation/Reference.java @@ -26,8 +26,8 @@ import java.lang.annotation.Target; */ @Retention(RetentionPolicy.RUNTIME) @Target({ - ElementType.ANNOTATION_TYPE, - ElementType.FIELD + ElementType.ANNOTATION_TYPE, + ElementType.FIELD }) public @interface Reference { } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Auditable.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Auditable.java index 7b66bed55..ec46019e0 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Auditable.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Auditable.java @@ -24,73 +24,73 @@ import org.joda.time.DateTime; * Interface for auditable entities. Allows storing and retrieving creation and * modification information. The changing instance (typically some user) is to * be defined by a generics definition. - * - * @author Oliver Gierke - * @param the auditing type. Typically some kind of user. + * + * @param the auditing type. Typically some kind of user. * @param the type of the auditing type's idenifier + * @author Oliver Gierke */ public interface Auditable extends Persistable { - /** - * Returns the user who created this entity. - * - * @return the createdBy - */ - U getCreatedBy(); + /** + * Returns the user who created this entity. + * + * @return the createdBy + */ + U getCreatedBy(); - /** - * Sets the user who created this entity. - * - * @param createdBy the creating entity to set - */ - void setCreatedBy(final U createdBy); + /** + * Sets the user who created this entity. + * + * @param createdBy the creating entity to set + */ + void setCreatedBy(final U createdBy); - /** - * Returns the creation date of the entity. - * - * @return the createdDate - */ - DateTime getCreatedDate(); + /** + * Returns the creation date of the entity. + * + * @return the createdDate + */ + DateTime getCreatedDate(); - /** - * Sets the creation date of the entity. - * - * @param creationDate the creation date to set - */ - void setCreatedDate(final DateTime creationDate); + /** + * Sets the creation date of the entity. + * + * @param creationDate the creation date to set + */ + void setCreatedDate(final DateTime creationDate); - /** - * Returns the user who modified the entity lastly. - * - * @return the lastModifiedBy - */ - U getLastModifiedBy(); + /** + * Returns the user who modified the entity lastly. + * + * @return the lastModifiedBy + */ + U getLastModifiedBy(); - /** - * Sets the user who modified the entity lastly. - * - * @param lastModifiedBy the last modifying entity to set - */ - void setLastModifiedBy(final U lastModifiedBy); + /** + * Sets the user who modified the entity lastly. + * + * @param lastModifiedBy the last modifying entity to set + */ + void setLastModifiedBy(final U lastModifiedBy); - /** - * Returns the date of the last modification. - * - * @return the lastModifiedDate - */ - DateTime getLastModifiedDate(); + /** + * Returns the date of the last modification. + * + * @return the lastModifiedDate + */ + DateTime getLastModifiedDate(); - /** - * Sets the date of the last modification. - * - * @param lastModifiedDate the date of the last modification to set - */ - void setLastModifiedDate(final DateTime lastModifiedDate); + /** + * Sets the date of the last modification. + * + * @param lastModifiedDate the date of the last modification to set + */ + void setLastModifiedDate(final DateTime lastModifiedDate); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/AuditorAware.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/AuditorAware.java index d8ba081f5..83869166e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/AuditorAware.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/AuditorAware.java @@ -18,16 +18,16 @@ package org.springframework.data.domain; /** * Interface for components that are aware of the application's current auditor. * This will be some kind of user mostly. - * - * @author Oliver Gierke + * * @param the type of the auditing instance + * @author Oliver Gierke */ public interface AuditorAware { - /** - * Returns the current auditor of the application. - * - * @return the current auditor - */ - T getCurrentAuditor(); + /** + * Returns the current auditor of the application. + * + * @return the current auditor + */ + T getCurrentAuditor(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java index aec3ac952..ddab1aee7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Page.java @@ -22,113 +22,113 @@ import java.util.List; /** * A page is a sublist of a list of objects. It allows gain information about * the position of it in the containing entire list. - * - * @author Oliver Gierke + * * @param + * @author Oliver Gierke */ public interface Page extends Iterable { - /** - * Returns the number of the current page. Is always positive and less that - * {@code Page#getTotalPages()}. - * - * @return the number of the current page - */ - int getNumber(); + /** + * Returns the number of the current page. Is always positive and less that + * {@code Page#getTotalPages()}. + * + * @return the number of the current page + */ + int getNumber(); - /** - * Returns the size of the page. - * - * @return the size of the page - */ - int getSize(); + /** + * Returns the size of the page. + * + * @return the size of the page + */ + int getSize(); - /** - * Returns the number of total pages. - * - * @return the number of toral pages - */ - int getTotalPages(); + /** + * Returns the number of total pages. + * + * @return the number of toral pages + */ + int getTotalPages(); - /** - * Returns the number of elements currently on this page. - * - * @return the number of elements currently on this page - */ - int getNumberOfElements(); + /** + * Returns the number of elements currently on this page. + * + * @return the number of elements currently on this page + */ + int getNumberOfElements(); - /** - * Returns the total amount of elements. - * - * @return the total amount of elements - */ - long getTotalElements(); + /** + * Returns the total amount of elements. + * + * @return the total amount of elements + */ + long getTotalElements(); - /** - * Returns if there is a previous page. - * - * @return if there is a previous page - */ - boolean hasPreviousPage(); + /** + * Returns if there is a previous page. + * + * @return if there is a previous page + */ + boolean hasPreviousPage(); - /** - * Returns whether the current page is the first one. - * - * @return - */ - boolean isFirstPage(); + /** + * Returns whether the current page is the first one. + * + * @return + */ + boolean isFirstPage(); - /** - * Returns if there is a next page. - * - * @return if there is a next page - */ - boolean hasNextPage(); + /** + * Returns if there is a next page. + * + * @return if there is a next page + */ + boolean hasNextPage(); - /** - * Returns whether the current page is the last one. - * - * @return - */ - boolean isLastPage(); + /** + * Returns whether the current page is the last one. + * + * @return + */ + boolean isLastPage(); - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - Iterator iterator(); + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + Iterator iterator(); - /** - * Returns the page content as {@link List}. - * - * @return - */ - List getContent(); - - - /** - * Returns whether the {@link Page} has content at all. - * - * @return - */ - boolean hasContent(); + /** + * Returns the page content as {@link List}. + * + * @return + */ + List getContent(); - /** - * Returns the sorting parameters for the page. - * - * @return - */ - Sort getSort(); + /** + * Returns whether the {@link Page} has content at all. + * + * @return + */ + boolean hasContent(); + + + /** + * Returns the sorting parameters for the page. + * + * @return + */ + Sort getSort(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java index c5e82cec2..675bab8b3 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageImpl.java @@ -24,251 +24,251 @@ import java.util.List; /** * Basic {@code Page} implementation. - * - * @author Oliver Gierke + * * @param the type of which the page consists. + * @author Oliver Gierke */ public class PageImpl implements Page, Serializable { - private static final long serialVersionUID = 867755909294344406L; - - private final List content = new ArrayList(); - private final Pageable pageable; - private final long total; + private static final long serialVersionUID = 867755909294344406L; + + private final List content = new ArrayList(); + private final Pageable pageable; + private final long total; - /** - * Constructor of {@code PageImpl}. - * - * @param content the content of this page - * @param pageable the paging information - * @param total the total amount of items available - */ - public PageImpl(List content, Pageable pageable, long total) { - - if (null == content) { - throw new IllegalArgumentException("Content must not be null!"); - } - - this.content.addAll(content); - this.total = total; - this.pageable = pageable; - } - - - /** - * Creates a new {@link PageImpl} with the given content. This will result - * in the created {@link Page} being identical to the entire {@link List}. - * - * @param content - */ - public PageImpl(List content) { - - this(content, null, (null == content) ? 0 : content.size()); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getNumber() - */ - public int getNumber() { - - return pageable == null ? 0 : pageable.getPageNumber(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getSize() - */ - public int getSize() { - - return pageable == null ? 0 : pageable.getPageSize(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getTotalPages() - */ - public int getTotalPages() { - - return getSize() == 0 ? 0 : (int) Math.ceil((double) total / (double) getSize()); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getNumberOfElements() - */ - public int getNumberOfElements() { - - return content.size(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getTotalElements() - */ - public long getTotalElements() { - - return total; - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#hasPreviousPage() - */ - public boolean hasPreviousPage() { - - return getNumber() > 0; - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#isFirstPage() - */ - public boolean isFirstPage() { - - return !hasPreviousPage(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#hasNextPage() - */ - public boolean hasNextPage() { - - return ((getNumber() + 1) * getSize()) < total; - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#isLastPage() - */ - public boolean isLastPage() { - - return !hasNextPage(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#iterator() - */ - public Iterator iterator() { - - return content.iterator(); - } - - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#asList() - */ - public List getContent() { - - return Collections.unmodifiableList(content); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#hasContent() + /** + * Constructor of {@code PageImpl}. + * + * @param content the content of this page + * @param pageable the paging information + * @param total the total amount of items available */ - public boolean hasContent() { - - return !content.isEmpty(); - } + public PageImpl(List content, Pageable pageable, long total) { + + if (null == content) { + throw new IllegalArgumentException("Content must not be null!"); + } + + this.content.addAll(content); + this.total = total; + this.pageable = pageable; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Page#getSort() - */ - public Sort getSort() { + /** + * Creates a new {@link PageImpl} with the given content. This will result + * in the created {@link Page} being identical to the entire {@link List}. + * + * @param content + */ + public PageImpl(List content) { - return pageable == null ? null : pageable.getSort(); - } + this(content, null, (null == content) ? 0 : content.size()); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getNumber() + */ + public int getNumber() { - String contentType = "UNKNOWN"; - - if (content.size() > 0) { - contentType = content.get(0).getClass().getName(); - } - - return String.format("Page %s of %d containing %s instances", - getNumber(), getTotalPages(), contentType); - } + return pageable == null ? 0 : pageable.getPageNumber(); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getSize() + */ + public int getSize() { - if (this == obj) { - return true; - } + return pageable == null ? 0 : pageable.getPageSize(); + } - if (!(obj instanceof PageImpl)) { - return false; - } - PageImpl that = (PageImpl) obj; + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getTotalPages() + */ + public int getTotalPages() { - boolean totalEqual = this.total == that.total; - boolean contentEqual = this.content.equals(that.content); + return getSize() == 0 ? 0 : (int) Math.ceil((double) total / (double) getSize()); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getNumberOfElements() + */ + public int getNumberOfElements() { + + return content.size(); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getTotalElements() + */ + public long getTotalElements() { + + return total; + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#hasPreviousPage() + */ + public boolean hasPreviousPage() { + + return getNumber() > 0; + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#isFirstPage() + */ + public boolean isFirstPage() { + + return !hasPreviousPage(); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#hasNextPage() + */ + public boolean hasNextPage() { + + return ((getNumber() + 1) * getSize()) < total; + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#isLastPage() + */ + public boolean isLastPage() { + + return !hasNextPage(); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#iterator() + */ + public Iterator iterator() { + + return content.iterator(); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#asList() + */ + public List getContent() { + + return Collections.unmodifiableList(content); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#hasContent() + */ + public boolean hasContent() { + + return !content.isEmpty(); + } + + + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Page#getSort() + */ + public Sort getSort() { + + return pageable == null ? null : pageable.getSort(); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + String contentType = "UNKNOWN"; + + if (content.size() > 0) { + contentType = content.get(0).getClass().getName(); + } + + return String.format("Page %s of %d containing %s instances", + getNumber(), getTotalPages(), contentType); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (!(obj instanceof PageImpl)) { + return false; + } + + PageImpl that = (PageImpl) obj; + + boolean totalEqual = this.total == that.total; + boolean contentEqual = this.content.equals(that.content); boolean pageableEqual = this.pageable == null ? that.pageable == null : this.pageable.equals(that.pageable); - return totalEqual && contentEqual && pageableEqual; - } + return totalEqual && contentEqual && pageableEqual; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - int result = 17; + int result = 17; - result = 31 * result + (int) (total ^ total >>> 32); - result = 31 * result + (pageable == null ? 0 : pageable.hashCode()); - result = 31 * result + content.hashCode(); + result = 31 * result + (int) (total ^ total >>> 32); + result = 31 * result + (pageable == null ? 0 : pageable.hashCode()); + result = 31 * result + content.hashCode(); - return result; - } + return result; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageRequest.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageRequest.java index f4ca5dbec..7427d6d54 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageRequest.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/PageRequest.java @@ -22,158 +22,158 @@ import org.springframework.data.domain.Sort.Direction; /** * Basic Java Bean implementation of {@code Pageable}. - * + * * @author Oliver Gierke */ public class PageRequest implements Pageable, Serializable { - private static final long serialVersionUID = 8280485938848398236L; - - private final int page; - private final int size; - private final Sort sort; + private static final long serialVersionUID = 8280485938848398236L; + + private final int page; + private final int size; + private final Sort sort; - /** - * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing - * 0 for {@code page} will return the first page. - * - * @param size - * @param page - */ - public PageRequest(int page, int size) { + /** + * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing + * 0 for {@code page} will return the first page. + * + * @param size + * @param page + */ + public PageRequest(int page, int size) { - this(page, size, null); - } + this(page, size, null); + } - /** - * Creates a new {@link PageRequest} with sort parameters applied. - * - * @param page - * @param size - * @param direction - * @param properties - */ - public PageRequest(int page, int size, Direction direction, - String... properties) { + /** + * Creates a new {@link PageRequest} with sort parameters applied. + * + * @param page + * @param size + * @param direction + * @param properties + */ + public PageRequest(int page, int size, Direction direction, + String... properties) { - this(page, size, new Sort(direction, properties)); - } + this(page, size, new Sort(direction, properties)); + } - /** - * Creates a new {@link PageRequest} with sort parameters applied. - * - * @param page - * @param size - * @param sort - */ - public PageRequest(int page, int size, Sort sort) { + /** + * Creates a new {@link PageRequest} with sort parameters applied. + * + * @param page + * @param size + * @param sort + */ + public PageRequest(int page, int size, Sort sort) { - if (0 > page) { - throw new IllegalArgumentException( - "Page index must not be less than zero!"); - } + if (0 > page) { + throw new IllegalArgumentException( + "Page index must not be less than zero!"); + } - if (0 >= size) { - throw new IllegalArgumentException( - "Page size must not be less than or equal to zero!"); - } + if (0 >= size) { + throw new IllegalArgumentException( + "Page size must not be less than or equal to zero!"); + } - this.page = page; - this.size = size; - this.sort = sort; - } + this.page = page; + this.size = size; + this.sort = sort; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Pageable#getPageSize() - */ - public int getPageSize() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Pageable#getPageSize() + */ + public int getPageSize() { - return size; - } + return size; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Pageable#getPageNumber() - */ - public int getPageNumber() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Pageable#getPageNumber() + */ + public int getPageNumber() { - return page; - } + return page; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Pageable#getFirstItem() - */ - public int getOffset() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Pageable#getFirstItem() + */ + public int getOffset() { - return page * size; - } + return page * size; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Pageable#getSort() - */ - public Sort getSort() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Pageable#getSort() + */ + public Sort getSort() { - return sort; - } + return sort; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(final Object obj) { + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(final Object obj) { - if (this == obj) { - return true; - } + if (this == obj) { + return true; + } - if (!(obj instanceof PageRequest)) { - return false; - } + if (!(obj instanceof PageRequest)) { + return false; + } - PageRequest that = (PageRequest) obj; + PageRequest that = (PageRequest) obj; - boolean pageEqual = this.page == that.page; - boolean sizeEqual = this.size == that.size; + boolean pageEqual = this.page == that.page; + boolean sizeEqual = this.size == that.size; - boolean sortEqual = - this.sort == null ? that.sort == null : this.sort - .equals(that.sort); + boolean sortEqual = + this.sort == null ? that.sort == null : this.sort + .equals(that.sort); - return pageEqual && sizeEqual && sortEqual; - } + return pageEqual && sizeEqual && sortEqual; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - int result = 17; + int result = 17; - result = 31 * result + page; - result = 31 * result + size; - result = 31 * result + (null == sort ? 0 : sort.hashCode()); + result = 31 * result + page; + result = 31 * result + size; + result = 31 * result + (null == sort ? 0 : sort.hashCode()); - return result; - } + return result; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Pageable.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Pageable.java index dcaf2fa36..655d905b7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Pageable.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Pageable.java @@ -17,40 +17,40 @@ package org.springframework.data.domain; /** * Abstract interface for pagination information. - * + * * @author Oliver Gierke */ public interface Pageable { - /** - * Returns the page to be returned. - * - * @return the page to be returned. - */ - int getPageNumber(); + /** + * Returns the page to be returned. + * + * @return the page to be returned. + */ + int getPageNumber(); - /** - * Returns the number of items to be returned. - * - * @return the number of items of that page - */ - int getPageSize(); + /** + * Returns the number of items to be returned. + * + * @return the number of items of that page + */ + int getPageSize(); - /** - * Returns the offset to be taken according to the underlying page and page - * size. - * - * @return the offset to be taken - */ - int getOffset(); + /** + * Returns the offset to be taken according to the underlying page and page + * size. + * + * @return the offset to be taken + */ + int getOffset(); - /** - * Returns the sorting parameters. - * - * @return - */ - Sort getSort(); + /** + * Returns the sorting parameters. + * + * @return + */ + Sort getSort(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Persistable.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Persistable.java index 4f8c56e82..21e89db11 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Persistable.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Persistable.java @@ -20,24 +20,24 @@ import java.io.Serializable; /** * Simple interface for entities. - * - * @author Oliver Gierke + * * @param the type of the identifier + * @author Oliver Gierke */ public interface Persistable extends Serializable { - /** - * Returns the id of the entity. - * - * @return the id - */ - ID getId(); + /** + * Returns the id of the entity. + * + * @return the id + */ + ID getId(); - /** - * Returns if the {@code Persistable} is new or was persisted already. - * - * @return if the object is new - */ - boolean isNew(); + /** + * Returns if the {@code Persistable} is new or was persisted already. + * + * @return if the object is new + */ + boolean isNew(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Sort.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Sort.java index 51ebdeef5..57a3d85e6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/Sort.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/Sort.java @@ -29,355 +29,355 @@ import org.springframework.util.StringUtils; * Sort option for queries. You have to provide at least a list of properties to * sort for that must not include {@code null} or empty strings. The direction * defaults to {@value Sort#DEFAULT_DIRECTION}. - * + * * @author Oliver Gierke */ public class Sort implements - Iterable, Serializable { + Iterable, Serializable { - private static final long serialVersionUID = 5737186511678863905L; - public static final Direction DEFAULT_DIRECTION = Direction.ASC; + private static final long serialVersionUID = 5737186511678863905L; + public static final Direction DEFAULT_DIRECTION = Direction.ASC; - private List orders; + private List orders; - public Sort(Order... orders) { + public Sort(Order... orders) { - this(Arrays.asList(orders)); - } + this(Arrays.asList(orders)); + } - /** - * Creates a new {@link Sort} instance. - * - * @param orders must not be {@literal null} or contain {@literal null} or - * empty strings - */ - public Sort(List orders) { + /** + * Creates a new {@link Sort} instance. + * + * @param orders must not be {@literal null} or contain {@literal null} or + * empty strings + */ + public Sort(List orders) { - if (null == orders || orders.isEmpty()) { - throw new IllegalArgumentException( - "You have to provide at least one sort property to sort by!"); - } + if (null == orders || orders.isEmpty()) { + throw new IllegalArgumentException( + "You have to provide at least one sort property to sort by!"); + } - this.orders = orders; - } + this.orders = orders; + } - /** - * Creates a new {@link Sort} instance. Order defaults to - * {@value Direction#ASC}. - * - * @param properties must not be {@literal null} or contain {@literal null} - * or empty strings - */ - public Sort(String... properties) { + /** + * Creates a new {@link Sort} instance. Order defaults to + * {@value Direction#ASC}. + * + * @param properties must not be {@literal null} or contain {@literal null} + * or empty strings + */ + public Sort(String... properties) { - this(DEFAULT_DIRECTION, properties); - } + this(DEFAULT_DIRECTION, properties); + } - /** - * Creates a new {@link Sort} instance. - * - * @param direction defaults to {@value Sort#DEFAULT_DIRECTION} (for - * {@literal null} cases, too) - * @param properties must not be {@literal null} or contain {@literal null} - * or empty strings - */ - public Sort(Direction direction, String... properties) { + /** + * Creates a new {@link Sort} instance. + * + * @param direction defaults to {@value Sort#DEFAULT_DIRECTION} (for + * {@literal null} cases, too) + * @param properties must not be {@literal null} or contain {@literal null} + * or empty strings + */ + public Sort(Direction direction, String... properties) { - this(direction, properties == null ? new ArrayList() : Arrays - .asList(properties)); - } + this(direction, properties == null ? new ArrayList() : Arrays + .asList(properties)); + } - /** - * Creates a new {@link Sort} instance. - * - * @param direction - * @param properties - */ - public Sort(Direction direction, List properties) { + /** + * Creates a new {@link Sort} instance. + * + * @param direction + * @param properties + */ + public Sort(Direction direction, List properties) { - if (properties == null || properties.isEmpty()) { - throw new IllegalArgumentException( - "You have to provide at least one property to sort by!"); - } - - this.orders = new ArrayList(properties.size()); + if (properties == null || properties.isEmpty()) { + throw new IllegalArgumentException( + "You have to provide at least one property to sort by!"); + } + + this.orders = new ArrayList(properties.size()); - for (String property : properties) { - this.orders.add(new Order(direction, property)); - } - } - - - /** - * Returns the order registered for the given property. - * - * @param property - * @return - */ - public Order getOrderFor(String property) { - - for (Order order : this) { - if (order.getProperty().equals(property)) { - return order; - } - } - - return null; - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { - - return this.orders.iterator(); - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - - if (this == obj) { - return true; - } - - if (!(obj instanceof Sort)) { - return false; - } - - Sort that = (Sort) obj; - - return this.orders.equals(that.orders); - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { - - int result = 17; - result = 31 * result + orders.hashCode(); - return result; - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { - - return StringUtils.collectionToCommaDelimitedString(orders); - } - - /** - * Enumeration for sort directions. - * - * @author Oliver Gierke - */ - public static enum Direction { - - ASC, DESC; - - /** - * Returns the {@link Direction} enum for the given {@link String} - * value. - * - * @param value - * @return - */ - public static Direction fromString(String value) { - - try { - return Direction.valueOf(value.toUpperCase(Locale.US)); - } catch (Exception e) { - throw new IllegalArgumentException( - String.format( - "Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", - value), e); - } - } - } - - /** - * Property implements the pairing of an {@code Order} and a property. It is - * used to provide input for {@link Sort} - * - * @author Oliver Gierke - */ - public static class Order { - - private final Direction direction; - private final String property; - - - /** - * Creates a new {@link Order} instance. if order is {@literal null} - * then order defaults to {@value Sort#DEFAULT_DIRECTION} - * - * @param direction can be {@code null} - * @param property must not be {@code null} or empty - */ - public Order(Direction direction, String property) { - - if (property == null || "".equals(property.trim())) { - throw new IllegalArgumentException( - "Property must not null or empty!"); - } - - this.direction = direction == null ? DEFAULT_DIRECTION : direction; - this.property = property; - } - - - /** - * Creates a new {@link Order} instance. Takes a single property. Order - * defaults to {@value Sort.DEFAULT_ORDER} - * - * @param property - must not be {@code null} or empty - */ - public Order(String property) { - - this(DEFAULT_DIRECTION, property); - } - - - public static List create(Direction direction, - Iterable properties) { - - List orders = new ArrayList(); - for (String property : properties) { - orders.add(new Order(direction, property)); - } - return orders; - } - - - /** - * Returns the order the property shall be sorted for. - * - * @return - */ - public Direction getDirection() { - - return direction; - } - - - /** - * Returns the property to order for. - * - * @return - */ - public String getProperty() { - - return property; - } - - - /** - * Returns whether sorting for this property shall be ascending. - * - * @return - */ - public boolean isAscending() { - - return this.direction.equals(Direction.ASC); - } - - - /** - * Returns a new {@link Order} with the given {@link Order}. - * - * @param order - * @return - */ - public Order with(Direction order) { - - return new Order(order, this.property); - } + for (String property : properties) { + this.orders.add(new Order(direction, property)); + } + } + + + /** + * Returns the order registered for the given property. + * + * @param property + * @return + */ + public Order getOrderFor(String property) { + + for (Order order : this) { + if (order.getProperty().equals(property)) { + return order; + } + } + + return null; + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + public Iterator iterator() { + + return this.orders.iterator(); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + + if (!(obj instanceof Sort)) { + return false; + } + + Sort that = (Sort) obj; + + return this.orders.equals(that.orders); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + result = 31 * result + orders.hashCode(); + return result; + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + return StringUtils.collectionToCommaDelimitedString(orders); + } + + /** + * Enumeration for sort directions. + * + * @author Oliver Gierke + */ + public static enum Direction { + + ASC, DESC; + + /** + * Returns the {@link Direction} enum for the given {@link String} + * value. + * + * @param value + * @return + */ + public static Direction fromString(String value) { + + try { + return Direction.valueOf(value.toUpperCase(Locale.US)); + } catch (Exception e) { + throw new IllegalArgumentException( + String.format( + "Invalid value '%s' for orders given! Has to be either 'desc' or 'asc' (case insensitive).", + value), e); + } + } + } + + /** + * Property implements the pairing of an {@code Order} and a property. It is + * used to provide input for {@link Sort} + * + * @author Oliver Gierke + */ + public static class Order { + + private final Direction direction; + private final String property; + + + /** + * Creates a new {@link Order} instance. if order is {@literal null} + * then order defaults to {@value Sort#DEFAULT_DIRECTION} + * + * @param direction can be {@code null} + * @param property must not be {@code null} or empty + */ + public Order(Direction direction, String property) { + + if (property == null || "".equals(property.trim())) { + throw new IllegalArgumentException( + "Property must not null or empty!"); + } + + this.direction = direction == null ? DEFAULT_DIRECTION : direction; + this.property = property; + } + + + /** + * Creates a new {@link Order} instance. Takes a single property. Order + * defaults to {@value Sort.DEFAULT_ORDER} + * + * @param property - must not be {@code null} or empty + */ + public Order(String property) { + + this(DEFAULT_DIRECTION, property); + } + + + public static List create(Direction direction, + Iterable properties) { + + List orders = new ArrayList(); + for (String property : properties) { + orders.add(new Order(direction, property)); + } + return orders; + } + + + /** + * Returns the order the property shall be sorted for. + * + * @return + */ + public Direction getDirection() { + + return direction; + } + + + /** + * Returns the property to order for. + * + * @return + */ + public String getProperty() { + + return property; + } + + + /** + * Returns whether sorting for this property shall be ascending. + * + * @return + */ + public boolean isAscending() { + + return this.direction.equals(Direction.ASC); + } + + + /** + * Returns a new {@link Order} with the given {@link Order}. + * + * @param order + * @return + */ + public Order with(Direction order) { + + return new Order(order, this.property); + } - /** - * Returns a new {@link Sort} instance for the given properties. - * - * @param properties - * @return - */ - public Sort withProperties(String... properties) { + /** + * Returns a new {@link Sort} instance for the given properties. + * + * @param properties + * @return + */ + public Sort withProperties(String... properties) { - return new Sort(this.direction, properties); - } + return new Sort(this.direction, properties); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - int result = 17; + int result = 17; - result = 31 * result + direction.hashCode(); - result = 31 * result + property.hashCode(); + result = 31 * result + direction.hashCode(); + result = 31 * result + property.hashCode(); - return result; - } + return result; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { - if (this == obj) { - return true; - } + if (this == obj) { + return true; + } - if (!(obj instanceof Order)) { - return false; - } + if (!(obj instanceof Order)) { + return false; + } - Order that = (Order) obj; + Order that = (Order) obj; - return this.direction.equals(that.direction) - && this.property.equals(that.property); - } + return this.direction.equals(that.direction) + && this.property.equals(that.property); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return String.format("%s: %s", property, direction); - } - } + return String.format("%s: %s", property, direction); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/domain/package-info.java b/spring-data-commons-core/src/main/java/org/springframework/data/domain/package-info.java index b78eda01e..e4ecf3168 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/domain/package-info.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/domain/package-info.java @@ -1,6 +1,6 @@ /** * Central domain abstractions especially to be used in combination with the {@link org.springframework.data.repository.Repository} abstraction. - * + * * @see org.springframework.data.repository.Repository */ package org.springframework.data.domain; diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java index 099017ea8..2d3a419fa 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractMappingContext.java @@ -49,7 +49,7 @@ import org.springframework.validation.Validator; /** * Base class to build mapping metadata and thus create instances of {@link PersistentEntity} and {@link PersistentProperty}. - * + * * @param E the concrete {@link PersistentEntity} type the {@link MappingContext} implementation creates * @param P the concrete {@link PersistentProperty} type the {@link MappingContext} implementation creates * @author Jon Brisbin @@ -57,223 +57,222 @@ import org.springframework.validation.Validator; */ public abstract class AbstractMappingContext, P extends PersistentProperty

> implements MappingContext, InitializingBean, ApplicationEventPublisherAware { - private static final Set UNMAPPED_FIELDS = new HashSet(Arrays.asList("class", "this$0")); + private static final Set UNMAPPED_FIELDS = new HashSet(Arrays.asList("class", "this$0")); - private ApplicationEventPublisher applicationEventPublisher; - private ConcurrentMap, E> persistentEntities = new ConcurrentHashMap, E>(); - private ConcurrentMap> validators = new ConcurrentHashMap>(); - private List> customSimpleTypes = new ArrayList>(); - private Set> initialEntitySet = new HashSet>(); - private boolean strict = false; + private ApplicationEventPublisher applicationEventPublisher; + private ConcurrentMap, E> persistentEntities = new ConcurrentHashMap, E>(); + private ConcurrentMap> validators = new ConcurrentHashMap>(); + private List> customSimpleTypes = new ArrayList>(); + private Set> initialEntitySet = new HashSet>(); + private boolean strict = false; - /** - * @param customSimpleTypes the customSimpleTypes to set - */ - public void setCustomSimpleTypes(List> customSimpleTypes) { - this.customSimpleTypes = customSimpleTypes; - } + /** + * @param customSimpleTypes the customSimpleTypes to set + */ + public void setCustomSimpleTypes(List> customSimpleTypes) { + this.customSimpleTypes = customSimpleTypes; + } - /* - * (non-Javadoc) - * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) - */ - public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { - this.applicationEventPublisher = applicationEventPublisher; - } + /* + * (non-Javadoc) + * @see org.springframework.context.ApplicationEventPublisherAware#setApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) + */ + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { + this.applicationEventPublisher = applicationEventPublisher; + } - /** - * Sets the {@link Set} of types to populate the context initially. - * - * @param initialEntitySet - */ - public void setInitialEntitySet(Set> initialEntitySet) { - this.initialEntitySet = initialEntitySet; - } + /** + * Sets the {@link Set} of types to populate the context initially. + * + * @param initialEntitySet + */ + public void setInitialEntitySet(Set> initialEntitySet) { + this.initialEntitySet = initialEntitySet; + } - /** - * Configures whether the {@link MappingContext} is in strict mode which means, that it will throw - * {@link MappingException}s in case one tries to lookup a {@link PersistentEntity} not already in the context. This - * defaults to {@literal false} so that unknown types will be transparently added to the MappingContext if not known - * in advance. - * - * @param strict - */ - public void setStrict(boolean strict) { - this.strict = strict; - } + /** + * Configures whether the {@link MappingContext} is in strict mode which means, that it will throw + * {@link MappingException}s in case one tries to lookup a {@link PersistentEntity} not already in the context. This + * defaults to {@literal false} so that unknown types will be transparently added to the MappingContext if not known + * in advance. + * + * @param strict + */ + public void setStrict(boolean strict) { + this.strict = strict; + } - public Collection getPersistentEntities() { - return persistentEntities.values(); - } + public Collection getPersistentEntities() { + return persistentEntities.values(); + } - /* (non-Javadoc) - * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(java.lang.Class) - */ - public E getPersistentEntity(Class type) { - - return getPersistentEntity(ClassTypeInformation.from(type)); - } + /* (non-Javadoc) + * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(java.lang.Class) + */ + public E getPersistentEntity(Class type) { - /* - * (non-Javadoc) - * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation) - */ - public E getPersistentEntity(TypeInformation type) { - - E entity = persistentEntities.get(type); - - if (entity != null) { - return entity; - } - - if (strict) { - throw new MappingException("Unknown persistent entity " + type); - } - - return addPersistentEntity(type); - } + return getPersistentEntity(ClassTypeInformation.from(type)); + } - /** - * Adds the given type to the {@link MappingContext}. - * - * @param type - * @return - */ - protected E addPersistentEntity(Class type) { - - return addPersistentEntity(ClassTypeInformation.from(type)); - } + /* + * (non-Javadoc) + * @see org.springframework.data.mapping.model.MappingContext#getPersistentEntity(org.springframework.data.util.TypeInformation) + */ + public E getPersistentEntity(TypeInformation type) { - /** - * Adds the given {@link TypeInformation} to the {@link MappingContext}. - * - * @param typeInformation - * @return - */ - protected E addPersistentEntity(TypeInformation typeInformation) { + E entity = persistentEntities.get(type); - E persistentEntity = persistentEntities.get(typeInformation); + if (entity != null) { + return entity; + } - if (persistentEntity != null) { - return persistentEntity; - } + if (strict) { + throw new MappingException("Unknown persistent entity " + type); + } - Class type = typeInformation.getType(); + return addPersistentEntity(type); + } - try { - final E entity = createPersistentEntity(typeInformation); - BeanInfo info = Introspector.getBeanInfo(type); + /** + * Adds the given type to the {@link MappingContext}. + * + * @param type + * @return + */ + protected E addPersistentEntity(Class type) { - final Map descriptors = new HashMap(); - for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { - descriptors.put(descriptor.getName(), descriptor); - } + return addPersistentEntity(ClassTypeInformation.from(type)); + } - ReflectionUtils.doWithFields(type, new FieldCallback() { + /** + * Adds the given {@link TypeInformation} to the {@link MappingContext}. + * + * @param typeInformation + * @return + */ + protected E addPersistentEntity(TypeInformation typeInformation) { - public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { - - PropertyDescriptor descriptor = descriptors.get(field.getName()); - - ReflectionUtils.makeAccessible(field); - P property = createPersistentProperty(field, descriptor, entity); + E persistentEntity = persistentEntities.get(typeInformation); - if (property.isTransient()) { - return; - } - - entity.addPersistentProperty(property); - - if (property.isAssociation()) { - entity.addAssociation(property.getAssociation()); - } + if (persistentEntity != null) { + return persistentEntity; + } - if (property.isIdProperty()) { - entity.setIdProperty(property); - } + Class type = typeInformation.getType(); - TypeInformation nestedType = getNestedTypeToAdd(property, entity); - if (nestedType != null) { - addPersistentEntity(nestedType); - } - - - } - }, new ReflectionUtils.FieldFilter() { - public boolean matches(Field field) { - return !Modifier.isStatic(field.getModifiers()) && !UNMAPPED_FIELDS.contains(field.getName()); - } - }); - - entity.verify(); + try { + final E entity = createPersistentEntity(typeInformation); + BeanInfo info = Introspector.getBeanInfo(type); - // Inform listeners - if (null != applicationEventPublisher) { - applicationEventPublisher.publishEvent(new MappingContextEvent(entity, typeInformation)); - } + final Map descriptors = new HashMap(); + for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { + descriptors.put(descriptor.getName(), descriptor); + } - // Cache - persistentEntities.put(entity.getTypeInformation(), (E) entity); + ReflectionUtils.doWithFields(type, new FieldCallback() { - return entity; - } catch (IntrospectionException e) { - throw new MappingException(e.getMessage(), e); - } - } + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException { - /** - * Returns a potential nested type tha needs to be added when adding the given property in the course of adding a - * {@link PersistentEntity}. Will return the property's {@link TypeInformation} directly if it is a potential entity, - * a collections component type if it's a collection as well as the value type of a {@link Map} if it's a map - * property. - * - * @param property - * @return the TypeInformation to be added as {@link PersistentEntity} or {@literal - */ - private TypeInformation getNestedTypeToAdd(P property, PersistentEntity entity) { - - if (entity.getType().equals(property.getRawType())) { - return null; - } + PropertyDescriptor descriptor = descriptors.get(field.getName()); - TypeInformation typeInformation = property.getTypeInformation(); + ReflectionUtils.makeAccessible(field); + P property = createPersistentProperty(field, descriptor, entity); - if (customSimpleTypes.contains(typeInformation.getType())) { - return null; - } + if (property.isTransient()) { + return; + } - if (property.isEntity()) { - return typeInformation; - } + entity.addPersistentProperty(property); - if (property.isCollection()) { - return getTypeInformationIfNotSimpleType(typeInformation.getComponentType()); - } + if (property.isAssociation()) { + entity.addAssociation(property.getAssociation()); + } - if (property.isMap()) { - return getTypeInformationIfNotSimpleType(typeInformation.getMapValueType()); - } + if (property.isIdProperty()) { + entity.setIdProperty(property); + } - return null; - } - - private TypeInformation getTypeInformationIfNotSimpleType(TypeInformation information) { - return information == null || MappingBeanHelper.isSimpleType(information.getType()) ? null : information; - } - - public List getEntityValidators(E entity) { - return validators.get(entity); - } - - protected abstract E createPersistentEntity(TypeInformation typeInformation); - - protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner); + TypeInformation nestedType = getNestedTypeToAdd(property, entity); + if (nestedType != null) { + addPersistentEntity(nestedType); + } + } + }, new ReflectionUtils.FieldFilter() { + public boolean matches(Field field) { + return !Modifier.isStatic(field.getModifiers()) && !UNMAPPED_FIELDS.contains(field.getName()); + } + }); - public void afterPropertiesSet() throws Exception { - for (Class initialEntity : initialEntitySet) { - addPersistentEntity(initialEntity); - } - } + entity.verify(); + + // Inform listeners + if (null != applicationEventPublisher) { + applicationEventPublisher.publishEvent(new MappingContextEvent(entity, typeInformation)); + } + + // Cache + persistentEntities.put(entity.getTypeInformation(), (E) entity); + + return entity; + } catch (IntrospectionException e) { + throw new MappingException(e.getMessage(), e); + } + } + + /** + * Returns a potential nested type tha needs to be added when adding the given property in the course of adding a + * {@link PersistentEntity}. Will return the property's {@link TypeInformation} directly if it is a potential entity, + * a collections component type if it's a collection as well as the value type of a {@link Map} if it's a map + * property. + * + * @param property + * @return the TypeInformation to be added as {@link PersistentEntity} or {@literal + */ + private TypeInformation getNestedTypeToAdd(P property, PersistentEntity entity) { + + if (entity.getType().equals(property.getRawType())) { + return null; + } + + TypeInformation typeInformation = property.getTypeInformation(); + + if (customSimpleTypes.contains(typeInformation.getType())) { + return null; + } + + if (property.isEntity()) { + return typeInformation; + } + + if (property.isCollection()) { + return getTypeInformationIfNotSimpleType(typeInformation.getComponentType()); + } + + if (property.isMap()) { + return getTypeInformationIfNotSimpleType(typeInformation.getMapValueType()); + } + + return null; + } + + private TypeInformation getTypeInformationIfNotSimpleType(TypeInformation information) { + return information == null || MappingBeanHelper.isSimpleType(information.getType()) ? null : information; + } + + public List getEntityValidators(E entity) { + return validators.get(entity); + } + + protected abstract E createPersistentEntity(TypeInformation typeInformation); + + protected abstract P createPersistentProperty(Field field, PropertyDescriptor descriptor, E owner); + + + public void afterPropertiesSet() throws Exception { + for (Class initialEntity : initialEntitySet) { + addPersistentEntity(initialEntity); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java index 9a69ff8c0..91f20c2b5 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AbstractPersistentProperty.java @@ -54,7 +54,7 @@ public abstract class AbstractPersistentProperty

this.association = isAssociation() ? createAssociation() : null; this.owner = owner; } - + protected abstract Association

createAssociation(); public PersistentEntity getOwner() { @@ -94,16 +94,16 @@ public abstract class AbstractPersistentProperty

} public boolean isAssociation() { - if (field.isAnnotationPresent(Reference.class)) { - return true; - } - for (Annotation annotation : field.getDeclaredAnnotations()) { - if (annotation.annotationType().isAnnotationPresent(Reference.class)) { - return true; - } - } - - return false; + if (field.isAnnotationPresent(Reference.class)) { + return true; + } + for (Annotation annotation : field.getDeclaredAnnotations()) { + if (annotation.annotationType().isAnnotationPresent(Reference.class)) { + return true; + } + } + + return false; } public Association

getAssociation() { diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java index 198efd87a..a0f15f2ed 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AnnotationBasedPersistentProperty.java @@ -31,78 +31,78 @@ import org.springframework.data.mapping.model.PersistentProperty; /** * Special {@link PersistentProperty} that takes annotations at a property into account. - * + * * @author Oliver Gierke */ public abstract class AnnotationBasedPersistentProperty

> extends AbstractPersistentProperty

{ - - private final Value value; - - /** - * Creates a new {@link AnnotationBasedPersistentProperty}. - * - * @param field - * @param propertyDescriptor - * @param owner - */ - public AnnotationBasedPersistentProperty(Field field, - PropertyDescriptor propertyDescriptor, PersistentEntity owner) { - super(field, propertyDescriptor, owner); - this.value = field.getAnnotation(Value.class); - field.isAnnotationPresent(Autowired.class); - } + private final Value value; - /** - * Inspects a potentially available {@link Value} annotation at the property and returns the {@link String} value of - * it. - * - * @see org.springframework.data.mapping.AbstractPersistentProperty#getSpelExpression() - */ - public String getSpelExpression() { - return value == null ? null :value.value(); - } + /** + * Creates a new {@link AnnotationBasedPersistentProperty}. + * + * @param field + * @param propertyDescriptor + * @param owner + */ + public AnnotationBasedPersistentProperty(Field field, + PropertyDescriptor propertyDescriptor, PersistentEntity owner) { - /** - * Considers plain transient fields, fields annotated with {@link Transient}, {@link Value} or {@link Autowired} as - * transien. - * - * @see org.springframework.data.mapping.BasicPersistentProperty#isTransient() - */ - public boolean isTransient() { - - boolean isTransient = super.isTransient() || field.isAnnotationPresent(Transient.class); - - return isTransient || field.isAnnotationPresent(Value.class) || field.isAnnotationPresent(Autowired.class); - } - - /** - * Regards the property as ID if there is an {@link Id} annotation found on it. - */ - public boolean isIdProperty() { - return field.isAnnotationPresent(Id.class); - } - - /** - * Considers the property an {@link Association} if it is annotated with {@link Reference}. - */ - @Override - public boolean isAssociation() { - - if (isTransient()) { - return false; - } - if (field.isAnnotationPresent(Reference.class)) { - return true; - } - - // TODO: do we need this? Shouldn't the section above already find that annotation? - for (Annotation annotation : field.getDeclaredAnnotations()) { - if (annotation.annotationType().isAnnotationPresent(Reference.class)) { - return true; - } - } - - return false; - } + super(field, propertyDescriptor, owner); + this.value = field.getAnnotation(Value.class); + field.isAnnotationPresent(Autowired.class); + } + + /** + * Inspects a potentially available {@link Value} annotation at the property and returns the {@link String} value of + * it. + * + * @see org.springframework.data.mapping.AbstractPersistentProperty#getSpelExpression() + */ + public String getSpelExpression() { + return value == null ? null : value.value(); + } + + /** + * Considers plain transient fields, fields annotated with {@link Transient}, {@link Value} or {@link Autowired} as + * transien. + * + * @see org.springframework.data.mapping.BasicPersistentProperty#isTransient() + */ + public boolean isTransient() { + + boolean isTransient = super.isTransient() || field.isAnnotationPresent(Transient.class); + + return isTransient || field.isAnnotationPresent(Value.class) || field.isAnnotationPresent(Autowired.class); + } + + /** + * Regards the property as ID if there is an {@link Id} annotation found on it. + */ + public boolean isIdProperty() { + return field.isAnnotationPresent(Id.class); + } + + /** + * Considers the property an {@link Association} if it is annotated with {@link Reference}. + */ + @Override + public boolean isAssociation() { + + if (isTransient()) { + return false; + } + if (field.isAnnotationPresent(Reference.class)) { + return true; + } + + // TODO: do we need this? Shouldn't the section above already find that annotation? + for (Annotation annotation : field.getDeclaredAnnotations()) { + if (annotation.annotationType().isAnnotationPresent(Reference.class)) { + return true; + } + } + + return false; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AssociationHandler.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AssociationHandler.java index c835d2d04..aad45eb34 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AssociationHandler.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/AssociationHandler.java @@ -23,5 +23,5 @@ import org.springframework.data.mapping.model.PersistentProperty; * @author Jon Brisbin */ public interface AssociationHandler

> { - void doWithAssociation(Association

association); + void doWithAssociation(Association

association); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentEntity.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentEntity.java index 2335c32fd..e8db155e9 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentEntity.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/BasicPersistentEntity.java @@ -27,103 +27,103 @@ import org.springframework.data.util.TypeInformation; /** * Simple value object to capture information of {@link PersistentEntity}s. - * + * * @author Jon Brisbin */ public class BasicPersistentEntity> implements MutablePersistentEntity { - protected final PreferredConstructor preferredConstructor; - protected final TypeInformation information; - protected final Map persistentProperties = new HashMap(); - protected final Map> associations = new HashMap>(); - protected P idProperty; + protected final PreferredConstructor preferredConstructor; + protected final TypeInformation information; + protected final Map persistentProperties = new HashMap(); + protected final Map> associations = new HashMap>(); + protected P idProperty; - /** - * Creates a new {@link BasicPersistentEntity} from the given {@link TypeInformation}. - * - * @param information - */ - public BasicPersistentEntity(TypeInformation information) { - this.information = information; - this.preferredConstructor = new PreferredConstructorDiscoverer(information).getConstructor(); - } + /** + * Creates a new {@link BasicPersistentEntity} from the given {@link TypeInformation}. + * + * @param information + */ + public BasicPersistentEntity(TypeInformation information) { + this.information = information; + this.preferredConstructor = new PreferredConstructorDiscoverer(information).getConstructor(); + } - public PreferredConstructor getPreferredConstructor() { - return preferredConstructor; - } + public PreferredConstructor getPreferredConstructor() { + return preferredConstructor; + } - public String getName() { - return getType().getName(); - } + public String getName() { + return getType().getName(); + } - public P getIdProperty() { - return idProperty; - } + public P getIdProperty() { + return idProperty; + } - /* (non-Javadoc) - * @see org.springframework.data.mapping.MutablePersistentEntity#setIdProperty(P) - */ -public void setIdProperty(P property) { - idProperty = property; - } + /* (non-Javadoc) + * @see org.springframework.data.mapping.MutablePersistentEntity#setIdProperty(P) + */ + public void setIdProperty(P property) { + idProperty = property; + } - public Collection

getPersistentProperties() { - return persistentProperties.values(); - } + public Collection

getPersistentProperties() { + return persistentProperties.values(); + } - /* (non-Javadoc) - * @see org.springframework.data.mapping.MutablePersistentEntity#addPersistentProperty(P) - */ -public void addPersistentProperty(P property) { - persistentProperties.put(property.getName(), property); - } + /* (non-Javadoc) + * @see org.springframework.data.mapping.MutablePersistentEntity#addPersistentProperty(P) + */ + public void addPersistentProperty(P property) { + persistentProperties.put(property.getName(), property); + } - public Collection> getAssociations() { - return associations.values(); - } + public Collection> getAssociations() { + return associations.values(); + } - /* (non-Javadoc) - * @see org.springframework.data.mapping.MutablePersistentEntity#addAssociation(org.springframework.data.mapping.model.Association) - */ -public void addAssociation(Association

association) { - associations.put(association.getInverse().getName(), association); - } + /* (non-Javadoc) + * @see org.springframework.data.mapping.MutablePersistentEntity#addAssociation(org.springframework.data.mapping.model.Association) + */ + public void addAssociation(Association

association) { + associations.put(association.getInverse().getName(), association); + } - public P getPersistentProperty(String name) { - return persistentProperties.get(name); - } + public P getPersistentProperty(String name) { + return persistentProperties.get(name); + } - public Class getType() { - return information.getType(); - } + public Class getType() { + return information.getType(); + } - public TypeInformation getTypeInformation() { - return information; - } + public TypeInformation getTypeInformation() { + return information; + } - public Collection getPersistentPropertyNames() { - return persistentProperties.keySet(); - } + public Collection getPersistentPropertyNames() { + return persistentProperties.keySet(); + } - public void doWithProperties(PropertyHandler

handler) { - for (P property : persistentProperties.values()) { - if (!property.isTransient() && !property.isAssociation()) { - handler.doWithPersistentProperty(property); - } - } - } + public void doWithProperties(PropertyHandler

handler) { + for (P property : persistentProperties.values()) { + if (!property.isTransient() && !property.isAssociation()) { + handler.doWithPersistentProperty(property); + } + } + } - public void doWithAssociations(AssociationHandler

handler) { - for (Association

association : associations.values()) { - handler.doWithAssociation(association); - } - } + public void doWithAssociations(AssociationHandler

handler) { + for (Association

association : associations.values()) { + handler.doWithAssociation(association); + } + } - /* (non-Javadoc) - * @see org.springframework.data.mapping.MutablePersistentEntity#verify() - */ - public void verify() { + /* (non-Javadoc) + * @see org.springframework.data.mapping.MutablePersistentEntity#verify() + */ + public void verify() { - } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java index f430697ef..c1e7a5142 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MappingBeanHelper.java @@ -174,32 +174,32 @@ public abstract class MappingBeanHelper { Method setter = property.getPropertyDescriptor() != null ? property.getPropertyDescriptor().getWriteMethod() : null; - if (fieldAccessOnly || null == setter) { - Object valueToSet = getPotentiallyConvertedValue(value, property.getType()); - ReflectionUtils.makeAccessible(property.getField()); - ReflectionUtils.setField(property.getField(), on, valueToSet); - return; - } + if (fieldAccessOnly || null == setter) { + Object valueToSet = getPotentiallyConvertedValue(value, property.getType()); + ReflectionUtils.makeAccessible(property.getField()); + ReflectionUtils.setField(property.getField(), on, valueToSet); + return; + } Class[] paramTypes = setter.getParameterTypes(); Object valueToSet = getPotentiallyConvertedValue(value, paramTypes[0]); ReflectionUtils.makeAccessible(setter); ReflectionUtils.invokeMethod(setter, on, valueToSet); } - + /** * Converts the given source value if it is not assignable to the given target type. - * + * * @param source * @param targetType * @return */ private static Object getPotentiallyConvertedValue(Object source, Class targetType) { - if (source != null && source.getClass().isAssignableFrom(targetType)) { - return source; - } - - return conversionService.convert(source, targetType); + if (source != null && source.getClass().isAssignableFrom(targetType)) { + return source; + } + + return conversionService.convert(source, targetType); } @SuppressWarnings({"unchecked"}) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MutablePersistentEntity.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MutablePersistentEntity.java index 7c20f232e..aebec6553 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MutablePersistentEntity.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/MutablePersistentEntity.java @@ -22,37 +22,37 @@ import org.springframework.data.mapping.model.PersistentProperty; /** * Interface capturing mutator methods for {@link PersistentEntity}s. - * + * * @author Oliver Gierke */ public interface MutablePersistentEntity> - extends PersistentEntity { + extends PersistentEntity { - /** - * Sets the id property for the entity. - * - * @param property - */ - void setIdProperty(P property); + /** + * Sets the id property for the entity. + * + * @param property + */ + void setIdProperty(P property); - /** - * Adds a {@link PersistentProperty} to the entity. - * - * @param property - */ - void addPersistentProperty(P property); + /** + * Adds a {@link PersistentProperty} to the entity. + * + * @param property + */ + void addPersistentProperty(P property); - /** - * Adds an {@link Association} to the entity. - * - * @param association - */ - void addAssociation(Association

association); + /** + * Adds an {@link Association} to the entity. + * + * @param association + */ + void addAssociation(Association

association); - /** - * Callback method to trigger validation of the {@link PersistentEntity}. As - * {@link MutablePersistentEntity} is not immutable there might be some - * verification steps necessary after the object has reached is final state. - */ - void verify(); + /** + * Callback method to trigger validation of the {@link PersistentEntity}. As + * {@link MutablePersistentEntity} is not immutable there might be some + * verification steps necessary after the object has reached is final state. + */ + void verify(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructorDiscoverer.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructorDiscoverer.java index 4263fac43..3552636c7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructorDiscoverer.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PreferredConstructorDiscoverer.java @@ -34,85 +34,85 @@ import org.springframework.data.util.TypeInformation; */ public class PreferredConstructorDiscoverer { - private final ParameterNameDiscoverer nameDiscoverer = - new LocalVariableTableParameterNameDiscoverer(); + private final ParameterNameDiscoverer nameDiscoverer = + new LocalVariableTableParameterNameDiscoverer(); - private PreferredConstructor constructor; - - public PreferredConstructorDiscoverer(Class type) { - this(ClassTypeInformation.from(type)); - } + private PreferredConstructor constructor; - /** - * Creates a new {@link PreferredConstructorDiscoverer} for the given type. - * - * @param owningType - */ - protected PreferredConstructorDiscoverer(TypeInformation owningType) { - - boolean noArgConstructorFound = false; - int numberOfArgConstructors = 0; - Class rawOwningType = owningType.getType(); + public PreferredConstructorDiscoverer(Class type) { + this(ClassTypeInformation.from(type)); + } - for (Constructor constructor : rawOwningType.getDeclaredConstructors()) { + /** + * Creates a new {@link PreferredConstructorDiscoverer} for the given type. + * + * @param owningType + */ + protected PreferredConstructorDiscoverer(TypeInformation owningType) { - PreferredConstructor preferredConstructor = - buildPreferredConstructor(constructor, owningType); + boolean noArgConstructorFound = false; + int numberOfArgConstructors = 0; + Class rawOwningType = owningType.getType(); - // Explicitly defined constructor trumps all - if (preferredConstructor.isExplicitlyAnnotated()) { - this.constructor = preferredConstructor; - return; - } + for (Constructor constructor : rawOwningType.getDeclaredConstructors()) { - // No-arg constructor trumps custom ones - if (this.constructor == null || preferredConstructor.isNoArgConstructor()) { - this.constructor = preferredConstructor; - } - - if (preferredConstructor.isNoArgConstructor()) { - noArgConstructorFound = true; - } else { - numberOfArgConstructors++; - } - } - - if (!noArgConstructorFound && numberOfArgConstructors > 1) { - throw new IllegalArgumentException( - "Multiple no-arg constructors found! Annotate one with @PreferedConstructor explicitly to select it to be used in persistence operations."); - } - } + PreferredConstructor preferredConstructor = + buildPreferredConstructor(constructor, owningType); + + // Explicitly defined constructor trumps all + if (preferredConstructor.isExplicitlyAnnotated()) { + this.constructor = preferredConstructor; + return; + } + + // No-arg constructor trumps custom ones + if (this.constructor == null || preferredConstructor.isNoArgConstructor()) { + this.constructor = preferredConstructor; + } + + if (preferredConstructor.isNoArgConstructor()) { + noArgConstructorFound = true; + } else { + numberOfArgConstructors++; + } + } + + if (!noArgConstructorFound && numberOfArgConstructors > 1) { + throw new IllegalArgumentException( + "Multiple no-arg constructors found! Annotate one with @PreferedConstructor explicitly to select it to be used in persistence operations."); + } + } - @SuppressWarnings({ "unchecked", "rawtypes" }) - private PreferredConstructor buildPreferredConstructor( - Constructor constructor, TypeInformation typeInformation) { + @SuppressWarnings({"unchecked", "rawtypes"}) + private PreferredConstructor buildPreferredConstructor( + Constructor constructor, TypeInformation typeInformation) { - List> parameterTypes = typeInformation.getParameterTypes(constructor); - - if (parameterTypes.isEmpty()) { - return new PreferredConstructor((Constructor) constructor); - } - - String[] parameterNames = nameDiscoverer.getParameterNames(constructor); - Parameter[] parameters = new Parameter[parameterTypes.size()]; - Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); + List> parameterTypes = typeInformation.getParameterTypes(constructor); - for (int i = 0; i < parameterTypes.size(); i++) { - - String name = parameterNames == null ? null : parameterNames[i]; - TypeInformation type = parameterTypes.get(i); - Annotation[] annotations = parameterAnnotations[i]; - - parameters[i] = new Parameter(name, type, annotations); - } + if (parameterTypes.isEmpty()) { + return new PreferredConstructor((Constructor) constructor); + } - return new PreferredConstructor((Constructor) constructor, - parameters); - } + String[] parameterNames = nameDiscoverer.getParameterNames(constructor); + Parameter[] parameters = new Parameter[parameterTypes.size()]; + Annotation[][] parameterAnnotations = constructor.getParameterAnnotations(); + + for (int i = 0; i < parameterTypes.size(); i++) { + + String name = parameterNames == null ? null : parameterNames[i]; + TypeInformation type = parameterTypes.get(i); + Annotation[] annotations = parameterAnnotations[i]; + + parameters[i] = new Parameter(name, type, annotations); + } + + return new PreferredConstructor((Constructor) constructor, + parameters); + } - public PreferredConstructor getConstructor() { - return constructor; - } + public PreferredConstructor getConstructor() { + return constructor; + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PropertyHandler.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PropertyHandler.java index 97dca07a3..7a542ed28 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PropertyHandler.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/PropertyHandler.java @@ -21,10 +21,10 @@ import org.springframework.data.mapping.model.PersistentProperty; /** * Callback interface to do something with all plain {@link PersistentProperty} * instances except associations, transient properties and the id-property. - * + * * @author Jon Brisbin */ public interface PropertyHandler

> { - void doWithPersistentProperty(P persistentProperty); + void doWithPersistentProperty(P persistentProperty); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java index 0ba27f9a5..2d457b295 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAware.java @@ -25,11 +25,11 @@ import org.springframework.data.mapping.model.MappingContext; */ public interface MappingContextAware { - /** - * The active MappingContext for the environment. - * - * @param mappingContext - */ - void setMappingContext(MappingContext mappingContext); + /** + * The active MappingContext for the environment. + * + * @param mappingContext + */ + void setMappingContext(MappingContext mappingContext); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java index 495a95a4b..282089ab1 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/context/MappingContextAwareBeanPostProcessor.java @@ -61,7 +61,7 @@ public class MappingContextAwareBeanPostProcessor implements BeanPostProcessor, } @SuppressWarnings("rawtypes") - public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof MappingContextAware) { if (null == mappingContext) { Map mappingContexts = applicationContext.getBeansOfType(MappingContext.class); diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java index 77c8fd472..89cca9bde 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/event/MappingContextEvent.java @@ -26,20 +26,20 @@ import org.springframework.data.util.TypeInformation; */ public class MappingContextEvent, P extends PersistentProperty

> extends ApplicationEvent { - private static final long serialVersionUID = 1336466833846092490L; - private TypeInformation typeInformation; + private static final long serialVersionUID = 1336466833846092490L; + private TypeInformation typeInformation; - public MappingContextEvent(E source, TypeInformation typeInformation) { - super(source); - this.typeInformation = typeInformation; - } + public MappingContextEvent(E source, TypeInformation typeInformation) { + super(source); + this.typeInformation = typeInformation; + } - public TypeInformation getTypeInformation() { - return typeInformation; - } + public TypeInformation getTypeInformation() { + return typeInformation; + } - @SuppressWarnings("unchecked") - public E getPersistentEntity() { - return (E) source; - } + @SuppressWarnings("unchecked") + public E getPersistentEntity() { + return (E) source; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/Association.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/Association.java index e1930000f..4ef40cdc7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/Association.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/Association.java @@ -21,19 +21,19 @@ package org.springframework.data.mapping.model; */ public class Association

> { - protected P inverse; - protected P obverse; + protected P inverse; + protected P obverse; - public Association(P inverse, P obverse) { - this.inverse = inverse; - this.obverse = obverse; - } + public Association(P inverse, P obverse) { + this.inverse = inverse; + this.obverse = obverse; + } - public P getInverse() { - return inverse; - } + public P getInverse() { + return inverse; + } - public P getObverse() { - return obverse; - } + public P getObverse() { + return obverse; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/IllegalMappingException.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/IllegalMappingException.java index eacae59f7..bd96b96dc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/IllegalMappingException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/IllegalMappingException.java @@ -22,16 +22,16 @@ package org.springframework.data.mapping.model; */ public class IllegalMappingException extends RuntimeException { - /** - * - */ + /** + * + */ private static final long serialVersionUID = 1L; -public IllegalMappingException(String s, Throwable throwable) { - super(s, throwable); - } + public IllegalMappingException(String s, Throwable throwable) { + super(s, throwable); + } - public IllegalMappingException(String s) { - super(s); - } + public IllegalMappingException(String s) { + super(s); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingConfigurationException.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingConfigurationException.java index e8253dcb8..e79450169 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingConfigurationException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingConfigurationException.java @@ -20,16 +20,16 @@ package org.springframework.data.mapping.model; * @author Jon Brisbin */ public class MappingConfigurationException extends Throwable { - /** - * + /** + * */ private static final long serialVersionUID = 1L; -public MappingConfigurationException(String s) { - super(s); - } + public MappingConfigurationException(String s) { + super(s); + } - public MappingConfigurationException(String s, Throwable throwable) { - super(s, throwable); - } + public MappingConfigurationException(String s, Throwable throwable) { + super(s, throwable); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingContext.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingContext.java index eddf1463b..e79b337b8 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingContext.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingContext.java @@ -36,24 +36,24 @@ import org.springframework.validation.Validator; * @author Oliver Gierke */ public interface MappingContext, P extends PersistentProperty

> { - - /** - * Obtains a list of PersistentEntity instances - * - * @return A list of PersistentEntity instances - */ - Collection getPersistentEntities(); - E getPersistentEntity(Class type); + /** + * Obtains a list of PersistentEntity instances + * + * @return A list of PersistentEntity instances + */ + Collection getPersistentEntities(); - E getPersistentEntity(TypeInformation type); + E getPersistentEntity(Class type); - /** - * Obtains a validator for the given entity - * TODO: Why do we need validators at the {@link MappingContext}? - * - * @param entity The entity - * @return A validator or null if none exists for the given entity - */ - List getEntityValidators(E entity); + E getPersistentEntity(TypeInformation type); + + /** + * Obtains a validator for the given entity + * TODO: Why do we need validators at the {@link MappingContext}? + * + * @param entity The entity + * @return A validator or null if none exists for the given entity + */ + List getEntityValidators(E entity); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingException.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingException.java index 88af5f5da..2b62aea54 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingException.java @@ -20,17 +20,17 @@ package org.springframework.data.mapping.model; * @author Jon Brisbin */ public class MappingException extends RuntimeException { - + /** - * + * */ private static final long serialVersionUID = 1L; public MappingException(String s) { - super(s); - } + super(s); + } - public MappingException(String s, Throwable throwable) { - super(s, throwable); - } + public MappingException(String s, Throwable throwable) { + super(s, throwable); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java index 8f0bcce0e..7f8c88251 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/MappingInstantiationException.java @@ -20,14 +20,14 @@ package org.springframework.data.mapping.model; * @author Jon Brisbin */ public class MappingInstantiationException extends RuntimeException { - + /** - * + * */ private static final long serialVersionUID = 1L; public MappingInstantiationException(String s, Throwable throwable) { - super(s, throwable); - } - + super(s, throwable); + } + } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntity.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntity.java index b3809afe4..d3351055b 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntity.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PersistentEntity.java @@ -1,11 +1,11 @@ package org.springframework.data.mapping.model; +import java.util.Collection; + import org.springframework.data.mapping.AssociationHandler; import org.springframework.data.mapping.PropertyHandler; import org.springframework.data.util.TypeInformation; -import java.util.Collection; - /** * Represents a persistent entity @@ -16,62 +16,62 @@ import java.util.Collection; */ public interface PersistentEntity> { - /** - * The entity name including any package prefix - * - * @return The entity name - */ - String getName(); + /** + * The entity name including any package prefix + * + * @return The entity name + */ + String getName(); - PreferredConstructor getPreferredConstructor(); + PreferredConstructor getPreferredConstructor(); - /** - * Returns the id property of the {@link PersistentEntity}. Must never - * return {@literal null} as a {@link PersistentEntity} instance must not be - * created if there is no id property. - * - * @return the id property of the {@link PersistentEntity}. - */ - P getIdProperty(); + /** + * Returns the id property of the {@link PersistentEntity}. Must never + * return {@literal null} as a {@link PersistentEntity} instance must not be + * created if there is no id property. + * + * @return the id property of the {@link PersistentEntity}. + */ + P getIdProperty(); - /** - * A list of properties to be persisted - * - * @return A list of PersistentProperty instances - */ - Collection

getPersistentProperties(); + /** + * A list of properties to be persisted + * + * @return A list of PersistentProperty instances + */ + Collection

getPersistentProperties(); - /** - * A list of the associations for this entity. This is typically a subset of the list returned by {@link #getPersistentProperties()} - * - * @return A list of associations - */ - Collection> getAssociations(); + /** + * A list of the associations for this entity. This is typically a subset of the list returned by {@link #getPersistentProperties()} + * + * @return A list of associations + */ + Collection> getAssociations(); - /** - * Obtains a PersistentProperty instance by name - * - * @param name The name of the property - * @return The PersistentProperty or null if it doesn't exist - */ - P getPersistentProperty(String name); + /** + * Obtains a PersistentProperty instance by name + * + * @param name The name of the property + * @return The PersistentProperty or null if it doesn't exist + */ + P getPersistentProperty(String name); - /** - * @return The underlying Java class for this entity - */ - Class getType(); - - TypeInformation getTypeInformation(); + /** + * @return The underlying Java class for this entity + */ + Class getType(); - /** - * A list of property names - * - * @return A List of strings - */ - Collection getPersistentPropertyNames(); + TypeInformation getTypeInformation(); - void doWithProperties(PropertyHandler

handler); + /** + * A list of property names + * + * @return A List of strings + */ + Collection getPersistentPropertyNames(); - void doWithAssociations(AssociationHandler

handler); + void doWithProperties(PropertyHandler

handler); + + void doWithAssociations(AssociationHandler

handler); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructor.java b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructor.java index b2eacb19a..be98a1b01 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/mapping/model/PreferredConstructor.java @@ -28,8 +28,8 @@ import org.springframework.util.Assert; import org.springframework.util.ReflectionUtils; /** - * Value object to encapsulate the constructor to be used when mapping persistent data to objects. - * + * Value object to encapsulate the constructor to be used when mapping persistent data to objects. + * * @author Jon Brisbin * @author Oliver Gierke */ @@ -39,7 +39,7 @@ public class PreferredConstructor { private final List> parameters; public PreferredConstructor(Constructor constructor, Parameter... parameters) { - ReflectionUtils.makeAccessible(constructor); + ReflectionUtils.makeAccessible(constructor); this.constructor = constructor; this.parameters = Arrays.asList(parameters); } @@ -51,23 +51,23 @@ public class PreferredConstructor { public List> getParameters() { return parameters; } - + /** * Returns whether the constructor does not have any arguments. - * + * * @return */ public boolean isNoArgConstructor() { - return parameters.isEmpty(); + return parameters.isEmpty(); } - + /** * Returns whether the constructor was explicitly selected (by {@link PersistenceConstructor}). - * + * * @return */ public boolean isExplicitlyAnnotated() { - return constructor.isAnnotationPresent(PersistenceConstructor.class); + return constructor.isAnnotationPresent(PersistenceConstructor.class); } public static class Parameter { @@ -76,22 +76,22 @@ public class PreferredConstructor { private final String key; public Parameter(String name, TypeInformation type, Annotation[] annotations) { - - Assert.notNull(type); - Assert.notNull(annotations); - - this.name = name; - this.type = type; - this.key = getValue(annotations); + + Assert.notNull(type); + Assert.notNull(annotations); + + this.name = name; + this.type = type; + this.key = getValue(annotations); } - + private String getValue(Annotation[] annotations) { - for (Annotation anno : annotations) { - if (anno.annotationType() == Value.class) { - return ((Value) anno).value(); - } - } - return null; + for (Annotation anno : annotations) { + if (anno.annotationType() == Value.class) { + return ((Value) anno).value(); + } + } + return null; } public String getName() { @@ -101,9 +101,9 @@ public class PreferredConstructor { public TypeInformation getType() { return type; } - + public Class getRawType() { - return type.getType(); + return type.getType(); } public String getKey() { diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/AbstractConstructorEntityInstantiator.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/AbstractConstructorEntityInstantiator.java index 9fceb6812..9e6aadd62 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/AbstractConstructorEntityInstantiator.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/AbstractConstructorEntityInstantiator.java @@ -1,141 +1,142 @@ -package org.springframework.data.persistence; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; -import org.springframework.util.ClassUtils; -import sun.reflect.ReflectionFactory; - -import java.lang.reflect.Constructor; -import java.lang.reflect.InvocationTargetException; -import java.util.HashMap; -import java.util.Map; - -/** - * Try for a constructor taking state: failing that, try a no-arg - * constructor and then setUnderlyingNode(). - * - * @author Rod Johnson - */ -public abstract class AbstractConstructorEntityInstantiator implements EntityInstantiator { - - private final Log log = LogFactory.getLog(getClass()); - private final Map,StateBackedCreator> cache = new HashMap,StateBackedCreator>(); - - public T createEntityFromState(STATE n, Class c) { - try { - StateBackedCreator creator = (StateBackedCreator) cache.get(c); - if (creator !=null) return creator.create(n,c); - synchronized (cache) { - creator = (StateBackedCreator) cache.get(c); - if (creator !=null) return creator.create(n,c); - Class stateClass = (Class) n.getClass(); - creator =createInstantiator(c, stateClass); - cache.put(c, creator); - return creator.create(n,c); - } - } catch (IllegalArgumentException e) { - throw e; - } catch (InvocationTargetException e) { - throw new IllegalArgumentException(e.getTargetException()); - } catch (Exception e) { - throw new IllegalArgumentException(e); - } - } - - - public void setInstantiators(Map,StateBackedCreator> instantiators) { - this.cache.putAll(instantiators); - } - - protected StateBackedCreator createInstantiator(Class type, final Class stateType) { - StateBackedCreator creator = stateTakingConstructorInstantiator(type,stateType); - if (creator !=null) return creator; - creator = emptyConstructorStateSettingInstantiator(type,stateType); - if (creator !=null) return creator; - return createFailingInstantiator(stateType); - } - - protected StateBackedCreator createFailingInstantiator(final Class stateType) { - return new StateBackedCreator() { - public T create(STATE n, Class c) throws Exception { - throw new IllegalArgumentException(getFailingMessageForClass(c, stateType)); - } - }; - } - - protected String getFailingMessageForClass(Class entityClass, Class stateClass) { - return getClass().getSimpleName() + ": entity " + entityClass + - " must have either a constructor taking [" + stateClass + "] or a no-arg constructor and state setter."; - } - - private StateBackedCreator emptyConstructorStateSettingInstantiator(Class type, Class stateType) { - final Constructor constructor = getNoArgConstructor(type); - if (constructor == null) return null; - - log.info("Using " + type + " no-arg constructor"); - - return new StateBackedCreator() { - public T create(STATE state, Class c) throws Exception { - try { - StateProvider.setUnderlyingState(state); - T newInstance = constructor.newInstance(); - setState(newInstance, state); - return newInstance; - } finally { - StateProvider.retrieveState(); - } - } - }; - } - - protected StateBackedCreator createWithoutConstructorInvocation(final Class type, Class stateType) { - ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); - Constructor objectConstructor = getDeclaredConstructor(Object.class); - final Constructor serializationConstructor = rf.newConstructorForSerialization(type, objectConstructor); - return new StateBackedCreator() { - public T create(STATE state, Class c) throws Exception { - final T result = type.cast(serializationConstructor.newInstance()); - setState(result,state); - return result; - } - }; - } - - - protected Constructor getNoArgConstructor(Class type) { - Constructor constructor = ClassUtils.getConstructorIfAvailable(type); - if (constructor != null) return constructor; - return getDeclaredConstructor(type); - } - - protected StateBackedCreator stateTakingConstructorInstantiator(Class type, Class stateType) { - Class stateInterface = (Class) stateType.getInterfaces()[0]; - final Constructor constructor = ClassUtils.getConstructorIfAvailable(type, stateInterface); - if (constructor == null) return null; - - log.info("Using " + type + " constructor taking " + stateInterface); - return new StateBackedCreator() { - public T create(STATE n, Class c) throws Exception { - return constructor.newInstance(n); - } - }; - } - - protected Constructor getDeclaredConstructor(Class c) { - try { - final Constructor declaredConstructor = c.getDeclaredConstructor(); - declaredConstructor.setAccessible(true); - return declaredConstructor; - } catch (NoSuchMethodException e) { - return null; - } - } - - /** - * Subclasses must implement to set state - * @param entity - * @param s - */ - protected abstract void setState(BACKING_INTERFACE entity, STATE s); - -} +package org.springframework.data.persistence; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.HashMap; +import java.util.Map; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.springframework.util.ClassUtils; +import sun.reflect.ReflectionFactory; + +/** + * Try for a constructor taking state: failing that, try a no-arg + * constructor and then setUnderlyingNode(). + * + * @author Rod Johnson + */ +public abstract class AbstractConstructorEntityInstantiator implements EntityInstantiator { + + private final Log log = LogFactory.getLog(getClass()); + private final Map, StateBackedCreator> cache = new HashMap, StateBackedCreator>(); + + public T createEntityFromState(STATE n, Class c) { + try { + StateBackedCreator creator = (StateBackedCreator) cache.get(c); + if (creator != null) return creator.create(n, c); + synchronized (cache) { + creator = (StateBackedCreator) cache.get(c); + if (creator != null) return creator.create(n, c); + Class stateClass = (Class) n.getClass(); + creator = createInstantiator(c, stateClass); + cache.put(c, creator); + return creator.create(n, c); + } + } catch (IllegalArgumentException e) { + throw e; + } catch (InvocationTargetException e) { + throw new IllegalArgumentException(e.getTargetException()); + } catch (Exception e) { + throw new IllegalArgumentException(e); + } + } + + + public void setInstantiators(Map, StateBackedCreator> instantiators) { + this.cache.putAll(instantiators); + } + + protected StateBackedCreator createInstantiator(Class type, final Class stateType) { + StateBackedCreator creator = stateTakingConstructorInstantiator(type, stateType); + if (creator != null) return creator; + creator = emptyConstructorStateSettingInstantiator(type, stateType); + if (creator != null) return creator; + return createFailingInstantiator(stateType); + } + + protected StateBackedCreator createFailingInstantiator(final Class stateType) { + return new StateBackedCreator() { + public T create(STATE n, Class c) throws Exception { + throw new IllegalArgumentException(getFailingMessageForClass(c, stateType)); + } + }; + } + + protected String getFailingMessageForClass(Class entityClass, Class stateClass) { + return getClass().getSimpleName() + ": entity " + entityClass + + " must have either a constructor taking [" + stateClass + "] or a no-arg constructor and state setter."; + } + + private StateBackedCreator emptyConstructorStateSettingInstantiator(Class type, Class stateType) { + final Constructor constructor = getNoArgConstructor(type); + if (constructor == null) return null; + + log.info("Using " + type + " no-arg constructor"); + + return new StateBackedCreator() { + public T create(STATE state, Class c) throws Exception { + try { + StateProvider.setUnderlyingState(state); + T newInstance = constructor.newInstance(); + setState(newInstance, state); + return newInstance; + } finally { + StateProvider.retrieveState(); + } + } + }; + } + + protected StateBackedCreator createWithoutConstructorInvocation(final Class type, Class stateType) { + ReflectionFactory rf = ReflectionFactory.getReflectionFactory(); + Constructor objectConstructor = getDeclaredConstructor(Object.class); + final Constructor serializationConstructor = rf.newConstructorForSerialization(type, objectConstructor); + return new StateBackedCreator() { + public T create(STATE state, Class c) throws Exception { + final T result = type.cast(serializationConstructor.newInstance()); + setState(result, state); + return result; + } + }; + } + + + protected Constructor getNoArgConstructor(Class type) { + Constructor constructor = ClassUtils.getConstructorIfAvailable(type); + if (constructor != null) return constructor; + return getDeclaredConstructor(type); + } + + protected StateBackedCreator stateTakingConstructorInstantiator(Class type, Class stateType) { + Class stateInterface = (Class) stateType.getInterfaces()[0]; + final Constructor constructor = ClassUtils.getConstructorIfAvailable(type, stateInterface); + if (constructor == null) return null; + + log.info("Using " + type + " constructor taking " + stateInterface); + return new StateBackedCreator() { + public T create(STATE n, Class c) throws Exception { + return constructor.newInstance(n); + } + }; + } + + protected Constructor getDeclaredConstructor(Class c) { + try { + final Constructor declaredConstructor = c.getDeclaredConstructor(); + declaredConstructor.setAccessible(true); + return declaredConstructor; + } catch (NoSuchMethodException e) { + return null; + } + } + + /** + * Subclasses must implement to set state + * + * @param entity + * @param s + */ + protected abstract void setState(BACKING_INTERFACE entity, STATE s); + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSet.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSet.java index 0ddb04623..a226e6e9e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSet.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSet.java @@ -1,24 +1,23 @@ -package org.springframework.data.persistence; - -import java.util.Map; - -import org.springframework.core.convert.ConversionService; - -/** - * Interface representing the set of changes in an entity. - * - * @author Rod Johnson - * @author Thomas Risberg - * - */ -public interface ChangeSet { - - T get(String key, Class requiredClass, ConversionService cs); - - void set(String key, Object o); - - Map getValues(); - - Object removeProperty(String k); - -} +package org.springframework.data.persistence; + +import java.util.Map; + +import org.springframework.core.convert.ConversionService; + +/** + * Interface representing the set of changes in an entity. + * + * @author Rod Johnson + * @author Thomas Risberg + */ +public interface ChangeSet { + + T get(String key, Class requiredClass, ConversionService cs); + + void set(String key, Object o); + + Map getValues(); + + Object removeProperty(String k); + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetBacked.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetBacked.java index d2dc03c88..9c4303628 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetBacked.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetBacked.java @@ -1,13 +1,14 @@ -package org.springframework.data.persistence; - - -/** - * Interface introduced to objects exposing ChangeSet information - * @author Rod Johnson - * @author Thomas Risberg - */ -public interface ChangeSetBacked { - - ChangeSet getChangeSet(); - -} +package org.springframework.data.persistence; + + +/** + * Interface introduced to objects exposing ChangeSet information + * + * @author Rod Johnson + * @author Thomas Risberg + */ +public interface ChangeSetBacked { + + ChangeSet getChangeSet(); + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetConfiguration.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetConfiguration.java index 06c5a4d8c..4f8faf6c6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetConfiguration.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetConfiguration.java @@ -22,7 +22,6 @@ public class ChangeSetConfiguration { ChangeSetSynchronizer changeSetManager) { this.changeSetManager = changeSetManager; } - - - + + } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetPersister.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetPersister.java index 742c2e846..8984fbd63 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetPersister.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetPersister.java @@ -5,45 +5,47 @@ import org.springframework.dao.DataAccessException; /** * Interface to be implemented by classes that can synchronize * between data stores and ChangeSets. - * @author Rod Johnson * * @param entity key + * @author Rod Johnson */ public interface ChangeSetPersister { - + String ID_KEY = "_id"; String CLASS_KEY = "_class"; - + /** * TODO how to tell when not found? throw exception? */ void getPersistentState(Class entityClass, K key, ChangeSet changeSet) throws DataAccessException, NotFoundException; - + /** * Return id - * @param entity - * @param cs + * + * @param entity + * @param cs * @return * @throws DataAccessException */ K getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException; - + /** * Return key - * @param entity - * @param cs Key may be null if not persistent + * + * @param entity + * @param cs Key may be null if not persistent * @return * @throws DataAccessException */ K persistState(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException; - + /** * Exception thrown in alternate control flow if getPersistentState * finds no entity data. */ class NotFoundException extends Exception { - + } - + } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetSynchronizer.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetSynchronizer.java index c76ffba7d..43da163ab 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetSynchronizer.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/ChangeSetSynchronizer.java @@ -1,28 +1,29 @@ -package org.springframework.data.persistence; - -import java.util.Map; - -import org.springframework.dao.DataAccessException; - -/** - * Interface to be implemented by classes that can synchronize - * between entities and ChangeSets. - * @author Rod Johnson - * - * @param - */ -public interface ChangeSetSynchronizer { - - Map> persistentFields(Class entityClassClass); - - /** - * Take all entity fields into a changeSet. - * @param entity - * @return - * @throws DataAccessException - */ - void populateChangeSet(ChangeSet changeSet, E entity) throws DataAccessException; - - void populateEntity(ChangeSet changeSet, E entity) throws DataAccessException; - -} +package org.springframework.data.persistence; + +import java.util.Map; + +import org.springframework.dao.DataAccessException; + +/** + * Interface to be implemented by classes that can synchronize + * between entities and ChangeSets. + * + * @param + * @author Rod Johnson + */ +public interface ChangeSetSynchronizer { + + Map> persistentFields(Class entityClassClass); + + /** + * Take all entity fields into a changeSet. + * + * @param entity + * @return + * @throws DataAccessException + */ + void populateChangeSet(ChangeSet changeSet, E entity) throws DataAccessException; + + void populateEntity(ChangeSet changeSet, E entity) throws DataAccessException; + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/EntityInstantiator.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/EntityInstantiator.java index 489c29739..5af972cfc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/EntityInstantiator.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/EntityInstantiator.java @@ -1,32 +1,32 @@ -package org.springframework.data.persistence; - - -/** - * Interface to be implemented by classes that can instantiate and - * configure entities. - * The framework must do this when creating objects resulting from finders, - * even when there may be no no-arg constructor supplied by the user. - * - * @author Rod Johnson - */ -public interface EntityInstantiator { - - /* - * The best solution if available is to add a constructor that takes Node - * to each GraphEntity. This means generating an aspect beside every - * class as Roo presently does. - * - * An alternative that does not require Roo - * is a user-authored constructor taking Node and calling setUnderlyingNode() - * but this is less elegant and pollutes the domain object. - * - * If the user supplies a no-arg constructor, instantiation can occur by invoking it - * prior to calling setUnderlyingNode(). - * - * If the user does NOT supply a no-arg constructor, we must rely on Sun-specific - * code to instantiate entities without invoking a constructor. - */ - - T createEntityFromState(STATE s, Class c); - -} +package org.springframework.data.persistence; + + +/** + * Interface to be implemented by classes that can instantiate and + * configure entities. + * The framework must do this when creating objects resulting from finders, + * even when there may be no no-arg constructor supplied by the user. + * + * @author Rod Johnson + */ +public interface EntityInstantiator { + + /* + * The best solution if available is to add a constructor that takes Node + * to each GraphEntity. This means generating an aspect beside every + * class as Roo presently does. + * + * An alternative that does not require Roo + * is a user-authored constructor taking Node and calling setUnderlyingNode() + * but this is less elegant and pollutes the domain object. + * + * If the user supplies a no-arg constructor, instantiation can occur by invoking it + * prior to calling setUnderlyingNode(). + * + * If the user does NOT supply a no-arg constructor, we must rely on Sun-specific + * code to instantiate entities without invoking a constructor. + */ + + T createEntityFromState(STATE s, Class c); + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/HashMapChangeSet.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/HashMapChangeSet.java index ca9e72b30..391da33aa 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/HashMapChangeSet.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/HashMapChangeSet.java @@ -1,46 +1,47 @@ -package org.springframework.data.persistence; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.springframework.core.convert.ConversionService; - -/** - * Simple ChangeSet implementation backed by a HashMap. - * @author Thomas Risberg - * @author Rod Johnson - */ -public class HashMapChangeSet implements ChangeSet { - - private Map values; - - public HashMapChangeSet(Map values) { - this.values = values; - } - - public HashMapChangeSet() { - this(new HashMap()); - } - - public void set(String key, Object o) { - values.put(key, o); - } - - public String toString() { - return "HashMapChangeSet: values=[" + values + "]"; - } - - public Map getValues() { - return Collections.unmodifiableMap(values); - } - - public Object removeProperty(String k) { - return this.values.remove(k); - } - - public T get(String key, Class requiredClass, ConversionService conversionService) { - return conversionService.convert(values.get(key), requiredClass); - } - -} +package org.springframework.data.persistence; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import org.springframework.core.convert.ConversionService; + +/** + * Simple ChangeSet implementation backed by a HashMap. + * + * @author Thomas Risberg + * @author Rod Johnson + */ +public class HashMapChangeSet implements ChangeSet { + + private Map values; + + public HashMapChangeSet(Map values) { + this.values = values; + } + + public HashMapChangeSet() { + this(new HashMap()); + } + + public void set(String key, Object o) { + values.put(key, o); + } + + public String toString() { + return "HashMapChangeSet: values=[" + values + "]"; + } + + public Map getValues() { + return Collections.unmodifiableMap(values); + } + + public Object removeProperty(String k) { + return this.values.remove(k); + } + + public T get(String key, Class requiredClass, ConversionService conversionService) { + return conversionService.convert(values.get(key), requiredClass); + } + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateBackedCreator.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateBackedCreator.java index c65e97895..32978e302 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateBackedCreator.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateBackedCreator.java @@ -1,11 +1,11 @@ package org.springframework.data.persistence; /** -* encapsulates the instantiator of state-backed classes and populating them with the provided state. -* -* Can be implemented and registered with the concrete AbstractConstructorEntityInstantiator to provide -* non reflection bases instantiaton for domain classes -*/ + * encapsulates the instantiator of state-backed classes and populating them with the provided state. + *

+ * Can be implemented and registered with the concrete AbstractConstructorEntityInstantiator to provide + * non reflection bases instantiaton for domain classes + */ public interface StateBackedCreator { - T create(STATE n, Class c) throws Exception; + T create(STATE n, Class c) throws Exception; } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateProvider.java b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateProvider.java index 6b7f54799..6c12672f6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateProvider.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/persistence/StateProvider.java @@ -5,17 +5,20 @@ package org.springframework.data.persistence; * @since 24.09.2010 */ public abstract class StateProvider { - private final static ThreadLocal stateHolder=new ThreadLocal(); + private final static ThreadLocal stateHolder = new ThreadLocal(); - private StateProvider() {} + private StateProvider() { + } - public static void setUnderlyingState(STATE state) { - if (stateHolder.get()!=null) throw new IllegalStateException("StateHolder already contains state "+stateHolder.get()+" in thread "+Thread.currentThread()); - stateHolder.set(state); - } - public static STATE retrieveState() { - STATE result= (STATE) stateHolder.get(); - stateHolder.remove(); - return result; - } + public static void setUnderlyingState(STATE state) { + if (stateHolder.get() != null) + throw new IllegalStateException("StateHolder already contains state " + stateHolder.get() + " in thread " + Thread.currentThread()); + stateHolder.set(state); + } + + public static STATE retrieveState() { + STATE result = (STATE) stateHolder.get(); + stateHolder.remove(); + return result; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/EntityPathResolver.java b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/EntityPathResolver.java index 389a9aefb..03ecfeab3 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/EntityPathResolver.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/EntityPathResolver.java @@ -19,10 +19,10 @@ import com.mysema.query.types.EntityPath; /** * Strategy interface to abstract the ways to translate an plain domain class into a {@link EntityPath}. - * + * * @author Oliver Gierke */ public interface EntityPathResolver { - EntityPath createPath(Class domainClass); + EntityPath createPath(Class domainClass); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java index d9a4ca37a..29dba9819 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/QueryDslUtils.java @@ -17,11 +17,10 @@ package org.springframework.data.querydsl; /** - * * @author Oliver Gierke */ public class QueryDslUtils { - public static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent( - "com.mysema.query.types.Predicate", QueryDslUtils.class.getClassLoader()); + public static final boolean QUERY_DSL_PRESENT = org.springframework.util.ClassUtils.isPresent( + "com.mysema.query.types.Predicate", QueryDslUtils.class.getClassLoader()); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/SimpleEntityPathResolver.java b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/SimpleEntityPathResolver.java index 6d360001e..c31dd2758 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/SimpleEntityPathResolver.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/querydsl/SimpleEntityPathResolver.java @@ -18,118 +18,117 @@ package org.springframework.data.querydsl; import java.lang.reflect.Field; import java.lang.reflect.Modifier; +import com.mysema.query.types.EntityPath; import org.springframework.util.ClassUtils; import org.springframework.util.ReflectionUtils; -import com.mysema.query.types.EntityPath; - /** * Simple implementation of {@link EntityPathResolver} to lookup a query * class by reflection and using the static field of the same type. - * + * * @author Oliver Gierke */ public enum SimpleEntityPathResolver implements EntityPathResolver { - INSTANCE; + INSTANCE; - private static final String NO_CLASS_FOUND_TEMPLATE = - "Did not find a query class %s for domain class %s!"; - private static final String NO_FIELD_FOUND_TEMPLATE = - "Did not find a static field of the same type in %s!"; + private static final String NO_CLASS_FOUND_TEMPLATE = + "Did not find a query class %s for domain class %s!"; + private static final String NO_FIELD_FOUND_TEMPLATE = + "Did not find a static field of the same type in %s!"; - /** - * Creates an {@link EntityPath} instance for the given domain class. - * Tries to lookup a class matching the naming convention (prepend Q to - * the simple name of the class, same package) and find a static field - * of the same type in it. - * - * @param domainClass - * @return - */ - @SuppressWarnings("unchecked") - public EntityPath createPath(Class domainClass) { + /** + * Creates an {@link EntityPath} instance for the given domain class. + * Tries to lookup a class matching the naming convention (prepend Q to + * the simple name of the class, same package) and find a static field + * of the same type in it. + * + * @param domainClass + * @return + */ + @SuppressWarnings("unchecked") + public EntityPath createPath(Class domainClass) { - String pathClassName = getQueryClassName(domainClass); + String pathClassName = getQueryClassName(domainClass); - try { - Class pathClass = - ClassUtils.forName(pathClassName, - SimpleEntityPathResolver.class.getClassLoader()); - Field field = getStaticFieldOfType(pathClass); + try { + Class pathClass = + ClassUtils.forName(pathClassName, + SimpleEntityPathResolver.class.getClassLoader()); + Field field = getStaticFieldOfType(pathClass); - if (field == null) { - throw new IllegalStateException(String.format( - NO_FIELD_FOUND_TEMPLATE, pathClass)); - } else { - return (EntityPath) ReflectionUtils - .getField(field, null); - } + if (field == null) { + throw new IllegalStateException(String.format( + NO_FIELD_FOUND_TEMPLATE, pathClass)); + } else { + return (EntityPath) ReflectionUtils + .getField(field, null); + } - } catch (ClassNotFoundException e) { - throw new IllegalArgumentException(String.format( - NO_CLASS_FOUND_TEMPLATE, pathClassName, - domainClass.getName()), e); - } - } + } catch (ClassNotFoundException e) { + throw new IllegalArgumentException(String.format( + NO_CLASS_FOUND_TEMPLATE, pathClassName, + domainClass.getName()), e); + } + } - /** - * Returns the first static field of the given type inside the given - * type. - * - * @param type - * @return - */ - private Field getStaticFieldOfType(Class type) { + /** + * Returns the first static field of the given type inside the given + * type. + * + * @param type + * @return + */ + private Field getStaticFieldOfType(Class type) { - for (Field field : type.getDeclaredFields()) { + for (Field field : type.getDeclaredFields()) { - boolean isStatic = Modifier.isStatic(field.getModifiers()); - boolean hasSameType = type.equals(field.getType()); + boolean isStatic = Modifier.isStatic(field.getModifiers()); + boolean hasSameType = type.equals(field.getType()); - if (isStatic && hasSameType) { - return field; - } - } + if (isStatic && hasSameType) { + return field; + } + } - Class superclass = type.getSuperclass(); - return Object.class.equals(superclass) ? null - : getStaticFieldOfType(superclass); - } + Class superclass = type.getSuperclass(); + return Object.class.equals(superclass) ? null + : getStaticFieldOfType(superclass); + } - /** - * Returns the name of the query class for the given domain class. - * - * @param domainClass - * @return - */ - private String getQueryClassName(Class domainClass) { + /** + * Returns the name of the query class for the given domain class. + * + * @param domainClass + * @return + */ + private String getQueryClassName(Class domainClass) { - String simpleClassName = ClassUtils.getShortName(domainClass); - return String.format("%s.Q%s%s", - domainClass.getPackage().getName(), - getClassBase(simpleClassName), domainClass.getSimpleName()); - } + String simpleClassName = ClassUtils.getShortName(domainClass); + return String.format("%s.Q%s%s", + domainClass.getPackage().getName(), + getClassBase(simpleClassName), domainClass.getSimpleName()); + } - /** - * Analyzes the short class name and potentially returns the outer - * class. - * - * @param shortName - * @return - */ - private String getClassBase(String shortName) { + /** + * Analyzes the short class name and potentially returns the outer + * class. + * + * @param shortName + * @return + */ + private String getClassBase(String shortName) { - String[] parts = shortName.split("\\."); + String[] parts = shortName.split("\\."); - if (parts.length < 2) { - return ""; - } + if (parts.length < 2) { + return ""; + } - return parts[0] + "_"; - } + return parts[0] + "_"; + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/NoRepositoryBean.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/NoRepositoryBean.java index 376bf3e1d..c29fbf0ff 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/NoRepositoryBean.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/NoRepositoryBean.java @@ -25,13 +25,13 @@ import java.lang.annotation.Target; /** * Annotation to exclude repository interfaces from being picked up and thus in * consequence getting an instance being created. - *

+ *

* This will typically be used when providing an extended base interface for all * repositories in combination with a custom repository base class to implement * methods declared in that intermediate interface. In this case you typically * derive your concrete repository interfaces from the intermediate one but * don't want to create a Spring bean for the intermediate interface. - * + * * @author Oliver Gierke */ @Retention(RetentionPolicy.RUNTIME) diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/PagingAndSortingRepository.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/PagingAndSortingRepository.java index 87ba2f078..3bbbefab1 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/PagingAndSortingRepository.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/PagingAndSortingRepository.java @@ -25,30 +25,30 @@ import org.springframework.data.domain.Sort; /** * Extension of {@link Repository} to provide additional methods to retrieve * entities using the pagination and sorting abstraction. - * + * + * @author Oliver Gierke * @see Sort * @see Pageable * @see Page - * @author Oliver Gierke */ public interface PagingAndSortingRepository extends - Repository { + Repository { - /** - * Returns all entities sorted by the given options. - * - * @param sort - * @return all entities sorted by the given options - */ - Iterable findAll(Sort sort); + /** + * Returns all entities sorted by the given options. + * + * @param sort + * @return all entities sorted by the given options + */ + Iterable findAll(Sort sort); - /** - * Returns a {@link Page} of entities meeting the paging restriction - * provided in the {@code Pageable} object. - * - * @param pageable - * @return a page of entities - */ - Page findAll(Pageable pageable); + /** + * Returns a {@link Page} of entities meeting the paging restriction + * provided in the {@code Pageable} object. + * + * @param pageable + * @return a page of entities + */ + Page findAll(Pageable pageable); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/Repository.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/Repository.java index f17c3a13a..25557fe40 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/Repository.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/Repository.java @@ -20,86 +20,86 @@ import java.io.Serializable; /** * Interface for generic CRUD operations on a repository for a specific type. - * + * * @author Oliver Gierke * @author Eberhard Wolff */ public interface Repository { - /** - * Saves a given entity. Use the returned instance for further operations as - * the save operation might have changed the entity instance completely. - * - * @param entity - * @return the saved entity - */ - T save(T entity); + /** + * Saves a given entity. Use the returned instance for further operations as + * the save operation might have changed the entity instance completely. + * + * @param entity + * @return the saved entity + */ + T save(T entity); - /** - * Saves all given entities. - * - * @param entities - * @return - */ - Iterable save(Iterable entities); + /** + * Saves all given entities. + * + * @param entities + * @return + */ + Iterable save(Iterable entities); - /** - * Retrives an entity by its primary key. - * - * @param id - * @return the entity with the given primary key or {@code null} if none - * found - * @throws IllegalArgumentException if primaryKey is {@code null} - */ - T findOne(ID id); + /** + * Retrives an entity by its primary key. + * + * @param id + * @return the entity with the given primary key or {@code null} if none + * found + * @throws IllegalArgumentException if primaryKey is {@code null} + */ + T findOne(ID id); - /** - * Returns whether an entity with the given id exists. - * - * @param id - * @return true if an entity with the given id exists, alse otherwise - * @throws IllegalArgumentException if primaryKey is {@code null} - */ - boolean exists(ID id); + /** + * Returns whether an entity with the given id exists. + * + * @param id + * @return true if an entity with the given id exists, alse otherwise + * @throws IllegalArgumentException if primaryKey is {@code null} + */ + boolean exists(ID id); - /** - * Returns all instances of the type. - * - * @return all entities - */ - Iterable findAll(); + /** + * Returns all instances of the type. + * + * @return all entities + */ + Iterable findAll(); - /** - * Returns the number of entities available. - * - * @return the number of entities - */ - long count(); + /** + * Returns the number of entities available. + * + * @return the number of entities + */ + long count(); - /** - * Deletes a given entity. - * - * @param entity - */ - void delete(T entity); + /** + * Deletes a given entity. + * + * @param entity + */ + void delete(T entity); - /** - * Deletes the given entities. - * - * @param entities - */ - void delete(Iterable entities); + /** + * Deletes the given entities. + * + * @param entities + */ + void delete(Iterable entities); - /** - * Deletes all entities managed by the repository. - */ - void deleteAll(); + /** + * Deletes all entities managed by the repository. + */ + void deleteAll(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java index 33dd0b7c8..5417b006d 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AbstractRepositoryConfigDefinitionParser.java @@ -52,429 +52,429 @@ import org.w3c.dom.Element; * the XML element into a {@link GlobalRepositoryConfigInformation} object and * allow either manual configuration or automatic detection of repository * interfaces. - * + * * @author Oliver Gierke */ public abstract class AbstractRepositoryConfigDefinitionParser, T extends SingleRepositoryConfigInformation> - implements BeanDefinitionParser { + implements BeanDefinitionParser { - private static final Log LOG = LogFactory.getLog( - AbstractRepositoryConfigDefinitionParser.class); + private static final Log LOG = LogFactory.getLog( + AbstractRepositoryConfigDefinitionParser.class); - private static final String REPOSITORY_INTERFACE_POST_PROCESSOR = - "org.springframework.data.repository.support.RepositoryInterfaceAwareBeanPostProcessor"; + private static final String REPOSITORY_INTERFACE_POST_PROCESSOR = + "org.springframework.data.repository.support.RepositoryInterfaceAwareBeanPostProcessor"; - /* - * (non-Javadoc) - * - * @see - * org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org. - * w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext) - */ - public BeanDefinition parse(Element element, ParserContext parser) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.factory.xml.BeanDefinitionParser#parse(org. + * w3c.dom.Element, org.springframework.beans.factory.xml.ParserContext) + */ + public BeanDefinition parse(Element element, ParserContext parser) { - try { - S configContext = getGlobalRepositoryConfigInformation(element); + try { + S configContext = getGlobalRepositoryConfigInformation(element); - if (configContext.configureManually()) { - doManualConfiguration(configContext, parser); - } else { - doAutoConfiguration(configContext, parser); - } + if (configContext.configureManually()) { + doManualConfiguration(configContext, parser); + } else { + doAutoConfiguration(configContext, parser); + } - Object beanSource = parser.extractSource(element); - registerBeansForRoot(parser.getRegistry(), beanSource); + Object beanSource = parser.extractSource(element); + registerBeansForRoot(parser.getRegistry(), beanSource); - } catch (RuntimeException e) { - handleError(e, element, parser.getReaderContext()); - } + } catch (RuntimeException e) { + handleError(e, element, parser.getReaderContext()); + } - return null; - } + return null; + } - /** - * Executes repository auto configuration by scanning the provided base - * package for repository interfaces. - * - * @param config - * @param parser - */ - private void doAutoConfiguration(S config, ParserContext parser) { + /** + * Executes repository auto configuration by scanning the provided base + * package for repository interfaces. + * + * @param config + * @param parser + */ + private void doAutoConfiguration(S config, ParserContext parser) { - LOG.debug("Triggering auto repository detection"); + LOG.debug("Triggering auto repository detection"); - ResourceLoader resourceLoader = - parser.getReaderContext().getResourceLoader(); + ResourceLoader resourceLoader = + parser.getReaderContext().getResourceLoader(); - // Detect available repository interfaces - Set repositoryInterfaces = - getRepositoryInterfacesForAutoConfig(config, resourceLoader, - parser.getReaderContext()); + // Detect available repository interfaces + Set repositoryInterfaces = + getRepositoryInterfacesForAutoConfig(config, resourceLoader, + parser.getReaderContext()); - for (String repositoryInterface : repositoryInterfaces) { - registerGenericRepositoryFactoryBean( - parser, - config.getAutoconfigRepositoryInformation(repositoryInterface)); - } - } + for (String repositoryInterface : repositoryInterfaces) { + registerGenericRepositoryFactoryBean( + parser, + config.getAutoconfigRepositoryInformation(repositoryInterface)); + } + } - private Set getRepositoryInterfacesForAutoConfig(S config, - ResourceLoader loader, ReaderContext reader) { + private Set getRepositoryInterfacesForAutoConfig(S config, + ResourceLoader loader, ReaderContext reader) { - ClassPathScanningCandidateComponentProvider scanner = - new RepositoryComponentProvider( - config.getRepositoryBaseInterface()); - scanner.setResourceLoader(loader); + ClassPathScanningCandidateComponentProvider scanner = + new RepositoryComponentProvider( + config.getRepositoryBaseInterface()); + scanner.setResourceLoader(loader); - TypeFilterParser parser = - new TypeFilterParser(loader.getClassLoader(), reader); - parser.parseFilters(config.getSource(), scanner); + TypeFilterParser parser = + new TypeFilterParser(loader.getClassLoader(), reader); + parser.parseFilters(config.getSource(), scanner); - Set findCandidateComponents = - scanner.findCandidateComponents(config.getBasePackage()); + Set findCandidateComponents = + scanner.findCandidateComponents(config.getBasePackage()); - Set interfaceNames = new HashSet(); - for (BeanDefinition definition : findCandidateComponents) { - interfaceNames.add(definition.getBeanClassName()); - } + Set interfaceNames = new HashSet(); + for (BeanDefinition definition : findCandidateComponents) { + interfaceNames.add(definition.getBeanClassName()); + } - return interfaceNames; - } + return interfaceNames; + } - /** - * Returns a {@link GlobalRepositoryConfigInformation} implementation for - * the given element. - * - * @param element - * @return - */ - protected abstract S getGlobalRepositoryConfigInformation(Element element); + /** + * Returns a {@link GlobalRepositoryConfigInformation} implementation for + * the given element. + * + * @param element + * @return + */ + protected abstract S getGlobalRepositoryConfigInformation(Element element); - /** - * Proceeds manual configuration by traversing the context's - * {@link SingleRepositoryConfigInformation}s. - * - * @param context - * @param parser - */ - private void doManualConfiguration(S context, ParserContext parser) { - - LOG.debug("Triggering manual repository detection"); - - for (T repositoryContext : context - .getSingleRepositoryConfigInformations()) { - registerGenericRepositoryFactoryBean(parser, repositoryContext); - } - } - - - private void handleError(Exception e, Element source, ReaderContext reader) { - - reader.error(e.getMessage(), reader.extractSource(source), e.getCause()); - } - - - /** - * Registers a generic repository factory bean for a bean with the given - * name and the provided configuration context. - * - * @param parser - * @param name - * @param context - */ - private void registerGenericRepositoryFactoryBean(ParserContext parser, - T context) { - - try { - - Object beanSource = parser.extractSource(context.getSource()); - - BeanDefinitionBuilder builder = - BeanDefinitionBuilder.rootBeanDefinition(context - .getRepositoryFactoryBeanClassName()); - - builder.addPropertyValue("repositoryInterface", - context.getInterfaceName()); - builder.addPropertyValue("queryLookupStrategyKey", - context.getQueryLookupStrategyKey()); - - String transactionManagerRef = context.getTransactionManagerRef(); - - if (StringUtils.hasText(transactionManagerRef)) { - builder.addPropertyValue("transactionManager", - transactionManagerRef); - } - - String customImplementationBeanName = - registerCustomImplementation(context, parser, beanSource); - - if (customImplementationBeanName != null) { - builder.addPropertyReference("customImplementation", - customImplementationBeanName); - } - - postProcessBeanDefinition(context, builder, parser.getRegistry(), beanSource); - - AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); - beanDefinition.setSource(beanSource); - - if (LOG.isDebugEnabled()) { - LOG.debug( - "Registering repository: " + context.getBeanId() + - " - Interface: " + context.getInterfaceName() + - " - Factory: " + context.getRepositoryFactoryBeanClassName() + - ", - Custom implementation: " + customImplementationBeanName); - } - - BeanComponentDefinition definition = - new BeanComponentDefinition(beanDefinition, - context.getBeanId()); - parser.registerBeanComponent(definition); - } catch (RuntimeException e) { - handleError(e, context.getSource(), parser.getReaderContext()); - } - } - - - /** - * Callback to post process a repository bean definition prior to actual - * registration. - * - * @param context - * @param builder - * @param beanSource - */ - protected void postProcessBeanDefinition(T context, - BeanDefinitionBuilder builder, BeanDefinitionRegistry registry, Object beanSource) { - - } - - - /** - * Registers a possibly available custom repository implementation on the - * repository bean. Tries to find an already registered bean to reference or - * tries to detect a custom implementation itself. - * - * @param config - * @param parser - * @param source - * @return the bean name of the custom implementation or {@code null} if - * none available - */ - private String registerCustomImplementation(T config, ParserContext parser, - Object source) { - - String beanName = config.getImplementationBeanName(); - - // Already a bean configured? - if (parser.getRegistry().containsBeanDefinition(beanName)) { - return beanName; - } - - // Autodetect implementation - if (config.autodetectCustomImplementation()) { - - AbstractBeanDefinition beanDefinition = - detectCustomImplementation(config, parser); - - if (null == beanDefinition) { - return null; - } - - if (LOG.isDebugEnabled()) { - LOG.debug("Registering custom repository implementation: " + - config.getImplementationBeanName() + " " + - beanDefinition.getBeanClassName()); - } - - beanDefinition.setSource(source); - parser.registerBeanComponent(new BeanComponentDefinition( - beanDefinition, beanName)); - - } else { - beanName = config.getCustomImplementationRef(); - } - - return beanName; - } - - - /** - * Tries to detect a custom implementation for a repository bean by - * classpath scanning. - * - * @param config - * @param parser - * @return the {@code AbstractBeanDefinition} of the custom implementation - * or {@literal null} if none found - */ - private AbstractBeanDefinition detectCustomImplementation(T config, - ParserContext parser) { - - // Build pattern to lookup implementation class - Pattern pattern = - Pattern.compile(".*" + config.getImplementationClassName()); - - // Build classpath scanner and lookup bean definition - ClassPathScanningCandidateComponentProvider provider = - new ClassPathScanningCandidateComponentProvider(false); - provider.setResourceLoader(parser.getReaderContext() - .getResourceLoader()); - provider.addIncludeFilter(new RegexPatternTypeFilter(pattern)); - Set definitions = - provider.findCandidateComponents(config.getBasePackage()); - - return (0 == definitions.size() ? null - : (AbstractBeanDefinition) definitions.iterator().next()); - } - - - /** - * Callback to register additional bean definitions for a - * {@literal repositories} root node. This usually includes beans you have - * to set up once independently of the number of repositories to be created. - * Will be called before any repositories bean definitions have been - * registered. - * - * @param registry - * @param source - */ - protected void registerBeansForRoot(BeanDefinitionRegistry registry, - Object source) { - - AbstractBeanDefinition definition = - BeanDefinitionBuilder.rootBeanDefinition( - REPOSITORY_INTERFACE_POST_PROCESSOR) - .getBeanDefinition(); - - registerWithSourceAndGeneratedBeanName(registry, definition, source); - } - - - /** - * Returns whether the given {@link BeanDefinitionRegistry} already contains - * a bean of the given type assuming the bean name has been autogenerated. - * - * @param type - * @param registry - * @return - */ - protected static boolean hasBean(Class type, - BeanDefinitionRegistry registry) { - - String name = - String.format("%s%s0", type.getName(), - GENERATED_BEAN_NAME_SEPARATOR); - return registry.containsBeanDefinition(name); - } - - - /** - * Sets the given source on the given {@link AbstractBeanDefinition} and - * registers it inside the given {@link BeanDefinitionRegistry}. - * - * @param registry - * @param bean - * @param source - * @return - */ - protected static String registerWithSourceAndGeneratedBeanName( - BeanDefinitionRegistry registry, AbstractBeanDefinition bean, - Object source) { - - bean.setSource(source); - - String beanName = generateBeanName(bean, registry); - registry.registerBeanDefinition(beanName, bean); - - return beanName; - } - - /** - * Custom {@link ClassPathScanningCandidateComponentProvider} scanning for - * interfaces extending the given base interface. Skips interfaces annotated - * with {@link NoRepositoryBean}. - * - * @author Oliver Gierke - */ - static class RepositoryComponentProvider extends - ClassPathScanningCandidateComponentProvider { - - /** - * Creates a new {@link RepositoryComponentProvider}. - * - * @param repositoryInterface the interface to scan for - */ - public RepositoryComponentProvider(Class repositoryInterface) { - - super(false); - addIncludeFilter(new InterfaceTypeFilter(repositoryInterface)); - addExcludeFilter(new AnnotationTypeFilter(NoRepositoryBean.class)); - } - - - /* - * (non-Javadoc) - * - * @seeorg.springframework.context.annotation. - * ClassPathScanningCandidateComponentProvider - * #isCandidateComponent(org.springframework - * .beans.factory.annotation.AnnotatedBeanDefinition) - */ - @Override - protected boolean isCandidateComponent( - AnnotatedBeanDefinition beanDefinition) { - - boolean isNonRepositoryInterface = - !isGenericRepositoryInterface(beanDefinition - .getBeanClassName()); - boolean isTopLevelType = - !beanDefinition.getMetadata().hasEnclosingClass(); - - return isNonRepositoryInterface && isTopLevelType; - } - - /** - * {@link org.springframework.core.type.filter.TypeFilter} that only - * matches interfaces. Thus setting this up makes only sense providing - * an interface type as {@code targetType}. - * - * @author Oliver Gierke - */ - private static class InterfaceTypeFilter extends AssignableTypeFilter { - - /** - * Creates a new {@link InterfaceTypeFilter}. - * - * @param targetType - */ - public InterfaceTypeFilter(Class targetType) { - - super(targetType); - } - - - /* - * (non-Javadoc) - * - * @seeorg.springframework.core.type.filter. - * AbstractTypeHierarchyTraversingFilter - * #match(org.springframework.core.type.classreading.MetadataReader, - * org.springframework.core.type.classreading.MetadataReaderFactory) - */ - @Override - public boolean match(MetadataReader metadataReader, - MetadataReaderFactory metadataReaderFactory) - throws IOException { - - return metadataReader.getClassMetadata().isInterface() - && super.match(metadataReader, metadataReaderFactory); - } - } - } + /** + * Proceeds manual configuration by traversing the context's + * {@link SingleRepositoryConfigInformation}s. + * + * @param context + * @param parser + */ + private void doManualConfiguration(S context, ParserContext parser) { + + LOG.debug("Triggering manual repository detection"); + + for (T repositoryContext : context + .getSingleRepositoryConfigInformations()) { + registerGenericRepositoryFactoryBean(parser, repositoryContext); + } + } + + + private void handleError(Exception e, Element source, ReaderContext reader) { + + reader.error(e.getMessage(), reader.extractSource(source), e.getCause()); + } + + + /** + * Registers a generic repository factory bean for a bean with the given + * name and the provided configuration context. + * + * @param parser + * @param name + * @param context + */ + private void registerGenericRepositoryFactoryBean(ParserContext parser, + T context) { + + try { + + Object beanSource = parser.extractSource(context.getSource()); + + BeanDefinitionBuilder builder = + BeanDefinitionBuilder.rootBeanDefinition(context + .getRepositoryFactoryBeanClassName()); + + builder.addPropertyValue("repositoryInterface", + context.getInterfaceName()); + builder.addPropertyValue("queryLookupStrategyKey", + context.getQueryLookupStrategyKey()); + + String transactionManagerRef = context.getTransactionManagerRef(); + + if (StringUtils.hasText(transactionManagerRef)) { + builder.addPropertyValue("transactionManager", + transactionManagerRef); + } + + String customImplementationBeanName = + registerCustomImplementation(context, parser, beanSource); + + if (customImplementationBeanName != null) { + builder.addPropertyReference("customImplementation", + customImplementationBeanName); + } + + postProcessBeanDefinition(context, builder, parser.getRegistry(), beanSource); + + AbstractBeanDefinition beanDefinition = builder.getBeanDefinition(); + beanDefinition.setSource(beanSource); + + if (LOG.isDebugEnabled()) { + LOG.debug( + "Registering repository: " + context.getBeanId() + + " - Interface: " + context.getInterfaceName() + + " - Factory: " + context.getRepositoryFactoryBeanClassName() + + ", - Custom implementation: " + customImplementationBeanName); + } + + BeanComponentDefinition definition = + new BeanComponentDefinition(beanDefinition, + context.getBeanId()); + parser.registerBeanComponent(definition); + } catch (RuntimeException e) { + handleError(e, context.getSource(), parser.getReaderContext()); + } + } + + + /** + * Callback to post process a repository bean definition prior to actual + * registration. + * + * @param context + * @param builder + * @param beanSource + */ + protected void postProcessBeanDefinition(T context, + BeanDefinitionBuilder builder, BeanDefinitionRegistry registry, Object beanSource) { + + } + + + /** + * Registers a possibly available custom repository implementation on the + * repository bean. Tries to find an already registered bean to reference or + * tries to detect a custom implementation itself. + * + * @param config + * @param parser + * @param source + * @return the bean name of the custom implementation or {@code null} if + * none available + */ + private String registerCustomImplementation(T config, ParserContext parser, + Object source) { + + String beanName = config.getImplementationBeanName(); + + // Already a bean configured? + if (parser.getRegistry().containsBeanDefinition(beanName)) { + return beanName; + } + + // Autodetect implementation + if (config.autodetectCustomImplementation()) { + + AbstractBeanDefinition beanDefinition = + detectCustomImplementation(config, parser); + + if (null == beanDefinition) { + return null; + } + + if (LOG.isDebugEnabled()) { + LOG.debug("Registering custom repository implementation: " + + config.getImplementationBeanName() + " " + + beanDefinition.getBeanClassName()); + } + + beanDefinition.setSource(source); + parser.registerBeanComponent(new BeanComponentDefinition( + beanDefinition, beanName)); + + } else { + beanName = config.getCustomImplementationRef(); + } + + return beanName; + } + + + /** + * Tries to detect a custom implementation for a repository bean by + * classpath scanning. + * + * @param config + * @param parser + * @return the {@code AbstractBeanDefinition} of the custom implementation + * or {@literal null} if none found + */ + private AbstractBeanDefinition detectCustomImplementation(T config, + ParserContext parser) { + + // Build pattern to lookup implementation class + Pattern pattern = + Pattern.compile(".*" + config.getImplementationClassName()); + + // Build classpath scanner and lookup bean definition + ClassPathScanningCandidateComponentProvider provider = + new ClassPathScanningCandidateComponentProvider(false); + provider.setResourceLoader(parser.getReaderContext() + .getResourceLoader()); + provider.addIncludeFilter(new RegexPatternTypeFilter(pattern)); + Set definitions = + provider.findCandidateComponents(config.getBasePackage()); + + return (0 == definitions.size() ? null + : (AbstractBeanDefinition) definitions.iterator().next()); + } + + + /** + * Callback to register additional bean definitions for a + * {@literal repositories} root node. This usually includes beans you have + * to set up once independently of the number of repositories to be created. + * Will be called before any repositories bean definitions have been + * registered. + * + * @param registry + * @param source + */ + protected void registerBeansForRoot(BeanDefinitionRegistry registry, + Object source) { + + AbstractBeanDefinition definition = + BeanDefinitionBuilder.rootBeanDefinition( + REPOSITORY_INTERFACE_POST_PROCESSOR) + .getBeanDefinition(); + + registerWithSourceAndGeneratedBeanName(registry, definition, source); + } + + + /** + * Returns whether the given {@link BeanDefinitionRegistry} already contains + * a bean of the given type assuming the bean name has been autogenerated. + * + * @param type + * @param registry + * @return + */ + protected static boolean hasBean(Class type, + BeanDefinitionRegistry registry) { + + String name = + String.format("%s%s0", type.getName(), + GENERATED_BEAN_NAME_SEPARATOR); + return registry.containsBeanDefinition(name); + } + + + /** + * Sets the given source on the given {@link AbstractBeanDefinition} and + * registers it inside the given {@link BeanDefinitionRegistry}. + * + * @param registry + * @param bean + * @param source + * @return + */ + protected static String registerWithSourceAndGeneratedBeanName( + BeanDefinitionRegistry registry, AbstractBeanDefinition bean, + Object source) { + + bean.setSource(source); + + String beanName = generateBeanName(bean, registry); + registry.registerBeanDefinition(beanName, bean); + + return beanName; + } + + /** + * Custom {@link ClassPathScanningCandidateComponentProvider} scanning for + * interfaces extending the given base interface. Skips interfaces annotated + * with {@link NoRepositoryBean}. + * + * @author Oliver Gierke + */ + static class RepositoryComponentProvider extends + ClassPathScanningCandidateComponentProvider { + + /** + * Creates a new {@link RepositoryComponentProvider}. + * + * @param repositoryInterface the interface to scan for + */ + public RepositoryComponentProvider(Class repositoryInterface) { + + super(false); + addIncludeFilter(new InterfaceTypeFilter(repositoryInterface)); + addExcludeFilter(new AnnotationTypeFilter(NoRepositoryBean.class)); + } + + + /* + * (non-Javadoc) + * + * @seeorg.springframework.context.annotation. + * ClassPathScanningCandidateComponentProvider + * #isCandidateComponent(org.springframework + * .beans.factory.annotation.AnnotatedBeanDefinition) + */ + @Override + protected boolean isCandidateComponent( + AnnotatedBeanDefinition beanDefinition) { + + boolean isNonRepositoryInterface = + !isGenericRepositoryInterface(beanDefinition + .getBeanClassName()); + boolean isTopLevelType = + !beanDefinition.getMetadata().hasEnclosingClass(); + + return isNonRepositoryInterface && isTopLevelType; + } + + /** + * {@link org.springframework.core.type.filter.TypeFilter} that only + * matches interfaces. Thus setting this up makes only sense providing + * an interface type as {@code targetType}. + * + * @author Oliver Gierke + */ + private static class InterfaceTypeFilter extends AssignableTypeFilter { + + /** + * Creates a new {@link InterfaceTypeFilter}. + * + * @param targetType + */ + public InterfaceTypeFilter(Class targetType) { + + super(targetType); + } + + + /* + * (non-Javadoc) + * + * @seeorg.springframework.core.type.filter. + * AbstractTypeHierarchyTraversingFilter + * #match(org.springframework.core.type.classreading.MetadataReader, + * org.springframework.core.type.classreading.MetadataReaderFactory) + */ + @Override + public boolean match(MetadataReader metadataReader, + MetadataReaderFactory metadataReaderFactory) + throws IOException { + + return metadataReader.getClassMetadata().isInterface() + && super.match(metadataReader, metadataReaderFactory); + } + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AutomaticRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AutomaticRepositoryConfigInformation.java index 16e9b4981..fb045edd9 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AutomaticRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/AutomaticRepositoryConfigInformation.java @@ -25,52 +25,52 @@ import org.springframework.util.Assert; * A {@link SingleRepositoryConfigInformation} implementation that is not backed * by an XML element but by a scanned interface. As this is derived from the * parent, most of the lookup logic is delegated to the parent as well. - * + * * @author Oliver Gierke */ public class AutomaticRepositoryConfigInformation - extends ParentDelegatingRepositoryConfigInformation { + extends ParentDelegatingRepositoryConfigInformation { - private final String interfaceName; + private final String interfaceName; - /** - * Creates a new {@link AutomaticRepositoryConfigInformation} for the given - * interface name and {@link CommonRepositoryConfigInformation} parent. - * - * @param interfaceName - * @param parent - */ - public AutomaticRepositoryConfigInformation(String interfaceName, S parent) { + /** + * Creates a new {@link AutomaticRepositoryConfigInformation} for the given + * interface name and {@link CommonRepositoryConfigInformation} parent. + * + * @param interfaceName + * @param parent + */ + public AutomaticRepositoryConfigInformation(String interfaceName, S parent) { - super(parent); - Assert.notNull(interfaceName); - this.interfaceName = interfaceName; - } + super(parent); + Assert.notNull(interfaceName); + this.interfaceName = interfaceName; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.SingleRepositoryConfigInformation - * #getBeanId() - */ - public String getBeanId() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.SingleRepositoryConfigInformation + * #getBeanId() + */ + public String getBeanId() { - return uncapitalize(getShortName(interfaceName)); - } + return uncapitalize(getShortName(interfaceName)); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.SingleRepositoryConfigInformation - * #getInterfaceName() - */ - public String getInterfaceName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.SingleRepositoryConfigInformation + * #getInterfaceName() + */ + public String getInterfaceName() { - return interfaceName; - } + return interfaceName; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/CommonRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/CommonRepositoryConfigInformation.java index 42dcfeb3a..d4d5356e0 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/CommonRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/CommonRepositoryConfigInformation.java @@ -22,59 +22,59 @@ import org.w3c.dom.Element; /** * Interface for shared repository information. - * + * * @author Oliver Gierke */ public interface CommonRepositoryConfigInformation { - /** - * Returns the element the repository information is derived from. - * - * @return - */ - Element getSource(); + /** + * Returns the element the repository information is derived from. + * + * @return + */ + Element getSource(); - /** - * Returns the base package. - * - * @return - */ - String getBasePackage(); + /** + * Returns the base package. + * + * @return + */ + String getBasePackage(); - /** - * Returns the suffix to use for implementation bean lookup or class - * detection. - * - * @return - */ - String getRepositoryImplementationSuffix(); + /** + * Returns the suffix to use for implementation bean lookup or class + * detection. + * + * @return + */ + String getRepositoryImplementationSuffix(); - /** - * Returns the configured repository factory class. - * - * @return - */ - String getRepositoryFactoryBeanClassName(); + /** + * Returns the configured repository factory class. + * + * @return + */ + String getRepositoryFactoryBeanClassName(); - /** - * Returns the bean name of the {@link PlatformTransactionManager} to be - * used. Returns {@literal null} if no reference has been configured - * explicitly. - * - * @return - */ - String getTransactionManagerRef(); + /** + * Returns the bean name of the {@link PlatformTransactionManager} to be + * used. Returns {@literal null} if no reference has been configured + * explicitly. + * + * @return + */ + String getTransactionManagerRef(); - /** - * Returns the strategy finder methods should be resolved. - * - * @return - */ - Key getQueryLookupStrategyKey(); + /** + * Returns the strategy finder methods should be resolved. + * + * @return + */ + Key getQueryLookupStrategyKey(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/GlobalRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/GlobalRepositoryConfigInformation.java index 646210537..6ad473832 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/GlobalRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/GlobalRepositoryConfigInformation.java @@ -19,40 +19,40 @@ package org.springframework.data.repository.config; * @author Oliver Gierke */ public interface GlobalRepositoryConfigInformation> - extends CommonRepositoryConfigInformation { + extends CommonRepositoryConfigInformation { - /** - * Returns the - * - * @param interfaceName - * @return - */ - T getAutoconfigRepositoryInformation(String interfaceName); + /** + * Returns the + * + * @param interfaceName + * @return + */ + T getAutoconfigRepositoryInformation(String interfaceName); - /** - * Returns all {@link SingleRepositoryConfigInformation} instances used for - * manual configuration. - * - * @return - */ - Iterable getSingleRepositoryConfigInformations(); + /** + * Returns all {@link SingleRepositoryConfigInformation} instances used for + * manual configuration. + * + * @return + */ + Iterable getSingleRepositoryConfigInformations(); - /** - * Returns whether to consider manual configuration. If this returns true, - * clients should use {@link #getSingleRepositoryConfigInformations()} to - * lookup configuration information for individual repository beans. - * - * @return - */ - boolean configureManually(); + /** + * Returns whether to consider manual configuration. If this returns true, + * clients should use {@link #getSingleRepositoryConfigInformations()} to + * lookup configuration information for individual repository beans. + * + * @return + */ + boolean configureManually(); - /** - * Returns the base interface to use - * - * @return - */ - Class getRepositoryBaseInterface(); + /** + * Returns the base interface to use + * + * @return + */ + Class getRepositoryBaseInterface(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ManualRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ManualRepositoryConfigInformation.java index 26e540632..0221504d7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ManualRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ManualRepositoryConfigInformation.java @@ -23,165 +23,165 @@ import org.w3c.dom.Element; /** * Configuration information for manual repository configuration. - * + * * @author Oliver Gierke */ public class ManualRepositoryConfigInformation - extends ParentDelegatingRepositoryConfigInformation { + extends ParentDelegatingRepositoryConfigInformation { - private static final String CUSTOM_IMPL_REF = "custom-impl-ref"; + private static final String CUSTOM_IMPL_REF = "custom-impl-ref"; - private Element element; + private Element element; - /** - * @param parent - */ - public ManualRepositoryConfigInformation(Element element, T parent) { + /** + * @param parent + */ + public ManualRepositoryConfigInformation(Element element, T parent) { - super(parent); - this.element = element; - } + super(parent); + this.element = element; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getBeanName() - */ - public String getBeanId() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getBeanName() + */ + public String getBeanId() { - return element.getAttribute("id"); - } + return element.getAttribute("id"); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getInterfaceName() - */ - public String getInterfaceName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getInterfaceName() + */ + public String getInterfaceName() { - return getBasePackage() + "." + capitalize(getBeanId()); - } + return getBasePackage() + "." + capitalize(getBeanId()); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getCustomImplementationRef() - */ - @Override - public String getCustomImplementationRef() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getCustomImplementationRef() + */ + @Override + public String getCustomImplementationRef() { - return element.getAttribute(CUSTOM_IMPL_REF); - } + return element.getAttribute(CUSTOM_IMPL_REF); + } - /** - * Returns if a custom implementation shall be autodetected. - * - * @return - */ - @Override - public boolean autodetectCustomImplementation() { + /** + * Returns if a custom implementation shall be autodetected. + * + * @return + */ + @Override + public boolean autodetectCustomImplementation() { - return !hasText(getCustomImplementationRef()); - } + return !hasText(getCustomImplementationRef()); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.AbstractRepositoryInformation - * #getRepositoryImplementationSuffix() - */ - @Override - public String getRepositoryImplementationSuffix() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.AbstractRepositoryInformation + * #getRepositoryImplementationSuffix() + */ + @Override + public String getRepositoryImplementationSuffix() { - String value = - element.getAttribute(RepositoryConfig.REPOSITORY_IMPL_POSTFIX); - return hasText(value) ? value : getParent() - .getRepositoryImplementationSuffix(); - } + String value = + element.getAttribute(RepositoryConfig.REPOSITORY_IMPL_POSTFIX); + return hasText(value) ? value : getParent() + .getRepositoryImplementationSuffix(); + } - @Override - public String getTransactionManagerRef() { + @Override + public String getTransactionManagerRef() { - return getAttribute(RepositoryConfig.TRANSACTION_MANAGER_REF); - } + return getAttribute(RepositoryConfig.TRANSACTION_MANAGER_REF); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.CommonRepositoryInformation - * #getSource() - */ - @Override - public Element getSource() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.CommonRepositoryInformation + * #getSource() + */ + @Override + public Element getSource() { - return element; - } + return element; + } - /** - * Returns the attribute of the current context. If it's not set the method - * will fall back to the parent's source. - * - * @param attribute - * @return - */ - protected String getAttribute(String attribute) { + /** + * Returns the attribute of the current context. If it's not set the method + * will fall back to the parent's source. + * + * @param attribute + * @return + */ + protected String getAttribute(String attribute) { - String value = getSource().getAttribute(attribute); + String value = getSource().getAttribute(attribute); - if (hasText(value)) { - return value; - } + if (hasText(value)) { + return value; + } - value = getParent().getSource().getAttribute(attribute); + value = getParent().getSource().getAttribute(attribute); - return hasText(value) ? value : null; - } + return hasText(value) ? value : null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.CommonRepositoryInformation - * #getRepositoryFactoryClassName() - */ - @Override - public String getRepositoryFactoryBeanClassName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.CommonRepositoryInformation + * #getRepositoryFactoryClassName() + */ + @Override + public String getRepositoryFactoryBeanClassName() { - String value = - element.getAttribute(RepositoryConfig.REPOSITORY_FACTORY_CLASS_NAME); - return hasText(value) ? value : getParent() - .getRepositoryFactoryBeanClassName(); - } + String value = + element.getAttribute(RepositoryConfig.REPOSITORY_FACTORY_CLASS_NAME); + return hasText(value) ? value : getParent() + .getRepositoryFactoryBeanClassName(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.CommonRepositoryInformation - * #getQueryLookupStrategyKey() - */ - @Override - public Key getQueryLookupStrategyKey() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.CommonRepositoryInformation + * #getQueryLookupStrategyKey() + */ + @Override + public Key getQueryLookupStrategyKey() { - return Key.create(getAttribute(RepositoryConfig.QUERY_LOOKUP_STRATEGY)); - } + return Key.create(getAttribute(RepositoryConfig.QUERY_LOOKUP_STRATEGY)); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ParentDelegatingRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ParentDelegatingRepositoryConfigInformation.java index 5ecdd985c..2de789255 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ParentDelegatingRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/ParentDelegatingRepositoryConfigInformation.java @@ -26,167 +26,167 @@ import org.w3c.dom.Element; * Base class for {@link SingleRepositoryConfigInformation} implementations. So * these implementations will capture information for XML elements manually * configuring a single repository bean. - * + * * @author Oliver Gierke */ public abstract class ParentDelegatingRepositoryConfigInformation - implements SingleRepositoryConfigInformation { + implements SingleRepositoryConfigInformation { - private final T parent; + private final T parent; - /** - * Creates a new {@link ParentDelegatingRepositoryConfigInformation} with - * the given {@link CommonRepositoryConfigInformation} as parent. - * - * @param parent - */ - public ParentDelegatingRepositoryConfigInformation(T parent) { + /** + * Creates a new {@link ParentDelegatingRepositoryConfigInformation} with + * the given {@link CommonRepositoryConfigInformation} as parent. + * + * @param parent + */ + public ParentDelegatingRepositoryConfigInformation(T parent) { - Assert.notNull(parent); - this.parent = parent; - } + Assert.notNull(parent); + this.parent = parent; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getParent() - */ - protected T getParent() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getParent() + */ + protected T getParent() { - return parent; - } + return parent; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.CommonRepositoryInformation - * #getBasePackage() - */ - public String getBasePackage() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.CommonRepositoryInformation + * #getBasePackage() + */ + public String getBasePackage() { - return parent.getBasePackage(); - } + return parent.getBasePackage(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getImplementationClassName() - */ - public String getImplementationClassName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getImplementationClassName() + */ + public String getImplementationClassName() { - return capitalize(getBeanId()) + getRepositoryImplementationSuffix(); - } + return capitalize(getBeanId()) + getRepositoryImplementationSuffix(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getImplementationBeanName() - */ - public String getImplementationBeanName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getImplementationBeanName() + */ + public String getImplementationBeanName() { - return getBeanId() + getRepositoryImplementationSuffix(); - } + return getBeanId() + getRepositoryImplementationSuffix(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * autodetectCustomImplementation() - */ - public boolean autodetectCustomImplementation() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * autodetectCustomImplementation() + */ + public boolean autodetectCustomImplementation() { - return true; - } + return true; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getCustomImplementationRef() - */ - public String getCustomImplementationRef() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getCustomImplementationRef() + */ + public String getCustomImplementationRef() { - return getBeanId() + getRepositoryImplementationSuffix(); - } + return getBeanId() + getRepositoryImplementationSuffix(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.CommonRepositoryInformation - * #getSource() - */ - public Element getSource() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.CommonRepositoryInformation + * #getSource() + */ + public Element getSource() { - return parent.getSource(); - } + return parent.getSource(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getRepositoryImplementationSuffix() - */ - public String getRepositoryImplementationSuffix() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getRepositoryImplementationSuffix() + */ + public String getRepositoryImplementationSuffix() { - return parent.getRepositoryImplementationSuffix(); - } + return parent.getRepositoryImplementationSuffix(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getRepositoryFactoryBeanClassName() - */ - public String getRepositoryFactoryBeanClassName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getRepositoryFactoryBeanClassName() + */ + public String getRepositoryFactoryBeanClassName() { - return parent.getRepositoryFactoryBeanClassName(); - } + return parent.getRepositoryFactoryBeanClassName(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getTransactionManagerRef() - */ - public String getTransactionManagerRef() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getTransactionManagerRef() + */ + public String getTransactionManagerRef() { - return parent.getTransactionManagerRef(); - } + return parent.getTransactionManagerRef(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.RepositoryInformation# - * getQueryLookupStrategyKey() - */ - public Key getQueryLookupStrategyKey() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.RepositoryInformation# + * getQueryLookupStrategyKey() + */ + public Key getQueryLookupStrategyKey() { - return parent.getQueryLookupStrategyKey(); - } + return parent.getQueryLookupStrategyKey(); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryConfig.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryConfig.java index 25c8387c3..96f657d3a 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryConfig.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/RepositoryConfig.java @@ -31,191 +31,191 @@ import org.w3c.dom.NodeList; * Class defining access to the repository configuration abstracting the content * of the {@code repositories} element in XML namespcae configuration. Defines * default values to populate resulting repository beans with. - * + * * @author Oliver Gierke */ public abstract class RepositoryConfig, S extends CommonRepositoryConfigInformation> - implements GlobalRepositoryConfigInformation { + implements GlobalRepositoryConfigInformation { - public static final String DEFAULT_REPOSITORY_IMPL_POSTFIX = "Impl"; - public static final String QUERY_LOOKUP_STRATEGY = "query-lookup-strategy"; - public static final String BASE_PACKAGE = "base-package"; - public static final String REPOSITORY_IMPL_POSTFIX = - "repository-impl-postfix"; - public static final String REPOSITORY_FACTORY_CLASS_NAME = "factory-class"; - public static final String TRANSACTION_MANAGER_REF = - "transaction-manager-ref"; + public static final String DEFAULT_REPOSITORY_IMPL_POSTFIX = "Impl"; + public static final String QUERY_LOOKUP_STRATEGY = "query-lookup-strategy"; + public static final String BASE_PACKAGE = "base-package"; + public static final String REPOSITORY_IMPL_POSTFIX = + "repository-impl-postfix"; + public static final String REPOSITORY_FACTORY_CLASS_NAME = "factory-class"; + public static final String TRANSACTION_MANAGER_REF = + "transaction-manager-ref"; - private final Element element; - private final String defaultRepositoryFactoryBeanClassName; + private final Element element; + private final String defaultRepositoryFactoryBeanClassName; - /** - * Creates an instance of {@code RepositoryConfig}. - * - * @param repositoriesElement - */ - protected RepositoryConfig(Element repositoriesElement, - String defaultRepositoryFactoryBeanClassName) { + /** + * Creates an instance of {@code RepositoryConfig}. + * + * @param repositoriesElement + */ + protected RepositoryConfig(Element repositoriesElement, + String defaultRepositoryFactoryBeanClassName) { - Assert.notNull(repositoriesElement, "Element must not be null!"); - Assert.notNull(defaultRepositoryFactoryBeanClassName, - "Default repository factory bean class name must not be null!"); + Assert.notNull(repositoriesElement, "Element must not be null!"); + Assert.notNull(defaultRepositoryFactoryBeanClassName, + "Default repository factory bean class name must not be null!"); - this.element = repositoriesElement; - this.defaultRepositoryFactoryBeanClassName = - defaultRepositoryFactoryBeanClassName; + this.element = repositoriesElement; + this.defaultRepositoryFactoryBeanClassName = + defaultRepositoryFactoryBeanClassName; - } + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getSource() - */ - public Element getSource() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getSource() + */ + public Element getSource() { - return element; - } + return element; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.GlobalRepositoryConfigInformation - * #configureManually() - */ - public boolean configureManually() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.GlobalRepositoryConfigInformation + * #configureManually() + */ + public boolean configureManually() { - return getRepositoryElements().size() > 0; - } + return getRepositoryElements().size() > 0; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getQueryLookupStrategyKey() - */ - public Key getQueryLookupStrategyKey() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getQueryLookupStrategyKey() + */ + public Key getQueryLookupStrategyKey() { - String createFinderQueries = - element.getAttribute(QUERY_LOOKUP_STRATEGY); + String createFinderQueries = + element.getAttribute(QUERY_LOOKUP_STRATEGY); - return StringUtils.hasText(createFinderQueries) ? Key - .create(createFinderQueries) : null; - } + return StringUtils.hasText(createFinderQueries) ? Key + .create(createFinderQueries) : null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getBasePackage() - */ - public String getBasePackage() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getBasePackage() + */ + public String getBasePackage() { - return element.getAttribute(BASE_PACKAGE); - } + return element.getAttribute(BASE_PACKAGE); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getRepositoryFactoryClassName() - */ - public String getRepositoryFactoryBeanClassName() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getRepositoryFactoryClassName() + */ + public String getRepositoryFactoryBeanClassName() { - String factoryClassName = - getSource().getAttribute(REPOSITORY_FACTORY_CLASS_NAME); - return StringUtils.hasText(factoryClassName) ? factoryClassName - : defaultRepositoryFactoryBeanClassName; - } + String factoryClassName = + getSource().getAttribute(REPOSITORY_FACTORY_CLASS_NAME); + return StringUtils.hasText(factoryClassName) ? factoryClassName + : defaultRepositoryFactoryBeanClassName; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getRepositoryImplementationSuffix() - */ - public String getRepositoryImplementationSuffix() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getRepositoryImplementationSuffix() + */ + public String getRepositoryImplementationSuffix() { - String postfix = element.getAttribute(REPOSITORY_IMPL_POSTFIX); - return StringUtils.hasText(postfix) ? postfix - : DEFAULT_REPOSITORY_IMPL_POSTFIX; - } + String postfix = element.getAttribute(REPOSITORY_IMPL_POSTFIX); + return StringUtils.hasText(postfix) ? postfix + : DEFAULT_REPOSITORY_IMPL_POSTFIX; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.config.CommonRepositoryConfigInformation - * #getTransactionManagerRef() - */ - public String getTransactionManagerRef() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.config.CommonRepositoryConfigInformation + * #getTransactionManagerRef() + */ + public String getTransactionManagerRef() { - String ref = element.getAttribute(TRANSACTION_MANAGER_REF); - return StringUtils.hasText(ref) ? ref : null; - } + String ref = element.getAttribute(TRANSACTION_MANAGER_REF); + return StringUtils.hasText(ref) ? ref : null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.jpa.repository.config.GlobalRepositoryInformation - * #getManualRepositoryInformations() - */ - public Iterable getSingleRepositoryConfigInformations() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.jpa.repository.config.GlobalRepositoryInformation + * #getManualRepositoryInformations() + */ + public Iterable getSingleRepositoryConfigInformations() { - Set infos = new HashSet(); - for (Element repositoryElement : getRepositoryElements()) { - infos.add(createSingleRepositoryConfigInformationFor(repositoryElement)); - } + Set infos = new HashSet(); + for (Element repositoryElement : getRepositoryElements()) { + infos.add(createSingleRepositoryConfigInformationFor(repositoryElement)); + } - return infos; - } + return infos; + } - private Collection getRepositoryElements() { + private Collection getRepositoryElements() { - NodeList nodes = element.getChildNodes(); - Set result = new HashSet(); + NodeList nodes = element.getChildNodes(); + Set result = new HashSet(); - for (int i = 0; i < nodes.getLength(); i++) { + for (int i = 0; i < nodes.getLength(); i++) { - Node node = nodes.item(i); + Node node = nodes.item(i); - boolean isElement = Node.ELEMENT_NODE == node.getNodeType(); - boolean isRepository = "repository".equals(node.getLocalName()); + boolean isElement = Node.ELEMENT_NODE == node.getNodeType(); + boolean isRepository = "repository".equals(node.getLocalName()); - if (isElement && isRepository) { - result.add((Element) node); - } - } + if (isElement && isRepository) { + result.add((Element) node); + } + } - return result; - } + return result; + } - /** - * Creates a {@link SingleRepositoryConfigInformation} for the given - * {@link Element}. - * - * @param element - * @return - */ - protected abstract T createSingleRepositoryConfigInformationFor( - Element element); + /** + * Creates a {@link SingleRepositoryConfigInformation} for the given + * {@link Element}. + * + * @param element + * @return + */ + protected abstract T createSingleRepositoryConfigInformationFor( + Element element); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/SingleRepositoryConfigInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/SingleRepositoryConfigInformation.java index 42a0cd668..8bb093272 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/SingleRepositoryConfigInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/SingleRepositoryConfigInformation.java @@ -18,58 +18,58 @@ package org.springframework.data.repository.config; /** * Interface to capture configuration information necessary to set up a single * repository instance. - * + * * @author Oliver Gierke */ public interface SingleRepositoryConfigInformation - extends CommonRepositoryConfigInformation { + extends CommonRepositoryConfigInformation { - /** - * Returns the bean name to be used for the repository. - * - * @return - */ - String getBeanId(); + /** + * Returns the bean name to be used for the repository. + * + * @return + */ + String getBeanId(); - /** - * Returns the name of the repository interface. - * - * @return - */ - String getInterfaceName(); + /** + * Returns the name of the repository interface. + * + * @return + */ + String getInterfaceName(); - /** - * Returns the class name of a possible custom repository implementation - * class to detect. - * - * @return - */ - String getImplementationClassName(); + /** + * Returns the class name of a possible custom repository implementation + * class to detect. + * + * @return + */ + String getImplementationClassName(); - /** - * Returns the bean name a possibly found custom implementation shall be - * registered under. - * - * @return - */ - String getImplementationBeanName(); + /** + * Returns the bean name a possibly found custom implementation shall be + * registered under. + * + * @return + */ + String getImplementationBeanName(); - /** - * Returns the bean reference to the custom repository implementation. - * - * @return - */ - String getCustomImplementationRef(); + /** + * Returns the bean reference to the custom repository implementation. + * + * @return + */ + String getCustomImplementationRef(); - /** - * Returns whether to try to autodetect a custom implementation. - * - * @return - */ - boolean autodetectCustomImplementation(); + /** + * Returns whether to try to autodetect a custom implementation. + * + * @return + */ + boolean autodetectCustomImplementation(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/TypeFilterParser.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/TypeFilterParser.java index 43171957a..80b6aa6da 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/TypeFilterParser.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/config/TypeFilterParser.java @@ -36,251 +36,244 @@ import org.w3c.dom.NodeList; * Parser to populate the given * {@link ClassPathScanningCandidateComponentProvider} with {@link TypeFilter}s * parsed from the given {@link Element}'s children. - * + * * @author Oliver Gierke */ class TypeFilterParser { - private static final String FILTER_TYPE_ATTRIBUTE = "type"; - private static final String FILTER_EXPRESSION_ATTRIBUTE = "expression"; + private static final String FILTER_TYPE_ATTRIBUTE = "type"; + private static final String FILTER_EXPRESSION_ATTRIBUTE = "expression"; - private final ClassLoader classLoader; - private final ReaderContext readerContext; + private final ClassLoader classLoader; + private final ReaderContext readerContext; - /** - * Creates a new {@link TypeFilterParser} with the given {@link ClassLoader} - * and {@link ReaderContext}. - * - * @param classLoader - * @param readerContext - */ - public TypeFilterParser(ClassLoader classLoader, ReaderContext readerContext) { + /** + * Creates a new {@link TypeFilterParser} with the given {@link ClassLoader} + * and {@link ReaderContext}. + * + * @param classLoader + * @param readerContext + */ + public TypeFilterParser(ClassLoader classLoader, ReaderContext readerContext) { - this.classLoader = classLoader; - this.readerContext = readerContext; - } + this.classLoader = classLoader; + this.readerContext = readerContext; + } - /** - * Parses include and exclude filters form the given {@link Element}'s child - * elements and populates the given - * {@link ClassPathScanningCandidateComponentProvider} with the according - * {@link TypeFilter}s. - * - * @param element - * @param scanner - */ - public void parseFilters(Element element, - ClassPathScanningCandidateComponentProvider scanner) { + /** + * Parses include and exclude filters form the given {@link Element}'s child + * elements and populates the given + * {@link ClassPathScanningCandidateComponentProvider} with the according + * {@link TypeFilter}s. + * + * @param element + * @param scanner + */ + public void parseFilters(Element element, + ClassPathScanningCandidateComponentProvider scanner) { - parseTypeFilters(element, scanner, Type.INCLUDE); - parseTypeFilters(element, scanner, Type.EXCLUDE); - } + parseTypeFilters(element, scanner, Type.INCLUDE); + parseTypeFilters(element, scanner, Type.EXCLUDE); + } - private void parseTypeFilters(Element element, - ClassPathScanningCandidateComponentProvider scanner, Type type) { + private void parseTypeFilters(Element element, + ClassPathScanningCandidateComponentProvider scanner, Type type) { - NodeList nodeList = element.getChildNodes(); - for (int i = 0; i < nodeList.getLength(); i++) { - Node node = nodeList.item(i); + NodeList nodeList = element.getChildNodes(); + for (int i = 0; i < nodeList.getLength(); i++) { + Node node = nodeList.item(i); - Element childElement = type.getElement(node); + Element childElement = type.getElement(node); - if (childElement != null) { + if (childElement != null) { - try { + try { - type.addFilter( - createTypeFilter((Element) node, classLoader), - scanner); + type.addFilter( + createTypeFilter((Element) node, classLoader), + scanner); - } catch (RuntimeException e) { - readerContext.error(e.getMessage(), - readerContext.extractSource(element), e.getCause()); - } - } - } - } + } catch (RuntimeException e) { + readerContext.error(e.getMessage(), + readerContext.extractSource(element), e.getCause()); + } + } + } + } - protected TypeFilter createTypeFilter(Element element, - ClassLoader classLoader) { + protected TypeFilter createTypeFilter(Element element, + ClassLoader classLoader) { - String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); - String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); + String filterType = element.getAttribute(FILTER_TYPE_ATTRIBUTE); + String expression = element.getAttribute(FILTER_EXPRESSION_ATTRIBUTE); - try { + try { - FilterType filter = FilterType.fromString(filterType); - return filter.getFilter(expression, classLoader); + FilterType filter = FilterType.fromString(filterType); + return filter.getFilter(expression, classLoader); - } catch (ClassNotFoundException ex) { - throw new FatalBeanException("Type filter class not found: " - + expression, ex); - } - } + } catch (ClassNotFoundException ex) { + throw new FatalBeanException("Type filter class not found: " + + expression, ex); + } + } - /** - * Enum representing all the filter types available for {@code include} and - * {@code exclude} elements. This acts as factory for {@link TypeFilter} - * instances. - * - * @see #getFilter(String, ClassLoader) - * @author Oliver Gierke - */ - private static enum FilterType { + /** + * Enum representing all the filter types available for {@code include} and + * {@code exclude} elements. This acts as factory for {@link TypeFilter} + * instances. + * + * @author Oliver Gierke + * @see #getFilter(String, ClassLoader) + */ + private static enum FilterType { - ANNOTATION { + ANNOTATION { + @Override + @SuppressWarnings("unchecked") + public TypeFilter getFilter(String expression, + ClassLoader classLoader) throws ClassNotFoundException { - @Override - @SuppressWarnings("unchecked") - public TypeFilter getFilter(String expression, - ClassLoader classLoader) throws ClassNotFoundException { + return new AnnotationTypeFilter( + (Class) classLoader.loadClass(expression)); + } + }, - return new AnnotationTypeFilter( - (Class) classLoader.loadClass(expression)); - } - }, + ASSIGNABLE { + @Override + public TypeFilter getFilter(String expression, + ClassLoader classLoader) throws ClassNotFoundException { - ASSIGNABLE { + return new AssignableTypeFilter( + classLoader.loadClass(expression)); + } - @Override - public TypeFilter getFilter(String expression, - ClassLoader classLoader) throws ClassNotFoundException { + }, - return new AssignableTypeFilter( - classLoader.loadClass(expression)); - } + ASPECTJ { + @Override + public TypeFilter getFilter(String expression, + ClassLoader classLoader) { - }, + return new AspectJTypeFilter(expression, classLoader); + } - ASPECTJ { + }, - @Override - public TypeFilter getFilter(String expression, - ClassLoader classLoader) { + REGEX { + @Override + public TypeFilter getFilter(String expression, + ClassLoader classLoader) { - return new AspectJTypeFilter(expression, classLoader); - } + return new RegexPatternTypeFilter(Pattern.compile(expression)); + } - }, + }, - REGEX { + CUSTOM { + @Override + public TypeFilter getFilter(String expression, + ClassLoader classLoader) throws ClassNotFoundException { - @Override - public TypeFilter getFilter(String expression, - ClassLoader classLoader) { + Class filterClass = classLoader.loadClass(expression); + if (!TypeFilter.class.isAssignableFrom(filterClass)) { + throw new IllegalArgumentException( + "Class is not assignable to [" + + TypeFilter.class.getName() + "]: " + + expression); + } + return (TypeFilter) BeanUtils.instantiateClass(filterClass); + } + }; - return new RegexPatternTypeFilter(Pattern.compile(expression)); - } - - }, - - CUSTOM { - - @Override - public TypeFilter getFilter(String expression, - ClassLoader classLoader) throws ClassNotFoundException { - - Class filterClass = classLoader.loadClass(expression); - if (!TypeFilter.class.isAssignableFrom(filterClass)) { - throw new IllegalArgumentException( - "Class is not assignable to [" - + TypeFilter.class.getName() + "]: " - + expression); - } - return (TypeFilter) BeanUtils.instantiateClass(filterClass); - } - }; - - /** - * Returns the {@link TypeFilter} for the given expression and - * {@link ClassLoader}. - * - * @param expression - * @param classLoader - * @return - * @throws ClassNotFoundException - */ - abstract TypeFilter getFilter(String expression, ClassLoader classLoader) - throws ClassNotFoundException; + /** + * Returns the {@link TypeFilter} for the given expression and + * {@link ClassLoader}. + * + * @param expression + * @param classLoader + * @return + * @throws ClassNotFoundException + */ + abstract TypeFilter getFilter(String expression, ClassLoader classLoader) + throws ClassNotFoundException; - /** - * Returns the {@link FilterType} for the given type as {@link String}. - * - * @param typeString - * @return - * @throws IllegalArgumentException if no {@link FilterType} could be - * found for the given argument. - */ - static FilterType fromString(String typeString) { + /** + * Returns the {@link FilterType} for the given type as {@link String}. + * + * @param typeString + * @return + * @throws IllegalArgumentException if no {@link FilterType} could be + * found for the given argument. + */ + static FilterType fromString(String typeString) { - for (FilterType filter : FilterType.values()) { - if (filter.name().equalsIgnoreCase(typeString)) { - return filter; - } - } + for (FilterType filter : FilterType.values()) { + if (filter.name().equalsIgnoreCase(typeString)) { + return filter; + } + } - throw new IllegalArgumentException("Unsupported filter type: " - + typeString); - } - } + throw new IllegalArgumentException("Unsupported filter type: " + + typeString); + } + } - private static enum Type { + private static enum Type { - INCLUDE("include-filter") { + INCLUDE("include-filter") { + @Override + public void addFilter(TypeFilter filter, + ClassPathScanningCandidateComponentProvider scanner) { - @Override - public void addFilter(TypeFilter filter, - ClassPathScanningCandidateComponentProvider scanner) { + scanner.addIncludeFilter(filter); + } - scanner.addIncludeFilter(filter); - } + }, + EXCLUDE("exclude-filter") { + @Override + public void addFilter(TypeFilter filter, + ClassPathScanningCandidateComponentProvider scanner) { - }, - EXCLUDE("exclude-filter") { + scanner.addExcludeFilter(filter); + } + }; - @Override - public void addFilter(TypeFilter filter, - ClassPathScanningCandidateComponentProvider scanner) { - - scanner.addExcludeFilter(filter); - } - }; - - private String elementName; + private String elementName; - private Type(String elementName) { + private Type(String elementName) { - this.elementName = elementName; - } + this.elementName = elementName; + } - /** - * Returns the {@link Element} if the given {@link Node} is an - * {@link Element} and it's name equals the one of the type. - * - * @param node - * @return - */ - Element getElement(Node node) { + /** + * Returns the {@link Element} if the given {@link Node} is an + * {@link Element} and it's name equals the one of the type. + * + * @param node + * @return + */ + Element getElement(Node node) { - if (node.getNodeType() == Node.ELEMENT_NODE) { - String localName = node.getLocalName(); - if (elementName.equals(localName)) { - return (Element) node; - } - } + if (node.getNodeType() == Node.ELEMENT_NODE) { + String localName = node.getLocalName(); + if (elementName.equals(localName)) { + return (Element) node; + } + } - return null; - } + return null; + } - abstract void addFilter(TypeFilter filter, - ClassPathScanningCandidateComponentProvider scanner); - } + abstract void addFilter(TypeFilter filter, + ClassPathScanningCandidateComponentProvider scanner); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Param.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Param.java index ab3fa4bcf..a8c0a5848 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Param.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Param.java @@ -25,7 +25,7 @@ import java.lang.annotation.Target; /** * Annotation to bind let method parameters be bound to a query via a named * parameter. - * + * * @author Oliver Gierke */ @Target(ElementType.PARAMETER) @@ -33,5 +33,5 @@ import java.lang.annotation.Target; @Documented public @interface Param { - String value(); + String value(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameter.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameter.java index 337572e1e..d0c23256e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameter.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameter.java @@ -29,198 +29,198 @@ import org.springframework.util.Assert; /** * Class to abstract a single parameter of a query method. It is held in the * context of a {@link Parameters} instance. - * + * * @author Oliver Gierke */ public final class Parameter { - @SuppressWarnings("unchecked") - static final List> TYPES = Arrays.asList(Pageable.class, - Sort.class); + @SuppressWarnings("unchecked") + static final List> TYPES = Arrays.asList(Pageable.class, + Sort.class); - private static final String PARAM_ON_SPECIAL = format( - "You must not user @%s on a parameter typed %s or %s", - Param.class.getSimpleName(), Pageable.class.getSimpleName(), - Sort.class.getSimpleName()); + private static final String PARAM_ON_SPECIAL = format( + "You must not user @%s on a parameter typed %s or %s", + Param.class.getSimpleName(), Pageable.class.getSimpleName(), + Sort.class.getSimpleName()); - private static final String NAMED_PARAMETER_TEMPLATE = ":%s"; - private static final String POSITION_PARAMETER_TEMPLATE = "?%s"; + private static final String NAMED_PARAMETER_TEMPLATE = ":%s"; + private static final String POSITION_PARAMETER_TEMPLATE = "?%s"; - private final Class type; - private final Parameters parameters; - private final int index; - private final String name; + private final Class type; + private final Parameters parameters; + private final int index; + private final String name; - /** - * Creates a new {@link Parameter} for the given type, {@link Annotation}s, - * positioned at the given index inside the given {@link Parameters}. - * - * @param type - * @param parameters - * @param index - * @param name - */ - Parameter(Class type, Parameters parameters, int index, String name) { + /** + * Creates a new {@link Parameter} for the given type, {@link Annotation}s, + * positioned at the given index inside the given {@link Parameters}. + * + * @param type + * @param parameters + * @param index + * @param name + */ + Parameter(Class type, Parameters parameters, int index, String name) { - Assert.notNull(type); - Assert.notNull(parameters); + Assert.notNull(type); + Assert.notNull(parameters); - this.parameters = parameters; - this.index = index; + this.parameters = parameters; + this.index = index; - this.type = type; - this.name = name; + this.type = type; + this.name = name; - if (isSpecialParameter() && isNamedParameter()) { - throw new IllegalArgumentException(PARAM_ON_SPECIAL); - } - } + if (isSpecialParameter() && isNamedParameter()) { + throw new IllegalArgumentException(PARAM_ON_SPECIAL); + } + } - /** - * Returns whether the {@link Parameter} is the first one. - * - * @return - */ - boolean isFirst() { + /** + * Returns whether the {@link Parameter} is the first one. + * + * @return + */ + boolean isFirst() { - return index == 0; - } + return index == 0; + } - /** - * Returns the next {@link Parameter} from the surrounding - * {@link Parameters}. - * - * @throws ParameterOutOfBoundsException - * @return - */ - public Parameter getNext() { + /** + * Returns the next {@link Parameter} from the surrounding + * {@link Parameters}. + * + * @return + * @throws ParameterOutOfBoundsException + */ + public Parameter getNext() { - return parameters.getParameter(index + 1); - } + return parameters.getParameter(index + 1); + } - /** - * Returns the previous {@link Parameter}. - * - * @return - */ - Parameter getPrevious() { + /** + * Returns the previous {@link Parameter}. + * + * @return + */ + Parameter getPrevious() { - return parameters.getParameter(index - 1); - } + return parameters.getParameter(index - 1); + } - /** - * Returns whether the parameter is a special parameter. - * - * @see #TYPES - * @param index - * @return - */ - public boolean isSpecialParameter() { + /** + * Returns whether the parameter is a special parameter. + * + * @param index + * @return + * @see #TYPES + */ + public boolean isSpecialParameter() { - return TYPES.contains(type); - } + return TYPES.contains(type); + } - /** - * Returns whether the {@link Parameter} is to be bound to a query. - * - * @return - */ - public boolean isBindable() { + /** + * Returns whether the {@link Parameter} is to be bound to a query. + * + * @return + */ + public boolean isBindable() { - return !isSpecialParameter(); - } + return !isSpecialParameter(); + } - /** - * Returns the placeholder to be used for the parameter. Can either be a - * named one or positional. - * - * @param index - * @return - */ - public String getPlaceholder() { + /** + * Returns the placeholder to be used for the parameter. Can either be a + * named one or positional. + * + * @param index + * @return + */ + public String getPlaceholder() { - if (isNamedParameter()) { - return format(NAMED_PARAMETER_TEMPLATE, getName()); - } else { - return format(POSITION_PARAMETER_TEMPLATE, getIndex()); - } - } + if (isNamedParameter()) { + return format(NAMED_PARAMETER_TEMPLATE, getName()); + } else { + return format(POSITION_PARAMETER_TEMPLATE, getIndex()); + } + } - /** - * Returns the position index the parameter is bound to in the context of - * its surrounding {@link Parameters}. - * - * @return - */ - public int getIndex() { + /** + * Returns the position index the parameter is bound to in the context of + * its surrounding {@link Parameters}. + * + * @return + */ + public int getIndex() { - return index; - } + return index; + } - /** - * Returns whether the parameter is annotated with {@link Param}. - * - * @param index - * @return - */ - public boolean isNamedParameter() { + /** + * Returns whether the parameter is annotated with {@link Param}. + * + * @param index + * @return + */ + public boolean isNamedParameter() { - return !isSpecialParameter() && getName() != null; - } + return !isSpecialParameter() && getName() != null; + } - /** - * Returns the name of the parameter (through {@link Param} annotation) or - * null if none can be found. - * - * @return - */ - public String getName() { + /** + * Returns the name of the parameter (through {@link Param} annotation) or + * null if none can be found. + * + * @return + */ + public String getName() { - return name; - } + return name; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return format("%s:%s", isNamedParameter() ? getName() : "#" + index, - type.getName()); - } + return format("%s:%s", isNamedParameter() ? getName() : "#" + index, + type.getName()); + } - /** - * Returns whether the {@link Parameter} is a {@link Pageable} parameter. - * - * @return - */ - boolean isPageable() { + /** + * Returns whether the {@link Parameter} is a {@link Pageable} parameter. + * + * @return + */ + boolean isPageable() { - return Pageable.class.isAssignableFrom(type); - } + return Pageable.class.isAssignableFrom(type); + } - /** - * Returns whether the {@link Parameter} is a {@link Sort} parameter. - * - * @return - */ - boolean isSort() { + /** + * Returns whether the {@link Parameter} is a {@link Sort} parameter. + * + * @return + */ + boolean isSort() { - return Sort.class.isAssignableFrom(type); - } + return Sort.class.isAssignableFrom(type); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterAccessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterAccessor.java index a15c175b6..bb46f977c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterAccessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterAccessor.java @@ -24,50 +24,50 @@ import org.springframework.data.domain.Sort; /** * Interface to access method parameters. Allows dedicated access to parameters * of special types - * + * * @author Oliver Gierke */ public interface ParameterAccessor extends Iterable { - /** - * Returns the {@link Pageable} of the parameters, if available. Returns - * {@code null} otherwise. - * - * @return - */ - Pageable getPageable(); + /** + * Returns the {@link Pageable} of the parameters, if available. Returns + * {@code null} otherwise. + * + * @return + */ + Pageable getPageable(); - /** - * Returns the sort instance to be used for query creation. Will use a - * {@link Sort} parameter if available or the {@link Sort} contained in a - * {@link Pageable} if available. Returns {@code null} if no {@link Sort} - * can be found. - * - * @return - */ - Sort getSort(); + /** + * Returns the sort instance to be used for query creation. Will use a + * {@link Sort} parameter if available or the {@link Sort} contained in a + * {@link Pageable} if available. Returns {@code null} if no {@link Sort} + * can be found. + * + * @return + */ + Sort getSort(); - /** - * Returns the bindable value with the given index. Bindable means, that - * {@link Pageable} and {@link Sort} values are skipped without noticed in - * the index. For a method signature taking {@link String}, {@link Pageable} - * , {@link String}, {@code #getBindableParameter(1)} would return the - * second {@link String} value. - * - * @param index - * @return - */ - Object getBindableValue(int index); + /** + * Returns the bindable value with the given index. Bindable means, that + * {@link Pageable} and {@link Sort} values are skipped without noticed in + * the index. For a method signature taking {@link String}, {@link Pageable} + * , {@link String}, {@code #getBindableParameter(1)} would return the + * second {@link String} value. + * + * @param index + * @return + */ + Object getBindableValue(int index); - /** - * Returns an iterator over all bindable parameters. This means - * parameters implementing {@link Pageable} or {@link Sort} will not be - * included in this {@link Iterator}. - * - * @return - */ - Iterator iterator(); + /** + * Returns an iterator over all bindable parameters. This means + * parameters implementing {@link Pageable} or {@link Sort} will not be + * included in this {@link Iterator}. + * + * @return + */ + Iterator iterator(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterOutOfBoundsException.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterOutOfBoundsException.java index 94a2680fc..485209cd0 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterOutOfBoundsException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParameterOutOfBoundsException.java @@ -18,22 +18,22 @@ package org.springframework.data.repository.query; /** * Exception to be thrown when trying to access a {@link Parameter} with an * invalid index inside a {@link Parameters} instance. - * + * * @author Oliver Gierke */ public class ParameterOutOfBoundsException extends RuntimeException { - private static final long serialVersionUID = 8433209953653278886L; + private static final long serialVersionUID = 8433209953653278886L; - /** - * Creates a new {@link ParameterOutOfBoundsException} with the given - * exception as cause. - * - * @param cause - */ - public ParameterOutOfBoundsException(Throwable cause) { + /** + * Creates a new {@link ParameterOutOfBoundsException} with the given + * exception as cause. + * + * @param cause + */ + public ParameterOutOfBoundsException(Throwable cause) { - super(cause); - } + super(cause); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameters.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameters.java index 71f6e9f57..39bfa4e67 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameters.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/Parameters.java @@ -32,293 +32,293 @@ import org.springframework.util.Assert; /** * Abstracts method parameters that have to be bound to query parameters or * applied to the query independently. - * + * * @author Oliver Gierke */ public final class Parameters implements Iterable { - @SuppressWarnings("unchecked") - public static final List> TYPES = Arrays.asList(Pageable.class, - Sort.class); + @SuppressWarnings("unchecked") + public static final List> TYPES = Arrays.asList(Pageable.class, + Sort.class); - private static final String ALL_OR_NOTHING = - String.format( - "Either use @%s " - + "on all parameters except %s and %s typed once, or none at all!", - Param.class.getSimpleName(), - Pageable.class.getSimpleName(), Sort.class.getSimpleName()); + private static final String ALL_OR_NOTHING = + String.format( + "Either use @%s " + + "on all parameters except %s and %s typed once, or none at all!", + Param.class.getSimpleName(), + Pageable.class.getSimpleName(), Sort.class.getSimpleName()); - private final int pageableIndex; - private final int sortIndex; + private final int pageableIndex; + private final int sortIndex; - private final List parameters; - private final ParameterNameDiscoverer discoverer = - new LocalVariableTableParameterNameDiscoverer(); + private final List parameters; + private final ParameterNameDiscoverer discoverer = + new LocalVariableTableParameterNameDiscoverer(); - /** - * Creates a new instance of {@link Parameters}. - * - * @param method - */ - public Parameters(Method method) { + /** + * Creates a new instance of {@link Parameters}. + * + * @param method + */ + public Parameters(Method method) { - Assert.notNull(method); + Assert.notNull(method); - this.parameters = new ArrayList(); + this.parameters = new ArrayList(); - List> types = Arrays.asList(method.getParameterTypes()); + List> types = Arrays.asList(method.getParameterTypes()); - for (int i = 0; i < types.size(); i++) { - String name = getParameterName(method, i); - parameters.add(new Parameter(types.get(i), this, i, name)); - } + for (int i = 0; i < types.size(); i++) { + String name = getParameterName(method, i); + parameters.add(new Parameter(types.get(i), this, i, name)); + } - this.pageableIndex = types.indexOf(Pageable.class); - this.sortIndex = types.indexOf(Sort.class); + this.pageableIndex = types.indexOf(Pageable.class); + this.sortIndex = types.indexOf(Sort.class); - assertEitherAllParamAnnotatedOrNone(); - } + assertEitherAllParamAnnotatedOrNone(); + } - /** - * Returns the name of the parameter of the given {@link Method} with the - * given index. Inspects {@link Param} annotation before falling back to a - * {@link ParameterNameDiscoverer}. - * - * @param method - * @param index - * @return - */ - private String getParameterName(Method method, int index) { + /** + * Returns the name of the parameter of the given {@link Method} with the + * given index. Inspects {@link Param} annotation before falling back to a + * {@link ParameterNameDiscoverer}. + * + * @param method + * @param index + * @return + */ + private String getParameterName(Method method, int index) { - for (Annotation annotation : method.getParameterAnnotations()[index]) { - if (annotation instanceof Param) { - return ((Param) annotation).value(); - } - } + for (Annotation annotation : method.getParameterAnnotations()[index]) { + if (annotation instanceof Param) { + return ((Param) annotation).value(); + } + } - String[] parameterNames = discoverer.getParameterNames(method); + String[] parameterNames = discoverer.getParameterNames(method); - if (parameterNames != null) { - return parameterNames[index]; - } + if (parameterNames != null) { + return parameterNames[index]; + } - return null; - } + return null; + } - /** - * Creates a new {@link Parameters} instance with the given - * {@link Parameter}s put into new context. - * - * @param originals - */ - private Parameters(List originals) { + /** + * Creates a new {@link Parameters} instance with the given + * {@link Parameter}s put into new context. + * + * @param originals + */ + private Parameters(List originals) { - this.parameters = new ArrayList(); + this.parameters = new ArrayList(); - int pageableIndexTemp = -1; - int sortIndexTemp = -1; - - for (int i = 0; i < originals.size(); i++) { - - Parameter original = originals.get(i); - this.parameters.add(original); - - pageableIndexTemp = original.isPageable() ? i : -1; - sortIndexTemp = original.isSort() ? i : -1; - } + int pageableIndexTemp = -1; + int sortIndexTemp = -1; + + for (int i = 0; i < originals.size(); i++) { + + Parameter original = originals.get(i); + this.parameters.add(original); + + pageableIndexTemp = original.isPageable() ? i : -1; + sortIndexTemp = original.isSort() ? i : -1; + } - this.pageableIndex = pageableIndexTemp; - this.sortIndex = sortIndexTemp; - } - - - /** - * Returns whether the method the {@link Parameters} was created for - * contains a {@link Pageable} argument. - * - * @return - */ - public boolean hasPageableParameter() { + this.pageableIndex = pageableIndexTemp; + this.sortIndex = sortIndexTemp; + } + + + /** + * Returns whether the method the {@link Parameters} was created for + * contains a {@link Pageable} argument. + * + * @return + */ + public boolean hasPageableParameter() { - return pageableIndex != -1; - } + return pageableIndex != -1; + } - /** - * Returns the index of the {@link Pageable} {@link Method} parameter if - * available. Will return {@literal -1} if there is no {@link Pageable} - * argument in the {@link Method}'s parameter list. - * - * @return the pageableIndex - */ - public int getPageableIndex() { + /** + * Returns the index of the {@link Pageable} {@link Method} parameter if + * available. Will return {@literal -1} if there is no {@link Pageable} + * argument in the {@link Method}'s parameter list. + * + * @return the pageableIndex + */ + public int getPageableIndex() { - return pageableIndex; - } + return pageableIndex; + } - /** - * Returns the index of the {@link Sort} {@link Method} parameter if - * available. Will return {@literal -1} if there is no {@link Sort} argument - * in the {@link Method}'s parameter list. - * - * @return - */ - public int getSortIndex() { - - return sortIndex; - } - - - /** - * Returns whether the method the {@link Parameters} was created for - * contains a {@link Sort} argument. - * - * @return - */ - public boolean hasSortParameter() { - - return sortIndex != -1; - } - - - /** - * Returns the parameter with the given index. - * - * @param index - * @return - */ - public Parameter getParameter(int index) { - - try { - return parameters.get(index); - } catch (IndexOutOfBoundsException e) { - throw new ParameterOutOfBoundsException(e); - } - } + /** + * Returns the index of the {@link Sort} {@link Method} parameter if + * available. Will return {@literal -1} if there is no {@link Sort} argument + * in the {@link Method}'s parameter list. + * + * @return + */ + public int getSortIndex() { + + return sortIndex; + } + + + /** + * Returns whether the method the {@link Parameters} was created for + * contains a {@link Sort} argument. + * + * @return + */ + public boolean hasSortParameter() { + + return sortIndex != -1; + } + + + /** + * Returns the parameter with the given index. + * + * @param index + * @return + */ + public Parameter getParameter(int index) { + + try { + return parameters.get(index); + } catch (IndexOutOfBoundsException e) { + throw new ParameterOutOfBoundsException(e); + } + } - - /** - * Returns whether we have a parameter at the given position. - * - * @param position - * @return - */ - public boolean hasParameterAt(int position) { - - try { - return null != getParameter(position); - } catch (ParameterOutOfBoundsException e) { - return false; - } - } - - - /** - * Returns whether the method signature contains one of the special - * parameters ({@link Pageable}, {@link Sort}). - * - * @return - */ - public boolean hasSpecialParameter() { - - return hasSortParameter() || hasPageableParameter(); - } - - - /** - * Returns the number of parameters. - * - * @return - */ - public int getNumberOfParameters() { - - return parameters.size(); - } - - - /** - * Returns a {@link Parameters} instance with effectively all special - * parameters removed. - * - * @see Parameter#TYPES - * @see Parameter#isSpecialParameter() - * @return - */ - public Parameters getBindableParameters() { - - List bindables = new ArrayList(); - - for (Parameter candidate : this) { - - if (candidate.isBindable()) { - bindables.add(candidate); - } - } - - return new Parameters(bindables); - } - - - /** - * Returns a bindable parameter with the given index. So for a method with a - * signature of {@code (Pageable pageable, String name)} a call to - * {@code #getBindableParameter(0)} will return the {@link String} - * parameter. - * - * @param bindableIndex - * @return - */ - public Parameter getBindableParameter(int bindableIndex) { - - return getBindableParameters().getParameter(bindableIndex); - } - - - /** - * Asserts that either all of the non special parameters ({@link Pageable}, - * {@link Sort}) are annotated with {@link Param} or none of them is. - * - * @param method - */ - private void assertEitherAllParamAnnotatedOrNone() { - - boolean nameFound = false; - - for (Parameter parameter : this.getBindableParameters()) { - - if (parameter.isNamedParameter()) { - Assert.isTrue(nameFound || parameter.isFirst(), ALL_OR_NOTHING); - nameFound = true; - } else { - Assert.isTrue(!nameFound, ALL_OR_NOTHING); - } - } - } - - - /** - * Returns whether the given type is a bindable parameter. - * - * @param type - * @return - */ - public static boolean isBindable(Class type) { - - return !TYPES.contains(type); - } - - - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { - - return parameters.iterator(); - } + + /** + * Returns whether we have a parameter at the given position. + * + * @param position + * @return + */ + public boolean hasParameterAt(int position) { + + try { + return null != getParameter(position); + } catch (ParameterOutOfBoundsException e) { + return false; + } + } + + + /** + * Returns whether the method signature contains one of the special + * parameters ({@link Pageable}, {@link Sort}). + * + * @return + */ + public boolean hasSpecialParameter() { + + return hasSortParameter() || hasPageableParameter(); + } + + + /** + * Returns the number of parameters. + * + * @return + */ + public int getNumberOfParameters() { + + return parameters.size(); + } + + + /** + * Returns a {@link Parameters} instance with effectively all special + * parameters removed. + * + * @return + * @see Parameter#TYPES + * @see Parameter#isSpecialParameter() + */ + public Parameters getBindableParameters() { + + List bindables = new ArrayList(); + + for (Parameter candidate : this) { + + if (candidate.isBindable()) { + bindables.add(candidate); + } + } + + return new Parameters(bindables); + } + + + /** + * Returns a bindable parameter with the given index. So for a method with a + * signature of {@code (Pageable pageable, String name)} a call to + * {@code #getBindableParameter(0)} will return the {@link String} + * parameter. + * + * @param bindableIndex + * @return + */ + public Parameter getBindableParameter(int bindableIndex) { + + return getBindableParameters().getParameter(bindableIndex); + } + + + /** + * Asserts that either all of the non special parameters ({@link Pageable}, + * {@link Sort}) are annotated with {@link Param} or none of them is. + * + * @param method + */ + private void assertEitherAllParamAnnotatedOrNone() { + + boolean nameFound = false; + + for (Parameter parameter : this.getBindableParameters()) { + + if (parameter.isNamedParameter()) { + Assert.isTrue(nameFound || parameter.isFirst(), ALL_OR_NOTHING); + nameFound = true; + } else { + Assert.isTrue(!nameFound, ALL_OR_NOTHING); + } + } + } + + + /** + * Returns whether the given type is a bindable parameter. + * + * @param type + * @return + */ + public static boolean isBindable(Class type) { + + return !TYPES.contains(type); + } + + + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + public Iterator iterator() { + + return parameters.iterator(); + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java index 47c38be4f..758a625a6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/ParametersParameterAccessor.java @@ -25,135 +25,135 @@ import org.springframework.util.Assert; /** * {@link ParameterAccessor} implementation using a {@link Parameters} instance * to find special parameters. - * + * * @author Oliver Gierke */ public class ParametersParameterAccessor implements ParameterAccessor { - private final Parameters parameters; - private final Object[] values; + private final Parameters parameters; + private final Object[] values; - /** - * Creates a new {@link ParametersParameterAccessor}. - * - * @param parameters - * @param values - */ - public ParametersParameterAccessor(Parameters parameters, Object[] values) { + /** + * Creates a new {@link ParametersParameterAccessor}. + * + * @param parameters + * @param values + */ + public ParametersParameterAccessor(Parameters parameters, Object[] values) { - Assert.notNull(parameters); - Assert.notNull(values); + Assert.notNull(parameters); + Assert.notNull(values); - Assert.isTrue(parameters.getNumberOfParameters() == values.length, - "Invalid number of parameters given!"); + Assert.isTrue(parameters.getNumberOfParameters() == values.length, + "Invalid number of parameters given!"); - this.parameters = parameters; - this.values = values.clone(); - } + this.parameters = parameters; + this.values = values.clone(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.query.ParameterAccessor#getPageable() - */ - public Pageable getPageable() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.query.ParameterAccessor#getPageable() + */ + public Pageable getPageable() { - if (!parameters.hasPageableParameter()) { - return null; - } + if (!parameters.hasPageableParameter()) { + return null; + } - return (Pageable) values[parameters.getPageableIndex()]; - } + return (Pageable) values[parameters.getPageableIndex()]; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.query.ParameterAccessor#getSort() - */ - public Sort getSort() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.query.ParameterAccessor#getSort() + */ + public Sort getSort() { - if (parameters.hasSortParameter()) { - return (Sort) values[parameters.getSortIndex()]; - } + if (parameters.hasSortParameter()) { + return (Sort) values[parameters.getSortIndex()]; + } - if (parameters.hasPageableParameter() && getPageable() != null) { - return getPageable().getSort(); - } + if (parameters.hasPageableParameter() && getPageable() != null) { + return getPageable().getSort(); + } - return null; - } + return null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.query.ParameterAccessor#getBindableValue - * (int) - */ - public Object getBindableValue(int index) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.query.ParameterAccessor#getBindableValue + * (int) + */ + public Object getBindableValue(int index) { - return values[parameters.getBindableParameter(index).getIndex()]; - } + return values[parameters.getBindableParameter(index).getIndex()]; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.query.ParameterAccessor#iterator() - */ - public BindableParameterIterator iterator() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.query.ParameterAccessor#iterator() + */ + public BindableParameterIterator iterator() { - return new BindableParameterIterator(); - } + return new BindableParameterIterator(); + } - /** - * Iterator class to allow traversing all bindable parameters inside the - * accessor. - * - * @author Oliver Gierke - */ - private class BindableParameterIterator implements Iterator { + /** + * Iterator class to allow traversing all bindable parameters inside the + * accessor. + * + * @author Oliver Gierke + */ + private class BindableParameterIterator implements Iterator { - private int currentIndex = 0; + private int currentIndex = 0; - /** - * Returns the next bindable parameter. - * - * @return - */ - public Object next() { + /** + * Returns the next bindable parameter. + * + * @return + */ + public Object next() { - return getBindableValue(currentIndex++); - } + return getBindableValue(currentIndex++); + } - /* - * (non-Javadoc) - * - * @see java.util.Iterator#hasNext() - */ - public boolean hasNext() { + /* + * (non-Javadoc) + * + * @see java.util.Iterator#hasNext() + */ + public boolean hasNext() { - return values.length <= currentIndex; - } + return values.length <= currentIndex; + } - /* - * (non-Javadoc) - * - * @see java.util.Iterator#remove() - */ - public void remove() { + /* + * (non-Javadoc) + * + * @see java.util.Iterator#remove() + */ + public void remove() { - throw new UnsupportedOperationException(); - } - } + throw new UnsupportedOperationException(); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryCreationException.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryCreationException.java index 552bc52c5..92ff845c8 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryCreationException.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryCreationException.java @@ -18,68 +18,68 @@ package org.springframework.data.repository.query; /** * Exception to be thrown if a query cannot be created from a * {@link QueryMethod}. - * + * * @author Oliver Gierke */ public final class QueryCreationException extends RuntimeException { - private static final long serialVersionUID = -1238456123580L; - private static final String MESSAGE_TEMPLATE = - "Could not create query for method %s! Could not find property %s on domain class %s."; + private static final long serialVersionUID = -1238456123580L; + private static final String MESSAGE_TEMPLATE = + "Could not create query for method %s! Could not find property %s on domain class %s."; - /** - * Creates a new {@link QueryCreationException}. - * - * @param method - */ - private QueryCreationException(String message) { + /** + * Creates a new {@link QueryCreationException}. + * + * @param method + */ + private QueryCreationException(String message) { - super(message); - } + super(message); + } - /** - * Rejects the given domain class property. - * - * @param method - * @param propertyName - * @return - */ - public static QueryCreationException invalidProperty(QueryMethod method, - String propertyName) { + /** + * Rejects the given domain class property. + * + * @param method + * @param propertyName + * @return + */ + public static QueryCreationException invalidProperty(QueryMethod method, + String propertyName) { - return new QueryCreationException(String.format(MESSAGE_TEMPLATE, - method, propertyName, method.getDomainClass().getName())); - } + return new QueryCreationException(String.format(MESSAGE_TEMPLATE, + method, propertyName, method.getDomainClass().getName())); + } - /** - * Creates a new {@link QueryCreationException}. - * - * @param method - * @param message - * @return - */ - public static QueryCreationException create(QueryMethod method, - String message) { + /** + * Creates a new {@link QueryCreationException}. + * + * @param method + * @param message + * @return + */ + public static QueryCreationException create(QueryMethod method, + String message) { - return new QueryCreationException(String.format( - "Could not create query for %s! Reason: %s", method, message)); - } + return new QueryCreationException(String.format( + "Could not create query for %s! Reason: %s", method, message)); + } - /** - * Creates a new {@link QueryCreationException} for the given - * {@link QueryMethod} and {@link Throwable} as cause. - * - * @param method - * @param cause - * @return - */ - public static QueryCreationException create(QueryMethod method, - Throwable cause) { + /** + * Creates a new {@link QueryCreationException} for the given + * {@link QueryMethod} and {@link Throwable} as cause. + * + * @param method + * @param cause + * @return + */ + public static QueryCreationException create(QueryMethod method, + Throwable cause) { - return create(method, cause.getMessage()); - } + return create(method, cause.getMessage()); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryLookupStrategy.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryLookupStrategy.java index 61fe4d540..63b9a9cbc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryLookupStrategy.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryLookupStrategy.java @@ -24,39 +24,39 @@ import org.springframework.util.StringUtils; /** * Strategy interface for which way to lookup {@link RepositoryQuery}s. - * + * * @author Oliver Gierke */ public interface QueryLookupStrategy { - public static enum Key { + public static enum Key { - CREATE, USE_DECLARED_QUERY, CREATE_IF_NOT_FOUND; + CREATE, USE_DECLARED_QUERY, CREATE_IF_NOT_FOUND; - /** - * Returns a strategy key from the given XML value. - * - * @param xml - * @return a strategy key from the given XML value - */ - public static Key create(String xml) { + /** + * Returns a strategy key from the given XML value. + * + * @param xml + * @return a strategy key from the given XML value + */ + public static Key create(String xml) { - if (!StringUtils.hasText(xml)) { - return null; - } + if (!StringUtils.hasText(xml)) { + return null; + } - return valueOf(xml.toUpperCase(Locale.US).replace("-", "_")); - } - } + return valueOf(xml.toUpperCase(Locale.US).replace("-", "_")); + } + } - /** - * Resolves a {@link RepositoryQuery} from the given {@link QueryMethod} - * that can be executed afterwards. - * - * @param method - * @param metadata - * @return - */ - RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata); + /** + * Resolves a {@link RepositoryQuery} from the given {@link QueryMethod} + * that can be executed afterwards. + * + * @param method + * @param metadata + * @return + */ + RepositoryQuery resolveQuery(Method method, RepositoryMetadata metadata); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java index 3c06c2a22..7941dfbcc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/QueryMethod.java @@ -33,159 +33,159 @@ import org.springframework.util.Assert; * Abstraction of a method that is designated to execute a finder query. * Enriches the standard {@link Method} interface with specific information that * is necessary to construct {@link RepositoryQuery}s for the method. - * + * * @author Oliver Gierke */ public class QueryMethod { - public static enum Type { + public static enum Type { - SINGLE_ENTITY, PAGING, COLLECTION, MODIFYING; - } + SINGLE_ENTITY, PAGING, COLLECTION, MODIFYING; + } - private final RepositoryMetadata metadata; - private final Method method; - private final Parameters parameters; + private final RepositoryMetadata metadata; + private final Method method; + private final Parameters parameters; - /** - * Creates a new {@link QueryMethod} from the given parameters. Looks up the - * correct query to use for following invocations of the method given. - * - * @param method must not be {@literal null} - */ - public QueryMethod(Method method, RepositoryMetadata metadata) { + /** + * Creates a new {@link QueryMethod} from the given parameters. Looks up the + * correct query to use for following invocations of the method given. + * + * @param method must not be {@literal null} + */ + public QueryMethod(Method method, RepositoryMetadata metadata) { - Assert.notNull(method, "Method must not be null!"); + Assert.notNull(method, "Method must not be null!"); - for (Class type : Parameters.TYPES) { - if (getNumberOfOccurences(method, type) > 1) { - throw new IllegalStateException(String.format( - "Method must only one argument of type %s!", - type.getSimpleName())); - } - } + for (Class type : Parameters.TYPES) { + if (getNumberOfOccurences(method, type) > 1) { + throw new IllegalStateException(String.format( + "Method must only one argument of type %s!", + type.getSimpleName())); + } + } - if (hasParameterOfType(method, Pageable.class)) { - assertReturnType(method, Page.class, List.class); - if (hasParameterOfType(method, Sort.class)) { - throw new IllegalStateException( - "Method must not have Pageable *and* Sort parameter. " - + "Use sorting capabilities on Pageble instead!"); - } - } + if (hasParameterOfType(method, Pageable.class)) { + assertReturnType(method, Page.class, List.class); + if (hasParameterOfType(method, Sort.class)) { + throw new IllegalStateException( + "Method must not have Pageable *and* Sort parameter. " + + "Use sorting capabilities on Pageble instead!"); + } + } - this.method = method; - this.parameters = new Parameters(method); - this.metadata = metadata; - } + this.method = method; + this.parameters = new Parameters(method); + this.metadata = metadata; + } - /** - * Returns the method's name. - * - * @return - */ - public String getName() { + /** + * Returns the method's name. + * + * @return + */ + public String getName() { - return method.getName(); - } + return method.getName(); + } - @SuppressWarnings("rawtypes") - public EntityMetadata getEntityInformation() { + @SuppressWarnings("rawtypes") + public EntityMetadata getEntityInformation() { - return new EntityMetadata() { + return new EntityMetadata() { - public Class getJavaType() { + public Class getJavaType() { - return getDomainClass(); - } - }; - } + return getDomainClass(); + } + }; + } - protected Class getDomainClass() { - - Class repositoryDomainClass = metadata.getDomainClass(); - Class methodDomainClass = ClassUtils.getReturnedDomainClass(method); + protected Class getDomainClass() { - return repositoryDomainClass == null || repositoryDomainClass.isAssignableFrom(methodDomainClass) ? methodDomainClass - : repositoryDomainClass; - } + Class repositoryDomainClass = metadata.getDomainClass(); + Class methodDomainClass = ClassUtils.getReturnedDomainClass(method); + + return repositoryDomainClass == null || repositoryDomainClass.isAssignableFrom(methodDomainClass) ? methodDomainClass + : repositoryDomainClass; + } - /** - * Returns whether the finder will actually return a collection of entities - * or a single one. - * - * @return - */ - protected boolean isCollectionQuery() { + /** + * Returns whether the finder will actually return a collection of entities + * or a single one. + * + * @return + */ + protected boolean isCollectionQuery() { - Class returnType = method.getReturnType(); - return org.springframework.util.ClassUtils.isAssignable(List.class, - returnType); - } + Class returnType = method.getReturnType(); + return org.springframework.util.ClassUtils.isAssignable(List.class, + returnType); + } - /** - * Returns whether the finder will return a {@link Page} of results. - * - * @return - */ - protected boolean isPageQuery() { + /** + * Returns whether the finder will return a {@link Page} of results. + * + * @return + */ + protected boolean isPageQuery() { - Class returnType = method.getReturnType(); - return org.springframework.util.ClassUtils.isAssignable(Page.class, - returnType); - } + Class returnType = method.getReturnType(); + return org.springframework.util.ClassUtils.isAssignable(Page.class, + returnType); + } - public Type getType() { + public Type getType() { - if (isModifyingQuery()) { - return Type.MODIFYING; - } + if (isModifyingQuery()) { + return Type.MODIFYING; + } - if (isPageQuery()) { - return Type.PAGING; - } + if (isPageQuery()) { + return Type.PAGING; + } - if (isCollectionQuery()) { - return Type.COLLECTION; - } + if (isCollectionQuery()) { + return Type.COLLECTION; + } - return Type.SINGLE_ENTITY; - } + return Type.SINGLE_ENTITY; + } - protected boolean isModifyingQuery() { + protected boolean isModifyingQuery() { - return false; - } + return false; + } - /** - * Returns the {@link Parameters} wrapper to gain additional information - * about {@link Method} parameters. - * - * @return - */ - public Parameters getParameters() { + /** + * Returns the {@link Parameters} wrapper to gain additional information + * about {@link Method} parameters. + * + * @return + */ + public Parameters getParameters() { - return parameters; - } + return parameters; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return method.toString(); - } + return method.toString(); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/RepositoryQuery.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/RepositoryQuery.java index 69bf0f0f1..991d63ea2 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/RepositoryQuery.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/RepositoryQuery.java @@ -16,28 +16,27 @@ package org.springframework.data.repository.query; - /** * Interface for a query abstraction. - * + * * @author Oliver Gierke */ public interface RepositoryQuery { - /** - * Executes the {@link RepositoryQuery} with the given parameters. - * - * @param store - * @param parameters - * @return - */ - public Object execute(Object[] parameters); + /** + * Executes the {@link RepositoryQuery} with the given parameters. + * + * @param store + * @param parameters + * @return + */ + public Object execute(Object[] parameters); - /** - * Returns the - * - * @return - */ - public QueryMethod getQueryMethod(); + /** + * Returns the + * + * @return + */ + public QueryMethod getQueryMethod(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java index 182811375..047f58fd9 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/AbstractQueryCreator.java @@ -27,117 +27,117 @@ import org.springframework.util.Assert; /** * Base class for query creators that create criteria based queries from a * {@link PartTree}. - * + * * @param T the actual query type to be created * @param S the intermediate criteria type * @author Oliver Gierke */ public abstract class AbstractQueryCreator { - private final ParameterAccessor parameters; - private final PartTree tree; + private final ParameterAccessor parameters; + private final PartTree tree; - /** - * Creates a new {@link AbstractQueryCreator} for the given {@link PartTree} - * and {@link ParametersParameterAccessor}. - * - * @param tree - * @param parameters - */ - public AbstractQueryCreator(PartTree tree, ParameterAccessor parameters) { + /** + * Creates a new {@link AbstractQueryCreator} for the given {@link PartTree} + * and {@link ParametersParameterAccessor}. + * + * @param tree + * @param parameters + */ + public AbstractQueryCreator(PartTree tree, ParameterAccessor parameters) { - Assert.notNull(tree); - Assert.notNull(parameters); + Assert.notNull(tree); + Assert.notNull(parameters); - this.tree = tree; - this.parameters = parameters; - } + this.tree = tree; + this.parameters = parameters; + } - /** - * Creates the actual query object. - * - * @return - */ - public T createQuery() { + /** + * Creates the actual query object. + * + * @return + */ + public T createQuery() { - Sort treeSort = tree.getSort(); - Sort sort = treeSort != null ? treeSort : parameters.getSort(); + Sort treeSort = tree.getSort(); + Sort sort = treeSort != null ? treeSort : parameters.getSort(); - return complete(createCriteria(tree), sort); - } + return complete(createCriteria(tree), sort); + } - /** - * Actual query building logic. Traverses the {@link PartTree} and invokes - * callback methods to delegate actual criteria creation and concatenation. - * - * @param tree - * @return - */ - private S createCriteria(PartTree tree) { + /** + * Actual query building logic. Traverses the {@link PartTree} and invokes + * callback methods to delegate actual criteria creation and concatenation. + * + * @param tree + * @return + */ + private S createCriteria(PartTree tree) { - S base = null; - Iterator iterator = parameters.iterator(); + S base = null; + Iterator iterator = parameters.iterator(); - for (OrPart node : tree) { + for (OrPart node : tree) { - S criteria = null; + S criteria = null; - for (Part part : node) { + for (Part part : node) { - criteria = - criteria == null ? create(part, iterator) : and(part, - criteria, iterator); - } + criteria = + criteria == null ? create(part, iterator) : and(part, + criteria, iterator); + } - base = base == null ? criteria : or(base, criteria); - } + base = base == null ? criteria : or(base, criteria); + } - return base; - } + return base; + } - /** - * Creates a new atomic instance of the criteria object. - * - * @param part - * @param iterator - * @return - */ - protected abstract S create(Part part, Iterator iterator); + /** + * Creates a new atomic instance of the criteria object. + * + * @param part + * @param iterator + * @return + */ + protected abstract S create(Part part, Iterator iterator); - /** - * Creates a new criteria object from the given part and and-concatenates it - * to the given base criteria. - * - * @param part - * @param base will never be {@literal null}. - * @param iterator - * @return - */ - protected abstract S and(Part part, S base, Iterator iterator); + /** + * Creates a new criteria object from the given part and and-concatenates it + * to the given base criteria. + * + * @param part + * @param base will never be {@literal null}. + * @param iterator + * @return + */ + protected abstract S and(Part part, S base, Iterator iterator); - /** - * Or-concatenates the given base criteria to the given new criteria. - * - * @param base - * @param criteria - * @return - */ - protected abstract S or(S base, S criteria); + /** + * Or-concatenates the given base criteria to the given new criteria. + * + * @param base + * @param criteria + * @return + */ + protected abstract S or(S base, S criteria); - /** - * Actually creates the query object applying the given criteria object and - * {@link Sort} definition. - * - * @param criteria - * @param sort - * @return - */ - protected abstract T complete(S criteria, Sort sort); + /** + * Actually creates the query object applying the given criteria object and + * {@link Sort} definition. + * + * @param criteria + * @param sort + * @return + */ + protected abstract T complete(S criteria, Sort sort); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java index e7000befc..6bc2431d6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/OrderBySource.java @@ -32,69 +32,69 @@ import org.springframework.util.StringUtils; * up multiple properties ending with the sorting direction. So the following * method ends are valid: {@code LastnameUsernameDesc}, * {@code LastnameAscUsernameDesc}. - * + * * @author Oliver Gierke */ public class OrderBySource { - private final String BLOCK_SPLIT = "(?<=Asc|Desc)(?=[A-Z])"; - private final Pattern DIRECTION_SPLIT = Pattern.compile("(.+)(Asc|Desc)$"); + private final String BLOCK_SPLIT = "(?<=Asc|Desc)(?=[A-Z])"; + private final Pattern DIRECTION_SPLIT = Pattern.compile("(.+)(Asc|Desc)$"); - private final List orders; + private final List orders; - public OrderBySource(String clause) { + public OrderBySource(String clause) { - this(clause, null); - } + this(clause, null); + } - public OrderBySource(String clause, Class domainClass) { + public OrderBySource(String clause, Class domainClass) { - this.orders = new ArrayList(); + this.orders = new ArrayList(); - for (String part : clause.split(BLOCK_SPLIT)) { + for (String part : clause.split(BLOCK_SPLIT)) { - Matcher matcher = DIRECTION_SPLIT.matcher(part); + Matcher matcher = DIRECTION_SPLIT.matcher(part); - if (!matcher.find()) { - throw new IllegalArgumentException(String.format( - "Invalid order syntax for part %s!", part)); - } + if (!matcher.find()) { + throw new IllegalArgumentException(String.format( + "Invalid order syntax for part %s!", part)); + } - Direction direction = Direction.fromString(matcher.group(2)); - this.orders.add(createOrder(matcher.group(1), direction, - domainClass)); - } - } + Direction direction = Direction.fromString(matcher.group(2)); + this.orders.add(createOrder(matcher.group(1), direction, + domainClass)); + } + } - /** - * Creates an {@link Order} instance from the given property source, - * direction and domain class. If the domain class is given, we will use it - * for nested property traversal checks. - * - * @see Property#from(String, Class) - * @param propertySource - * @param direction - * @param domainClass can be {@literal null}. - * @return - */ - private Order createOrder(String propertySource, Direction direction, - Class domainClass) { + /** + * Creates an {@link Order} instance from the given property source, + * direction and domain class. If the domain class is given, we will use it + * for nested property traversal checks. + * + * @param propertySource + * @param direction + * @param domainClass can be {@literal null}. + * @return + * @see Property#from(String, Class) + */ + private Order createOrder(String propertySource, Direction direction, + Class domainClass) { - if (null == domainClass) { - return new Order(direction, - StringUtils.uncapitalize(propertySource)); - } + if (null == domainClass) { + return new Order(direction, + StringUtils.uncapitalize(propertySource)); + } - Property property = Property.from(propertySource, domainClass); - return new Order(direction, property.toDotPath()); - } + Property property = Property.from(propertySource, domainClass); + return new Order(direction, property.toDotPath()); + } - public Sort toSort() { + public Sort toSort() { - return this.orders.isEmpty() ? null : new Sort(this.orders); - } + return this.orders.isEmpty() ? null : new Sort(this.orders); + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Part.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Part.java index 8363b75d7..e67009c20 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Part.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Part.java @@ -26,256 +26,256 @@ import org.springframework.util.StringUtils; * The actual transformation is defined by a {@link Type} that is determined * from inspecting the given part. The query part can then be looked up via * {@link #getQueryPart()}. - * + * * @author Oliver Gierke */ public class Part { - private final Property property; - private final Part.Type type; + private final Property property; + private final Part.Type type; - /** - * Creates a new {@link Part} from the given method name part, the - * {@link Class} the part originates from and the start parameter index. - * - * @param part - * @param clazz - */ - public Part(String part, Class clazz) { + /** + * Creates a new {@link Part} from the given method name part, the + * {@link Class} the part originates from and the start parameter index. + * + * @param part + * @param clazz + */ + public Part(String part, Class clazz) { - this.type = Type.fromProperty(part, clazz); - this.property = Property.from(type.extractProperty(part), clazz); - } + this.type = Type.fromProperty(part, clazz); + this.property = Property.from(type.extractProperty(part), clazz); + } - public boolean getParameterRequired() { + public boolean getParameterRequired() { - return getNumberOfArguments() > 0; - } + return getNumberOfArguments() > 0; + } - /** - * Returns how many method parameters are bound by this part. - * - * @return - */ - public int getNumberOfArguments() { + /** + * Returns how many method parameters are bound by this part. + * + * @return + */ + public int getNumberOfArguments() { - return type.getNumberOfArguments(); - } + return type.getNumberOfArguments(); + } - /** - * @return the part - */ - public Property getProperty() { + /** + * @return the part + */ + public Property getProperty() { - return property; - } + return property; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { - if (obj == this) { - return true; - } + if (obj == this) { + return true; + } - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } - Part that = (Part) obj; + Part that = (Part) obj; - return this.property.equals(that.property) - && this.type.equals(that.type); - } + return this.property.equals(that.property) + && this.type.equals(that.type); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - int result = 37; - result += 17 * property.hashCode(); - result += 17 * type.hashCode(); - return result; - } + int result = 37; + result += 17 * property.hashCode(); + result += 17 * type.hashCode(); + return result; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return String.format("%s %s", property.getName(), type); - } + return String.format("%s %s", property.getName(), type); + } - /** - * @return the type - */ - public Part.Type getType() { + /** + * @return the type + */ + public Part.Type getType() { - return type; - } + return type; + } - /** - * The type of a method name part. Used to create query parts in various - * ways. - * - * @author Oliver Gierke - */ - public static enum Type { + /** + * The type of a method name part. Used to create query parts in various + * ways. + * + * @author Oliver Gierke + */ + public static enum Type { - BETWEEN(2, "Between"), + BETWEEN(2, "Between"), - IS_NOT_NULL(0, "IsNotNull", "NotNull"), + IS_NOT_NULL(0, "IsNotNull", "NotNull"), - IS_NULL(0, "IsNull", "Null"), + IS_NULL(0, "IsNull", "Null"), - LESS_THAN("LessThan"), + LESS_THAN("LessThan"), - GREATER_THAN("GreaterThan"), + GREATER_THAN("GreaterThan"), - NOT_LIKE("NotLike"), + NOT_LIKE("NotLike"), - LIKE("Like"), + LIKE("Like"), - NOT_IN("NotIn"), + NOT_IN("NotIn"), - IN("In"), - - NEAR("Near"), - - WITHIN("Within"), + IN("In"), - NEGATING_SIMPLE_PROPERTY("Not"), - - SIMPLE_PROPERTY; + NEAR("Near"), - // Need to list them again explicitly as the order is important - // (esp. for IS_NULL, IS_NOT_NULL) - private static final List ALL = Arrays.asList(IS_NOT_NULL, - IS_NULL, BETWEEN, LESS_THAN, GREATER_THAN, NOT_LIKE, LIKE, - NOT_IN, IN, NEAR, WITHIN, NEGATING_SIMPLE_PROPERTY, SIMPLE_PROPERTY); - private List keywords; - private int numberOfArguments; + WITHIN("Within"), + + NEGATING_SIMPLE_PROPERTY("Not"), + + SIMPLE_PROPERTY; + + // Need to list them again explicitly as the order is important + // (esp. for IS_NULL, IS_NOT_NULL) + private static final List ALL = Arrays.asList(IS_NOT_NULL, + IS_NULL, BETWEEN, LESS_THAN, GREATER_THAN, NOT_LIKE, LIKE, + NOT_IN, IN, NEAR, WITHIN, NEGATING_SIMPLE_PROPERTY, SIMPLE_PROPERTY); + private List keywords; + private int numberOfArguments; - /** - * Creates a new {@link Type} using the given keyword, number of - * arguments to be bound and operator. Keyword and operator can be - * {@literal null}. - * - * @param operator - * @param numberOfArguments - * @param keywords - */ - private Type(int numberOfArguments, String... keywords) { + /** + * Creates a new {@link Type} using the given keyword, number of + * arguments to be bound and operator. Keyword and operator can be + * {@literal null}. + * + * @param operator + * @param numberOfArguments + * @param keywords + */ + private Type(int numberOfArguments, String... keywords) { - this.numberOfArguments = numberOfArguments; - this.keywords = Arrays.asList(keywords); - } + this.numberOfArguments = numberOfArguments; + this.keywords = Arrays.asList(keywords); + } - private Type(String... keywords) { + private Type(String... keywords) { - this(1, keywords); - } + this(1, keywords); + } - /** - * Returns the {@link Type} of the {@link Part} for the given raw - * property and the given {@link Class}. This will try to detect e.g. - * keywords contained in the raw property that trigger special query - * creation. Returns {@link #SIMPLE_PROPERTY} by default. - * - * @param rawProperty - * @param clazz - * @return - */ - public static Part.Type fromProperty(String rawProperty, Class clazz) { + /** + * Returns the {@link Type} of the {@link Part} for the given raw + * property and the given {@link Class}. This will try to detect e.g. + * keywords contained in the raw property that trigger special query + * creation. Returns {@link #SIMPLE_PROPERTY} by default. + * + * @param rawProperty + * @param clazz + * @return + */ + public static Part.Type fromProperty(String rawProperty, Class clazz) { - for (Part.Type type : ALL) { - if (type.supports(rawProperty, clazz)) { - return type; - } - } + for (Part.Type type : ALL) { + if (type.supports(rawProperty, clazz)) { + return type; + } + } - return SIMPLE_PROPERTY; - } + return SIMPLE_PROPERTY; + } - /** - * Returns whether the the type supports the given raw property. Default - * implementation checks whether the property ends with the registered - * keyword. Does not support the keyword if the property is a valid - * field as is. - * - * @param property - * @param clazz - * @return - */ - protected boolean supports(String property, Class clazz) { + /** + * Returns whether the the type supports the given raw property. Default + * implementation checks whether the property ends with the registered + * keyword. Does not support the keyword if the property is a valid + * field as is. + * + * @param property + * @param clazz + * @return + */ + protected boolean supports(String property, Class clazz) { - if (keywords == null) { - return true; - } + if (keywords == null) { + return true; + } - for (String keyword : keywords) { - if (property.endsWith(keyword)) { - return true; - } - } + for (String keyword : keywords) { + if (property.endsWith(keyword)) { + return true; + } + } - return false; - } + return false; + } - /** - * Returns the number of arguments the property binds. By default this - * exactly one argument. - * - * @return - */ - public int getNumberOfArguments() { + /** + * Returns the number of arguments the property binds. By default this + * exactly one argument. + * + * @return + */ + public int getNumberOfArguments() { - return numberOfArguments; - } + return numberOfArguments; + } - /** - * Callback method to extract the actual property to be bound from the - * given part. Strips the keyword from the part's end if available. - * - * @param part - * @return - */ - public String extractProperty(String part) { + /** + * Callback method to extract the actual property to be bound from the + * given part. Strips the keyword from the part's end if available. + * + * @param part + * @return + */ + public String extractProperty(String part) { - String candidate = StringUtils.uncapitalize(part); + String candidate = StringUtils.uncapitalize(part); - for (String keyword : keywords) { - if (candidate.endsWith(keyword)) { - return candidate.substring(0, candidate.indexOf(keyword)); - } - } + for (String keyword : keywords) { + if (candidate.endsWith(keyword)) { + return candidate.substring(0, candidate.indexOf(keyword)); + } + } - return candidate; - } - } + return candidate; + } + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java index a9f644c67..830489078 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/PartTree.java @@ -36,234 +36,234 @@ import org.springframework.util.StringUtils; * validate that each of the {@link Part}s are refering to a property of the * domain class. The {@link PartTree} can then be used to build queries based on * its API instead of parsing the method name for each query execution. - * + * * @author Oliver Gierke */ public class PartTree implements Iterable { - private static final String ORDER_BY = "OrderBy"; - private static final String KEYWORD_TEMPLATE = "(%s)(?=[A-Z])"; - private static final String DISTINCT = "Distinct"; + private static final String ORDER_BY = "OrderBy"; + private static final String KEYWORD_TEMPLATE = "(%s)(?=[A-Z])"; + private static final String DISTINCT = "Distinct"; - private static final Pattern PREFIX_TEMPLATE = Pattern - .compile("^(find|read|get)(\\p{Upper}.*?)??By"); + private static final Pattern PREFIX_TEMPLATE = Pattern + .compile("^(find|read|get)(\\p{Upper}.*?)??By"); - private final boolean distinct; - private final OrderBySource orderBySource; - private final List nodes = new ArrayList(); + private final boolean distinct; + private final OrderBySource orderBySource; + private final List nodes = new ArrayList(); - /** - * Creates a new {@link PartTree} by parsing the given {@link String} - * - * @param source the {@link String} to parse - * @param domainClass the domain class to check indiviual parts against to - * ensure they refer to a property of the class - */ - public PartTree(String source, Class domainClass) { + /** + * Creates a new {@link PartTree} by parsing the given {@link String} + * + * @param source the {@link String} to parse + * @param domainClass the domain class to check indiviual parts against to + * ensure they refer to a property of the class + */ + public PartTree(String source, Class domainClass) { - Assert.notNull(source); - Assert.notNull(domainClass); + Assert.notNull(source); + Assert.notNull(domainClass); - this.distinct = detectDistinct(source); - String foo = strip(source); - String[] parts = split(foo, ORDER_BY); + this.distinct = detectDistinct(source); + String foo = strip(source); + String[] parts = split(foo, ORDER_BY); - if (parts.length > 2) { - throw new IllegalArgumentException( - "OrderBy must not be used more than once in a method name!"); - } + if (parts.length > 2) { + throw new IllegalArgumentException( + "OrderBy must not be used more than once in a method name!"); + } - buildTree(parts[0], domainClass); - this.orderBySource = - parts.length == 2 ? new OrderBySource(parts[1], domainClass) - : null; + buildTree(parts[0], domainClass); + this.orderBySource = + parts.length == 2 ? new OrderBySource(parts[1], domainClass) + : null; - } + } - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + public Iterator iterator() { - return nodes.iterator(); - } + return nodes.iterator(); + } - private void buildTree(String source, Class domainClass) { + private void buildTree(String source, Class domainClass) { - String[] split = split(source, "Or"); + String[] split = split(source, "Or"); - for (String part : split) { - nodes.add(new OrPart(part, domainClass)); - } - } + for (String part : split) { + nodes.add(new OrPart(part, domainClass)); + } + } - /** - * Returns the {@link Sort} specification parsed from the source. - * - * @return - */ - public Sort getSort() { + /** + * Returns the {@link Sort} specification parsed from the source. + * + * @return + */ + public Sort getSort() { - return orderBySource == null ? null : orderBySource.toSort(); - } + return orderBySource == null ? null : orderBySource.toSort(); + } - /** - * Returns whether we indicate distinct lookup of entities. - * - * @return - */ - public boolean isDistinct() { + /** + * Returns whether we indicate distinct lookup of entities. + * + * @return + */ + public boolean isDistinct() { - return distinct; - } + return distinct; + } - /** - * Returns an {@link Iterable} of all parts contained in the - * {@link PartTree}. - * - * @return - */ - public Iterable getParts() { + /** + * Returns an {@link Iterable} of all parts contained in the + * {@link PartTree}. + * + * @return + */ + public Iterable getParts() { - List result = new ArrayList(); + List result = new ArrayList(); - for (OrPart orPart : this) { - for (Part part : orPart) { - result.add(part); - } - } + for (OrPart orPart : this) { + for (Part part : orPart) { + result.add(part); + } + } - return result; - } + return result; + } - /** - * Splits the given text at the given keywords. Expects camelcase style to - * only match concrete keywords and not derivatives of it. - * - * @param text - * @param keyword - * @return - */ - private static String[] split(String text, String keyword) { + /** + * Splits the given text at the given keywords. Expects camelcase style to + * only match concrete keywords and not derivatives of it. + * + * @param text + * @param keyword + * @return + */ + private static String[] split(String text, String keyword) { - String regex = format(KEYWORD_TEMPLATE, keyword); + String regex = format(KEYWORD_TEMPLATE, keyword); - Pattern pattern = compile(regex); - return pattern.split(text); - } + Pattern pattern = compile(regex); + return pattern.split(text); + } - /** - * Strips a prefix from the given method name if it starts with one of - * {@value #PREFIXES}. - * - * @param methodName - * @return - */ - private String strip(String methodName) { + /** + * Strips a prefix from the given method name if it starts with one of + * {@value #PREFIXES}. + * + * @param methodName + * @return + */ + private String strip(String methodName) { - Matcher matcher = PREFIX_TEMPLATE.matcher(methodName); + Matcher matcher = PREFIX_TEMPLATE.matcher(methodName); - if (matcher.find()) { - return methodName.substring(matcher.group().length()); - } else { - return methodName; - } - } + if (matcher.find()) { + return methodName.substring(matcher.group().length()); + } else { + return methodName; + } + } - /** - * Checks whether the given source string contains the {@link #DISTINCT} - * keyword in it's prefix. - * - * @param source - * @return - */ - private boolean detectDistinct(String source) { + /** + * Checks whether the given source string contains the {@link #DISTINCT} + * keyword in it's prefix. + * + * @param source + * @return + */ + private boolean detectDistinct(String source) { - Matcher matcher = PREFIX_TEMPLATE.matcher(source); + Matcher matcher = PREFIX_TEMPLATE.matcher(source); - if (!matcher.find()) { - return false; - } + if (!matcher.find()) { + return false; + } - String group = matcher.group(2); - return group != null && group.contains(DISTINCT); - } + String group = matcher.group(2); + return group != null && group.contains(DISTINCT); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return String.format("%s %s", - StringUtils.collectionToDelimitedString(nodes, " or "), - orderBySource.toString()); - } + return String.format("%s %s", + StringUtils.collectionToDelimitedString(nodes, " or "), + orderBySource.toString()); + } - /** - * A part of the parsed source that results from splitting up the resource - * ar {@literal Or} keywords. Consists of {@link Part}s that have to be - * concatenated by {@literal And}. - * - * @author Oliver Gierke - */ - public static class OrPart implements Iterable { + /** + * A part of the parsed source that results from splitting up the resource + * ar {@literal Or} keywords. Consists of {@link Part}s that have to be + * concatenated by {@literal And}. + * + * @author Oliver Gierke + */ + public static class OrPart implements Iterable { - private final List children = new ArrayList(); + private final List children = new ArrayList(); - /** - * Creates a new {@link OrPart}. - * - * @param source the source to split up into {@literal And} parts in - * turn. - * @param domainClass the domain class to check the resulting - * {@link Part}s against. - */ - OrPart(String source, Class domainClass) { + /** + * Creates a new {@link OrPart}. + * + * @param source the source to split up into {@literal And} parts in + * turn. + * @param domainClass the domain class to check the resulting + * {@link Part}s against. + */ + OrPart(String source, Class domainClass) { - String[] split = split(source, "And"); + String[] split = split(source, "And"); - for (String part : split) { - children.add(new Part(part, domainClass)); - } - } + for (String part : split) { + children.add(new Part(part, domainClass)); + } + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#toString() - */ - @Override - public String toString() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#toString() + */ + @Override + public String toString() { - return StringUtils.collectionToDelimitedString(children, " and "); - } + return StringUtils.collectionToDelimitedString(children, " and "); + } - /* - * (non-Javadoc) - * - * @see java.lang.Iterable#iterator() - */ - public Iterator iterator() { + /* + * (non-Javadoc) + * + * @see java.lang.Iterable#iterator() + */ + public Iterator iterator() { - return children.iterator(); - } - } + return children.iterator(); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java index 047d10f70..4f4e4222d 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/query/parser/Property.java @@ -28,292 +28,292 @@ import org.springframework.util.StringUtils; /** * Abstraction of a {@link Property} of a domain class. - * + * * @author Oliver Gierke */ public class Property { - private static String ERROR_TEMPLATE = "No property %s found for type %s"; + private static String ERROR_TEMPLATE = "No property %s found for type %s"; - private final String name; - private final TypeInformation type; - private final boolean isCollection; + private final String name; + private final TypeInformation type; + private final boolean isCollection; - private Property next; + private Property next; - /** - * Creates a leaf property (no nested ones) with the given name inside the - * given owning type. - * - * @param name - * @param owningType - */ - Property(String name, Class owningType) { + /** + * Creates a leaf property (no nested ones) with the given name inside the + * given owning type. + * + * @param name + * @param owningType + */ + Property(String name, Class owningType) { - this(name, ClassTypeInformation.from(owningType)); - } - - Property(String name, TypeInformation owningType) { - - Assert.hasText(name); - Assert.notNull(owningType); - - String propertyName = StringUtils.uncapitalize(name); - TypeInformation type = owningType.getProperty(propertyName); + this(name, ClassTypeInformation.from(owningType)); + } - if (type == null) { - throw new IllegalArgumentException(String.format(ERROR_TEMPLATE, - propertyName, owningType.getType())); - } - - this.isCollection = type.isCollectionLike(); - this.type = getPropertyType(type); - this.name = propertyName; - } - - private TypeInformation getPropertyType(TypeInformation type) { - - if (type.isCollectionLike()) { - return type.getComponentType(); - } - - if (type.isMap()) { - return type.getMapValueType(); - } - - return type; - } + Property(String name, TypeInformation owningType) { + + Assert.hasText(name); + Assert.notNull(owningType); + + String propertyName = StringUtils.uncapitalize(name); + TypeInformation type = owningType.getProperty(propertyName); + + if (type == null) { + throw new IllegalArgumentException(String.format(ERROR_TEMPLATE, + propertyName, owningType.getType())); + } + + this.isCollection = type.isCollectionLike(); + this.type = getPropertyType(type); + this.name = propertyName; + } + + private TypeInformation getPropertyType(TypeInformation type) { + + if (type.isCollectionLike()) { + return type.getComponentType(); + } + + if (type.isMap()) { + return type.getMapValueType(); + } + + return type; + } - /** - * Creates a {@link Property} with the given name inside the given owning - * type and tries to resolve the other {@link String} to create nested - * properties. - * - * @param name - * @param owningType - * @param toTraverse - */ - Property(String name, TypeInformation owningType, String toTraverse) { + /** + * Creates a {@link Property} with the given name inside the given owning + * type and tries to resolve the other {@link String} to create nested + * properties. + * + * @param name + * @param owningType + * @param toTraverse + */ + Property(String name, TypeInformation owningType, String toTraverse) { - this(name, owningType); + this(name, owningType); - if (StringUtils.hasText(toTraverse)) { - this.next = from(toTraverse, type); - } - } + if (StringUtils.hasText(toTraverse)) { + this.next = from(toTraverse, type); + } + } - /** - * Returns the name of the {@link Property}. - * - * @return the name - */ - public String getName() { + /** + * Returns the name of the {@link Property}. + * + * @return the name + */ + public String getName() { - return name; - } + return name; + } /** * Returns the type of the property will return the plain resolved type for * simple properties, the component type for any {@link Iterable} or the * value type of a {@link java.util.Map} if the property is one. - * + * * @return */ public Class getType() { - + return this.type.getType(); } - /** - * Returns the next nested {@link Property}. - * - * @see #hasNext() - * @return the next nested {@link Property} or {@literal null} if no nested - * {@link Property} available. - */ - public Property next() { + /** + * Returns the next nested {@link Property}. + * + * @return the next nested {@link Property} or {@literal null} if no nested + * {@link Property} available. + * @see #hasNext() + */ + public Property next() { - return next; - } + return next; + } - /** - * Returns whether there is a nested {@link Property}. If this returns - * {@literal true} you can expect {@link #next()} to return a non- - * {@literal null} value. - * - * @return - */ - public boolean hasNext() { + /** + * Returns whether there is a nested {@link Property}. If this returns + * {@literal true} you can expect {@link #next()} to return a non- + * {@literal null} value. + * + * @return + */ + public boolean hasNext() { - return next != null; - } + return next != null; + } - /** - * Returns the {@link Property} path in dot notation. - * - * @return - */ - public String toDotPath() { + /** + * Returns the {@link Property} path in dot notation. + * + * @return + */ + public String toDotPath() { - if (hasNext()) { - return getName() + "." + next().toDotPath(); - } + if (hasNext()) { + return getName() + "." + next().toDotPath(); + } - return getName(); - } + return getName(); + } - /** - * Returns whether the {@link Property} is actually a collection. - * - * @return - */ - public boolean isCollection() { + /** + * Returns whether the {@link Property} is actually a collection. + * + * @return + */ + public boolean isCollection() { - return isCollection; - } + return isCollection; + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { - if (this == obj) { - return true; - } + if (this == obj) { + return true; + } - if (obj == null || !getClass().equals(obj.getClass())) { - return false; - } + if (obj == null || !getClass().equals(obj.getClass())) { + return false; + } - Property that = (Property) obj; + Property that = (Property) obj; - return this.name.equals(that.name) && this.type.equals(type); - } + return this.name.equals(that.name) && this.type.equals(type); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - return name.hashCode() + type.hashCode(); - } + return name.hashCode() + type.hashCode(); + } - /** - * Extracts the {@link Property} chain from the given source {@link String} - * and type. - * - * @param source - * @param type - * @return - */ - public static Property from(String source, Class type) { + /** + * Extracts the {@link Property} chain from the given source {@link String} + * and type. + * + * @param source + * @param type + * @return + */ + public static Property from(String source, Class type) { - return from(source, ClassTypeInformation.from(type)); - } - - private static Property from(String source, TypeInformation type) { - - Iterator parts = Arrays.asList(source.split("_")).iterator(); + return from(source, ClassTypeInformation.from(type)); + } - Property result = null; - Property current = null; + private static Property from(String source, TypeInformation type) { - while (parts.hasNext()) { - if (result == null) { - result = create(parts.next(), type); - current = result; - } else { - current = create(parts.next(), current); - } - } + Iterator parts = Arrays.asList(source.split("_")).iterator(); - return result; - } + Property result = null; + Property current = null; + + while (parts.hasNext()) { + if (result == null) { + result = create(parts.next(), type); + current = result; + } else { + current = create(parts.next(), current); + } + } + + return result; + } - /** - * Creates a new {@link Property} as subordinary of the given - * {@link Property}. - * - * @param source - * @param base - * @return - */ - private static Property create(String source, Property base) { + /** + * Creates a new {@link Property} as subordinary of the given + * {@link Property}. + * + * @param source + * @param base + * @return + */ + private static Property create(String source, Property base) { - Property property = create(source, base.type); - base.next = property; - return property; - } + Property property = create(source, base.type); + base.next = property; + return property; + } - /** - * Factory method to create a new {@link Property} for the given - * {@link String} and owning type. It will inspect the given source for - * camel-case parts and traverse the {@link String} along its parts starting - * with the entire one and chewing off parts from the right side then. - * Whenever a valid property for the given class is found, the tail will be - * traversed for subordinary properties of the just found one and so on. - * - * @param source - * @param type - * @return - */ - private static Property create(String source, TypeInformation type) { + /** + * Factory method to create a new {@link Property} for the given + * {@link String} and owning type. It will inspect the given source for + * camel-case parts and traverse the {@link String} along its parts starting + * with the entire one and chewing off parts from the right side then. + * Whenever a valid property for the given class is found, the tail will be + * traversed for subordinary properties of the just found one and so on. + * + * @param source + * @param type + * @return + */ + private static Property create(String source, TypeInformation type) { - return create(source, type, ""); - } + return create(source, type, ""); + } - /** - * Tries to look up a chain of {@link Property}s by trying the givne source - * first. If that fails it will split the source apart at camel case borders - * (starting from the right side) and try to look up a {@link Property} from - * the calculated head and recombined new tail and additional tail. - * - * @param source - * @param type - * @param addTail - * @return - */ - private static Property create(String source, TypeInformation type, String addTail) { + /** + * Tries to look up a chain of {@link Property}s by trying the givne source + * first. If that fails it will split the source apart at camel case borders + * (starting from the right side) and try to look up a {@link Property} from + * the calculated head and recombined new tail and additional tail. + * + * @param source + * @param type + * @param addTail + * @return + */ + private static Property create(String source, TypeInformation type, String addTail) { - IllegalArgumentException exception = null; + IllegalArgumentException exception = null; - try { - return new Property(source, type, addTail); - } catch (IllegalArgumentException e) { - exception = e; - } + try { + return new Property(source, type, addTail); + } catch (IllegalArgumentException e) { + exception = e; + } - Pattern pattern = Pattern.compile("[A-Z]?[a-z]*$"); - Matcher matcher = pattern.matcher(source); + Pattern pattern = Pattern.compile("[A-Z]?[a-z]*$"); + Matcher matcher = pattern.matcher(source); - if (matcher.find() && matcher.start() != 0) { + if (matcher.find() && matcher.start() != 0) { - int position = matcher.start(); - String head = source.substring(0, position); - String tail = source.substring(position); + int position = matcher.start(); + String head = source.substring(0, position); + String tail = source.substring(position); - return create(head, type, tail + addTail); - } + return create(head, type, tail + addTail); + } - throw exception; - } + throw exception; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/AbstractEntityInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/AbstractEntityInformation.java index 2a6ed38dc..857c9a633 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/AbstractEntityInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/AbstractEntityInformation.java @@ -23,50 +23,50 @@ import org.springframework.util.Assert; /** * Base class for implementations of {@link EntityInformation}. Considers an * entity to be new whenever {@link #getId(Object)} returns {@literal null}. - * + * * @author Oliver Gierke */ public abstract class AbstractEntityInformation implements - EntityInformation { + EntityInformation { - private final Class domainClass; + private final Class domainClass; - /** - * Creates a new {@link AbstractEntityInformation} from the given domain - * class. - * - * @param domainClass - */ - public AbstractEntityInformation(Class domainClass) { + /** + * Creates a new {@link AbstractEntityInformation} from the given domain + * class. + * + * @param domainClass + */ + public AbstractEntityInformation(Class domainClass) { - Assert.notNull(domainClass); - this.domainClass = domainClass; - } + Assert.notNull(domainClass); + this.domainClass = domainClass; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.IsNewAware#isNew(java.lang - * .Object) - */ - public boolean isNew(T entity) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.IsNewAware#isNew(java.lang + * .Object) + */ + public boolean isNew(T entity) { - return getId(entity) == null; - } + return getId(entity) == null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.EntityInformation#getJavaType - * () - */ - public Class getJavaType() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.EntityInformation#getJavaType + * () + */ + public Class getJavaType() { - return this.domainClass; - } + return this.domainClass; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryInformation.java index 002b78a36..60ca20488 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryInformation.java @@ -30,258 +30,258 @@ import org.springframework.util.Assert; /** * Default implementation of {@link RepositoryInformation}. - * + * * @author Oliver Gierke */ class DefaultRepositoryInformation implements RepositoryInformation { - @SuppressWarnings("rawtypes") - private static final TypeVariable>[] PARAMETERS = Repository.class.getTypeParameters(); - private static final String DOMAIN_TYPE_NAME = PARAMETERS[0].getName(); - private static final String ID_TYPE_NAME = PARAMETERS[1].getName(); + @SuppressWarnings("rawtypes") + private static final TypeVariable>[] PARAMETERS = Repository.class.getTypeParameters(); + private static final String DOMAIN_TYPE_NAME = PARAMETERS[0].getName(); + private static final String ID_TYPE_NAME = PARAMETERS[1].getName(); - private final Map methodCache = new ConcurrentHashMap(); + private final Map methodCache = new ConcurrentHashMap(); - private final RepositoryMetadata metadata; - private final Class repositoryBaseClass; + private final RepositoryMetadata metadata; + private final Class repositoryBaseClass; - /** - * Creates a new {@link DefaultRepositoryMetadata} for the given repository interface and repository base class. - * - * @param repositoryInterface - */ - public DefaultRepositoryInformation(RepositoryMetadata metadata, Class repositoryBaseClass) { + /** + * Creates a new {@link DefaultRepositoryMetadata} for the given repository interface and repository base class. + * + * @param repositoryInterface + */ + public DefaultRepositoryInformation(RepositoryMetadata metadata, Class repositoryBaseClass) { - Assert.notNull(metadata); - Assert.notNull(repositoryBaseClass); - this.metadata = metadata; - this.repositoryBaseClass = repositoryBaseClass; - } + Assert.notNull(metadata); + Assert.notNull(repositoryBaseClass); + this.metadata = metadata; + this.repositoryBaseClass = repositoryBaseClass; + } - /* (non-Javadoc) - * @see org.springframework.data.repository.support.RepositoryMetadata#getRepositoryInterface() - */ - public Class getRepositoryInterface() { - return metadata.getRepositoryInterface(); - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.RepositoryMetadata#getDomainClass() - */ - public Class getDomainClass() { - return metadata.getDomainClass(); - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.RepositoryMetadata#getIdClass() - */ - public Class getIdClass() { - return metadata.getIdClass(); - } + /* (non-Javadoc) + * @see org.springframework.data.repository.support.RepositoryMetadata#getRepositoryInterface() + */ + public Class getRepositoryInterface() { + return metadata.getRepositoryInterface(); + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.support.RepositoryMetadata# - * getRepositoryBaseClass() - */ - public Class getRepositoryBaseClass() { + /* (non-Javadoc) + * @see org.springframework.data.repository.support.RepositoryMetadata#getDomainClass() + */ + public Class getDomainClass() { + return metadata.getDomainClass(); + } - return this.repositoryBaseClass; - } + /* (non-Javadoc) + * @see org.springframework.data.repository.support.RepositoryMetadata#getIdClass() + */ + public Class getIdClass() { + return metadata.getIdClass(); + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.support.RepositoryMetadata# - * getBaseClassMethod(java.lang.reflect.Method) - */ - public Method getBaseClassMethod(Method method) { + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.support.RepositoryMetadata# + * getRepositoryBaseClass() + */ + public Class getRepositoryBaseClass() { - Assert.notNull(method); + return this.repositoryBaseClass; + } - Method result = methodCache.get(method); + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.support.RepositoryMetadata# + * getBaseClassMethod(java.lang.reflect.Method) + */ + public Method getBaseClassMethod(Method method) { - if (null != result) { - return result; - } + Assert.notNull(method); - result = getBaseClassMethodFor(method); - methodCache.put(method, result); + Method result = methodCache.get(method); - return result; - } + if (null != result) { + return result; + } - /** - * Returns whether the given method is considered to be a repository base class method. - * - * @param method - * @return - */ - private boolean isBaseClassMethod(Method method) { + result = getBaseClassMethodFor(method); + methodCache.put(method, result); - Assert.notNull(method); + return result; + } - if (method.getDeclaringClass().isAssignableFrom(repositoryBaseClass)) { - return true; - } + /** + * Returns whether the given method is considered to be a repository base class method. + * + * @param method + * @return + */ + private boolean isBaseClassMethod(Method method) { - return !method.equals(getBaseClassMethod(method)); - } + Assert.notNull(method); - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.support.RepositoryMetadata# - * getFinderMethods() - */ - public Iterable getQueryMethods() { + if (method.getDeclaringClass().isAssignableFrom(repositoryBaseClass)) { + return true; + } - Set result = new HashSet(); + return !method.equals(getBaseClassMethod(method)); + } - for (Method method : getRepositoryInterface().getDeclaredMethods()) { - if (!isCustomMethod(method) && !isBaseClassMethod(method)) { - result.add(method); - } - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.support.RepositoryMetadata# + * getFinderMethods() + */ + public Iterable getQueryMethods() { - return result; - } + Set result = new HashSet(); - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.RepositoryMetadata#isCustomMethod - * (java.lang.reflect.Method) - */ - public boolean isCustomMethod(Method method) { + for (Method method : getRepositoryInterface().getDeclaredMethods()) { + if (!isCustomMethod(method) && !isBaseClassMethod(method)) { + result.add(method); + } + } - Class declaringClass = method.getDeclaringClass(); + return result; + } - boolean isQueryMethod = declaringClass.equals(getRepositoryInterface()); - boolean isRepositoryInterface = isGenericRepositoryInterface(declaringClass); - boolean isBaseClassMethod = isBaseClassMethod(method); + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.RepositoryMetadata#isCustomMethod + * (java.lang.reflect.Method) + */ + public boolean isCustomMethod(Method method) { - return !(isRepositoryInterface || isBaseClassMethod || isQueryMethod); - } + Class declaringClass = method.getDeclaringClass(); - /** - * Returns the given base class' method if the given method (declared in the repository interface) was also declared - * at the repository base class. Returns the given method if the given base class does not declare the method given. - * Takes generics into account. - * - * @param method - * @return - */ - Method getBaseClassMethodFor(Method method) { + boolean isQueryMethod = declaringClass.equals(getRepositoryInterface()); + boolean isRepositoryInterface = isGenericRepositoryInterface(declaringClass); + boolean isBaseClassMethod = isBaseClassMethod(method); - for (Method baseClassMethod : repositoryBaseClass.getMethods()) { + return !(isRepositoryInterface || isBaseClassMethod || isQueryMethod); + } - // Wrong name - if (!method.getName().equals(baseClassMethod.getName())) { - continue; - } + /** + * Returns the given base class' method if the given method (declared in the repository interface) was also declared + * at the repository base class. Returns the given method if the given base class does not declare the method given. + * Takes generics into account. + * + * @param method + * @return + */ + Method getBaseClassMethodFor(Method method) { - // Wrong number of arguments - if (!(method.getParameterTypes().length == baseClassMethod.getParameterTypes().length)) { - continue; - } + for (Method baseClassMethod : repositoryBaseClass.getMethods()) { - // Check whether all parameters match - if (!parametersMatch(method, baseClassMethod)) { - continue; - } + // Wrong name + if (!method.getName().equals(baseClassMethod.getName())) { + continue; + } - return baseClassMethod; - } + // Wrong number of arguments + if (!(method.getParameterTypes().length == baseClassMethod.getParameterTypes().length)) { + continue; + } - return method; - } + // Check whether all parameters match + if (!parametersMatch(method, baseClassMethod)) { + continue; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.support.RepositoryMetadata# - * hasCustomMethod() - */ - public boolean hasCustomMethod() { + return baseClassMethod; + } - Class repositoryInterface = getRepositoryInterface(); + return method; + } - // No detection required if no typing interface was configured - if (isGenericRepositoryInterface(repositoryInterface)) { - return false; - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.support.RepositoryMetadata# + * hasCustomMethod() + */ + public boolean hasCustomMethod() { - for (Method method : repositoryInterface.getMethods()) { - if (isCustomMethod(method) && !isBaseClassMethod(method)) { - return true; - } - } + Class repositoryInterface = getRepositoryInterface(); - return false; - } + // No detection required if no typing interface was configured + if (isGenericRepositoryInterface(repositoryInterface)) { + return false; + } - /** - * Checks the given method's parameters to match the ones of the given base class method. Matches generic arguments - * agains the ones bound in the given repository interface. - * - * @param method - * @param baseClassMethod - * @return - */ - private boolean parametersMatch(Method method, Method baseClassMethod) { + for (Method method : repositoryInterface.getMethods()) { + if (isCustomMethod(method) && !isBaseClassMethod(method)) { + return true; + } + } - Type[] genericTypes = baseClassMethod.getGenericParameterTypes(); - Class[] types = baseClassMethod.getParameterTypes(); - Class[] methodParameters = method.getParameterTypes(); + return false; + } - for (int i = 0; i < genericTypes.length; i++) { + /** + * Checks the given method's parameters to match the ones of the given base class method. Matches generic arguments + * agains the ones bound in the given repository interface. + * + * @param method + * @param baseClassMethod + * @return + */ + private boolean parametersMatch(Method method, Method baseClassMethod) { - Type type = genericTypes[i]; + Type[] genericTypes = baseClassMethod.getGenericParameterTypes(); + Class[] types = baseClassMethod.getParameterTypes(); + Class[] methodParameters = method.getParameterTypes(); - if (type instanceof TypeVariable) { + for (int i = 0; i < genericTypes.length; i++) { - String name = ((TypeVariable) type).getName(); + Type type = genericTypes[i]; - if (!matchesGenericType(name, methodParameters[i])) { - return false; - } + if (type instanceof TypeVariable) { - } else { + String name = ((TypeVariable) type).getName(); - if (!types[i].equals(methodParameters[i])) { - return false; - } - } - } + if (!matchesGenericType(name, methodParameters[i])) { + return false; + } - return true; - } + } else { - /** - * Checks whether the given parameter type matches the generic type of the given parameter. Thus when {@literal PK} is - * declared, the method ensures that given method parameter is the primary key type declared in the given repository - * interface e.g. - * - * @param name - * @param parameterType - * @return - */ - private boolean matchesGenericType(String name, Class parameterType) { + if (!types[i].equals(methodParameters[i])) { + return false; + } + } + } - Class entityType = getDomainClass(); - Class idClass = getIdClass(); + return true; + } - if (ID_TYPE_NAME.equals(name) && parameterType.equals(idClass)) { - return true; - } + /** + * Checks whether the given parameter type matches the generic type of the given parameter. Thus when {@literal PK} is + * declared, the method ensures that given method parameter is the primary key type declared in the given repository + * interface e.g. + * + * @param name + * @param parameterType + * @return + */ + private boolean matchesGenericType(String name, Class parameterType) { - if (DOMAIN_TYPE_NAME.equals(name) && parameterType.equals(entityType)) { - return true; - } + Class entityType = getDomainClass(); + Class idClass = getIdClass(); - return false; - } + if (ID_TYPE_NAME.equals(name) && parameterType.equals(idClass)) { + return true; + } + + if (DOMAIN_TYPE_NAME.equals(name) && parameterType.equals(entityType)) { + return true; + } + + return false; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryMetadata.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryMetadata.java index f0c2bf4c5..de72a340b 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryMetadata.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DefaultRepositoryMetadata.java @@ -23,66 +23,65 @@ import org.springframework.util.Assert; /** * Default implementation of {@link RepositoryMetadata}. - * + * * @author Oliver Gierke */ public class DefaultRepositoryMetadata implements RepositoryMetadata { - private final Class repositoryInterface; + private final Class repositoryInterface; - /** - * Creates a new {@link DefaultRepositoryMetadata} for the given repository - * interface and repository base class. - * - * @param repositoryInterface - */ - public DefaultRepositoryMetadata(Class repositoryInterface) { + /** + * Creates a new {@link DefaultRepositoryMetadata} for the given repository + * interface and repository base class. + * + * @param repositoryInterface + */ + public DefaultRepositoryMetadata(Class repositoryInterface) { - Assert.notNull(repositoryInterface); - this.repositoryInterface = repositoryInterface; - } + Assert.notNull(repositoryInterface); + this.repositoryInterface = repositoryInterface; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.repository.support.RepositoryMetadata# - * getRepositoryInterface() - */ - public Class getRepositoryInterface() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.repository.support.RepositoryMetadata# + * getRepositoryInterface() + */ + public Class getRepositoryInterface() { - return repositoryInterface; - } + return repositoryInterface; + } + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.RepositoryMetadata#getDomainClass + * () + */ + public Class getDomainClass() { - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.RepositoryMetadata#getDomainClass - * () - */ - public Class getDomainClass() { - - Class[] arguments = - resolveTypeArguments(repositoryInterface, Repository.class); - return arguments == null ? null : arguments[0]; - } + Class[] arguments = + resolveTypeArguments(repositoryInterface, Repository.class); + return arguments == null ? null : arguments[0]; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.RepositoryMetadata#getIdClass - * () - */ - public Class getIdClass() { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.RepositoryMetadata#getIdClass + * () + */ + public Class getIdClass() { - Class[] arguments = - resolveTypeArguments(repositoryInterface, Repository.class); - return arguments == null ? null : arguments[1]; - } + Class[] arguments = + resolveTypeArguments(repositoryInterface, Repository.class); + return arguments == null ? null : arguments[1]; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java index aa655d2da..31c233dce 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassConverter.java @@ -29,8 +29,6 @@ import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.ConditionalGenericConverter; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; -import org.springframework.data.repository.support.RepositoryFactoryInformation; /** @@ -39,121 +37,121 @@ import org.springframework.data.repository.support.RepositoryFactoryInformation; * s. The implementation uses a {@link ConversionService} in turn to convert the * source type into the domain class' id type which is then converted into a * domain class object by using a {@link Repository}. - * + * * @author Oliver Gierke */ public class DomainClassConverter implements ConditionalGenericConverter, - ApplicationContextAware { + ApplicationContextAware { - private final Map, Repository> repositories = - new HashMap, Repository>(); - private final ConversionService service; + private final Map, Repository> repositories = + new HashMap, Repository>(); + private final ConversionService service; - /** - * Creates a new {@link DomainClassConverter}. - * - * @param service - */ - public DomainClassConverter(ConversionService service) { + /** + * Creates a new {@link DomainClassConverter}. + * + * @param service + */ + public DomainClassConverter(ConversionService service) { - this.service = service; - } + this.service = service; + } - /* - * (non-Javadoc) - * - * @see org.springframework.core.convert.converter.GenericConverter# - * getConvertibleTypes() - */ - public Set getConvertibleTypes() { + /* + * (non-Javadoc) + * + * @see org.springframework.core.convert.converter.GenericConverter# + * getConvertibleTypes() + */ + public Set getConvertibleTypes() { - return Collections.singleton(new ConvertiblePair(Object.class, - Object.class)); - } + return Collections.singleton(new ConvertiblePair(Object.class, + Object.class)); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.core.convert.converter.GenericConverter#convert(java - * .lang.Object, org.springframework.core.convert.TypeDescriptor, - * org.springframework.core.convert.TypeDescriptor) - */ - public Object convert(Object source, TypeDescriptor sourceType, - TypeDescriptor targetType) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.core.convert.converter.GenericConverter#convert(java + * .lang.Object, org.springframework.core.convert.TypeDescriptor, + * org.springframework.core.convert.TypeDescriptor) + */ + public Object convert(Object source, TypeDescriptor sourceType, + TypeDescriptor targetType) { - EntityInformation info = - getRepositoryForDomainType(targetType.getType()); + EntityInformation info = + getRepositoryForDomainType(targetType.getType()); - Repository repository = repositories.get(info); - Serializable id = service.convert(source, info.getIdType()); - return repository.findOne(id); - } + Repository repository = repositories.get(info); + Serializable id = service.convert(source, info.getIdType()); + return repository.findOne(id); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.core.convert.converter.ConditionalGenericConverter - * #matches(org.springframework.core.convert.TypeDescriptor, - * org.springframework.core.convert.TypeDescriptor) - */ - public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.core.convert.converter.ConditionalGenericConverter + * #matches(org.springframework.core.convert.TypeDescriptor, + * org.springframework.core.convert.TypeDescriptor) + */ + public boolean matches(TypeDescriptor sourceType, TypeDescriptor targetType) { - EntityInformation info = - getRepositoryForDomainType(targetType.getType()); + EntityInformation info = + getRepositoryForDomainType(targetType.getType()); - if (info == null) { - return false; - } + if (info == null) { + return false; + } - return service.canConvert(sourceType.getType(), info.getIdType()); - } + return service.canConvert(sourceType.getType(), info.getIdType()); + } - private EntityInformation getRepositoryForDomainType( - Class domainType) { + private EntityInformation getRepositoryForDomainType( + Class domainType) { - for (EntityInformation information : repositories - .keySet()) { + for (EntityInformation information : repositories + .keySet()) { - if (domainType.equals(information.getJavaType())) { - return information; - } - } + if (domainType.equals(information.getJavaType())) { + return information; + } + } - return null; - } + return null; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.context.ApplicationContextAware#setApplicationContext - * (org.springframework.context.ApplicationContext) - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void setApplicationContext(ApplicationContext context) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.context.ApplicationContextAware#setApplicationContext + * (org.springframework.context.ApplicationContext) + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public void setApplicationContext(ApplicationContext context) { - Collection providers = - BeanFactoryUtils.beansOfTypeIncludingAncestors(context, - RepositoryFactoryInformation.class).values(); + Collection providers = + BeanFactoryUtils.beansOfTypeIncludingAncestors(context, + RepositoryFactoryInformation.class).values(); - for (RepositoryFactoryInformation entry : providers) { + for (RepositoryFactoryInformation entry : providers) { - EntityInformation metadata = - entry.getEntityInformation(); - Class> objectType = - entry.getRepositoryInterface(); - Repository repository = - BeanFactoryUtils.beanOfType(context, objectType); + EntityInformation metadata = + entry.getEntityInformation(); + Class> objectType = + entry.getRepositoryInterface(); + Repository repository = + BeanFactoryUtils.beanOfType(context, objectType); - this.repositories.put(metadata, repository); - } - } + this.repositories.put(metadata, repository); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java index 22125a024..7e4a5befa 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditor.java @@ -22,7 +22,6 @@ import java.io.Serializable; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.SimpleTypeConverter; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -30,151 +29,151 @@ import org.springframework.util.StringUtils; /** * Generic {@link PropertyEditor} to map entities handled by a * {@link Repository} to their id's and vice versa. - * + * * @author Oliver Gierke */ public class DomainClassPropertyEditor extends - PropertyEditorSupport { + PropertyEditorSupport { - private final Repository repository; - private final EntityInformation information; - private final PropertyEditorRegistry registry; + private final Repository repository; + private final EntityInformation information; + private final PropertyEditorRegistry registry; - /** - * Creates a new {@link DomainClassPropertyEditor} for the given - * {@link Repository}. - * - * @param repository - * @param registry - */ - public DomainClassPropertyEditor(Repository repository, - EntityInformation information, - PropertyEditorRegistry registry) { + /** + * Creates a new {@link DomainClassPropertyEditor} for the given + * {@link Repository}. + * + * @param repository + * @param registry + */ + public DomainClassPropertyEditor(Repository repository, + EntityInformation information, + PropertyEditorRegistry registry) { - Assert.notNull(repository); - Assert.notNull(registry); + Assert.notNull(repository); + Assert.notNull(registry); - this.repository = repository; - this.information = information; - this.registry = registry; - } + this.repository = repository; + this.information = information; + this.registry = registry; + } - /* - * (non-Javadoc) - * - * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) - */ - @Override - public void setAsText(String idAsString) throws IllegalArgumentException { + /* + * (non-Javadoc) + * + * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) + */ + @Override + public void setAsText(String idAsString) throws IllegalArgumentException { - if (!StringUtils.hasText(idAsString)) { - setValue(null); - return; - } + if (!StringUtils.hasText(idAsString)) { + setValue(null); + return; + } - setValue(repository.findOne(getId(idAsString))); - } + setValue(repository.findOne(getId(idAsString))); + } - /* - * (non-Javadoc) - * - * @see java.beans.PropertyEditorSupport#getAsText() - */ - @Override - @SuppressWarnings("unchecked") - public String getAsText() { + /* + * (non-Javadoc) + * + * @see java.beans.PropertyEditorSupport#getAsText() + */ + @Override + @SuppressWarnings("unchecked") + public String getAsText() { - T entity = (T) getValue(); + T entity = (T) getValue(); - if (null == entity) { - return null; - } + if (null == entity) { + return null; + } - Object id = getId(entity); - return id == null ? null : id.toString(); - } + Object id = getId(entity); + return id == null ? null : id.toString(); + } - /** - * Looks up the id of the given entity using one of the - * {@link org.synyx.hades.dao.orm.GenericDaoSupport.IdAware} implementations - * of Hades. - * - * @param entity - * @return - */ - private ID getId(T entity) { + /** + * Looks up the id of the given entity using one of the + * {@link org.synyx.hades.dao.orm.GenericDaoSupport.IdAware} implementations + * of Hades. + * + * @param entity + * @return + */ + private ID getId(T entity) { - return information.getId(entity); - } + return information.getId(entity); + } - /** - * Returns the actual typed id. Looks up an available customly registered - * {@link PropertyEditor} from the {@link PropertyEditorRegistry} before - * falling back on a {@link SimpleTypeConverter} to translate the - * {@link String} id into the type one. - * - * @param idAsString - * @return - */ - @SuppressWarnings("unchecked") - private ID getId(String idAsString) { + /** + * Returns the actual typed id. Looks up an available customly registered + * {@link PropertyEditor} from the {@link PropertyEditorRegistry} before + * falling back on a {@link SimpleTypeConverter} to translate the + * {@link String} id into the type one. + * + * @param idAsString + * @return + */ + @SuppressWarnings("unchecked") + private ID getId(String idAsString) { - Class idClass = information.getIdType(); + Class idClass = information.getIdType(); - PropertyEditor idEditor = registry.findCustomEditor(idClass, null); + PropertyEditor idEditor = registry.findCustomEditor(idClass, null); - if (idEditor != null) { - idEditor.setAsText(idAsString); - return (ID) idEditor.getValue(); - } + if (idEditor != null) { + idEditor.setAsText(idAsString); + return (ID) idEditor.getValue(); + } - return new SimpleTypeConverter() - .convertIfNecessary(idAsString, idClass); - } + return new SimpleTypeConverter() + .convertIfNecessary(idAsString, idClass); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { - if (this == obj) { - return true; - } + if (this == obj) { + return true; + } - if (obj == null || this.getClass() != obj.getClass()) { - return false; - } + if (obj == null || this.getClass() != obj.getClass()) { + return false; + } - DomainClassPropertyEditor that = - (DomainClassPropertyEditor) obj; + DomainClassPropertyEditor that = + (DomainClassPropertyEditor) obj; - return this.repository.equals(that.repository) - && this.registry.equals(that.registry) - && this.information.equals(that.information); - } + return this.repository.equals(that.repository) + && this.registry.equals(that.registry) + && this.information.equals(that.information); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { - int hashCode = 17; - hashCode += repository.hashCode() * 32; - hashCode += information.hashCode() * 32; - hashCode += registry.hashCode() * 32; - return hashCode; - } + int hashCode = 17; + hashCode += repository.hashCode() * 32; + hashCode += information.hashCode() * 32; + hashCode += registry.hashCode() * 32; + return hashCode; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java index 30a0870e3..031520fe4 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrar.java @@ -27,8 +27,6 @@ import org.springframework.beans.factory.BeanFactoryUtils; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; -import org.springframework.data.repository.support.RepositoryFactoryInformation; /** @@ -40,76 +38,76 @@ import org.springframework.data.repository.support.RepositoryFactoryInformation; * {@link org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter} * and register the {@link DomainClassPropertyEditorRegistrar} there: * <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> - * <property name="webBindingInitializer"> - * <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> - * <property name="propertyEditorRegistrars"> - * <bean class="org.springframework.data.extensions.beans.DomainClassPropertyEditorRegistrar" /> - * </property> - * </bean> - * </property> + * <property name="webBindingInitializer"> + * <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> + * <property name="propertyEditorRegistrars"> + * <bean class="org.springframework.data.extensions.beans.DomainClassPropertyEditorRegistrar" /> + * </property> + * </bean> + * </property> * </bean> * Make sure this bean declaration is in the {@link ApplicationContext} * created by the {@link DispatcherServlet} whereas the repositories need to be * declared in the root * {@link org.springframework.web.context.WebApplicationContext}. - * + * * @author Oliver Gierke */ public class DomainClassPropertyEditorRegistrar implements - PropertyEditorRegistrar, ApplicationContextAware { + PropertyEditorRegistrar, ApplicationContextAware { - private final Map, Repository> repositories = - new HashMap, Repository>(); + private final Map, Repository> repositories = + new HashMap, Repository>(); - /* - * (non-Javadoc) - * - * @see - * org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors - * (org.springframework.beans.PropertyEditorRegistry) - */ - public void registerCustomEditors(PropertyEditorRegistry registry) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.PropertyEditorRegistrar#registerCustomEditors + * (org.springframework.beans.PropertyEditorRegistry) + */ + public void registerCustomEditors(PropertyEditorRegistry registry) { - for (Entry, Repository> entry : repositories - .entrySet()) { + for (Entry, Repository> entry : repositories + .entrySet()) { - EntityInformation metadata = entry.getKey(); - Repository repository = entry.getValue(); + EntityInformation metadata = entry.getKey(); + Repository repository = entry.getValue(); - DomainClassPropertyEditor editor = - new DomainClassPropertyEditor( - repository, metadata, registry); + DomainClassPropertyEditor editor = + new DomainClassPropertyEditor( + repository, metadata, registry); - registry.registerCustomEditor(metadata.getJavaType(), editor); - } - } + registry.registerCustomEditor(metadata.getJavaType(), editor); + } + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.context.ApplicationContextAware#setApplicationContext - * (org.springframework.context.ApplicationContext) - */ - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void setApplicationContext(ApplicationContext context) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.context.ApplicationContextAware#setApplicationContext + * (org.springframework.context.ApplicationContext) + */ + @SuppressWarnings({"unchecked", "rawtypes"}) + public void setApplicationContext(ApplicationContext context) { - Collection providers = - BeanFactoryUtils.beansOfTypeIncludingAncestors(context, - RepositoryFactoryInformation.class).values(); + Collection providers = + BeanFactoryUtils.beansOfTypeIncludingAncestors(context, + RepositoryFactoryInformation.class).values(); - for (RepositoryFactoryInformation information : providers) { + for (RepositoryFactoryInformation information : providers) { - EntityInformation metadata = - information.getEntityInformation(); - Class> objectType = - information.getRepositoryInterface(); - Repository repository = - BeanFactoryUtils.beanOfType(context, objectType); + EntityInformation metadata = + information.getEntityInformation(); + Class> objectType = + information.getRepositoryInterface(); + Repository repository = + BeanFactoryUtils.beanOfType(context, objectType); - this.repositories.put(metadata, repository); - } - } + this.repositories.put(metadata, repository); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityInformation.java index 8ccf3b538..d2760d03e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityInformation.java @@ -20,32 +20,32 @@ import java.io.Serializable; /** * Extension of {@link EntityMetadata} to add functionality to query information * of entity instances. - * + * * @author Oliver Gierke */ public interface EntityInformation extends EntityMetadata { - /** - * Returns whether the given entity is considered to be new. - * - * @param entity must never be {@literal null} - * @return - */ - boolean isNew(T entity); + /** + * Returns whether the given entity is considered to be new. + * + * @param entity must never be {@literal null} + * @return + */ + boolean isNew(T entity); - /** - * Returns the id of the given entity. - * - * @param entity must never be {@literal null} - * @return - */ - ID getId(T entity); - - /** - * Returns the type of the id of the entity. - * - * @return - */ - Class getIdType(); + /** + * Returns the id of the given entity. + * + * @param entity must never be {@literal null} + * @return + */ + ID getId(T entity); + + /** + * Returns the type of the id of the entity. + * + * @return + */ + Class getIdType(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityMetadata.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityMetadata.java index ffc93e070..4963359c3 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityMetadata.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/EntityMetadata.java @@ -17,15 +17,15 @@ package org.springframework.data.repository.support; /** * Metadata for entity types. - * + * * @author Oliver Gierke */ public interface EntityMetadata { - /** - * Returns the actual domain class type. - * - * @return - */ - Class getJavaType(); + /** + * Returns the actual domain class type. + * + * @return + */ + Class getJavaType(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/PersistableEntityInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/PersistableEntityInformation.java index d99c74314..f2619dcdb 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/PersistableEntityInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/PersistableEntityInformation.java @@ -25,57 +25,57 @@ import org.springframework.data.domain.Persistable; * Implementation of {@link EntityMetadata} that assumes the entity handled * implements {@link Persistable} and uses {@link Persistable#isNew()} for the * {@link #isNew(Object)} check. - * + * * @author Oliver Gierke */ public class PersistableEntityInformation, ID extends Serializable> extends - AbstractEntityInformation { - - private Class idClass; + AbstractEntityInformation { - /** - * Creates a new {@link PersistableEntityInformation}. - * - * @param domainClass - */ - @SuppressWarnings("unchecked") - public PersistableEntityInformation(Class domainClass) { + private Class idClass; - super(domainClass); - this.idClass = (Class) GenericTypeResolver.resolveTypeArgument(domainClass, Persistable.class); - } + /** + * Creates a new {@link PersistableEntityInformation}. + * + * @param domainClass + */ + @SuppressWarnings("unchecked") + public PersistableEntityInformation(Class domainClass) { + + super(domainClass); + this.idClass = (Class) GenericTypeResolver.resolveTypeArgument(domainClass, Persistable.class); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.IsNewAware#isNew(java.lang - * .Object) - */ - @Override - public boolean isNew(T entity) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.IsNewAware#isNew(java.lang + * .Object) + */ + @Override + public boolean isNew(T entity) { - return entity.isNew(); - } + return entity.isNew(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.IdAware#getId(java.lang.Object - * ) - */ - public ID getId(T entity) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.IdAware#getId(java.lang.Object + * ) + */ + public ID getId(T entity) { - return entity.getId(); - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.EntityInformation#getIdType() - */ - public Class getIdType() { - return this.idClass; - } + return entity.getId(); + } + + /* (non-Javadoc) + * @see org.springframework.data.repository.support.EntityInformation#getIdType() + */ + public Class getIdType() { + return this.idClass; + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/QueryCreationListener.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/QueryCreationListener.java index b704dfa78..0ffb5e8f6 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/QueryCreationListener.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/QueryCreationListener.java @@ -21,15 +21,15 @@ import org.springframework.data.repository.query.RepositoryQuery; /** * Callback for listeners that want to execute functionality on * {@link RepositoryQuery} creation. - * + * * @author Oliver Gierke */ public interface QueryCreationListener { - /** - * Will be invoked just after the {@link RepositoryQuery} was created. - * - * @param query - */ - void onCreation(T query); + /** + * Will be invoked just after the {@link RepositoryQuery} was created. + * + * @param query + */ + void onCreation(T query); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryBeanSupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryBeanSupport.java index ce574b292..f1756bc67 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryBeanSupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryBeanSupport.java @@ -29,126 +29,126 @@ import org.springframework.util.Assert; /** * Adapter for Springs {@link FactoryBean} interface to allow easy setup of * repository factories via Spring configuration. - * - * @author Oliver Gierke + * * @param the type of the repository + * @author Oliver Gierke */ public abstract class RepositoryFactoryBeanSupport, S, ID extends Serializable> - implements InitializingBean, RepositoryFactoryInformation, FactoryBean { + implements InitializingBean, RepositoryFactoryInformation, FactoryBean { - private RepositoryFactorySupport factory; + private RepositoryFactorySupport factory; - private Key queryLookupStrategyKey; - private Class repositoryInterface; - private Object customImplementation; + private Key queryLookupStrategyKey; + private Class repositoryInterface; + private Object customImplementation; - /** - * Setter to inject the repository interface to implement. - * - * @param repositoryInterface the repository interface to set - */ - @Required - public void setRepositoryInterface(Class repositoryInterface) { + /** + * Setter to inject the repository interface to implement. + * + * @param repositoryInterface the repository interface to set + */ + @Required + public void setRepositoryInterface(Class repositoryInterface) { - Assert.notNull(repositoryInterface); - this.repositoryInterface = repositoryInterface; - } + Assert.notNull(repositoryInterface); + this.repositoryInterface = repositoryInterface; + } - /** - * Set the {@link QueryLookupStrategy.Key} to be used. - * - * @param queryLookupStrategyKey - */ - public void setQueryLookupStrategyKey(Key queryLookupStrategyKey) { + /** + * Set the {@link QueryLookupStrategy.Key} to be used. + * + * @param queryLookupStrategyKey + */ + public void setQueryLookupStrategyKey(Key queryLookupStrategyKey) { - this.queryLookupStrategyKey = queryLookupStrategyKey; - } + this.queryLookupStrategyKey = queryLookupStrategyKey; + } - /** - * Setter to inject a custom repository implementation. - * - * @param customImplementation - */ - public void setCustomImplementation(Object customImplementation) { + /** + * Setter to inject a custom repository implementation. + * + * @param customImplementation + */ + public void setCustomImplementation(Object customImplementation) { - this.customImplementation = customImplementation; - } - - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.EntityMetadataProvider#getEntityMetadata() - */ - @SuppressWarnings("unchecked") - public EntityInformation getEntityInformation() { - - RepositoryMetadata repositoryMetadata = factory.getRepositoryMetadata(repositoryInterface); - return (EntityInformation) factory.getEntityInformation(repositoryMetadata.getDomainClass()); - } - - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.RepositoryFactoryInformation#getRepositoryInterface() - */ - public Class getRepositoryInterface() { - - return repositoryInterface; - } - - /* - * (non-Javadoc) - * - * @see org.springframework.beans.factory.FactoryBean#getObject() - */ - public T getObject() { - - return factory.getRepository(repositoryInterface, customImplementation); - } + this.customImplementation = customImplementation; + } - /* - * (non-Javadoc) - * - * @see org.springframework.beans.factory.FactoryBean#getObjectType() - */ - @SuppressWarnings("unchecked") - public Class getObjectType() { + /* (non-Javadoc) + * @see org.springframework.data.repository.support.EntityMetadataProvider#getEntityMetadata() + */ + @SuppressWarnings("unchecked") + public EntityInformation getEntityInformation() { - return (Class) (null == repositoryInterface ? Repository.class - : repositoryInterface); - } + RepositoryMetadata repositoryMetadata = factory.getRepositoryMetadata(repositoryInterface); + return (EntityInformation) factory.getEntityInformation(repositoryMetadata.getDomainClass()); + } - /* - * (non-Javadoc) - * - * @see org.springframework.beans.factory.FactoryBean#isSingleton() - */ - public boolean isSingleton() { + /* (non-Javadoc) + * @see org.springframework.data.repository.support.RepositoryFactoryInformation#getRepositoryInterface() + */ + public Class getRepositoryInterface() { - return true; - } + return repositoryInterface; + } + + /* + * (non-Javadoc) + * + * @see org.springframework.beans.factory.FactoryBean#getObject() + */ + public T getObject() { + + return factory.getRepository(repositoryInterface, customImplementation); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() - */ - public void afterPropertiesSet() { + /* + * (non-Javadoc) + * + * @see org.springframework.beans.factory.FactoryBean#getObjectType() + */ + @SuppressWarnings("unchecked") + public Class getObjectType() { - this.factory = createRepositoryFactory(); - this.factory.setQueryLookupStrategyKey(queryLookupStrategyKey); - } + return (Class) (null == repositoryInterface ? Repository.class + : repositoryInterface); + } - /** - * Create the actual {@link RepositoryFactorySupport} instance. - * - * @return - */ - protected abstract RepositoryFactorySupport createRepositoryFactory(); + /* + * (non-Javadoc) + * + * @see org.springframework.beans.factory.FactoryBean#isSingleton() + */ + public boolean isSingleton() { + + return true; + } + + + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() + */ + public void afterPropertiesSet() { + + this.factory = createRepositoryFactory(); + this.factory.setQueryLookupStrategyKey(queryLookupStrategyKey); + } + + + /** + * Create the actual {@link RepositoryFactorySupport} instance. + * + * @return + */ + protected abstract RepositoryFactorySupport createRepositoryFactory(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryInformation.java index 1a2a0831c..1b434365c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactoryInformation.java @@ -23,23 +23,23 @@ import org.springframework.data.repository.Repository; /** * Interface for components that can provide {@link EntityInformation} this * interface - * + * * @author Oliver Gierke */ public interface RepositoryFactoryInformation { - /** - * Returns {@link EntityInformation} the repository factory is using. - * - * @return - */ - EntityInformation getEntityInformation(); + /** + * Returns {@link EntityInformation} the repository factory is using. + * + * @return + */ + EntityInformation getEntityInformation(); - /** - * Returns the interface of the {@link Repository} the factory will create. - * - * @return - */ - Class> getRepositoryInterface(); + /** + * Returns the interface of the {@link Repository} the factory will create. + * + * @return + */ + Class> getRepositoryInterface(); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java index c4ecf35dd..883033a9e 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryFactorySupport.java @@ -43,351 +43,351 @@ import org.springframework.util.Assert; * handing the control to the {@code QueryExecuterMethodInterceptor}. Query * detection strategy can be configured by setting * {@link QueryLookupStrategy.Key}. - * + * * @author Oliver Gierke */ public abstract class RepositoryFactorySupport { - private final List postProcessors = - new ArrayList(); - private QueryLookupStrategy.Key queryLookupStrategyKey; - private List> queryPostProcessors = - new ArrayList>(); + private final List postProcessors = + new ArrayList(); + private QueryLookupStrategy.Key queryLookupStrategyKey; + private List> queryPostProcessors = + new ArrayList>(); - /** - * Sets the strategy of how to lookup a query to execute finders. - * - * @param queryLookupStrategy the createFinderQueries to set - */ - public void setQueryLookupStrategyKey(Key key) { + /** + * Sets the strategy of how to lookup a query to execute finders. + * + * @param queryLookupStrategy the createFinderQueries to set + */ + public void setQueryLookupStrategyKey(Key key) { - this.queryLookupStrategyKey = key; - } + this.queryLookupStrategyKey = key; + } - /** - * Adds a {@link QueryCreationListener} to the factory to plug in - * functionality triggered right after creation of {@link RepositoryQuery} - * instances. - * - * @param listener - */ - public void addQueryCreationListener(QueryCreationListener listener) { + /** + * Adds a {@link QueryCreationListener} to the factory to plug in + * functionality triggered right after creation of {@link RepositoryQuery} + * instances. + * + * @param listener + */ + public void addQueryCreationListener(QueryCreationListener listener) { - Assert.notNull(listener); - this.queryPostProcessors.add(listener); - } + Assert.notNull(listener); + this.queryPostProcessors.add(listener); + } - /** - * Adds {@link RepositoryProxyPostProcessor}s to the factory to allow - * manipulation of the {@link ProxyFactory} before the proxy gets created. - * Note that the {@link QueryExecuterMethodInterceptor} will be added to the - * proxy after the {@link RepositoryProxyPostProcessor}s are - * considered. - * - * @param processor - */ - public void addRepositoryProxyPostProcessor( - RepositoryProxyPostProcessor processor) { + /** + * Adds {@link RepositoryProxyPostProcessor}s to the factory to allow + * manipulation of the {@link ProxyFactory} before the proxy gets created. + * Note that the {@link QueryExecuterMethodInterceptor} will be added to the + * proxy after the {@link RepositoryProxyPostProcessor}s are + * considered. + * + * @param processor + */ + public void addRepositoryProxyPostProcessor( + RepositoryProxyPostProcessor processor) { - Assert.notNull(processor); - this.postProcessors.add(processor); - } + Assert.notNull(processor); + this.postProcessors.add(processor); + } - /** - * Returns a repository instance for the given interface. - * - * @param - * @param repositoryInterface - * @return - */ - public > T getRepository( - Class repositoryInterface) { + /** + * Returns a repository instance for the given interface. + * + * @param + * @param repositoryInterface + * @return + */ + public > T getRepository( + Class repositoryInterface) { - return getRepository(repositoryInterface, null); - } + return getRepository(repositoryInterface, null); + } - /** - * Returns a repository instance for the given interface backed by an - * instance providing implementation logic for custom logic. - * - * @param - * @param repositoryInterface - * @param customImplementation - * @return - */ - @SuppressWarnings({ "unchecked" }) - public T getRepository(Class repositoryInterface, - Object customImplementation) { + /** + * Returns a repository instance for the given interface backed by an + * instance providing implementation logic for custom logic. + * + * @param + * @param repositoryInterface + * @param customImplementation + * @return + */ + @SuppressWarnings({"unchecked"}) + public T getRepository(Class repositoryInterface, + Object customImplementation) { - RepositoryMetadata metadata = getRepositoryMetadata(repositoryInterface); - RepositoryInformation information = getRepositoryInformation(metadata); + RepositoryMetadata metadata = getRepositoryMetadata(repositoryInterface); + RepositoryInformation information = getRepositoryInformation(metadata); - validate(information, customImplementation); + validate(information, customImplementation); - Object target = getTargetRepository(information); + Object target = getTargetRepository(information); - // Create proxy - ProxyFactory result = new ProxyFactory(); - result.setTarget(target); - result.setInterfaces(new Class[] { repositoryInterface }); + // Create proxy + ProxyFactory result = new ProxyFactory(); + result.setTarget(target); + result.setInterfaces(new Class[]{repositoryInterface}); - for (RepositoryProxyPostProcessor processor : postProcessors) { - processor.postProcess(result); - } + for (RepositoryProxyPostProcessor processor : postProcessors) { + processor.postProcess(result); + } - result.addAdvice(new QueryExecuterMethodInterceptor(information, - customImplementation, target)); + result.addAdvice(new QueryExecuterMethodInterceptor(information, + customImplementation, target)); - return (T) result.getProxy(); - } - - /** - * Returns the {@link RepositoryMetadata} for the given repository interface. - * - * @param repositoryInterface - * @return - */ - RepositoryMetadata getRepositoryMetadata(Class repositoryInterface) { - return new DefaultRepositoryMetadata(repositoryInterface); - } - - - /** - * Returns the {@link RepositoryInformation} for the given repository interface. - * - * @param repositoryInterface - * @return - */ - private RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata) { - return new DefaultRepositoryInformation(metadata, getRepositoryBaseClass(metadata)); - } - - - /** - * Returns the {@link EntityInformation} for the given domain class. - * - * @param the entity type - * @param the id type - * @param domainClass - * @return - */ - public abstract EntityInformation getEntityInformation(Class domainClass); + return (T) result.getProxy(); + } + + /** + * Returns the {@link RepositoryMetadata} for the given repository interface. + * + * @param repositoryInterface + * @return + */ + RepositoryMetadata getRepositoryMetadata(Class repositoryInterface) { + return new DefaultRepositoryMetadata(repositoryInterface); + } - /** - * Create a repository instance as backing for the query proxy. - * - * @param metadata - * @return - */ - protected abstract Object getTargetRepository(RepositoryMetadata metadata); + /** + * Returns the {@link RepositoryInformation} for the given repository interface. + * + * @param repositoryInterface + * @return + */ + private RepositoryInformation getRepositoryInformation(RepositoryMetadata metadata) { + return new DefaultRepositoryInformation(metadata, getRepositoryBaseClass(metadata)); + } - /** - * Returns the base class backing the actual repository instance. Make sure - * {@link #getTargetRepository(RepositoryMetadata)} returns an instance of - * this class. - * - * @param metadata - * @return - */ - protected abstract Class getRepositoryBaseClass( - RepositoryMetadata metadata); + /** + * Returns the {@link EntityInformation} for the given domain class. + * + * @param the entity type + * @param the id type + * @param domainClass + * @return + */ + public abstract EntityInformation getEntityInformation(Class domainClass); + + + /** + * Create a repository instance as backing for the query proxy. + * + * @param metadata + * @return + */ + protected abstract Object getTargetRepository(RepositoryMetadata metadata); + + + /** + * Returns the base class backing the actual repository instance. Make sure + * {@link #getTargetRepository(RepositoryMetadata)} returns an instance of + * this class. + * + * @param metadata + * @return + */ + protected abstract Class getRepositoryBaseClass( + RepositoryMetadata metadata); /** * Returns the {@link QueryLookupStrategy} for the given {@link Key}. - * + * * @param key can be {@literal null} * @return the {@link QueryLookupStrategy} to use or {@literal null} if no queries should be looked up. */ - protected QueryLookupStrategy getQueryLookupStrategy(Key key) { - return null; - } + protected QueryLookupStrategy getQueryLookupStrategy(Key key) { + return null; + } - /** - * Validates the given repository interface as well as the given custom - * implementation. - * - * @param repositoryInformation - * @param customImplementation - */ - private void validate(RepositoryInformation repositoryInformation, - Object customImplementation) { + /** + * Validates the given repository interface as well as the given custom + * implementation. + * + * @param repositoryInformation + * @param customImplementation + */ + private void validate(RepositoryInformation repositoryInformation, + Object customImplementation) { - if (null == customImplementation - && repositoryInformation.hasCustomMethod()) { + if (null == customImplementation + && repositoryInformation.hasCustomMethod()) { - throw new IllegalArgumentException( - String.format( - "You have custom methods in %s but not provided a custom implementation!", - repositoryInformation.getRepositoryInterface())); - } - - validate(repositoryInformation); - } - - protected void validate(RepositoryMetadata repositoryMetadata) { - - } + throw new IllegalArgumentException( + String.format( + "You have custom methods in %s but not provided a custom implementation!", + repositoryInformation.getRepositoryInterface())); + } - /** - * This {@code MethodInterceptor} intercepts calls to methods of the custom - * implementation and delegates the to it if configured. Furthermore it - * resolves method calls to finders and triggers execution of them. You can - * rely on having a custom repository implementation instance set if this - * returns true. - * - * @author Oliver Gierke - */ - public class QueryExecuterMethodInterceptor implements MethodInterceptor { + validate(repositoryInformation); + } - private final Map queries = - new ConcurrentHashMap(); + protected void validate(RepositoryMetadata repositoryMetadata) { - private final Object customImplementation; - private final RepositoryInformation repositoryInformation; - private final Object target; + } + + /** + * This {@code MethodInterceptor} intercepts calls to methods of the custom + * implementation and delegates the to it if configured. Furthermore it + * resolves method calls to finders and triggers execution of them. You can + * rely on having a custom repository implementation instance set if this + * returns true. + * + * @author Oliver Gierke + */ + public class QueryExecuterMethodInterceptor implements MethodInterceptor { + + private final Map queries = + new ConcurrentHashMap(); + + private final Object customImplementation; + private final RepositoryInformation repositoryInformation; + private final Object target; - /** - * Creates a new {@link QueryExecuterMethodInterceptor}. Builds a model - * of {@link QueryMethod}s to be invoked on execution of repository - * interface methods. - */ - public QueryExecuterMethodInterceptor( - RepositoryInformation repositoryInformation, - Object customImplementation, Object target) { + /** + * Creates a new {@link QueryExecuterMethodInterceptor}. Builds a model + * of {@link QueryMethod}s to be invoked on execution of repository + * interface methods. + */ + public QueryExecuterMethodInterceptor( + RepositoryInformation repositoryInformation, + Object customImplementation, Object target) { - this.repositoryInformation = repositoryInformation; - this.customImplementation = customImplementation; - this.target = target; + this.repositoryInformation = repositoryInformation; + this.customImplementation = customImplementation; + this.target = target; - QueryLookupStrategy lookupStrategy = - getQueryLookupStrategy(queryLookupStrategyKey); - - if (lookupStrategy == null) { - - if (repositoryInformation.hasCustomMethod()) { + QueryLookupStrategy lookupStrategy = + getQueryLookupStrategy(queryLookupStrategyKey); + + if (lookupStrategy == null) { + + if (repositoryInformation.hasCustomMethod()) { throw new IllegalStateException( "You have defined query method in the repository but " + - "you don't have no query lookup strategy defined. The " + - "infrastructure apparently does not support query methods!"); - } - - return; - } + "you don't have no query lookup strategy defined. The " + + "infrastructure apparently does not support query methods!"); + } - for (Method method : repositoryInformation.getQueryMethods()) { - RepositoryQuery query = - lookupStrategy.resolveQuery(method,repositoryInformation); - invokeListeners(query); - queries.put(method, query); - } - } + return; + } + + for (Method method : repositoryInformation.getQueryMethods()) { + RepositoryQuery query = + lookupStrategy.resolveQuery(method, repositoryInformation); + invokeListeners(query); + queries.put(method, query); + } + } - @SuppressWarnings({ "rawtypes", "unchecked" }) - private void invokeListeners(RepositoryQuery query) { + @SuppressWarnings({"rawtypes", "unchecked"}) + private void invokeListeners(RepositoryQuery query) { - for (QueryCreationListener listener : queryPostProcessors) { - Class typeArgument = - GenericTypeResolver.resolveTypeArgument( - listener.getClass(), - QueryCreationListener.class); - if (typeArgument != null - && typeArgument.isAssignableFrom(query.getClass())) { - listener.onCreation(query); - } - } - } + for (QueryCreationListener listener : queryPostProcessors) { + Class typeArgument = + GenericTypeResolver.resolveTypeArgument( + listener.getClass(), + QueryCreationListener.class); + if (typeArgument != null + && typeArgument.isAssignableFrom(query.getClass())) { + listener.onCreation(query); + } + } + } - /* - * (non-Javadoc) - * - * @see - * org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance - * .intercept.MethodInvocation) - */ - public Object invoke(MethodInvocation invocation) throws Throwable { + /* + * (non-Javadoc) + * + * @see + * org.aopalliance.intercept.MethodInterceptor#invoke(org.aopalliance + * .intercept.MethodInvocation) + */ + public Object invoke(MethodInvocation invocation) throws Throwable { - Method method = invocation.getMethod(); + Method method = invocation.getMethod(); - if (isCustomMethodInvocation(invocation)) { + if (isCustomMethodInvocation(invocation)) { - makeAccessible(method); - return executeMethodOn(customImplementation, method, - invocation.getArguments()); - } + makeAccessible(method); + return executeMethodOn(customImplementation, method, + invocation.getArguments()); + } - if (hasQueryFor(method)) { - return queries.get(method).execute(invocation.getArguments()); - } + if (hasQueryFor(method)) { + return queries.get(method).execute(invocation.getArguments()); + } - // Lookup actual method as it might be redeclared in the interface - // and we have to use the repository instance nevertheless - Method actualMethod = repositoryInformation.getBaseClassMethod(method); - return executeMethodOn(target, actualMethod, - invocation.getArguments()); - } + // Lookup actual method as it might be redeclared in the interface + // and we have to use the repository instance nevertheless + Method actualMethod = repositoryInformation.getBaseClassMethod(method); + return executeMethodOn(target, actualMethod, + invocation.getArguments()); + } - /** - * Executes the given method on the given target. Correctly unwraps - * exceptions not caused by the reflection magic. - * - * @param target - * @param method - * @param parameters - * @return - * @throws Throwable - */ - private Object executeMethodOn(Object target, Method method, - Object[] parameters) throws Throwable { + /** + * Executes the given method on the given target. Correctly unwraps + * exceptions not caused by the reflection magic. + * + * @param target + * @param method + * @param parameters + * @return + * @throws Throwable + */ + private Object executeMethodOn(Object target, Method method, + Object[] parameters) throws Throwable { - try { - return method.invoke(target, parameters); - } catch (Exception e) { - ClassUtils.unwrapReflectionException(e); - } + try { + return method.invoke(target, parameters); + } catch (Exception e) { + ClassUtils.unwrapReflectionException(e); + } - throw new IllegalStateException("Should not occur!"); - } + throw new IllegalStateException("Should not occur!"); + } - /** - * Returns whether we know of a query to execute for the given - * {@link Method}; - * - * @param method - * @return - */ - private boolean hasQueryFor(Method method) { + /** + * Returns whether we know of a query to execute for the given + * {@link Method}; + * + * @param method + * @return + */ + private boolean hasQueryFor(Method method) { - return queries.containsKey(method); - } + return queries.containsKey(method); + } - /** - * Returns whether the given {@link MethodInvocation} is considered to - * be targeted as an invocation of a custom method. - * - * @param method - * @return - */ - private boolean isCustomMethodInvocation(MethodInvocation invocation) { + /** + * Returns whether the given {@link MethodInvocation} is considered to + * be targeted as an invocation of a custom method. + * + * @param method + * @return + */ + private boolean isCustomMethodInvocation(MethodInvocation invocation) { - if (null == customImplementation) { - return false; - } + if (null == customImplementation) { + return false; + } - return repositoryInformation.isCustomMethod(invocation.getMethod()); - } - } + return repositoryInformation.isCustomMethod(invocation.getMethod()); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInformation.java index 62b878833..a7282c3bf 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInformation.java @@ -24,52 +24,52 @@ import java.lang.reflect.Method; */ interface RepositoryInformation extends RepositoryMetadata { - /** - * Returns the base class to be used to create the proxy backing instance. - * - * @return - */ - Class getRepositoryBaseClass(); + /** + * Returns the base class to be used to create the proxy backing instance. + * + * @return + */ + Class getRepositoryBaseClass(); - /** - * Returns if the configured repository interface has custom methods, that - * might have to be delegated to a custom implementation. This is used to - * verify repository configuration. - * - * @return - */ - boolean hasCustomMethod(); + /** + * Returns if the configured repository interface has custom methods, that + * might have to be delegated to a custom implementation. This is used to + * verify repository configuration. + * + * @return + */ + boolean hasCustomMethod(); - /** - * Returns whether the given method is a custom repository method. - * - * @param method - * @param baseClass - * @return - */ - boolean isCustomMethod(Method method); + /** + * Returns whether the given method is a custom repository method. + * + * @param method + * @param baseClass + * @return + */ + boolean isCustomMethod(Method method); - /** - * Returns all methods considered to be query methods. - * - * @param repositoryInterface - * @return - */ - Iterable getQueryMethods(); + /** + * Returns all methods considered to be query methods. + * + * @param repositoryInterface + * @return + */ + Iterable getQueryMethods(); - /** - * Returns the base class method that is backing the given method. This can - * be necessary if a repository interface redeclares a method of the core - * repository interface (e.g. for transaction behaviour customization). - * Returns the method itself if the base class does not implement the given - * method. - * - * @param method - * @return - */ - Method getBaseClassMethod(Method method); + /** + * Returns the base class method that is backing the given method. This can + * be necessary if a repository interface redeclares a method of the core + * repository interface (e.g. for transaction behaviour customization). + * Returns the method itself if the base class does not implement the given + * method. + * + * @param method + * @return + */ + Method getBaseClassMethod(Method method); } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessor.java index 96674f671..b5aafd448 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessor.java @@ -33,85 +33,85 @@ import org.springframework.util.ClassUtils; * as shortcut to prevent the need of instantiating * {@link RepositoryFactoryBeanSupport}s just to find out what repository * interface they actually create. - * + * * @author Oliver Gierke */ class RepositoryInterfaceAwareBeanPostProcessor extends - InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { + InstantiationAwareBeanPostProcessorAdapter implements BeanFactoryAware { - private static final Class REPOSITORY_TYPE = - RepositoryFactoryBeanSupport.class; + private static final Class REPOSITORY_TYPE = + RepositoryFactoryBeanSupport.class; - private ConfigurableListableBeanFactory context; + private ConfigurableListableBeanFactory context; - /* - * (non-Javadoc) - * - * @see - * org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org - * .springframework.beans.factory.BeanFactory) - */ - public void setBeanFactory(BeanFactory beanFactory) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org + * .springframework.beans.factory.BeanFactory) + */ + public void setBeanFactory(BeanFactory beanFactory) { - if (beanFactory instanceof ConfigurableListableBeanFactory) { - this.context = (ConfigurableListableBeanFactory) beanFactory; - } - } + if (beanFactory instanceof ConfigurableListableBeanFactory) { + this.context = (ConfigurableListableBeanFactory) beanFactory; + } + } - /* - * (non-Javadoc) - * - * @see org.springframework.beans.factory.config. - * InstantiationAwareBeanPostProcessorAdapter - * #predictBeanType(java.lang.Class, java.lang.String) - */ - @Override - public Class predictBeanType(Class beanClass, String beanName) { + /* + * (non-Javadoc) + * + * @see org.springframework.beans.factory.config. + * InstantiationAwareBeanPostProcessorAdapter + * #predictBeanType(java.lang.Class, java.lang.String) + */ + @Override + public Class predictBeanType(Class beanClass, String beanName) { - if (null == context || !REPOSITORY_TYPE.isAssignableFrom(beanClass)) { - return null; - } + if (null == context || !REPOSITORY_TYPE.isAssignableFrom(beanClass)) { + return null; + } - BeanDefinition definition = context.getBeanDefinition(beanName); - PropertyValue value = - definition.getPropertyValues().getPropertyValue( - "repositoryInterface"); + BeanDefinition definition = context.getBeanDefinition(beanName); + PropertyValue value = + definition.getPropertyValues().getPropertyValue( + "repositoryInterface"); - return getClassForPropertyValue(value); - } + return getClassForPropertyValue(value); + } - /** - * Returns the class which is configured in the given {@link PropertyValue}. - * In case it is not a {@link TypedStringValue} or the value contained - * cannot be interpreted as {@link Class} it will return null. - * - * @param propertyValue - * @return - */ - private Class getClassForPropertyValue(PropertyValue propertyValue) { + /** + * Returns the class which is configured in the given {@link PropertyValue}. + * In case it is not a {@link TypedStringValue} or the value contained + * cannot be interpreted as {@link Class} it will return null. + * + * @param propertyValue + * @return + */ + private Class getClassForPropertyValue(PropertyValue propertyValue) { - Object value = propertyValue.getValue(); - String className = null; + Object value = propertyValue.getValue(); + String className = null; - if (value instanceof TypedStringValue) { - className = ((TypedStringValue) value).getValue(); - } else if (value instanceof String) { - className = (String) value; - } else if (value instanceof Class) { - return (Class) value; - } else { - return null; - } + if (value instanceof TypedStringValue) { + className = ((TypedStringValue) value).getValue(); + } else if (value instanceof String) { + className = (String) value; + } else if (value instanceof Class) { + return (Class) value; + } else { + return null; + } - try { - return ClassUtils.resolveClassName(className, - RepositoryInterfaceAwareBeanPostProcessor.class - .getClassLoader()); - } catch (IllegalArgumentException ex) { - return null; - } - } + try { + return ClassUtils.resolveClassName(className, + RepositoryInterfaceAwareBeanPostProcessor.class + .getClassLoader()); + } catch (IllegalArgumentException ex) { + return null; + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryMetadata.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryMetadata.java index 07972f75b..5a9a2290a 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryMetadata.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryMetadata.java @@ -16,17 +16,16 @@ package org.springframework.data.repository.support; - /** * Metadata for repository interfaces. - * + * * @author Oliver Gierke */ public interface RepositoryMetadata { - + /** * Returns the id class the given class is declared for. - * + * * @param clazz * @return the id class of the entity managed by the repository for or * {@code null} if none found. @@ -35,7 +34,7 @@ public interface RepositoryMetadata { /** * Returns the domain class the repository is declared for. - * + * * @param clazz * @return the domain class the repository is handling or {@code null} if * none found. @@ -44,7 +43,7 @@ public interface RepositoryMetadata { /** * Returns the repository interface. - * + * * @return */ Class getRepositoryInterface(); diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryProxyPostProcessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryProxyPostProcessor.java index d4bcdf73e..1ad741a34 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryProxyPostProcessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/RepositoryProxyPostProcessor.java @@ -21,16 +21,16 @@ import org.springframework.aop.framework.ProxyFactory; /** * Callback interface used during repository proxy creation. Allows manipulating * the {@link ProxyFactory} creating the repository. - * + * * @author Oliver Gierke */ public interface RepositoryProxyPostProcessor { - /** - * Manipulates the {@link ProxyFactory}, e.g. add further interceptors to - * it. - * - * @param factory - */ - void postProcess(ProxyFactory factory); + /** + * Manipulates the {@link ProxyFactory}, e.g. add further interceptors to + * it. + * + * @param factory + */ + void postProcess(ProxyFactory factory); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java index ec236563b..e7f89e9df 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryFactoryBeanSupport.java @@ -31,73 +31,73 @@ import org.springframework.util.Assert; * capabilities to the repository proxy. Will register a * {@link TransactionalRepositoryProxyPostProcessor} that in turn adds a * {@link TransactionInterceptor} to the repository proxy to be created. - * + * * @author Oliver Gierke */ public abstract class TransactionalRepositoryFactoryBeanSupport, S, ID extends Serializable> - extends RepositoryFactoryBeanSupport implements BeanFactoryAware { + extends RepositoryFactoryBeanSupport implements BeanFactoryAware { - private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER; - private RepositoryProxyPostProcessor txPostProcessor; + private String transactionManagerName = TxUtils.DEFAULT_TRANSACTION_MANAGER; + private RepositoryProxyPostProcessor txPostProcessor; - /** - * Setter to configure which transaction manager to be used. We have to use - * the bean name explicitly as otherwise the qualifier of the - * {@link org.springframework.transaction.annotation.Transactional} - * annotation is used. By explicitly defining the transaction manager bean - * name we favour let this one be the default one chosen. - * - * @param transactionManager - */ - public void setTransactionManager(String transactionManager) { + /** + * Setter to configure which transaction manager to be used. We have to use + * the bean name explicitly as otherwise the qualifier of the + * {@link org.springframework.transaction.annotation.Transactional} + * annotation is used. By explicitly defining the transaction manager bean + * name we favour let this one be the default one chosen. + * + * @param transactionManager + */ + public void setTransactionManager(String transactionManager) { - this.transactionManagerName = - transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER - : transactionManager; - } + this.transactionManagerName = + transactionManager == null ? TxUtils.DEFAULT_TRANSACTION_MANAGER + : transactionManager; + } - /** - * Delegates {@link RepositoryFactorySupport} creation to - * {@link #doCreateRepositoryFactory()} and applies the - * {@link TransactionalRepositoryProxyPostProcessor} to the created - * instance. - * - * @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport - * #createRepositoryFactory() - */ - @Override - protected final RepositoryFactorySupport createRepositoryFactory() { + /** + * Delegates {@link RepositoryFactorySupport} creation to + * {@link #doCreateRepositoryFactory()} and applies the + * {@link TransactionalRepositoryProxyPostProcessor} to the created + * instance. + * + * @see org.springframework.data.repository.support.RepositoryFactoryBeanSupport + * #createRepositoryFactory() + */ + @Override + protected final RepositoryFactorySupport createRepositoryFactory() { - RepositoryFactorySupport factory = doCreateRepositoryFactory(); - factory.addRepositoryProxyPostProcessor(txPostProcessor); - return factory; - } + RepositoryFactorySupport factory = doCreateRepositoryFactory(); + factory.addRepositoryProxyPostProcessor(txPostProcessor); + return factory; + } - /** - * Creates the actual {@link RepositoryFactorySupport} instance. - * - * @return - */ - protected abstract RepositoryFactorySupport doCreateRepositoryFactory(); + /** + * Creates the actual {@link RepositoryFactorySupport} instance. + * + * @return + */ + protected abstract RepositoryFactorySupport doCreateRepositoryFactory(); - /* - * (non-Javadoc) - * - * @see - * org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org - * .springframework.beans.factory.BeanFactory) - */ - public void setBeanFactory(BeanFactory beanFactory) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.beans.factory.BeanFactoryAware#setBeanFactory(org + * .springframework.beans.factory.BeanFactory) + */ + public void setBeanFactory(BeanFactory beanFactory) { - Assert.isInstanceOf(ListableBeanFactory.class, beanFactory); + Assert.isInstanceOf(ListableBeanFactory.class, beanFactory); - this.txPostProcessor = - new TransactionalRepositoryProxyPostProcessor( - (ListableBeanFactory) beanFactory, - transactionManagerName); - } + this.txPostProcessor = + new TransactionalRepositoryProxyPostProcessor( + (ListableBeanFactory) beanFactory, + transactionManagerName); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryProxyPostProcessor.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryProxyPostProcessor.java index 09d892503..7e0a12db7 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryProxyPostProcessor.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/support/TransactionalRepositoryProxyPostProcessor.java @@ -27,49 +27,49 @@ import org.springframework.util.Assert; * {@link RepositoryProxyPostProcessor} to add transactional behaviour to * repository proxies. Adds a {@link PersistenceExceptionTranslationInterceptor} * as well as an annotation based {@link TransactionInterceptor} to the proxy. - * + * * @author Oliver Gierke */ class TransactionalRepositoryProxyPostProcessor implements - RepositoryProxyPostProcessor { + RepositoryProxyPostProcessor { - private final TransactionInterceptor transactionInterceptor; - private final PersistenceExceptionTranslationInterceptor petInterceptor; + private final TransactionInterceptor transactionInterceptor; + private final PersistenceExceptionTranslationInterceptor petInterceptor; - /** - * Creates a new {@link TransactionalRepositoryProxyPostProcessor}. - */ - public TransactionalRepositoryProxyPostProcessor( - ListableBeanFactory beanFactory, String transactionManagerName) { + /** + * Creates a new {@link TransactionalRepositoryProxyPostProcessor}. + */ + public TransactionalRepositoryProxyPostProcessor( + ListableBeanFactory beanFactory, String transactionManagerName) { - Assert.notNull(beanFactory); - Assert.notNull(transactionManagerName); + Assert.notNull(beanFactory); + Assert.notNull(transactionManagerName); - this.petInterceptor = new PersistenceExceptionTranslationInterceptor(); - this.petInterceptor.setBeanFactory(beanFactory); - this.petInterceptor.afterPropertiesSet(); + this.petInterceptor = new PersistenceExceptionTranslationInterceptor(); + this.petInterceptor.setBeanFactory(beanFactory); + this.petInterceptor.afterPropertiesSet(); - this.transactionInterceptor = - new TransactionInterceptor(null, - new AnnotationTransactionAttributeSource()); - this.transactionInterceptor - .setTransactionManagerBeanName(transactionManagerName); - this.transactionInterceptor.setBeanFactory(beanFactory); - this.transactionInterceptor.afterPropertiesSet(); - } + this.transactionInterceptor = + new TransactionInterceptor(null, + new AnnotationTransactionAttributeSource()); + this.transactionInterceptor + .setTransactionManagerBeanName(transactionManagerName); + this.transactionInterceptor.setBeanFactory(beanFactory); + this.transactionInterceptor.afterPropertiesSet(); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.RepositoryProxyPostProcessor - * #postProcess(org.springframework.aop.framework.ProxyFactory) - */ - public void postProcess(ProxyFactory factory) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.RepositoryProxyPostProcessor + * #postProcess(org.springframework.aop.framework.ProxyFactory) + */ + public void postProcess(ProxyFactory factory) { - factory.addAdvice(petInterceptor); - factory.addAdvice(transactionInterceptor); - } + factory.addAdvice(petInterceptor); + factory.addAdvice(transactionInterceptor); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java index dd41b9e76..c18c5dfaf 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/ClassUtils.java @@ -30,175 +30,175 @@ import org.springframework.util.StringUtils; /** * Utility class to work with classes. - * + * * @author Oliver Gierke */ public abstract class ClassUtils { - /** - * Private constructor to prevent instantiation. - */ - private ClassUtils() { + /** + * Private constructor to prevent instantiation. + */ + private ClassUtils() { - } + } - /** - * Returns the domain class returned by the given {@link Method}. Will - * extract the type from {@link Collection}s and - * {@link org.springframework.data.domain.Page} as well. - * - * @param method - * @return - */ - public static Class getReturnedDomainClass(Method method) { + /** + * Returns the domain class returned by the given {@link Method}. Will + * extract the type from {@link Collection}s and + * {@link org.springframework.data.domain.Page} as well. + * + * @param method + * @return + */ + public static Class getReturnedDomainClass(Method method) { - Class type = method.getReturnType(); + Class type = method.getReturnType(); - if (Collection.class.isAssignableFrom(type) - || Page.class.isAssignableFrom(type)) { + if (Collection.class.isAssignableFrom(type) + || Page.class.isAssignableFrom(type)) { - ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); - Type componentType = returnType.getActualTypeArguments()[0]; - - return componentType instanceof ParameterizedType ? (Class) ((ParameterizedType) componentType).getRawType() - : (Class) componentType; - } + ParameterizedType returnType = (ParameterizedType) method.getGenericReturnType(); + Type componentType = returnType.getActualTypeArguments()[0]; - return type; - } + return componentType instanceof ParameterizedType ? (Class) ((ParameterizedType) componentType).getRawType() + : (Class) componentType; + } + + return type; + } - /** - * Returns whether the given class contains a property with the given name. - * - * @param fieldName - * @return - */ - public static boolean hasProperty(Class type, String property) { + /** + * Returns whether the given class contains a property with the given name. + * + * @param fieldName + * @return + */ + public static boolean hasProperty(Class type, String property) { - if (null != ReflectionUtils.findMethod(type, "get" + property)) { - return true; - } + if (null != ReflectionUtils.findMethod(type, "get" + property)) { + return true; + } - return null != ReflectionUtils.findField(type, - StringUtils.uncapitalize(property)); - } + return null != ReflectionUtils.findField(type, + StringUtils.uncapitalize(property)); + } - /** - * Returns wthere the given type is the {@link Repository} interface. - * - * @param interfaze - * @return - */ - public static boolean isGenericRepositoryInterface(Class interfaze) { + /** + * Returns wthere the given type is the {@link Repository} interface. + * + * @param interfaze + * @return + */ + public static boolean isGenericRepositoryInterface(Class interfaze) { - return Repository.class.equals(interfaze); - } + return Repository.class.equals(interfaze); + } - /** - * Returns whether the given type name is a repository interface name. - * - * @param interfaceName - * @return - */ - public static boolean isGenericRepositoryInterface(String interfaceName) { + /** + * Returns whether the given type name is a repository interface name. + * + * @param interfaceName + * @return + */ + public static boolean isGenericRepositoryInterface(String interfaceName) { - return Repository.class.getName().equals(interfaceName); - } + return Repository.class.getName().equals(interfaceName); + } - /** - * Returns the number of occurences of the given type in the given - * {@link Method}s parameters. - * - * @param method - * @param type - * @return - */ - public static int getNumberOfOccurences(Method method, Class type) { + /** + * Returns the number of occurences of the given type in the given + * {@link Method}s parameters. + * + * @param method + * @param type + * @return + */ + public static int getNumberOfOccurences(Method method, Class type) { - int result = 0; - for (Class clazz : method.getParameterTypes()) { - if (type.equals(clazz)) { - result++; - } - } + int result = 0; + for (Class clazz : method.getParameterTypes()) { + if (type.equals(clazz)) { + result++; + } + } - return result; - } + return result; + } - /** - * Asserts the given {@link Method}'s return type to be one of the given - * types. - * - * @param method - * @param types - */ - public static void assertReturnType(Method method, Class... types) { + /** + * Asserts the given {@link Method}'s return type to be one of the given + * types. + * + * @param method + * @param types + */ + public static void assertReturnType(Method method, Class... types) { - if (!Arrays.asList(types).contains(method.getReturnType())) { - throw new IllegalStateException( - "Method has to have one of the following return types! " - + Arrays.toString(types)); - } - } + if (!Arrays.asList(types).contains(method.getReturnType())) { + throw new IllegalStateException( + "Method has to have one of the following return types! " + + Arrays.toString(types)); + } + } - /** - * Returns whether the given object is of one of the given types. Will - * return {@literal false} for {@literal null}. - * - * @param object - * @param types - * @return - */ - public static boolean isOfType(Object object, Collection> types) { + /** + * Returns whether the given object is of one of the given types. Will + * return {@literal false} for {@literal null}. + * + * @param object + * @param types + * @return + */ + public static boolean isOfType(Object object, Collection> types) { - if (null == object) { - return false; - } + if (null == object) { + return false; + } - for (Class type : types) { - if (type.isAssignableFrom(object.getClass())) { - return true; - } - } + for (Class type : types) { + if (type.isAssignableFrom(object.getClass())) { + return true; + } + } - return false; - } + return false; + } - /** - * Returns whether the given {@link Method} has a parameter of the given - * type. - * - * @param method - * @param type - * @return - */ - public static boolean hasParameterOfType(Method method, Class type) { + /** + * Returns whether the given {@link Method} has a parameter of the given + * type. + * + * @param method + * @param type + * @return + */ + public static boolean hasParameterOfType(Method method, Class type) { - return Arrays.asList(method.getParameterTypes()).contains(type); - } + return Arrays.asList(method.getParameterTypes()).contains(type); + } - /** - * Helper method to extract the original exception that can possibly occur - * during a reflection call. - * - * @param ex - * @throws Throwable - */ - public static void unwrapReflectionException(Exception ex) throws Throwable { + /** + * Helper method to extract the original exception that can possibly occur + * during a reflection call. + * + * @param ex + * @throws Throwable + */ + public static void unwrapReflectionException(Exception ex) throws Throwable { - if (ex instanceof InvocationTargetException) { - throw ((InvocationTargetException) ex).getTargetException(); - } + if (ex instanceof InvocationTargetException) { + throw ((InvocationTargetException) ex).getTargetException(); + } - throw ex; - } + throw ex; + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/TxUtils.java b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/TxUtils.java index 14da61359..62fd13b6d 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/TxUtils.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/repository/util/TxUtils.java @@ -17,15 +17,15 @@ package org.springframework.data.repository.util; /** * Simple constants holder. - * + * * @author Oliver Gierke */ public abstract class TxUtils { - private TxUtils() { + private TxUtils() { - } + } - public static final String DEFAULT_TRANSACTION_MANAGER = - "transactionManager"; + public static final String DEFAULT_TRANSACTION_MANAGER = + "transactionManager"; } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/transaction/ChangeSetBackedTransactionSynchronization.java b/spring-data-commons-core/src/main/java/org/springframework/data/transaction/ChangeSetBackedTransactionSynchronization.java index a13afcfe5..564c639a0 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/transaction/ChangeSetBackedTransactionSynchronization.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/transaction/ChangeSetBackedTransactionSynchronization.java @@ -20,7 +20,7 @@ public class ChangeSetBackedTransactionSynchronization implements TransactionSyn this.changeSetPersister = changeSetPersister; this.entity = entity; } - + public void afterCommit() { log.debug("After Commit called for " + entity); changeSetPersister.persistState(entity, entity.getChangeSet()); @@ -33,8 +33,7 @@ public class ChangeSetBackedTransactionSynchronization implements TransactionSyn if (status == STATUS_COMMITTED) { // this is good log.debug("ChangedSetBackedTransactionSynchronization completed successfully for " + this.entity); - } - else { + } else { // this could be bad - TODO: compensate log.error("ChangedSetBackedTransactionSynchronization failed for " + this.entity); } @@ -57,5 +56,5 @@ public class ChangeSetBackedTransactionSynchronization implements TransactionSyn public void suspend() { throw new IllegalStateException("ChangedSetBackedTransactionSynchronization does not support transaction suspension currently."); } - + } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/transaction/NaiveDoubleTransactionManager.java b/spring-data-commons-core/src/main/java/org/springframework/data/transaction/NaiveDoubleTransactionManager.java index 5633521c7..0dc840319 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/transaction/NaiveDoubleTransactionManager.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/transaction/NaiveDoubleTransactionManager.java @@ -1,75 +1,73 @@ -package org.springframework.data.transaction; - -import java.util.IdentityHashMap; -import java.util.Map; - -import org.springframework.transaction.PlatformTransactionManager; -import org.springframework.transaction.TransactionDefinition; -import org.springframework.transaction.TransactionException; -import org.springframework.transaction.TransactionStatus; -import org.springframework.transaction.support.DefaultTransactionStatus; - -public class NaiveDoubleTransactionManager implements PlatformTransactionManager { - Map status=new IdentityHashMap(); - private final PlatformTransactionManager a; - - private final PlatformTransactionManager b; - - public NaiveDoubleTransactionManager(PlatformTransactionManager a, PlatformTransactionManager b) { - System.err.println("WARNING: Naive JTA/Neo4j Spring transaction manager--must implement properly"); - this.a = a; - this.b = b; - } - - public void commit(TransactionStatus ts) throws TransactionException { - try { - final TransactionStatus tsb = copyTransactionStatus(status.get(ts)); - try { - a.commit(ts); - } - catch (Throwable t) { - System.err.println("Continuing to commit tx despite this:" + t); - } - try { - b.commit(tsb); - } - catch (Throwable t) { - System.err.println("Can't commit tx" + t); - throw new TransactionException(t.getMessage(), t) {}; - } - } finally { - status.remove(ts); - } - } - - private TransactionStatus copyTransactionStatus(TransactionStatus ts) { - Object t = (ts instanceof DefaultTransactionStatus) ? ((DefaultTransactionStatus) ts).getTransaction() : null; - return new DefaultTransactionStatus(t,ts.isNewTransaction(), false, false, false, null); - } - - public TransactionStatus getTransaction(TransactionDefinition td) - throws TransactionException { - TransactionStatus atx = a.getTransaction(td); - TransactionStatus btx = b.getTransaction(td); - status.put(atx, btx); - return atx; - } - - public void rollback(TransactionStatus ts) throws TransactionException { - final TransactionStatus tsb = copyTransactionStatus(status.remove(ts)); - try { - a.rollback(ts); - } - catch (Throwable t) { - System.err.println("Continuing to rollback tx despite this:" + t); - } - try { - b.rollback(tsb); - } - catch (Throwable t) { - System.err.println("Can't rollback tx" + t); - throw new TransactionException(t.getMessage(), t) {}; - } - } - -} +package org.springframework.data.transaction; + +import java.util.IdentityHashMap; +import java.util.Map; + +import org.springframework.transaction.PlatformTransactionManager; +import org.springframework.transaction.TransactionDefinition; +import org.springframework.transaction.TransactionException; +import org.springframework.transaction.TransactionStatus; +import org.springframework.transaction.support.DefaultTransactionStatus; + +public class NaiveDoubleTransactionManager implements PlatformTransactionManager { + Map status = new IdentityHashMap(); + private final PlatformTransactionManager a; + + private final PlatformTransactionManager b; + + public NaiveDoubleTransactionManager(PlatformTransactionManager a, PlatformTransactionManager b) { + System.err.println("WARNING: Naive JTA/Neo4j Spring transaction manager--must implement properly"); + this.a = a; + this.b = b; + } + + public void commit(TransactionStatus ts) throws TransactionException { + try { + final TransactionStatus tsb = copyTransactionStatus(status.get(ts)); + try { + a.commit(ts); + } catch (Throwable t) { + System.err.println("Continuing to commit tx despite this:" + t); + } + try { + b.commit(tsb); + } catch (Throwable t) { + System.err.println("Can't commit tx" + t); + throw new TransactionException(t.getMessage(), t) { + }; + } + } finally { + status.remove(ts); + } + } + + private TransactionStatus copyTransactionStatus(TransactionStatus ts) { + Object t = (ts instanceof DefaultTransactionStatus) ? ((DefaultTransactionStatus) ts).getTransaction() : null; + return new DefaultTransactionStatus(t, ts.isNewTransaction(), false, false, false, null); + } + + public TransactionStatus getTransaction(TransactionDefinition td) + throws TransactionException { + TransactionStatus atx = a.getTransaction(td); + TransactionStatus btx = b.getTransaction(td); + status.put(atx, btx); + return atx; + } + + public void rollback(TransactionStatus ts) throws TransactionException { + final TransactionStatus tsb = copyTransactionStatus(status.remove(ts)); + try { + a.rollback(ts); + } catch (Throwable t) { + System.err.println("Continuing to rollback tx despite this:" + t); + } + try { + b.rollback(tsb); + } catch (Throwable t) { + System.err.println("Can't rollback tx" + t); + throw new TransactionException(t.getMessage(), t) { + }; + } + } + +} diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/ArrayTypeDiscoverer.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/ArrayTypeDiscoverer.java index ff56ad55a..1ca2b5178 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/ArrayTypeDiscoverer.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/ArrayTypeDiscoverer.java @@ -11,35 +11,35 @@ import java.lang.reflect.Type; */ public class ArrayTypeDiscoverer extends TypeDiscoverer { - private GenericArrayType type; - - /** - * @param type - * @param parent - * @param parent - */ - protected ArrayTypeDiscoverer(GenericArrayType type, TypeDiscoverer parent) { - super(type, null, parent); - this.type = type; - } + private GenericArrayType type; - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#getType() - */ - @Override - @SuppressWarnings("unchecked") - public Class getType() { - - return (Class) Array.newInstance(resolveType(type.getGenericComponentType()), 0).getClass(); - } - - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#getComponentType() - */ - @Override - public TypeInformation getComponentType() { - - Type componentType = type.getGenericComponentType(); - return createInfo(componentType); - } + /** + * @param type + * @param parent + * @param parent + */ + protected ArrayTypeDiscoverer(GenericArrayType type, TypeDiscoverer parent) { + super(type, null, parent); + this.type = type; + } + + /* (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#getType() + */ + @Override + @SuppressWarnings("unchecked") + public Class getType() { + + return (Class) Array.newInstance(resolveType(type.getGenericComponentType()), 0).getClass(); + } + + /* (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#getComponentType() + */ + @Override + public TypeInformation getComponentType() { + + Type componentType = type.getGenericComponentType(); + return createInfo(componentType); + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java index 0fd32eb0c..941ecdeae 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -8,88 +8,87 @@ import org.springframework.util.Assert; /** * Property information for a plain {@link Class}. - * + * * @author Oliver Gierke */ public class ClassTypeInformation extends TypeDiscoverer { - private final Class type; - - public static TypeInformation from(Class type) { - return new ClassTypeInformation(type); - } + private final Class type; - /** - * Creates {@link ClassTypeInformation} for the given type. - * - * @param type - */ - public ClassTypeInformation(Class type) { - this(type, GenericTypeResolver.getTypeVariableMap(type), null); - } + public static TypeInformation from(Class type) { + return new ClassTypeInformation(type); + } - ClassTypeInformation(Class type, TypeDiscoverer parent) { - this(type, null, parent); - } + /** + * Creates {@link ClassTypeInformation} for the given type. + * + * @param type + */ + public ClassTypeInformation(Class type) { + this(type, GenericTypeResolver.getTypeVariableMap(type), null); + } - @SuppressWarnings("rawtypes") - ClassTypeInformation(Class type, Map typeVariableMap, - TypeDiscoverer parent) { - super(type, typeVariableMap, parent); - this.type = type; - } + ClassTypeInformation(Class type, TypeDiscoverer parent) { + this(type, null, parent); + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation#getType() - */ - @Override - public Class getType() { - return type; - } + @SuppressWarnings("rawtypes") ClassTypeInformation(Class type, Map typeVariableMap, + TypeDiscoverer parent) { + super(type, typeVariableMap, parent); + this.type = type; + } - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#getComponentType() - */ - @Override - @SuppressWarnings({ "rawtypes", "unchecked" }) - public TypeInformation getComponentType() { - - if (type.isArray()) { - return createInfo(resolveArrayType(type)); - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation#getType() + */ + @Override + public Class getType() { + return type; + } - TypeVariable[] typeParameters = type.getTypeParameters(); - return typeParameters.length > 0 ? new TypeVariableTypeInformation(typeParameters[0], this.getType(), this) : null; - } - - private static Type resolveArrayType(Class type) { - Assert.isTrue(type.isArray()); - Class componentType = type.getComponentType(); - return componentType.isArray() ? resolveArrayType(componentType) : componentType; - } + /* (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#getComponentType() + */ + @Override + @SuppressWarnings({"rawtypes", "unchecked"}) + public TypeInformation getComponentType() { - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { + if (type.isArray()) { + return createInfo(resolveArrayType(type)); + } - if (!super.equals(obj)) { - return false; - } + TypeVariable[] typeParameters = type.getTypeParameters(); + return typeParameters.length > 0 ? new TypeVariableTypeInformation(typeParameters[0], this.getType(), this) : null; + } - ClassTypeInformation that = (ClassTypeInformation) obj; - return this.type.equals(that.type); - } + private static Type resolveArrayType(Class type) { + Assert.isTrue(type.isArray()); + Class componentType = type.getComponentType(); + return componentType.isArray() ? resolveArrayType(componentType) : componentType; + } - /* (non-Javadoc) - * @see org.springframework.data.util.TypeDiscoverer#hashCode() - */ - @Override - public int hashCode() { - int result = super.hashCode(); - return result += 31 * type.hashCode(); - } + /* (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + + if (!super.equals(obj)) { + return false; + } + + ClassTypeInformation that = (ClassTypeInformation) obj; + return this.type.equals(that.type); + } + + /* (non-Javadoc) + * @see org.springframework.data.util.TypeDiscoverer#hashCode() + */ + @Override + public int hashCode() { + int result = super.hashCode(); + return result += 31 * type.hashCode(); + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java index c4cfa522c..70481b86f 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/GenericTypeResolver.java @@ -34,321 +34,315 @@ import org.springframework.util.Assert; /** * Copy of Spring's {@link org.springframework.core.GenericTypeResolver}. Needed * until {@link #getTypeVariableMap(Class)} gets public. - * + *

* TODO: remove that class, as soon as Spring 3.0.6 gets released. + * * @see SPR-8005 */ @SuppressWarnings({"rawtypes", "unchecked"}) abstract class GenericTypeResolver { - /** Cache from Class to TypeVariable Map */ - private static final Map>> typeVariableCache = Collections - .synchronizedMap(new WeakHashMap>>()); + /** + * Cache from Class to TypeVariable Map + */ + private static final Map>> typeVariableCache = Collections + .synchronizedMap(new WeakHashMap>>()); - /** - * Determine the target type for the given parameter specification. - * - * @param methodParam - * the method parameter specification - * @return the corresponding generic parameter type - */ - public static Type getTargetType(MethodParameter methodParam) { - Assert.notNull(methodParam, "MethodParameter must not be null"); - if (methodParam.getConstructor() != null) { - return methodParam.getConstructor().getGenericParameterTypes()[methodParam - .getParameterIndex()]; - } else { - if (methodParam.getParameterIndex() >= 0) { - return methodParam.getMethod().getGenericParameterTypes()[methodParam - .getParameterIndex()]; - } else { - return methodParam.getMethod().getGenericReturnType(); - } - } - } + /** + * Determine the target type for the given parameter specification. + * + * @param methodParam the method parameter specification + * @return the corresponding generic parameter type + */ + public static Type getTargetType(MethodParameter methodParam) { + Assert.notNull(methodParam, "MethodParameter must not be null"); + if (methodParam.getConstructor() != null) { + return methodParam.getConstructor().getGenericParameterTypes()[methodParam + .getParameterIndex()]; + } else { + if (methodParam.getParameterIndex() >= 0) { + return methodParam.getMethod().getGenericParameterTypes()[methodParam + .getParameterIndex()]; + } else { + return methodParam.getMethod().getGenericReturnType(); + } + } + } - /** - * Resolve the single type argument of the given generic interface against the - * given target class which is assumed to implement the generic interface and - * possibly declare a concrete type for its type variable. - * - * @param clazz - * the target class to check against - * @param genericIfc - * the generic interface or superclass to resolve the type argument - * from - * @return the resolved type of the argument, or null if not - * resolvable - */ - public static Class resolveTypeArgument(Class clazz, Class genericIfc) { - Class[] typeArgs = resolveTypeArguments(clazz, genericIfc); - if (typeArgs == null) { - return null; - } - if (typeArgs.length != 1) { - throw new IllegalArgumentException( - "Expected 1 type argument on generic interface [" - + genericIfc.getName() + "] but found " + typeArgs.length); - } - return typeArgs[0]; - } + /** + * Resolve the single type argument of the given generic interface against the + * given target class which is assumed to implement the generic interface and + * possibly declare a concrete type for its type variable. + * + * @param clazz the target class to check against + * @param genericIfc the generic interface or superclass to resolve the type argument + * from + * @return the resolved type of the argument, or null if not + * resolvable + */ + public static Class resolveTypeArgument(Class clazz, Class genericIfc) { + Class[] typeArgs = resolveTypeArguments(clazz, genericIfc); + if (typeArgs == null) { + return null; + } + if (typeArgs.length != 1) { + throw new IllegalArgumentException( + "Expected 1 type argument on generic interface [" + + genericIfc.getName() + "] but found " + typeArgs.length); + } + return typeArgs[0]; + } - /** - * Resolve the type arguments of the given generic interface against the given - * target class which is assumed to implement the generic interface and - * possibly declare concrete types for its type variables. - * - * @param clazz - * the target class to check against - * @param genericIfc - * the generic interface or superclass to resolve the type argument - * from - * @return the resolved type of each argument, with the array size matching - * the number of actual type arguments, or null if not - * resolvable - */ - public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) { - return doResolveTypeArguments(clazz, clazz, genericIfc); - } + /** + * Resolve the type arguments of the given generic interface against the given + * target class which is assumed to implement the generic interface and + * possibly declare concrete types for its type variables. + * + * @param clazz the target class to check against + * @param genericIfc the generic interface or superclass to resolve the type argument + * from + * @return the resolved type of each argument, with the array size matching + * the number of actual type arguments, or null if not + * resolvable + */ + public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) { + return doResolveTypeArguments(clazz, clazz, genericIfc); + } - private static Class[] doResolveTypeArguments(Class ownerClass, - Class classToIntrospect, Class genericIfc) { - while (classToIntrospect != null) { - if (genericIfc.isInterface()) { - Type[] ifcs = classToIntrospect.getGenericInterfaces(); - for (Type ifc : ifcs) { - Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc); - if (result != null) { - return result; - } - } - } else { - Class[] result = doResolveTypeArguments(ownerClass, - classToIntrospect.getGenericSuperclass(), genericIfc); - if (result != null) { - return result; - } - } - classToIntrospect = classToIntrospect.getSuperclass(); - } - return null; - } + private static Class[] doResolveTypeArguments(Class ownerClass, + Class classToIntrospect, Class genericIfc) { + while (classToIntrospect != null) { + if (genericIfc.isInterface()) { + Type[] ifcs = classToIntrospect.getGenericInterfaces(); + for (Type ifc : ifcs) { + Class[] result = doResolveTypeArguments(ownerClass, ifc, genericIfc); + if (result != null) { + return result; + } + } + } else { + Class[] result = doResolveTypeArguments(ownerClass, + classToIntrospect.getGenericSuperclass(), genericIfc); + if (result != null) { + return result; + } + } + classToIntrospect = classToIntrospect.getSuperclass(); + } + return null; + } - private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, - Class genericIfc) { - if (ifc instanceof ParameterizedType) { - ParameterizedType paramIfc = (ParameterizedType) ifc; - Type rawType = paramIfc.getRawType(); - if (genericIfc.equals(rawType)) { - Type[] typeArgs = paramIfc.getActualTypeArguments(); - Class[] result = new Class[typeArgs.length]; - for (int i = 0; i < typeArgs.length; i++) { - Type arg = typeArgs[i]; - result[i] = extractClass(ownerClass, arg); - } - return result; - } else if (genericIfc.isAssignableFrom((Class) rawType)) { - return doResolveTypeArguments(ownerClass, (Class) rawType, genericIfc); - } - } else if (genericIfc.isAssignableFrom((Class) ifc)) { - return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc); - } - return null; - } + private static Class[] doResolveTypeArguments(Class ownerClass, Type ifc, + Class genericIfc) { + if (ifc instanceof ParameterizedType) { + ParameterizedType paramIfc = (ParameterizedType) ifc; + Type rawType = paramIfc.getRawType(); + if (genericIfc.equals(rawType)) { + Type[] typeArgs = paramIfc.getActualTypeArguments(); + Class[] result = new Class[typeArgs.length]; + for (int i = 0; i < typeArgs.length; i++) { + Type arg = typeArgs[i]; + result[i] = extractClass(ownerClass, arg); + } + return result; + } else if (genericIfc.isAssignableFrom((Class) rawType)) { + return doResolveTypeArguments(ownerClass, (Class) rawType, genericIfc); + } + } else if (genericIfc.isAssignableFrom((Class) ifc)) { + return doResolveTypeArguments(ownerClass, (Class) ifc, genericIfc); + } + return null; + } - /** - * Extract a class instance from given Type. - */ - private static Class extractClass(Class ownerClass, Type arg) { - if (arg instanceof ParameterizedType) { - return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); - } else if (arg instanceof GenericArrayType) { - GenericArrayType gat = (GenericArrayType) arg; - Type gt = gat.getGenericComponentType(); - Class componentClass = extractClass(ownerClass, gt); - return Array.newInstance(componentClass, 0).getClass(); - } else if (arg instanceof TypeVariable) { - TypeVariable tv = (TypeVariable) arg; - arg = getTypeVariableMap(ownerClass).get(tv); - if (arg == null) { - arg = extractBoundForTypeVariable(tv); - } else { - arg = extractClass(ownerClass, arg); - } - } - return (arg instanceof Class ? (Class) arg : Object.class); - } + /** + * Extract a class instance from given Type. + */ + private static Class extractClass(Class ownerClass, Type arg) { + if (arg instanceof ParameterizedType) { + return extractClass(ownerClass, ((ParameterizedType) arg).getRawType()); + } else if (arg instanceof GenericArrayType) { + GenericArrayType gat = (GenericArrayType) arg; + Type gt = gat.getGenericComponentType(); + Class componentClass = extractClass(ownerClass, gt); + return Array.newInstance(componentClass, 0).getClass(); + } else if (arg instanceof TypeVariable) { + TypeVariable tv = (TypeVariable) arg; + arg = getTypeVariableMap(ownerClass).get(tv); + if (arg == null) { + arg = extractBoundForTypeVariable(tv); + } else { + arg = extractClass(ownerClass, arg); + } + } + return (arg instanceof Class ? (Class) arg : Object.class); + } - /** - * Resolve the specified generic type against the given TypeVariable map. - * - * @param genericType - * the generic type to resolve - * @param typeVariableMap - * the TypeVariable Map to resolved against - * @return the type if it resolves to a Class, or Object.class - * otherwise - */ - static Class resolveType(Type genericType, - Map typeVariableMap) { - Type rawType = getRawType(genericType, typeVariableMap); - return (rawType instanceof Class ? (Class) rawType : Object.class); - } + /** + * Resolve the specified generic type against the given TypeVariable map. + * + * @param genericType the generic type to resolve + * @param typeVariableMap the TypeVariable Map to resolved against + * @return the type if it resolves to a Class, or Object.class + * otherwise + */ + static Class resolveType(Type genericType, + Map typeVariableMap) { + Type rawType = getRawType(genericType, typeVariableMap); + return (rawType instanceof Class ? (Class) rawType : Object.class); + } - /** - * Determine the raw type for the given generic parameter type. - * - * @param genericType - * the generic type to resolve - * @param typeVariableMap - * the TypeVariable Map to resolved against - * @return the resolved raw type - */ - static Type getRawType(Type genericType, - Map typeVariableMap) { - Type resolvedType = genericType; - if (genericType instanceof TypeVariable) { - TypeVariable tv = (TypeVariable) genericType; - resolvedType = typeVariableMap.get(tv); - if (resolvedType == null) { - resolvedType = extractBoundForTypeVariable(tv); - } - } - if (resolvedType instanceof ParameterizedType) { - return ((ParameterizedType) resolvedType).getRawType(); - } else { - return resolvedType; - } - } + /** + * Determine the raw type for the given generic parameter type. + * + * @param genericType the generic type to resolve + * @param typeVariableMap the TypeVariable Map to resolved against + * @return the resolved raw type + */ + static Type getRawType(Type genericType, + Map typeVariableMap) { + Type resolvedType = genericType; + if (genericType instanceof TypeVariable) { + TypeVariable tv = (TypeVariable) genericType; + resolvedType = typeVariableMap.get(tv); + if (resolvedType == null) { + resolvedType = extractBoundForTypeVariable(tv); + } + } + if (resolvedType instanceof ParameterizedType) { + return ((ParameterizedType) resolvedType).getRawType(); + } else { + return resolvedType; + } + } - /** - * Build a mapping of {@link TypeVariable#getName TypeVariable names} to - * concrete {@link Class} for the specified {@link Class}. Searches all super - * types, enclosing types and interfaces. - */ - static Map getTypeVariableMap(Class clazz) { - Reference> ref = typeVariableCache.get(clazz); - Map typeVariableMap = (ref != null ? ref.get() : null); + /** + * Build a mapping of {@link TypeVariable#getName TypeVariable names} to + * concrete {@link Class} for the specified {@link Class}. Searches all super + * types, enclosing types and interfaces. + */ + static Map getTypeVariableMap(Class clazz) { + Reference> ref = typeVariableCache.get(clazz); + Map typeVariableMap = (ref != null ? ref.get() : null); - if (typeVariableMap == null) { - typeVariableMap = new HashMap(); + if (typeVariableMap == null) { + typeVariableMap = new HashMap(); - // interfaces - extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), - typeVariableMap); + // interfaces + extractTypeVariablesFromGenericInterfaces(clazz.getGenericInterfaces(), + typeVariableMap); - // super class - Type genericType = clazz.getGenericSuperclass(); - Class type = clazz.getSuperclass(); - while (type != null && !Object.class.equals(type)) { - if (genericType instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) genericType; - populateTypeMapFromParameterizedType(pt, typeVariableMap); - } - extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), - typeVariableMap); - genericType = type.getGenericSuperclass(); - type = type.getSuperclass(); - } + // super class + Type genericType = clazz.getGenericSuperclass(); + Class type = clazz.getSuperclass(); + while (type != null && !Object.class.equals(type)) { + if (genericType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) genericType; + populateTypeMapFromParameterizedType(pt, typeVariableMap); + } + extractTypeVariablesFromGenericInterfaces(type.getGenericInterfaces(), + typeVariableMap); + genericType = type.getGenericSuperclass(); + type = type.getSuperclass(); + } - // enclosing class - type = clazz; - while (type.isMemberClass()) { - genericType = type.getGenericSuperclass(); - if (genericType instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) genericType; - populateTypeMapFromParameterizedType(pt, typeVariableMap); - } - type = type.getEnclosingClass(); - } + // enclosing class + type = clazz; + while (type.isMemberClass()) { + genericType = type.getGenericSuperclass(); + if (genericType instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) genericType; + populateTypeMapFromParameterizedType(pt, typeVariableMap); + } + type = type.getEnclosingClass(); + } - typeVariableCache.put(clazz, new WeakReference>( - typeVariableMap)); - } + typeVariableCache.put(clazz, new WeakReference>( + typeVariableMap)); + } - return typeVariableMap; - } + return typeVariableMap; + } - /** - * Extracts the bound Type for a given {@link TypeVariable}. - */ - static Type extractBoundForTypeVariable(TypeVariable typeVariable) { - Type[] bounds = typeVariable.getBounds(); - if (bounds.length == 0) { - return Object.class; - } - Type bound = bounds[0]; - if (bound instanceof TypeVariable) { - bound = extractBoundForTypeVariable((TypeVariable) bound); - } - return bound; - } + /** + * Extracts the bound Type for a given {@link TypeVariable}. + */ + static Type extractBoundForTypeVariable(TypeVariable typeVariable) { + Type[] bounds = typeVariable.getBounds(); + if (bounds.length == 0) { + return Object.class; + } + Type bound = bounds[0]; + if (bound instanceof TypeVariable) { + bound = extractBoundForTypeVariable((TypeVariable) bound); + } + return bound; + } - private static void extractTypeVariablesFromGenericInterfaces( - Type[] genericInterfaces, Map typeVariableMap) { - for (Type genericInterface : genericInterfaces) { - if (genericInterface instanceof ParameterizedType) { - ParameterizedType pt = (ParameterizedType) genericInterface; - populateTypeMapFromParameterizedType(pt, typeVariableMap); - if (pt.getRawType() instanceof Class) { - extractTypeVariablesFromGenericInterfaces( - ((Class) pt.getRawType()).getGenericInterfaces(), typeVariableMap); - } - } else if (genericInterface instanceof Class) { - extractTypeVariablesFromGenericInterfaces( - ((Class) genericInterface).getGenericInterfaces(), typeVariableMap); - } - } - } + private static void extractTypeVariablesFromGenericInterfaces( + Type[] genericInterfaces, Map typeVariableMap) { + for (Type genericInterface : genericInterfaces) { + if (genericInterface instanceof ParameterizedType) { + ParameterizedType pt = (ParameterizedType) genericInterface; + populateTypeMapFromParameterizedType(pt, typeVariableMap); + if (pt.getRawType() instanceof Class) { + extractTypeVariablesFromGenericInterfaces( + ((Class) pt.getRawType()).getGenericInterfaces(), typeVariableMap); + } + } else if (genericInterface instanceof Class) { + extractTypeVariablesFromGenericInterfaces( + ((Class) genericInterface).getGenericInterfaces(), typeVariableMap); + } + } + } - /** - * Read the {@link TypeVariable TypeVariables} from the supplied - * {@link ParameterizedType} and add mappings corresponding to the - * {@link TypeVariable#getName TypeVariable name} -> concrete type to the - * supplied {@link Map}. - *

- * Consider this case: - * - *

+ *

+ * For 'FooImpl' the following mappings would be added to the + * {@link Map}: {S=java.lang.String, T=java.lang.Integer}. + */ + private static void populateTypeMapFromParameterizedType( + ParameterizedType type, Map typeVariableMap) { + if (type.getRawType() instanceof Class) { + Type[] actualTypeArguments = type.getActualTypeArguments(); + TypeVariable[] typeVariables = ((Class) type.getRawType()) + .getTypeParameters(); + for (int i = 0; i < actualTypeArguments.length; i++) { + Type actualTypeArgument = actualTypeArguments[i]; + TypeVariable variable = typeVariables[i]; + if (actualTypeArgument instanceof Class) { + typeVariableMap.put(variable, actualTypeArgument); + } else if (actualTypeArgument instanceof GenericArrayType) { + typeVariableMap.put(variable, actualTypeArgument); + } else if (actualTypeArgument instanceof ParameterizedType) { + typeVariableMap.put(variable, actualTypeArgument); + } else if (actualTypeArgument instanceof TypeVariable) { + // We have a type that is parameterized at instantiation time + // the nearest match on the bridge method will be the bounded type. + TypeVariable typeVariableArgument = (TypeVariable) actualTypeArgument; + Type resolvedType = typeVariableMap.get(typeVariableArgument); + if (resolvedType == null) { + resolvedType = extractBoundForTypeVariable(typeVariableArgument); + } + typeVariableMap.put(variable, resolvedType); + } + } + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeDiscoverer.java index d7b732f67..df7de4b48 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -19,241 +19,241 @@ import org.springframework.util.ReflectionUtils; /** * Basic {@link TypeDiscoverer} that contains basic functionality to discover * property types. - * + * * @author Oliver Gierke */ class TypeDiscoverer implements TypeInformation { - private final Type type; - @SuppressWarnings("rawtypes") - private final Map typeVariableMap; - private final Map> fieldTypes = new ConcurrentHashMap>(); - private final TypeDiscoverer parent; + private final Type type; + @SuppressWarnings("rawtypes") + private final Map typeVariableMap; + private final Map> fieldTypes = new ConcurrentHashMap>(); + private final TypeDiscoverer parent; - /** - * Creates a ne {@link TypeDiscoverer} for the given type, type variable map and parent. - * - * @param type must not be null. - * @param typeVariableMap - * @param parent - */ - @SuppressWarnings("rawtypes") - protected TypeDiscoverer(Type type, Map typeVariableMap, - TypeDiscoverer parent) { + /** + * Creates a ne {@link TypeDiscoverer} for the given type, type variable map and parent. + * + * @param type must not be null. + * @param typeVariableMap + * @param parent + */ + @SuppressWarnings("rawtypes") + protected TypeDiscoverer(Type type, Map typeVariableMap, + TypeDiscoverer parent) { - Assert.notNull(type); - this.type = type; - this.typeVariableMap = typeVariableMap; - this.parent = parent; - } + Assert.notNull(type); + this.type = type; + this.typeVariableMap = typeVariableMap; + this.parent = parent; + } - /** - * Returns the type variable map. Will traverse the parents up to the root on - * and use it's map. - * - * @return - */ - @SuppressWarnings("rawtypes") - private Map getTypeVariableMap() { + /** + * Returns the type variable map. Will traverse the parents up to the root on + * and use it's map. + * + * @return + */ + @SuppressWarnings("rawtypes") + private Map getTypeVariableMap() { - return parent != null ? parent.getTypeVariableMap() : typeVariableMap; - } + return parent != null ? parent.getTypeVariableMap() : typeVariableMap; + } - /** - * Creates {@link TypeInformation} for the given {@link Type}. - * - * @param fieldType - * @return - */ - @SuppressWarnings({ "rawtypes", "unchecked" }) - protected TypeInformation createInfo(Type fieldType) { - - if (fieldType.equals(this.type)) { - return this; - } + /** + * Creates {@link TypeInformation} for the given {@link Type}. + * + * @param fieldType + * @return + */ + @SuppressWarnings({"rawtypes", "unchecked"}) + protected TypeInformation createInfo(Type fieldType) { - if (fieldType instanceof ParameterizedType) { - ParameterizedType parameterizedType = (ParameterizedType) fieldType; - return new TypeDiscoverer(parameterizedType, null, this); - } + if (fieldType.equals(this.type)) { + return this; + } - if (fieldType instanceof TypeVariable) { - TypeVariable variable = (TypeVariable) fieldType; - return new TypeVariableTypeInformation(variable, type, this); - } + if (fieldType instanceof ParameterizedType) { + ParameterizedType parameterizedType = (ParameterizedType) fieldType; + return new TypeDiscoverer(parameterizedType, null, this); + } - if (fieldType instanceof Class) { - return new ClassTypeInformation((Class) fieldType, this); - } - - if (fieldType instanceof GenericArrayType) { - return new ArrayTypeDiscoverer((GenericArrayType) fieldType, this); - } + if (fieldType instanceof TypeVariable) { + TypeVariable variable = (TypeVariable) fieldType; + return new TypeVariableTypeInformation(variable, type, this); + } - throw new IllegalArgumentException(); - } + if (fieldType instanceof Class) { + return new ClassTypeInformation((Class) fieldType, this); + } - /** - * Resolves the given type into a plain {@link Class}. - * - * @param type - * @return - */ - @SuppressWarnings("unchecked") - protected Class resolveType(Type type) { + if (fieldType instanceof GenericArrayType) { + return new ArrayTypeDiscoverer((GenericArrayType) fieldType, this); + } - return GenericTypeResolver.resolveType(type, getTypeVariableMap()); - } - - /* - * (non-Javadoc) - * @see org.springframework.data.util.TypeInformation#getParameterTypes(java.lang.reflect.Constructor) - */ - public List> getParameterTypes(Constructor constructor) { + throw new IllegalArgumentException(); + } - List> result = new ArrayList>(); - - for (Class type : constructor.getParameterTypes()) { - result.add(createInfo(type)); - } - - return result; - } + /** + * Resolves the given type into a plain {@link Class}. + * + * @param type + * @return + */ + @SuppressWarnings("unchecked") + protected Class resolveType(Type type) { - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation - * #getField(java.lang.String) - */ - public TypeInformation getProperty(String fieldname) { + return GenericTypeResolver.resolveType(type, getTypeVariableMap()); + } - int separatorIndex = fieldname.indexOf("."); + /* + * (non-Javadoc) + * @see org.springframework.data.util.TypeInformation#getParameterTypes(java.lang.reflect.Constructor) + */ + public List> getParameterTypes(Constructor constructor) { - if (separatorIndex == -1) { - if (fieldTypes.containsKey(fieldname)) { - return fieldTypes.get(fieldname); - } + List> result = new ArrayList>(); - TypeInformation propertyInformation = getPropertyInformation(fieldname); - if (propertyInformation != null) { - fieldTypes.put(fieldname, propertyInformation); - } - return propertyInformation; - } + for (Class type : constructor.getParameterTypes()) { + result.add(createInfo(type)); + } - String head = fieldname.substring(0, separatorIndex); - TypeInformation info = fieldTypes.get(head); - return info.getProperty(fieldname.substring(separatorIndex + 1)); - } + return result; + } - private TypeInformation getPropertyInformation(String fieldname) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation + * #getField(java.lang.String) + */ + public TypeInformation getProperty(String fieldname) { - Field field = ReflectionUtils.findField(getType(), fieldname); + int separatorIndex = fieldname.indexOf("."); - if (field == null) { - return null; - } + if (separatorIndex == -1) { + if (fieldTypes.containsKey(fieldname)) { + return fieldTypes.get(fieldname); + } - return createInfo(field.getGenericType()); - } + TypeInformation propertyInformation = getPropertyInformation(fieldname); + if (propertyInformation != null) { + fieldTypes.put(fieldname, propertyInformation); + } + return propertyInformation; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation - * #getType() - */ - public Class getType() { - return resolveType(type); - } - - /* (non-Javadoc) - * @see org.springframework.data.util.TypeInformation#isMap() - */ - public boolean isMap() { - Class rawType = getType(); - return rawType == null ? false : Map.class.isAssignableFrom(rawType); - } - - /* (non-Javadoc) - * @see org.springframework.data.util.TypeInformation#getMapValueType() - */ - public TypeInformation getMapValueType() { - - if (!Map.class.isAssignableFrom(getType())) { - return null; - } - - ParameterizedType parameterizedType = (ParameterizedType) type; - return createInfo(parameterizedType.getActualTypeArguments()[1]); - } - - /* (non-Javadoc) - * @see org.springframework.data.util.TypeInformation#isCollectionLike() - */ - public boolean isCollectionLike() { - - Class rawType = getType(); - return rawType == null ? null : rawType.isArray() || Iterable.class.isAssignableFrom(rawType); - } - - /* (non-Javadoc) - * @see org.springframework.data.util.TypeInformation#getComponentType() - */ - public TypeInformation getComponentType() { - - if (!(Map.class.isAssignableFrom(getType()) || isCollectionLike())) { - return null; - } - - ParameterizedType parameterizedType = (ParameterizedType) type; - return createInfo(parameterizedType.getActualTypeArguments()[0]); - } + String head = fieldname.substring(0, separatorIndex); + TypeInformation info = fieldTypes.get(head); + return info.getProperty(fieldname.substring(separatorIndex + 1)); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (obj == this) { - return true; - } + private TypeInformation getPropertyInformation(String fieldname) { - if (obj == null) { - return false; - } + Field field = ReflectionUtils.findField(getType(), fieldname); - if (!this.getClass().equals(obj.getClass())) { - return false; - } + if (field == null) { + return null; + } - TypeDiscoverer that = (TypeDiscoverer) obj; + return createInfo(field.getGenericType()); + } - boolean typeEqual = nullSafeEquals(this.type, that.type); - boolean typeVariableMapEqual = nullSafeEquals(this.typeVariableMap, - that.typeVariableMap); - boolean parentEqual = nullSafeEquals(this.parent, that.parent); + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.document.mongodb.TypeDiscovererTest.FieldInformation + * #getType() + */ + public Class getType() { + return resolveType(type); + } - return typeEqual && typeVariableMapEqual && parentEqual; - } + /* (non-Javadoc) + * @see org.springframework.data.util.TypeInformation#isMap() + */ + public boolean isMap() { + Class rawType = getType(); + return rawType == null ? false : Map.class.isAssignableFrom(rawType); + } - /* - * (non-Javadoc) - * - * @see java.lang.Object#hashCode() - */ - @Override - public int hashCode() { + /* (non-Javadoc) + * @see org.springframework.data.util.TypeInformation#getMapValueType() + */ + public TypeInformation getMapValueType() { - int result = 17; - result += nullSafeHashCode(type); - result += nullSafeHashCode(typeVariableMap); - result += nullSafeHashCode(parent); - return result; - } + if (!Map.class.isAssignableFrom(getType())) { + return null; + } + + ParameterizedType parameterizedType = (ParameterizedType) type; + return createInfo(parameterizedType.getActualTypeArguments()[1]); + } + + /* (non-Javadoc) + * @see org.springframework.data.util.TypeInformation#isCollectionLike() + */ + public boolean isCollectionLike() { + + Class rawType = getType(); + return rawType == null ? null : rawType.isArray() || Iterable.class.isAssignableFrom(rawType); + } + + /* (non-Javadoc) + * @see org.springframework.data.util.TypeInformation#getComponentType() + */ + public TypeInformation getComponentType() { + + if (!(Map.class.isAssignableFrom(getType()) || isCollectionLike())) { + return null; + } + + ParameterizedType parameterizedType = (ParameterizedType) type; + return createInfo(parameterizedType.getActualTypeArguments()[0]); + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (obj == this) { + return true; + } + + if (obj == null) { + return false; + } + + if (!this.getClass().equals(obj.getClass())) { + return false; + } + + TypeDiscoverer that = (TypeDiscoverer) obj; + + boolean typeEqual = nullSafeEquals(this.type, that.type); + boolean typeVariableMapEqual = nullSafeEquals(this.typeVariableMap, + that.typeVariableMap); + boolean parentEqual = nullSafeEquals(this.parent, that.parent); + + return typeEqual && typeVariableMapEqual && parentEqual; + } + + /* + * (non-Javadoc) + * + * @see java.lang.Object#hashCode() + */ + @Override + public int hashCode() { + + int result = 17; + result += nullSafeHashCode(type); + result += nullSafeHashCode(typeVariableMap); + result += nullSafeHashCode(parent); + return result; + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeInformation.java index 51723df0e..5af9e2f5c 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeInformation.java @@ -10,58 +10,58 @@ import java.util.Map; * Interface to access property types and resolving generics on the way. * Starting with a {@link ClassTypeInformation} you can travers properties using * {@link #getProperty(String)} to access type information. - * + * * @author Oliver Gierke */ public interface TypeInformation { - - List> getParameterTypes(Constructor constructor); - /** - * Returns the property information for the property with the given name. - * Supports proeprty traversal through dot notation. - * - * @param fieldname - * @return - */ - TypeInformation getProperty(String fieldname); + List> getParameterTypes(Constructor constructor); - /** - * Returns whether the type can be considered a collection, which means it's a container of elements, e.g. a - * {@link Collection} and {@link Array} or anything implementing {@link Iterable}. If this returns {@literal true} you - * can expect {@link #getComponentType()} to return a non-{@literal null} value. - * - * @return - */ - boolean isCollectionLike(); - - /** - * Returns the component type for {@link Collection}s or the key type for {@link Map}s. - * - * @return - */ - TypeInformation getComponentType(); + /** + * Returns the property information for the property with the given name. + * Supports proeprty traversal through dot notation. + * + * @param fieldname + * @return + */ + TypeInformation getProperty(String fieldname); - /** - * Returns whether the property is a {@link Map}. If this returns {@literal true} you can expect - * {@link #getComponentType()} as well as {@link #getMapValueType()} to return something not {@literal null}. - * - * @return - */ - boolean isMap(); - - /** - * Will return the type of the value in case the underlying type is a {@link Map}. - * - * @return - */ - TypeInformation getMapValueType(); + /** + * Returns whether the type can be considered a collection, which means it's a container of elements, e.g. a + * {@link Collection} and {@link Array} or anything implementing {@link Iterable}. If this returns {@literal true} you + * can expect {@link #getComponentType()} to return a non-{@literal null} value. + * + * @return + */ + boolean isCollectionLike(); - /** - * Returns the type of the property. Will resolve generics and the generic - * context of - * - * @return - */ - Class getType(); + /** + * Returns the component type for {@link Collection}s or the key type for {@link Map}s. + * + * @return + */ + TypeInformation getComponentType(); + + /** + * Returns whether the property is a {@link Map}. If this returns {@literal true} you can expect + * {@link #getComponentType()} as well as {@link #getMapValueType()} to return something not {@literal null}. + * + * @return + */ + boolean isMap(); + + /** + * Will return the type of the value in case the underlying type is a {@link Map}. + * + * @return + */ + TypeInformation getMapValueType(); + + /** + * Returns the type of the property. Will resolve generics and the generic + * context of + * + * @return + */ + Class getType(); } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java index ca2ce9fed..9a84fee16 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java @@ -1,6 +1,7 @@ package org.springframework.data.util; import static org.springframework.util.ObjectUtils.*; + import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; @@ -10,7 +11,7 @@ import org.springframework.util.Assert; /** * Special {@link TypeDiscoverer} to determine the actual type for a {@link TypeVariable}. Will consider the * context the {@link TypeVariable} is being used in. - * + * * @author Oliver Gierke */ class TypeVariableTypeInformation extends TypeDiscoverer { @@ -21,8 +22,8 @@ class TypeVariableTypeInformation extends TypeDiscoverer { /** * Creates a bew {@link TypeVariableTypeInformation} for the given {@link TypeVariable} owning {@link Type} and * parent {@link TypeDiscoverer}. - * - * @param variable must not be {@literal null} + * + * @param variable must not be {@literal null} * @param owningType must not be {@literal null} * @param parent */ @@ -42,8 +43,8 @@ class TypeVariableTypeInformation extends TypeDiscoverer { @Override public Class getType() { - int index = getIndex(variable); - + int index = getIndex(variable); + if (owningType instanceof ParameterizedType && index != -1) { Type fieldType = ((ParameterizedType) owningType).getActualTypeArguments()[index]; return resolveType(fieldType); @@ -54,6 +55,7 @@ class TypeVariableTypeInformation extends TypeDiscoverer { /** * Returns the index of the type parameter binding the given {@link TypeVariable}. + * * @param variable * @return */ @@ -70,33 +72,33 @@ class TypeVariableTypeInformation extends TypeDiscoverer { return -1; } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.util.TypeDiscoverer#equals(java.lang.Object) - */ - @Override - public boolean equals(Object obj) { - if (!super.equals(obj)) { - return false; - } - TypeVariableTypeInformation that = (TypeVariableTypeInformation) obj; - return nullSafeEquals(this.owningType, that.owningType) - && nullSafeEquals(this.variable, that.variable); - } - - /* - * (non-Javadoc) - * - * @see org.springframework.data.util.TypeDiscoverer#hashCode() - */ - @Override - public int hashCode() { - int result = super.hashCode(); - result += 31 * nullSafeHashCode(this.owningType); - result += 31 * nullSafeHashCode(this.variable); - return result; - } + /* + * (non-Javadoc) + * + * @see org.springframework.data.util.TypeDiscoverer#equals(java.lang.Object) + */ + @Override + public boolean equals(Object obj) { + if (!super.equals(obj)) { + return false; + } + + TypeVariableTypeInformation that = (TypeVariableTypeInformation) obj; + return nullSafeEquals(this.owningType, that.owningType) + && nullSafeEquals(this.variable, that.variable); + } + + /* + * (non-Javadoc) + * + * @see org.springframework.data.util.TypeDiscoverer#hashCode() + */ + @Override + public int hashCode() { + int result = super.hashCode(); + result += 31 * nullSafeHashCode(this.owningType); + result += 31 * nullSafeHashCode(this.variable); + return result; + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableArgumentResolver.java b/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableArgumentResolver.java index eac582878..ca46ccfca 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableArgumentResolver.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableArgumentResolver.java @@ -20,7 +20,6 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.util.HashSet; import java.util.Set; - import javax.servlet.ServletRequest; import org.springframework.beans.PropertyValue; @@ -43,285 +42,285 @@ import org.springframework.web.context.request.NativeWebRequest; * {@link Pageable} instances into controller methods. Request properties to be * parsed can be configured. Default configuration uses request properties * beginning with {@link #DEFAULT_PREFIX}{@link #DEFAULT_SEPARATOR}. - * + * * @author Oliver Gierke */ public class PageableArgumentResolver implements WebArgumentResolver { - private static final Pageable DEFAULT_PAGE_REQUEST = new PageRequest(0, 10); - private static final String DEFAULT_PREFIX = "page"; - private static final String DEFAULT_SEPARATOR = "."; + private static final Pageable DEFAULT_PAGE_REQUEST = new PageRequest(0, 10); + private static final String DEFAULT_PREFIX = "page"; + private static final String DEFAULT_SEPARATOR = "."; - private Pageable fallbackPagable = DEFAULT_PAGE_REQUEST; - private String prefix = DEFAULT_PREFIX; - private String separator = DEFAULT_SEPARATOR; + private Pageable fallbackPagable = DEFAULT_PAGE_REQUEST; + private String prefix = DEFAULT_PREFIX; + private String separator = DEFAULT_SEPARATOR; - /** - * Setter to configure a fallback instance of {@link Pageable} that is being - * used to back missing parameters. Defaults to - * {@value #DEFAULT_PAGE_REQUEST}. - * - * @param fallbackPagable the fallbackPagable to set - */ - public void setFallbackPagable(Pageable fallbackPagable) { + /** + * Setter to configure a fallback instance of {@link Pageable} that is being + * used to back missing parameters. Defaults to + * {@value #DEFAULT_PAGE_REQUEST}. + * + * @param fallbackPagable the fallbackPagable to set + */ + public void setFallbackPagable(Pageable fallbackPagable) { - this.fallbackPagable = - null == fallbackPagable ? DEFAULT_PAGE_REQUEST - : fallbackPagable; - } + this.fallbackPagable = + null == fallbackPagable ? DEFAULT_PAGE_REQUEST + : fallbackPagable; + } - /** - * Setter to configure the prefix of request parameters to be used to - * retrieve paging information. Defaults to {@link #DEFAULT_PREFIX}. - * - * @param prefix the prefix to set - */ - public void setPrefix(String prefix) { + /** + * Setter to configure the prefix of request parameters to be used to + * retrieve paging information. Defaults to {@link #DEFAULT_PREFIX}. + * + * @param prefix the prefix to set + */ + public void setPrefix(String prefix) { - this.prefix = null == prefix ? DEFAULT_PREFIX : prefix; - } + this.prefix = null == prefix ? DEFAULT_PREFIX : prefix; + } - /** - * Setter to configure the separator between prefix and actual property - * value. Defaults to {@link #DEFAULT_SEPARATOR}. - * - * @param separator the separator to set - */ - public void setSeparator(String separator) { + /** + * Setter to configure the separator between prefix and actual property + * value. Defaults to {@link #DEFAULT_SEPARATOR}. + * + * @param separator the separator to set + */ + public void setSeparator(String separator) { - this.separator = null == separator ? DEFAULT_SEPARATOR : separator; - } + this.separator = null == separator ? DEFAULT_SEPARATOR : separator; + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.web.bind.support.WebArgumentResolver#resolveArgument - * (org.springframework.core.MethodParameter, - * org.springframework.web.context.request.NativeWebRequest) - */ - public Object resolveArgument(MethodParameter methodParameter, - NativeWebRequest webRequest) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.web.bind.support.WebArgumentResolver#resolveArgument + * (org.springframework.core.MethodParameter, + * org.springframework.web.context.request.NativeWebRequest) + */ + public Object resolveArgument(MethodParameter methodParameter, + NativeWebRequest webRequest) { - if (methodParameter.getParameterType().equals(Pageable.class)) { + if (methodParameter.getParameterType().equals(Pageable.class)) { - assertPageableUniqueness(methodParameter); + assertPageableUniqueness(methodParameter); - Pageable request = - getDefaultFromAnnotationOrFallback(methodParameter); + Pageable request = + getDefaultFromAnnotationOrFallback(methodParameter); - ServletRequest servletRequest = - (ServletRequest) webRequest.getNativeRequest(); + ServletRequest servletRequest = + (ServletRequest) webRequest.getNativeRequest(); - PropertyValues propertyValues = - new ServletRequestParameterPropertyValues(servletRequest, - getPrefix(methodParameter), separator); + PropertyValues propertyValues = + new ServletRequestParameterPropertyValues(servletRequest, + getPrefix(methodParameter), separator); - DataBinder binder = new ServletRequestDataBinder(request); + DataBinder binder = new ServletRequestDataBinder(request); - binder.initDirectFieldAccess(); - binder.registerCustomEditor(Sort.class, new SortPropertyEditor( - "sort.dir", propertyValues)); - binder.bind(propertyValues); + binder.initDirectFieldAccess(); + binder.registerCustomEditor(Sort.class, new SortPropertyEditor( + "sort.dir", propertyValues)); + binder.bind(propertyValues); - if (request.getPageNumber() > 0) { + if (request.getPageNumber() > 0) { - request = - new PageRequest(request.getPageNumber() - 1, - request.getPageSize(), request.getSort()); - } + request = + new PageRequest(request.getPageNumber() - 1, + request.getPageSize(), request.getSort()); + } - return request; - } + return request; + } - return UNRESOLVED; - } + return UNRESOLVED; + } - private Pageable getDefaultFromAnnotationOrFallback( - MethodParameter methodParameter) { + private Pageable getDefaultFromAnnotationOrFallback( + MethodParameter methodParameter) { - // search for PageableDefaults annotation - for (Annotation annotation : methodParameter.getParameterAnnotations()) { - if (annotation instanceof PageableDefaults) { - PageableDefaults defaults = (PageableDefaults) annotation; - // +1 is because we substract 1 later - return new PageRequest(defaults.pageNumber() + 1, - defaults.value()); - } - } + // search for PageableDefaults annotation + for (Annotation annotation : methodParameter.getParameterAnnotations()) { + if (annotation instanceof PageableDefaults) { + PageableDefaults defaults = (PageableDefaults) annotation; + // +1 is because we substract 1 later + return new PageRequest(defaults.pageNumber() + 1, + defaults.value()); + } + } - // Construct request with fallback request to ensure sensible - // default values. Create fresh copy as Spring will manipulate the - // instance under the covers - return new PageRequest(fallbackPagable.getPageNumber(), - fallbackPagable.getPageSize(), fallbackPagable.getSort()); - } + // Construct request with fallback request to ensure sensible + // default values. Create fresh copy as Spring will manipulate the + // instance under the covers + return new PageRequest(fallbackPagable.getPageNumber(), + fallbackPagable.getPageSize(), fallbackPagable.getSort()); + } - /** - * Resolves the prefix to use to bind properties from. Will prepend a - * possible {@link Qualifier} if available or return the configured prefix - * otherwise. - * - * @param parameter - * @return - */ - private String getPrefix(MethodParameter parameter) { + /** + * Resolves the prefix to use to bind properties from. Will prepend a + * possible {@link Qualifier} if available or return the configured prefix + * otherwise. + * + * @param parameter + * @return + */ + private String getPrefix(MethodParameter parameter) { - for (Annotation annotation : parameter.getParameterAnnotations()) { - if (annotation instanceof Qualifier) { - return new StringBuilder(((Qualifier) annotation).value()) - .append("_").append(prefix).toString(); - } - } + for (Annotation annotation : parameter.getParameterAnnotations()) { + if (annotation instanceof Qualifier) { + return new StringBuilder(((Qualifier) annotation).value()) + .append("_").append(prefix).toString(); + } + } - return prefix; - } + return prefix; + } - /** - * Asserts uniqueness of all {@link Pageable} parameters of the method of - * the given {@link MethodParameter}. - * - * @param parameter - */ - private void assertPageableUniqueness(MethodParameter parameter) { + /** + * Asserts uniqueness of all {@link Pageable} parameters of the method of + * the given {@link MethodParameter}. + * + * @param parameter + */ + private void assertPageableUniqueness(MethodParameter parameter) { - Method method = parameter.getMethod(); + Method method = parameter.getMethod(); - if (containsMoreThanOnePageableParameter(method)) { - Annotation[][] annotations = method.getParameterAnnotations(); - assertQualifiersFor(method.getParameterTypes(), annotations); - } - } + if (containsMoreThanOnePageableParameter(method)) { + Annotation[][] annotations = method.getParameterAnnotations(); + assertQualifiersFor(method.getParameterTypes(), annotations); + } + } - /** - * Returns whether the given {@link Method} has more than one - * {@link Pageable} parameter. - * - * @param method - * @return - */ - private boolean containsMoreThanOnePageableParameter(Method method) { + /** + * Returns whether the given {@link Method} has more than one + * {@link Pageable} parameter. + * + * @param method + * @return + */ + private boolean containsMoreThanOnePageableParameter(Method method) { - boolean pageableFound = false; + boolean pageableFound = false; - for (Class type : method.getParameterTypes()) { + for (Class type : method.getParameterTypes()) { - if (pageableFound && type.equals(Pageable.class)) { - return true; - } + if (pageableFound && type.equals(Pageable.class)) { + return true; + } - if (type.equals(Pageable.class)) { - pageableFound = true; - } - } + if (type.equals(Pageable.class)) { + pageableFound = true; + } + } - return false; - } + return false; + } - /** - * Asserts that every {@link Pageable} parameter of the given parameters - * carries an {@link Qualifier} annotation to distinguish them from each - * other. - * - * @param parameterTypes - * @param annotations - */ - private void assertQualifiersFor(Class[] parameterTypes, - Annotation[][] annotations) { + /** + * Asserts that every {@link Pageable} parameter of the given parameters + * carries an {@link Qualifier} annotation to distinguish them from each + * other. + * + * @param parameterTypes + * @param annotations + */ + private void assertQualifiersFor(Class[] parameterTypes, + Annotation[][] annotations) { - Set values = new HashSet(); + Set values = new HashSet(); - for (int i = 0; i < annotations.length; i++) { + for (int i = 0; i < annotations.length; i++) { - if (Pageable.class.equals(parameterTypes[i])) { + if (Pageable.class.equals(parameterTypes[i])) { - Qualifier qualifier = findAnnotation(annotations[i]); + Qualifier qualifier = findAnnotation(annotations[i]); - if (null == qualifier) { - throw new IllegalStateException( - "Ambiguous Pageable arguments in handler method. If you use multiple parameters of type Pageable you need to qualify them with @Qualifier"); - } + if (null == qualifier) { + throw new IllegalStateException( + "Ambiguous Pageable arguments in handler method. If you use multiple parameters of type Pageable you need to qualify them with @Qualifier"); + } - if (values.contains(qualifier.value())) { - throw new IllegalStateException( - "Values of the user Qualifiers must be unique!"); - } + if (values.contains(qualifier.value())) { + throw new IllegalStateException( + "Values of the user Qualifiers must be unique!"); + } - values.add(qualifier.value()); - } - } - } + values.add(qualifier.value()); + } + } + } - /** - * Returns a {@link Qualifier} annotation from the given array of - * {@link Annotation}s. Returns {@literal null} if the array does not - * contain a {@link Qualifier} annotation. - * - * @param annotations - * @return - */ - private Qualifier findAnnotation(Annotation[] annotations) { + /** + * Returns a {@link Qualifier} annotation from the given array of + * {@link Annotation}s. Returns {@literal null} if the array does not + * contain a {@link Qualifier} annotation. + * + * @param annotations + * @return + */ + private Qualifier findAnnotation(Annotation[] annotations) { - for (Annotation annotation : annotations) { - if (annotation instanceof Qualifier) { - return (Qualifier) annotation; - } - } + for (Annotation annotation : annotations) { + if (annotation instanceof Qualifier) { + return (Qualifier) annotation; + } + } - return null; - } + return null; + } - /** - * {@link java.beans.PropertyEditor} to create {@link Sort} instances from - * textual representations. The implementation interprets the string as a - * comma separated list where the first entry is the sort direction ( - * {@code asc}, {@code desc}) followed by the properties to sort by. - * - * @author Oliver Gierke - */ - private static class SortPropertyEditor extends PropertyEditorSupport { + /** + * {@link java.beans.PropertyEditor} to create {@link Sort} instances from + * textual representations. The implementation interprets the string as a + * comma separated list where the first entry is the sort direction ( + * {@code asc}, {@code desc}) followed by the properties to sort by. + * + * @author Oliver Gierke + */ + private static class SortPropertyEditor extends PropertyEditorSupport { - private final String orderProperty; - private final PropertyValues values; + private final String orderProperty; + private final PropertyValues values; - /** - * Creates a new {@link SortPropertyEditor}. - * - * @param orderProperty - * @param values - */ - public SortPropertyEditor(String orderProperty, PropertyValues values) { + /** + * Creates a new {@link SortPropertyEditor}. + * + * @param orderProperty + * @param values + */ + public SortPropertyEditor(String orderProperty, PropertyValues values) { - this.orderProperty = orderProperty; - this.values = values; - } + this.orderProperty = orderProperty; + this.values = values; + } - /* - * (non-Javadoc) - * - * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) - */ - @Override - public void setAsText(String text) throws IllegalArgumentException { + /* + * (non-Javadoc) + * + * @see java.beans.PropertyEditorSupport#setAsText(java.lang.String) + */ + @Override + public void setAsText(String text) throws IllegalArgumentException { - PropertyValue rawOrder = values.getPropertyValue(orderProperty); - Direction order = - null == rawOrder ? Direction.ASC : Direction - .fromString(rawOrder.getValue().toString()); + PropertyValue rawOrder = values.getPropertyValue(orderProperty); + Direction order = + null == rawOrder ? Direction.ASC : Direction + .fromString(rawOrder.getValue().toString()); - setValue(new Sort(order, text)); - } - } + setValue(new Sort(order, text)); + } + } } diff --git a/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableDefaults.java b/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableDefaults.java index 1dcc56f6e..cdbcfa3cc 100644 --- a/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableDefaults.java +++ b/spring-data-commons-core/src/main/java/org/springframework/data/web/PageableDefaults.java @@ -26,25 +26,25 @@ import org.springframework.data.domain.Pageable; /** * Annotation to set defaults when injecting a {@link Pageable} into a * controller method. - * + * * @author Oliver Gierke */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.PARAMETER) public @interface PageableDefaults { - /** - * The default-size the injected - * {@link org.springframework.data.domain.Pageable} should get if no - * corresponding parameter defined in request (default is 10). - */ - int value() default 10; + /** + * The default-size the injected + * {@link org.springframework.data.domain.Pageable} should get if no + * corresponding parameter defined in request (default is 10). + */ + int value() default 10; - /** - * The default-pagenumber the injected - * {@link org.synyx.hades.domain.Pageable} should get if no corresponding - * parameter defined in request (default is 0). - */ - int pageNumber() default 0; + /** + * The default-pagenumber the injected + * {@link org.synyx.hades.domain.Pageable} should get if no corresponding + * parameter defined in request (default is 0). + */ + int pageNumber() default 0; } diff --git a/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd b/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd index ec2fb81df..8221d9f43 100644 --- a/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd +++ b/spring-data-commons-core/src/main/resources/org/springframework/data/repository/config/spring-repository-1.0.xsd @@ -1,18 +1,18 @@ + xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tool="http://www.springframework.org/schema/tool" + xmlns:context="http://www.springframework.org/schema/context" + targetNamespace="http://www.springframework.org/schema/data/repository" + elementFormDefault="qualified" attributeFormDefault="unqualified"> - + + schemaLocation="http://www.springframework.org/schema/context/spring-context.xsd"/> + minOccurs="0" maxOccurs="unbounded"> - + @@ -43,23 +43,23 @@ - + - - + + - - - + + + - + - - + + @@ -99,43 +99,43 @@ - + - + - + - + - + - + - + - + \ No newline at end of file diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/domain/DirectionUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/domain/DirectionUnitTests.java index 8582d653a..72ea9e665 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/domain/DirectionUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/domain/DirectionUnitTests.java @@ -8,22 +8,22 @@ import org.springframework.data.domain.Sort.Direction; /** * Unit test for {@link Direction}. - * + * * @author Oliver Gierke */ public class DirectionUnitTests { - @Test - public void jpaValueMapping() throws Exception { + @Test + public void jpaValueMapping() throws Exception { - assertEquals(Direction.ASC, Direction.fromString("asc")); - assertEquals(Direction.DESC, Direction.fromString("desc")); - } + assertEquals(Direction.ASC, Direction.fromString("asc")); + assertEquals(Direction.DESC, Direction.fromString("desc")); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsInvalidString() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsInvalidString() throws Exception { - Direction.fromString("foo"); - } + Direction.fromString("foo"); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java index bf22c90c5..208794ea8 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageImplUnitTests.java @@ -29,62 +29,62 @@ import org.junit.Test; /** * Unit test for {@link PageImpl}. - * + * * @author Oliver Gierke */ public class PageImplUnitTests { - @Test - public void assertEqualsForSimpleSetup() throws Exception { + @Test + public void assertEqualsForSimpleSetup() throws Exception { - PageImpl page = new PageImpl(Arrays.asList("Foo")); + PageImpl page = new PageImpl(Arrays.asList("Foo")); - assertEqualsAndHashcode(page, page); - assertEqualsAndHashcode(page, - new PageImpl(Arrays.asList("Foo"))); - } + assertEqualsAndHashcode(page, page); + assertEqualsAndHashcode(page, + new PageImpl(Arrays.asList("Foo"))); + } - @Test - public void assertEqualsForComplexSetup() throws Exception { + @Test + public void assertEqualsForComplexSetup() throws Exception { - Pageable pageable = new PageRequest(0, 10); - List content = Arrays.asList("Foo"); + Pageable pageable = new PageRequest(0, 10); + List content = Arrays.asList("Foo"); - PageImpl page = new PageImpl(content, pageable, 100); + PageImpl page = new PageImpl(content, pageable, 100); - assertEqualsAndHashcode(page, page); + assertEqualsAndHashcode(page, page); - assertEqualsAndHashcode(page, new PageImpl(content, pageable, - 100)); + assertEqualsAndHashcode(page, new PageImpl(content, pageable, + 100)); - assertNotEqualsAndHashcode(page, new PageImpl(content, - pageable, 90)); + assertNotEqualsAndHashcode(page, new PageImpl(content, + pageable, 90)); - assertNotEqualsAndHashcode(page, new PageImpl(content, - new PageRequest(1, 10), 100)); + assertNotEqualsAndHashcode(page, new PageImpl(content, + new PageRequest(1, 10), 100)); - assertNotEqualsAndHashcode(page, new PageImpl(content, - new PageRequest(0, 15), 100)); - } + assertNotEqualsAndHashcode(page, new PageImpl(content, + new PageRequest(0, 15), 100)); + } - @Test(expected = IllegalArgumentException.class) - public void preventsNullContentForSimpleSetup() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void preventsNullContentForSimpleSetup() throws Exception { - new PageImpl(null); - } + new PageImpl(null); + } - @Test(expected = IllegalArgumentException.class) - public void preventsNullContentForAdvancedSetup() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void preventsNullContentForAdvancedSetup() throws Exception { - new PageImpl(null, null, 0); - } - - @Test + new PageImpl(null, null, 0); + } + + @Test public void createsPageForEmptyContentCorrectly() { - List list = Collections.emptyList(); + List list = Collections.emptyList(); Page page = new PageImpl(list); assertThat(page.getContent(), is(list)); assertThat(page.getNumber(), is(0)); diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageRequestUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageRequestUnitTests.java index 3977dd6f7..b9ea8e942 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageRequestUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/domain/PageRequestUnitTests.java @@ -24,71 +24,71 @@ import org.springframework.data.domain.Sort.Direction; /** * Unit test for {@link PageRequest}. - * + * * @author Oliver Gierke */ public class PageRequestUnitTests { - @Test(expected = IllegalArgumentException.class) - public void preventsNegativePage() { + @Test(expected = IllegalArgumentException.class) + public void preventsNegativePage() { - new PageRequest(-1, 10); - } + new PageRequest(-1, 10); + } - @Test(expected = IllegalArgumentException.class) - public void preventsNegativeSize() { + @Test(expected = IllegalArgumentException.class) + public void preventsNegativeSize() { - new PageRequest(0, -1); - } + new PageRequest(0, -1); + } - @Test(expected = IllegalArgumentException.class) - public void preventsZeroSize() { + @Test(expected = IllegalArgumentException.class) + public void preventsZeroSize() { - new PageRequest(0, 0); - } + new PageRequest(0, 0); + } - @Test - public void equalsRegardsSortCorrectly() { + @Test + public void equalsRegardsSortCorrectly() { - Sort sort = new Sort(Direction.DESC, "foo"); - PageRequest request = new PageRequest(0, 10, sort); + Sort sort = new Sort(Direction.DESC, "foo"); + PageRequest request = new PageRequest(0, 10, sort); - // Equals itself - assertEqualsAndHashcode(request, request); + // Equals itself + assertEqualsAndHashcode(request, request); - // Equals another instance with same setup - assertEqualsAndHashcode(request, new PageRequest(0, 10, sort)); + // Equals another instance with same setup + assertEqualsAndHashcode(request, new PageRequest(0, 10, sort)); - // Equals without sort entirely - assertEqualsAndHashcode(new PageRequest(0, 10), new PageRequest(0, 10)); + // Equals without sort entirely + assertEqualsAndHashcode(new PageRequest(0, 10), new PageRequest(0, 10)); - // Is not equal to instance without sort - assertNotEqualsAndHashcode(request, new PageRequest(0, 10)); + // Is not equal to instance without sort + assertNotEqualsAndHashcode(request, new PageRequest(0, 10)); - // Is not equal to instance with another sort - assertNotEqualsAndHashcode(request, new PageRequest(0, 10, - Direction.ASC, "foo")); - } + // Is not equal to instance with another sort + assertNotEqualsAndHashcode(request, new PageRequest(0, 10, + Direction.ASC, "foo")); + } - @Test - public void equalsHonoursPageAndSize() { + @Test + public void equalsHonoursPageAndSize() { - PageRequest request = new PageRequest(0, 10); + PageRequest request = new PageRequest(0, 10); - // Equals itself - assertEqualsAndHashcode(request, request); + // Equals itself + assertEqualsAndHashcode(request, request); - // Equals same setup - assertEqualsAndHashcode(request, new PageRequest(0, 10)); + // Equals same setup + assertEqualsAndHashcode(request, new PageRequest(0, 10)); - // Does not equal on different page - assertNotEqualsAndHashcode(request, new PageRequest(1, 10)); + // Does not equal on different page + assertNotEqualsAndHashcode(request, new PageRequest(1, 10)); - // Does not equal on different size - assertNotEqualsAndHashcode(request, new PageRequest(0, 11)); - } + // Does not equal on different size + assertNotEqualsAndHashcode(request, new PageRequest(0, 11)); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/domain/SortUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/domain/SortUnitTests.java index c6323910e..03112d23b 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/domain/SortUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/domain/SortUnitTests.java @@ -24,72 +24,72 @@ import org.springframework.data.domain.Sort.Direction; /** * Unit test for {@link Sort}. - * + * * @author Oliver Gierke */ public class SortUnitTests { - /** - * Asserts that the class applies the default sort order if no order or - * {@code null} was provided. - * - * @throws Exception - */ - @Test - public void appliesDefaultForOrder() throws Exception { + /** + * Asserts that the class applies the default sort order if no order or + * {@code null} was provided. + * + * @throws Exception + */ + @Test + public void appliesDefaultForOrder() throws Exception { - assertEquals(Sort.DEFAULT_DIRECTION, new Sort("foo").iterator().next() - .getDirection()); - assertEquals(Sort.DEFAULT_DIRECTION, new Sort((Direction) null, "foo") - .iterator().next().getDirection()); - } + assertEquals(Sort.DEFAULT_DIRECTION, new Sort("foo").iterator().next() + .getDirection()); + assertEquals(Sort.DEFAULT_DIRECTION, new Sort((Direction) null, "foo") + .iterator().next().getDirection()); + } - /** - * Asserts that the class rejects {@code null} as properties array. - * - * @throws Exception - */ - @Test(expected = IllegalArgumentException.class) - public void preventsNullProperties() throws Exception { + /** + * Asserts that the class rejects {@code null} as properties array. + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void preventsNullProperties() throws Exception { - new Sort(Direction.ASC, (String[]) null); - } + new Sort(Direction.ASC, (String[]) null); + } - /** - * Asserts that the class rejects {@code null} values in the properties - * array. - * - * @throws Exception - */ - @Test(expected = IllegalArgumentException.class) - public void preventsNullProperty() throws Exception { + /** + * Asserts that the class rejects {@code null} values in the properties + * array. + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void preventsNullProperty() throws Exception { - new Sort(Direction.ASC, (String) null); - } + new Sort(Direction.ASC, (String) null); + } - /** - * Asserts that the class rejects empty strings in the properties array. - * - * @throws Exception - */ - @Test(expected = IllegalArgumentException.class) - public void preventsEmptyProperty() throws Exception { + /** + * Asserts that the class rejects empty strings in the properties array. + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void preventsEmptyProperty() throws Exception { - new Sort(Direction.ASC, ""); - } + new Sort(Direction.ASC, ""); + } - /** - * Asserts that the class rejects no properties given at all. - * - * @throws Exception - */ - @Test(expected = IllegalArgumentException.class) - public void preventsNoProperties() throws Exception { + /** + * Asserts that the class rejects no properties given at all. + * + * @throws Exception + */ + @Test(expected = IllegalArgumentException.class) + public void preventsNoProperties() throws Exception { - new Sort(Direction.ASC); - } + new Sort(Direction.ASC); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/domain/UnitTestUtils.java b/spring-data-commons-core/src/test/java/org/springframework/data/domain/UnitTestUtils.java index 752a20186..a7f356a31 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/domain/UnitTestUtils.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/domain/UnitTestUtils.java @@ -8,37 +8,37 @@ import static org.junit.Assert.*; */ public abstract class UnitTestUtils { - private UnitTestUtils() { + private UnitTestUtils() { - } + } - /** - * Asserts that delivered objects both equal each other as well as return - * the same hash code. - * - * @param first - * @param second - */ - public static void assertEqualsAndHashcode(Object first, Object second) { + /** + * Asserts that delivered objects both equal each other as well as return + * the same hash code. + * + * @param first + * @param second + */ + public static void assertEqualsAndHashcode(Object first, Object second) { - assertEquals(first, second); - assertEquals(second, first); - assertEquals(first.hashCode(), second.hashCode()); - } + assertEquals(first, second); + assertEquals(second, first); + assertEquals(first.hashCode(), second.hashCode()); + } - /** - * Asserts that both objects are not equal to each other and differ in hash - * code, too. - * - * @param first - * @param second - */ - public static void assertNotEqualsAndHashcode(Object first, Object second) { + /** + * Asserts that both objects are not equal to each other and differ in hash + * code, too. + * + * @param first + * @param second + */ + public static void assertNotEqualsAndHashcode(Object first, Object second) { - assertFalse(first.equals(second)); - assertFalse(second.equals(first)); - assertFalse(first.hashCode() == second.hashCode()); - } + assertFalse(first.equals(second)); + assertFalse(second.equals(first)); + assertFalse(first.hashCode() == second.hashCode()); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Child.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Child.java index 000c13bdc..5f0fbae28 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Child.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Child.java @@ -24,8 +24,8 @@ import org.springframework.data.annotation.Persistent; @Persistent public class Child extends PersonWithId { - public Child(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + public Child(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Document.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Document.java index 1810d4407..6947fe621 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Document.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Document.java @@ -16,20 +16,20 @@ package org.springframework.data.mapping; -import org.springframework.data.annotation.Persistent; - import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.springframework.data.annotation.Persistent; + /** * @author Jon Brisbin */ @Retention(RetentionPolicy.RUNTIME) @Target({ - ElementType.TYPE, - ElementType.FIELD + ElementType.TYPE, + ElementType.FIELD }) @Persistent public @interface Document { diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java index f456b7520..5ccade0f6 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/MappingMetadataTests.java @@ -16,6 +16,8 @@ package org.springframework.data.mapping; +import static org.junit.Assert.*; + import java.beans.PropertyDescriptor; import java.lang.reflect.Field; @@ -26,71 +28,69 @@ import org.springframework.data.mapping.model.PersistentEntity; import org.springframework.data.mapping.model.PersistentProperty; import org.springframework.data.util.TypeInformation; -import static org.junit.Assert.*; - /** * @author Jon Brisbin */ public class MappingMetadataTests { - SampleMappingContext ctx; + SampleMappingContext ctx; - @Before - public void setup() { - ctx = new SampleMappingContext(); - } + @Before + public void setup() { + ctx = new SampleMappingContext(); + } - @Test - public void testPojoWithId() { - PersistentEntity person = ctx.addPersistentEntity(PersonWithId.class); - assertNotNull(person.getIdProperty()); - assertEquals(String.class, person.getIdProperty().getType()); - } + @Test + public void testPojoWithId() { + PersistentEntity person = ctx.addPersistentEntity(PersonWithId.class); + assertNotNull(person.getIdProperty()); + assertEquals(String.class, person.getIdProperty().getType()); + } - @Test - public void testAssociations() { - PersistentEntity person = ctx.addPersistentEntity(PersonWithChildren.class); - assertNotNull(person.getAssociations()); + @Test + public void testAssociations() { + PersistentEntity person = ctx.addPersistentEntity(PersonWithChildren.class); + assertNotNull(person.getAssociations()); - for (Association association : person.getAssociations()) { - assertEquals(Child.class, association.getInverse().getComponentType()); - } - } - - public interface SampleProperty extends PersistentProperty { - } - - public class SampleMappingContext extends AbstractMappingContext, SampleProperty> { + for (Association association : person.getAssociations()) { + assertEquals(Child.class, association.getInverse().getComponentType()); + } + } - @Override - protected MutablePersistentEntity createPersistentEntity( - TypeInformation typeInformation) { + public interface SampleProperty extends PersistentProperty { + } - return new BasicPersistentEntity(typeInformation); - } + public class SampleMappingContext extends AbstractMappingContext, SampleProperty> { - @Override - protected SampleProperty createPersistentProperty(Field field, - PropertyDescriptor descriptor, - MutablePersistentEntity owner) { - return new SamplePropertyImpl(field, descriptor, owner); - } - } - - public class SamplePropertyImpl extends AnnotationBasedPersistentProperty implements SampleProperty { + @Override + protected MutablePersistentEntity createPersistentEntity( + TypeInformation typeInformation) { - public SamplePropertyImpl(Field field, - PropertyDescriptor propertyDescriptor, - PersistentEntity owner) { + return new BasicPersistentEntity(typeInformation); + } - super(field, propertyDescriptor, owner); - } - - @Override - protected Association createAssociation() { - - return new Association(this, null); - } - } + @Override + protected SampleProperty createPersistentProperty(Field field, + PropertyDescriptor descriptor, + MutablePersistentEntity owner) { + return new SamplePropertyImpl(field, descriptor, owner); + } + } + + public class SamplePropertyImpl extends AnnotationBasedPersistentProperty implements SampleProperty { + + public SamplePropertyImpl(Field field, + PropertyDescriptor propertyDescriptor, + PersistentEntity owner) { + + super(field, propertyDescriptor, owner); + } + + @Override + protected Association createAssociation() { + + return new Association(this, null); + } + } } \ No newline at end of file diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Person.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Person.java index 404536d5b..e85815832 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Person.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/Person.java @@ -21,38 +21,38 @@ package org.springframework.data.mapping; */ public abstract class Person { - private Integer ssn; - private String firstName; - private String lastName; + private Integer ssn; + private String firstName; + private String lastName; - protected Person(Integer ssn, String firstName, String lastName) { - this.ssn = ssn; - this.firstName = firstName; - this.lastName = lastName; - } + protected Person(Integer ssn, String firstName, String lastName) { + this.ssn = ssn; + this.firstName = firstName; + this.lastName = lastName; + } - public Integer getSsn() { - return ssn; - } + public Integer getSsn() { + return ssn; + } - public void setSsn(Integer ssn) { - this.ssn = ssn; - } + public void setSsn(Integer ssn) { + this.ssn = ssn; + } - public String getFirstName() { - return firstName; - } + public String getFirstName() { + return firstName; + } - public void setFirstName(String firstName) { - this.firstName = firstName; - } + public void setFirstName(String firstName) { + this.firstName = firstName; + } - public String getLastName() { - return lastName; - } + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } - public void setLastName(String lastName) { - this.lastName = lastName; - } - } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonDocument.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonDocument.java index e8b0b0a4d..d5ad1bee2 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonDocument.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonDocument.java @@ -22,8 +22,8 @@ package org.springframework.data.mapping; @Document public class PersonDocument extends Person { - public PersonDocument(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + public PersonDocument(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonNoId.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonNoId.java index d6851bae7..39d0c6163 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonNoId.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonNoId.java @@ -22,8 +22,8 @@ package org.springframework.data.mapping; @Document public class PersonNoId extends Person { - public PersonNoId(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + public PersonNoId(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonPersistent.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonPersistent.java index 992ee0f04..a7f0758bc 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonPersistent.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonPersistent.java @@ -24,8 +24,8 @@ import org.springframework.data.annotation.Persistent; @Persistent public class PersonPersistent extends PersonWithId { - public PersonPersistent(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + public PersonPersistent(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithChildren.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithChildren.java index 7f629efc9..6ba1dd005 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithChildren.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithChildren.java @@ -16,32 +16,32 @@ package org.springframework.data.mapping; +import java.util.List; + import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Reference; -import java.util.List; - /** * @author Jon Brisbin */ public class PersonWithChildren extends Person { - @Id - String id; - - @Reference - private List children; + @Id + String id; - public PersonWithChildren(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + @Reference + private List children; - public List getChildren() { - return children; - } + public PersonWithChildren(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } - public void setChildren(List children) { - this.children = children; - } + public List getChildren() { + return children; + } + + public void setChildren(List children) { + this.children = children; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithId.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithId.java index 8fa88a5df..dab827caa 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithId.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PersonWithId.java @@ -23,15 +23,15 @@ import org.springframework.data.annotation.Id; */ public class PersonWithId extends Person { - @Id - private String id; + @Id + private String id; - public PersonWithId(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } + public PersonWithId(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } - public String getId() { - return id; - } + public String getId() { + return id; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java index 4161e63f7..3e5d24791 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/mapping/PreferredConstructorDiscovererUnitTests.java @@ -28,106 +28,106 @@ import org.springframework.data.mapping.model.PreferredConstructor.Parameter; /** * Unit tests for {@link PreferredConstructorDiscoverer}. - * + * * @author Oliver Gierke */ public class PreferredConstructorDiscovererUnitTests { - @Test - public void findsNoArgConstructorForClassWithoutExplicitConstructor() { + @Test + public void findsNoArgConstructorForClassWithoutExplicitConstructor() { - PreferredConstructorDiscoverer discoverer = - new PreferredConstructorDiscoverer( - EntityWithoutConstructor.class); - PreferredConstructor constructor = - discoverer.getConstructor(); + PreferredConstructorDiscoverer discoverer = + new PreferredConstructorDiscoverer( + EntityWithoutConstructor.class); + PreferredConstructor constructor = + discoverer.getConstructor(); - assertThat(constructor, is(notNullValue())); - assertThat(constructor.isNoArgConstructor(), is(true)); - assertThat(constructor.isExplicitlyAnnotated(), is(false)); - } + assertThat(constructor, is(notNullValue())); + assertThat(constructor.isNoArgConstructor(), is(true)); + assertThat(constructor.isExplicitlyAnnotated(), is(false)); + } - @Test - public void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne() { + @Test + public void findsNoArgConstructorForClassWithMultipleConstructorsAndNoArgOne() { - PreferredConstructorDiscoverer discoverer = - new PreferredConstructorDiscoverer( - ClassWithEmptyConstructor.class); - PreferredConstructor constructor = - discoverer.getConstructor(); + PreferredConstructorDiscoverer discoverer = + new PreferredConstructorDiscoverer( + ClassWithEmptyConstructor.class); + PreferredConstructor constructor = + discoverer.getConstructor(); - assertThat(constructor, is(notNullValue())); - assertThat(constructor.isNoArgConstructor(), is(true)); - assertThat(constructor.isExplicitlyAnnotated(), is(false)); - } + assertThat(constructor, is(notNullValue())); + assertThat(constructor.isNoArgConstructor(), is(true)); + assertThat(constructor.isExplicitlyAnnotated(), is(false)); + } - @Test(expected=IllegalArgumentException.class) - public void throwsExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() { + @Test(expected = IllegalArgumentException.class) + public void throwsExceptionForMultipleConstructorsAndNoNoArgConstructorWithoutAnnotation() { - new PreferredConstructorDiscoverer( - ClassWithMultipleConstructorsWithoutEmptyOne.class); - } - - @Test - public void usesConstructorWithAnnotationOverEveryOther() { + new PreferredConstructorDiscoverer( + ClassWithMultipleConstructorsWithoutEmptyOne.class); + } - PreferredConstructorDiscoverer discoverer = - new PreferredConstructorDiscoverer( - ClassWithMultipleConstructorsAndAnnotation.class); - PreferredConstructor constructor = - discoverer.getConstructor(); - - assertThat(constructor, is(notNullValue())); - assertThat(constructor.isNoArgConstructor(), is(false)); - assertThat(constructor.isExplicitlyAnnotated(), is(true)); - - List> parameters = constructor.getParameters(); - - assertThat(parameters.size(), is(1)); - assertThat(parameters.get(0).getType().getType(), typeCompatibleWith(Long.class)); - } + @Test + public void usesConstructorWithAnnotationOverEveryOther() { - static class EntityWithoutConstructor { + PreferredConstructorDiscoverer discoverer = + new PreferredConstructorDiscoverer( + ClassWithMultipleConstructorsAndAnnotation.class); + PreferredConstructor constructor = + discoverer.getConstructor(); - } + assertThat(constructor, is(notNullValue())); + assertThat(constructor.isNoArgConstructor(), is(false)); + assertThat(constructor.isExplicitlyAnnotated(), is(true)); - static class ClassWithEmptyConstructor { + List> parameters = constructor.getParameters(); - public ClassWithEmptyConstructor() { - } - } + assertThat(parameters.size(), is(1)); + assertThat(parameters.get(0).getType().getType(), typeCompatibleWith(Long.class)); + } - static class ClassWithMultipleConstructorsAndEmptyOne { + static class EntityWithoutConstructor { - public ClassWithMultipleConstructorsAndEmptyOne(String value) { - } + } + + static class ClassWithEmptyConstructor { + + public ClassWithEmptyConstructor() { + } + } + + static class ClassWithMultipleConstructorsAndEmptyOne { + + public ClassWithMultipleConstructorsAndEmptyOne(String value) { + } - public ClassWithMultipleConstructorsAndEmptyOne() { - } - } + public ClassWithMultipleConstructorsAndEmptyOne() { + } + } - static class ClassWithMultipleConstructorsWithoutEmptyOne { + static class ClassWithMultipleConstructorsWithoutEmptyOne { - public ClassWithMultipleConstructorsWithoutEmptyOne(String value) { - } + public ClassWithMultipleConstructorsWithoutEmptyOne(String value) { + } - public ClassWithMultipleConstructorsWithoutEmptyOne(Long value) { - } - } - - static class ClassWithMultipleConstructorsAndAnnotation { - - public ClassWithMultipleConstructorsAndAnnotation() { - } + public ClassWithMultipleConstructorsWithoutEmptyOne(Long value) { + } + } - public ClassWithMultipleConstructorsAndAnnotation(String value) { - } + static class ClassWithMultipleConstructorsAndAnnotation { - @PersistenceConstructor - public ClassWithMultipleConstructorsAndAnnotation(Long value) { - } - } + public ClassWithMultipleConstructorsAndAnnotation() { + } + + public ClassWithMultipleConstructorsAndAnnotation(String value) { + } + + @PersistenceConstructor + public ClassWithMultipleConstructorsAndAnnotation(Long value) { + } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/querydsl/SimpleEntityPathResolverUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/querydsl/SimpleEntityPathResolverUnitTests.java index 146f25a8a..db6bcaa14 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/querydsl/SimpleEntityPathResolverUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/querydsl/SimpleEntityPathResolverUnitTests.java @@ -18,55 +18,49 @@ package org.springframework.data.querydsl; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import org.junit.Test; -import org.springframework.data.querydsl.EntityPathResolver; -import org.springframework.data.querydsl.SimpleEntityPathResolver; -import org.springframework.data.querydsl.QSimpleEntityPathResolverUnitTests_NamedUser; -import org.springframework.data.querydsl.QSimpleEntityPathResolverUnitTests_Sample; -import org.springframework.data.querydsl.QUser; - import com.mysema.query.annotations.QueryEntity; +import org.junit.Test; /** * Unit test for {@link SimpleEntityPathResolver}. - * + * * @author Oliver Gierke */ public class SimpleEntityPathResolverUnitTests { - EntityPathResolver resolver = SimpleEntityPathResolver.INSTANCE; + EntityPathResolver resolver = SimpleEntityPathResolver.INSTANCE; - @Test - public void createsRepositoryFromDomainClassCorrectly() throws Exception { + @Test + public void createsRepositoryFromDomainClassCorrectly() throws Exception { - assertThat(resolver.createPath(User.class), is(QUser.class)); - } + assertThat(resolver.createPath(User.class), is(QUser.class)); + } - @Test - public void resolvesEntityPathForInnerClassCorrectly() throws Exception { + @Test + public void resolvesEntityPathForInnerClassCorrectly() throws Exception { - assertThat(resolver.createPath(NamedUser.class), - is(QSimpleEntityPathResolverUnitTests_NamedUser.class)); - } + assertThat(resolver.createPath(NamedUser.class), + is(QSimpleEntityPathResolverUnitTests_NamedUser.class)); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme() - throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsClassWithoutQueryClassConfrmingToTheNamingScheme() + throws Exception { - resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class); - } + resolver.createPath(QSimpleEntityPathResolverUnitTests_Sample.class); + } - @QueryEntity - static class Sample { + @QueryEntity + static class Sample { - } - - @QueryEntity - static class NamedUser { - - } + } + + @QueryEntity + static class NamedUser { + + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/config/TypeFilterParserUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/config/TypeFilterParserUnitTests.java index bc1c24603..89dc2626a 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/config/TypeFilterParserUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/config/TypeFilterParserUnitTests.java @@ -15,11 +15,10 @@ */ package org.springframework.data.repository.config; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.isA; import static org.mockito.Mockito.*; import java.io.IOException; - import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; @@ -40,69 +39,69 @@ import org.xml.sax.SAXException; /** * Unit test for {@link TypeFilterParser}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class TypeFilterParserUnitTests { - private TypeFilterParser parser; - private Element documentElement; + private TypeFilterParser parser; + private Element documentElement; - @Mock - private ClassLoader classLoader; + @Mock + private ClassLoader classLoader; - @Mock - private ReaderContext context; + @Mock + private ReaderContext context; - @Mock - private ClassPathScanningCandidateComponentProvider scanner; + @Mock + private ClassPathScanningCandidateComponentProvider scanner; - @Before - public void setUp() throws SAXException, IOException, - ParserConfigurationException { + @Before + public void setUp() throws SAXException, IOException, + ParserConfigurationException { - parser = new TypeFilterParser(classLoader, context); + parser = new TypeFilterParser(classLoader, context); - Resource sampleXmlFile = - new ClassPathResource("type-filter-test.xml", - TypeFilterParserUnitTests.class); + Resource sampleXmlFile = + new ClassPathResource("type-filter-test.xml", + TypeFilterParserUnitTests.class); - DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); - factory.setNamespaceAware(true); + DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); + factory.setNamespaceAware(true); - documentElement = - factory.newDocumentBuilder() - .parse(sampleXmlFile.getInputStream()) - .getDocumentElement(); - } + documentElement = + factory.newDocumentBuilder() + .parse(sampleXmlFile.getInputStream()) + .getDocumentElement(); + } - @Test - public void parsesIncludesCorrectly() throws Exception { + @Test + public void parsesIncludesCorrectly() throws Exception { - Element element = - DomUtils.getChildElementByTagName(documentElement, - "firstSample"); + Element element = + DomUtils.getChildElementByTagName(documentElement, + "firstSample"); - parser.parseFilters(element, scanner); + parser.parseFilters(element, scanner); - verify(scanner, atLeastOnce()).addIncludeFilter( - isA(AssignableTypeFilter.class)); - } + verify(scanner, atLeastOnce()).addIncludeFilter( + isA(AssignableTypeFilter.class)); + } - @Test - public void parsesExcludesCorrectly() throws Exception { + @Test + public void parsesExcludesCorrectly() throws Exception { - Element element = - DomUtils.getChildElementByTagName(documentElement, - "secondSample"); + Element element = + DomUtils.getChildElementByTagName(documentElement, + "secondSample"); - parser.parseFilters(element, scanner); + parser.parseFilters(element, scanner); - verify(scanner, atLeastOnce()).addExcludeFilter( - isA(AssignableTypeFilter.class)); - } + verify(scanner, atLeastOnce()).addExcludeFilter( + isA(AssignableTypeFilter.class)); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java index 53a53025a..13a022950 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/ParametersUnitTests.java @@ -28,137 +28,137 @@ import org.springframework.data.domain.Sort; /** * Unit test for {@link Parameters}. - * + * * @author Oliver Gierke */ public class ParametersUnitTests { - private Method valid; + private Method valid; - @Before - public void setUp() throws SecurityException, NoSuchMethodException { + @Before + public void setUp() throws SecurityException, NoSuchMethodException { - valid = SampleDao.class.getMethod("valid", String.class); - } + valid = SampleDao.class.getMethod("valid", String.class); + } - @Test - public void checksValidMethodCorrectly() throws Exception { + @Test + public void checksValidMethodCorrectly() throws Exception { - Method validWithPageable = - SampleDao.class.getMethod("validWithPageable", String.class, - Pageable.class); - Method validWithSort = - SampleDao.class.getMethod("validWithSort", String.class, - Sort.class); + Method validWithPageable = + SampleDao.class.getMethod("validWithPageable", String.class, + Pageable.class); + Method validWithSort = + SampleDao.class.getMethod("validWithSort", String.class, + Sort.class); - new Parameters(valid); - new Parameters(validWithPageable); - new Parameters(validWithSort); - } + new Parameters(valid); + new Parameters(validWithPageable); + new Parameters(validWithSort); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsInvalidMethodWithParamMissing() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsInvalidMethodWithParamMissing() throws Exception { - Method method = - SampleDao.class.getMethod("invalidParamMissing", String.class, - String.class); - new Parameters(method); - } + Method method = + SampleDao.class.getMethod("invalidParamMissing", String.class, + String.class); + new Parameters(method); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullMethod() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullMethod() throws Exception { - new Parameters(null); - } + new Parameters(null); + } - @Test - public void detectsNamedParameterCorrectly() throws Exception { + @Test + public void detectsNamedParameterCorrectly() throws Exception { - Parameters parameters = - getParametersFor("validWithSort", String.class, Sort.class); + Parameters parameters = + getParametersFor("validWithSort", String.class, Sort.class); - Parameter parameter = parameters.getParameter(0); + Parameter parameter = parameters.getParameter(0); - assertThat(parameter.isNamedParameter(), is(true)); - assertThat(parameter.getPlaceholder(), is(":username")); + assertThat(parameter.isNamedParameter(), is(true)); + assertThat(parameter.getPlaceholder(), is(":username")); - parameter = parameters.getParameter(1); + parameter = parameters.getParameter(1); - assertThat(parameter.isNamedParameter(), is(false)); - assertThat(parameter.isSpecialParameter(), is(true)); - } + assertThat(parameter.isNamedParameter(), is(false)); + assertThat(parameter.isSpecialParameter(), is(true)); + } - @Test - public void calculatesPlaceholderPositionCorrectly() throws Exception { + @Test + public void calculatesPlaceholderPositionCorrectly() throws Exception { - Method method = - SampleDao.class.getMethod("validWithSortFirst", Sort.class, - String.class); + Method method = + SampleDao.class.getMethod("validWithSortFirst", Sort.class, + String.class); - Parameters parameters = new Parameters(method); - assertThat(parameters.getBindableParameter(0).getIndex(), is(1)); + Parameters parameters = new Parameters(method); + assertThat(parameters.getBindableParameter(0).getIndex(), is(1)); - method = - SampleDao.class.getMethod("validWithSortInBetween", - String.class, Sort.class, String.class); + method = + SampleDao.class.getMethod("validWithSortInBetween", + String.class, Sort.class, String.class); - parameters = new Parameters(method); + parameters = new Parameters(method); - assertThat(parameters.getBindableParameter(0).getIndex(), is(0)); - assertThat(parameters.getBindableParameter(1).getIndex(), is(2)); - } + assertThat(parameters.getBindableParameter(0).getIndex(), is(0)); + assertThat(parameters.getBindableParameter(1).getIndex(), is(2)); + } - @Test - public void detectsEmptyParameterListCorrectly() throws Exception { + @Test + public void detectsEmptyParameterListCorrectly() throws Exception { - Parameters parameters = getParametersFor("emptyParameters"); - assertThat(parameters.hasParameterAt(0), is(false)); - } + Parameters parameters = getParametersFor("emptyParameters"); + assertThat(parameters.hasParameterAt(0), is(false)); + } - private Parameters getParametersFor(String methodName, - Class... parameterTypes) throws SecurityException, - NoSuchMethodException { + private Parameters getParametersFor(String methodName, + Class... parameterTypes) throws SecurityException, + NoSuchMethodException { - Method method = SampleDao.class.getMethod(methodName, parameterTypes); + Method method = SampleDao.class.getMethod(methodName, parameterTypes); - return new Parameters(method); - } + return new Parameters(method); + } - static class User { + static class User { - } + } - static interface SampleDao { + static interface SampleDao { - User valid(@Param("username") String username); + User valid(@Param("username") String username); - User invalidParamMissing(@Param("username") String username, - String lastname); + User invalidParamMissing(@Param("username") String username, + String lastname); - User validWithPageable(@Param("username") String username, - Pageable pageable); + User validWithPageable(@Param("username") String username, + Pageable pageable); - User validWithSort(@Param("username") String username, Sort sort); + User validWithSort(@Param("username") String username, Sort sort); - User validWithSortFirst(Sort sort, String username); + User validWithSortFirst(Sort sort, String username); - User validWithSortInBetween(String firstname, Sort sort, String lastname); + User validWithSortInBetween(String firstname, Sort sort, String lastname); - User emptyParameters(); + User emptyParameters(); - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/SimpleParameterAccessorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/SimpleParameterAccessorUnitTests.java index db8f5b104..e3e6182b9 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/SimpleParameterAccessorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/SimpleParameterAccessorUnitTests.java @@ -27,117 +27,117 @@ import org.springframework.data.domain.Sort; /** * Unit tests for {@link ParametersParameterAccessor}. - * + * * @author Oliver Gierke */ public class SimpleParameterAccessorUnitTests { - Parameters parameters, sortParameters, pageableParameters; + Parameters parameters, sortParameters, pageableParameters; - @Before - public void setUp() throws SecurityException, NoSuchMethodException { + @Before + public void setUp() throws SecurityException, NoSuchMethodException { - parameters = - new Parameters(Sample.class.getMethod("sample", String.class)); - sortParameters = - new Parameters(Sample.class.getMethod("sample1", String.class, - Sort.class)); - pageableParameters = - new Parameters(Sample.class.getMethod("sample2", String.class, - Pageable.class)); - } + parameters = + new Parameters(Sample.class.getMethod("sample", String.class)); + sortParameters = + new Parameters(Sample.class.getMethod("sample1", String.class, + Sort.class)); + pageableParameters = + new Parameters(Sample.class.getMethod("sample2", String.class, + Pageable.class)); + } - @Test - public void testname() throws Exception { + @Test + public void testname() throws Exception { - new ParametersParameterAccessor(parameters, new Object[] { "test" }); - } + new ParametersParameterAccessor(parameters, new Object[]{"test"}); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullParameters() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullParameters() throws Exception { - new ParametersParameterAccessor(null, new Object[0]); - } + new ParametersParameterAccessor(null, new Object[0]); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullValues() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullValues() throws Exception { - new ParametersParameterAccessor(parameters, null); - } + new ParametersParameterAccessor(parameters, null); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsTooLittleNumberOfArguments() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsTooLittleNumberOfArguments() throws Exception { - new ParametersParameterAccessor(parameters, new Object[0]); - } + new ParametersParameterAccessor(parameters, new Object[0]); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsTooManyArguments() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsTooManyArguments() throws Exception { - new ParametersParameterAccessor(parameters, new Object[] { "test", "test" }); - } + new ParametersParameterAccessor(parameters, new Object[]{"test", "test"}); + } - @Test - public void returnsNullForPageableAndSortIfNoneAvailable() throws Exception { + @Test + public void returnsNullForPageableAndSortIfNoneAvailable() throws Exception { - ParameterAccessor accessor = - new ParametersParameterAccessor(parameters, new Object[] { "test" }); - assertThat(accessor.getPageable(), is(nullValue())); - assertThat(accessor.getSort(), is(nullValue())); - } + ParameterAccessor accessor = + new ParametersParameterAccessor(parameters, new Object[]{"test"}); + assertThat(accessor.getPageable(), is(nullValue())); + assertThat(accessor.getSort(), is(nullValue())); + } - @Test - public void returnsSortIfAvailable() { + @Test + public void returnsSortIfAvailable() { - Sort sort = new Sort("foo"); - ParameterAccessor accessor = - new ParametersParameterAccessor(sortParameters, new Object[] { - "test", sort }); - assertThat(accessor.getSort(), is(sort)); - assertThat(accessor.getPageable(), is(nullValue())); - } + Sort sort = new Sort("foo"); + ParameterAccessor accessor = + new ParametersParameterAccessor(sortParameters, new Object[]{ + "test", sort}); + assertThat(accessor.getSort(), is(sort)); + assertThat(accessor.getPageable(), is(nullValue())); + } - @Test - public void returnsPageableIfAvailable() { + @Test + public void returnsPageableIfAvailable() { - Pageable pageable = new PageRequest(0, 10); - ParameterAccessor accessor = - new ParametersParameterAccessor(pageableParameters, new Object[] { - "test", pageable }); - assertThat(accessor.getPageable(), is(pageable)); - assertThat(accessor.getSort(), is(nullValue())); - } + Pageable pageable = new PageRequest(0, 10); + ParameterAccessor accessor = + new ParametersParameterAccessor(pageableParameters, new Object[]{ + "test", pageable}); + assertThat(accessor.getPageable(), is(pageable)); + assertThat(accessor.getSort(), is(nullValue())); + } - @Test - public void returnsSortFromPageableIfAvailable() throws Exception { + @Test + public void returnsSortFromPageableIfAvailable() throws Exception { - Sort sort = new Sort("foo"); - Pageable pageable = new PageRequest(0, 10, sort); - ParameterAccessor accessor = - new ParametersParameterAccessor(pageableParameters, new Object[] { - "test", pageable }); - assertThat(accessor.getPageable(), is(pageable)); - assertThat(accessor.getSort(), is(sort)); - } + Sort sort = new Sort("foo"); + Pageable pageable = new PageRequest(0, 10, sort); + ParameterAccessor accessor = + new ParametersParameterAccessor(pageableParameters, new Object[]{ + "test", pageable}); + assertThat(accessor.getPageable(), is(pageable)); + assertThat(accessor.getSort(), is(sort)); + } - interface Sample { + interface Sample { - void sample(String firstname); + void sample(String firstname); - void sample1(String firstname, Sort sort); + void sample1(String firstname, Sort sort); - void sample2(String firstname, Pageable pageable); - } + void sample2(String firstname, Pageable pageable); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java index 5f67ed439..4e1c03e89 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/OrderBySourceUnitTests.java @@ -26,61 +26,61 @@ import org.springframework.data.domain.Sort.Order; /** * Unit test for {@link OrderBySource}. - * + * * @author Oliver Gierke */ public class OrderBySourceUnitTests { - @Test - public void handlesSingleDirectionAndPropertyCorrectly() throws Exception { + @Test + public void handlesSingleDirectionAndPropertyCorrectly() throws Exception { - assertThat(new OrderBySource("UsernameDesc").toSort(), is(new Sort( - DESC, "username"))); - } + assertThat(new OrderBySource("UsernameDesc").toSort(), is(new Sort( + DESC, "username"))); + } - @Test - public void handlesCamelCasePropertyCorrecty() throws Exception { + @Test + public void handlesCamelCasePropertyCorrecty() throws Exception { - assertThat(new OrderBySource("LastnameUsernameDesc").toSort(), - is(new Sort(DESC, "lastnameUsername"))); - } + assertThat(new OrderBySource("LastnameUsernameDesc").toSort(), + is(new Sort(DESC, "lastnameUsername"))); + } - @Test - public void handlesMultipleDirectionsCorrectly() throws Exception { + @Test + public void handlesMultipleDirectionsCorrectly() throws Exception { - OrderBySource orderBySource = - new OrderBySource("LastnameAscUsernameDesc"); - assertThat(orderBySource.toSort(), is(new Sort(new Order(ASC, - "lastname"), new Order(DESC, "username")))); - } + OrderBySource orderBySource = + new OrderBySource("LastnameAscUsernameDesc"); + assertThat(orderBySource.toSort(), is(new Sort(new Order(ASC, + "lastname"), new Order(DESC, "username")))); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsMissingProperty() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsMissingProperty() throws Exception { - new OrderBySource("Desc"); - } + new OrderBySource("Desc"); + } - @Test - public void usesNestedPropertyCorrectly() throws Exception { + @Test + public void usesNestedPropertyCorrectly() throws Exception { - OrderBySource source = new OrderBySource("BarNameDesc", Foo.class); - assertThat(source.toSort(), is(new Sort(new Order(DESC, "bar.name")))); + OrderBySource source = new OrderBySource("BarNameDesc", Foo.class); + assertThat(source.toSort(), is(new Sort(new Order(DESC, "bar.name")))); - } + } - @SuppressWarnings("unused") - private class Foo { + @SuppressWarnings("unused") + private class Foo { - private Bar bar; - } + private Bar bar; + } - @SuppressWarnings("unused") - private class Bar { + @SuppressWarnings("unused") + private class Bar { - private String name; - } + private String name; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java index 2bcadd453..c48d3714c 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PartTreeUnitTests.java @@ -29,147 +29,147 @@ import org.springframework.data.repository.query.parser.PartTree.OrPart; /** * Unit tests for {@link PartTree}. - * + * * @author Oliver Gierke */ public class PartTreeUnitTests { - @Test(expected = IllegalArgumentException.class) - public void rejectsNullSource() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullSource() throws Exception { - new PartTree(null, getClass()); - } + new PartTree(null, getClass()); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullDomainClass() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullDomainClass() throws Exception { - new PartTree("test", null); - } + new PartTree("test", null); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsMultipleOrderBy() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsMultipleOrderBy() throws Exception { - new PartTree("firstnameOrderByLastnameOrderByFirstname", User.class); - } + new PartTree("firstnameOrderByLastnameOrderByFirstname", User.class); + } - @Test - public void parsesSimplePropertyCorrectly() throws Exception { + @Test + public void parsesSimplePropertyCorrectly() throws Exception { - PartTree partTree = new PartTree("firstname", User.class); - assertPart(partTree, new Part[] { new Part("firstname", User.class) }); - } + PartTree partTree = new PartTree("firstname", User.class); + assertPart(partTree, new Part[]{new Part("firstname", User.class)}); + } - @Test - public void parsesAndPropertiesCorrectly() throws Exception { + @Test + public void parsesAndPropertiesCorrectly() throws Exception { - PartTree partTree = new PartTree("firstnameAndLastname", User.class); - assertPart(partTree, new Part[] { new Part("firstname", User.class), - new Part("lastname", User.class) }); - assertThat(partTree.getSort(), is(nullValue())); - } + PartTree partTree = new PartTree("firstnameAndLastname", User.class); + assertPart(partTree, new Part[]{new Part("firstname", User.class), + new Part("lastname", User.class)}); + assertThat(partTree.getSort(), is(nullValue())); + } - @Test - public void parsesOrPropertiesCorrectly() throws Exception { + @Test + public void parsesOrPropertiesCorrectly() throws Exception { - PartTree partTree = new PartTree("firstnameOrLastname", User.class); - assertPart(partTree, new Part[] { new Part("firstname", User.class) }, - new Part[] { new Part("lastname", User.class) }); - assertThat(partTree.getSort(), is(nullValue())); - } + PartTree partTree = new PartTree("firstnameOrLastname", User.class); + assertPart(partTree, new Part[]{new Part("firstname", User.class)}, + new Part[]{new Part("lastname", User.class)}); + assertThat(partTree.getSort(), is(nullValue())); + } - @Test - public void parsesCombinedAndAndOrPropertiesCorrectly() throws Exception { + @Test + public void parsesCombinedAndAndOrPropertiesCorrectly() throws Exception { - PartTree tree = - new PartTree("firstnameAndLastnameOrLastname", User.class); - assertPart(tree, new Part[] { new Part("firstname", User.class), - new Part("lastname", User.class) }, new Part[] { new Part( - "lastname", User.class) }); - } + PartTree tree = + new PartTree("firstnameAndLastnameOrLastname", User.class); + assertPart(tree, new Part[]{new Part("firstname", User.class), + new Part("lastname", User.class)}, new Part[]{new Part( + "lastname", User.class)}); + } - @Test - public void hasSortIfOrderByIsGiven() throws Exception { + @Test + public void hasSortIfOrderByIsGiven() throws Exception { - PartTree partTree = - new PartTree("firstnameOrderByLastnameDesc", User.class); - assertThat(partTree.getSort(), is(new Sort(Direction.DESC, "lastname"))); - } + PartTree partTree = + new PartTree("firstnameOrderByLastnameDesc", User.class); + assertThat(partTree.getSort(), is(new Sort(Direction.DESC, "lastname"))); + } - @Test - public void detectsDistinctCorrectly() throws Exception { + @Test + public void detectsDistinctCorrectly() throws Exception { - PartTree tree = new PartTree("findDistinctByLastname", User.class); - assertThat(tree.isDistinct(), is(true)); + PartTree tree = new PartTree("findDistinctByLastname", User.class); + assertThat(tree.isDistinct(), is(true)); - tree = new PartTree("findUsersDistinctByLastname", User.class); - assertThat(tree.isDistinct(), is(true)); + tree = new PartTree("findUsersDistinctByLastname", User.class); + assertThat(tree.isDistinct(), is(true)); - tree = new PartTree("findDistinctUsersByLastname", User.class); - assertThat(tree.isDistinct(), is(true)); + tree = new PartTree("findDistinctUsersByLastname", User.class); + assertThat(tree.isDistinct(), is(true)); - tree = new PartTree("findUsersByLastname", User.class); - assertThat(tree.isDistinct(), is(false)); + tree = new PartTree("findUsersByLastname", User.class); + assertThat(tree.isDistinct(), is(false)); - tree = new PartTree("findByLastname", User.class); - assertThat(tree.isDistinct(), is(false)); + tree = new PartTree("findByLastname", User.class); + assertThat(tree.isDistinct(), is(false)); - // Check it's non-greedy (would strip everything until Order*By* - // otherwise) - tree = new PartTree("findByLastnameOrderByFirstnameDesc", User.class); - assertThat(tree.isDistinct(), is(false)); - assertThat(tree.getSort(), is(new Sort(Direction.DESC, "firstname"))); - } + // Check it's non-greedy (would strip everything until Order*By* + // otherwise) + tree = new PartTree("findByLastnameOrderByFirstnameDesc", User.class); + assertThat(tree.isDistinct(), is(false)); + assertThat(tree.getSort(), is(new Sort(Direction.DESC, "firstname"))); + } - - @Test - public void parsesWithinCorrectly () { - PartTree tree = new PartTree("findByLocationWithin", User.class); - for (Part part : tree.getParts()) { - assertThat(part.getType(), is(Type.WITHIN)); - assertThat(part.getProperty(), is(new Property("location", User.class))); - } - } - - @Test - public void parsesNearCorrectly () { - PartTree tree = new PartTree("findByLocationNear", User.class); - for (Part part : tree.getParts()) { - assertThat(part.getType(), is(Type.NEAR)); - assertThat(part.getProperty(), is(new Property("location", User.class))); - } - } - private void assertPart(PartTree tree, Part[]... parts) { + @Test + public void parsesWithinCorrectly() { + PartTree tree = new PartTree("findByLocationWithin", User.class); + for (Part part : tree.getParts()) { + assertThat(part.getType(), is(Type.WITHIN)); + assertThat(part.getProperty(), is(new Property("location", User.class))); + } + } - Iterator iterator = tree.iterator(); - for (Part[] part : parts) { - assertThat(iterator.hasNext(), is(true)); - Iterator partIterator = iterator.next().iterator(); - for (int k = 0; k < part.length; k++) { - assertThat(String.format("Expected %d parts but have %d", - part.length, k + 1), partIterator.hasNext(), is(true)); - Part next = partIterator.next(); - assertThat( - String.format("Expected %s but got %s!", part[k], next), - part[k], is(next)); - } - assertThat("Too many parts!", partIterator.hasNext(), is(false)); - } - assertThat("Too many or parts!", iterator.hasNext(), is(false)); - } + @Test + public void parsesNearCorrectly() { + PartTree tree = new PartTree("findByLocationNear", User.class); + for (Part part : tree.getParts()) { + assertThat(part.getType(), is(Type.NEAR)); + assertThat(part.getProperty(), is(new Property("location", User.class))); + } + } - class User { + private void assertPart(PartTree tree, Part[]... parts) { - String firstname; - String lastname; - double[] location; - } + Iterator iterator = tree.iterator(); + for (Part[] part : parts) { + assertThat(iterator.hasNext(), is(true)); + Iterator partIterator = iterator.next().iterator(); + for (int k = 0; k < part.length; k++) { + assertThat(String.format("Expected %d parts but have %d", + part.length, k + 1), partIterator.hasNext(), is(true)); + Part next = partIterator.next(); + assertThat( + String.format("Expected %s but got %s!", part[k], next), + part[k], is(next)); + } + assertThat("Too many parts!", partIterator.hasNext(), is(false)); + } + assertThat("Too many or parts!", iterator.hasNext(), is(false)); + } + + class User { + + String firstname; + String lastname; + double[] location; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java index f5426528a..a426919db 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/query/parser/PropertyUnitTests.java @@ -26,138 +26,138 @@ import org.junit.Test; /** * Unit tests for {@link Property}. - * + * * @author Oliver Gierke */ @SuppressWarnings("unused") public class PropertyUnitTests { - @Test - public void parsesSimplePropertyCorrectly() throws Exception { + @Test + public void parsesSimplePropertyCorrectly() throws Exception { - Property reference = Property.from("userName", Foo.class); - assertThat(reference.hasNext(), is(false)); - assertThat(reference.toDotPath(), is("userName")); - } + Property reference = Property.from("userName", Foo.class); + assertThat(reference.hasNext(), is(false)); + assertThat(reference.toDotPath(), is("userName")); + } - @Test - public void parsesPathPropertyCorrectly() throws Exception { + @Test + public void parsesPathPropertyCorrectly() throws Exception { - Property reference = Property.from("userName", Bar.class); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - assertThat(reference.toDotPath(), is("user.name")); - } + Property reference = Property.from("userName", Bar.class); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + assertThat(reference.toDotPath(), is("user.name")); + } - @Test - public void prefersLongerMatches() throws Exception { + @Test + public void prefersLongerMatches() throws Exception { - Property reference = Property.from("userName", Sample.class); - assertThat(reference.hasNext(), is(false)); - assertThat(reference.toDotPath(), is("userName")); - } + Property reference = Property.from("userName", Sample.class); + assertThat(reference.hasNext(), is(false)); + assertThat(reference.toDotPath(), is("userName")); + } - @Test - public void testname() throws Exception { + @Test + public void testname() throws Exception { - Property reference = Property.from("userName", Sample2.class); - assertThat(reference.getName(), is("user")); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - } + Property reference = Property.from("userName", Sample2.class); + assertThat(reference.getName(), is("user")); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + } - @Test - public void prfersExplicitPaths() throws Exception { + @Test + public void prfersExplicitPaths() throws Exception { - Property reference = Property.from("user_name", Sample.class); - assertThat(reference.getName(), is("user")); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - } + Property reference = Property.from("user_name", Sample.class); + assertThat(reference.getName(), is("user")); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + } - @Test - public void handlesGenericsCorrectly() throws Exception { + @Test + public void handlesGenericsCorrectly() throws Exception { - Property reference = Property.from("usersName", Bar.class); - assertThat(reference.getName(), is("users")); - assertThat(reference.isCollection(), is(true)); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - } + Property reference = Property.from("usersName", Bar.class); + assertThat(reference.getName(), is("users")); + assertThat(reference.isCollection(), is(true)); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + } - @Test - public void handlesMapCorrectly() throws Exception { + @Test + public void handlesMapCorrectly() throws Exception { - Property reference = Property.from("userMapName", Bar.class); - assertThat(reference.getName(), is("userMap")); - assertThat(reference.isCollection(), is(false)); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - } + Property reference = Property.from("userMapName", Bar.class); + assertThat(reference.getName(), is("userMap")); + assertThat(reference.isCollection(), is(false)); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + } - @Test - public void handlesArrayCorrectly() throws Exception { + @Test + public void handlesArrayCorrectly() throws Exception { - Property reference = Property.from("userArrayName", Bar.class); - assertThat(reference.getName(), is("userArray")); - assertThat(reference.isCollection(), is(true)); - assertThat(reference.hasNext(), is(true)); - assertThat(reference.next(), is(new Property("name", FooBar.class))); - } - - @Test(expected = IllegalArgumentException.class) - public void handlesInvalidCollectionCompountTypeProperl() { - - Property.from("usersMame", Bar.class); - } - - @Test(expected = IllegalArgumentException.class) - public void handlesInvalidMapValueTypeProperl() { - - Property.from("userMapMame", Bar.class); - } - - @Test - public void findsNested() { - - Property from = Property.from("barUserName", Sample.class); - } + Property reference = Property.from("userArrayName", Bar.class); + assertThat(reference.getName(), is("userArray")); + assertThat(reference.isCollection(), is(true)); + assertThat(reference.hasNext(), is(true)); + assertThat(reference.next(), is(new Property("name", FooBar.class))); + } - private class Foo { + @Test(expected = IllegalArgumentException.class) + public void handlesInvalidCollectionCompountTypeProperl() { - String userName; - } + Property.from("usersMame", Bar.class); + } - private class Bar { + @Test(expected = IllegalArgumentException.class) + public void handlesInvalidMapValueTypeProperl() { - private FooBar user; - private Set users; - private Map userMap; - private FooBar[] userArray; - } + Property.from("userMapMame", Bar.class); + } - private class FooBar { + @Test + public void findsNested() { - private String name; - } + Property from = Property.from("barUserName", Sample.class); + } - private class Sample { + private class Foo { - private String userName; - private FooBar user; - private Bar bar; - } + String userName; + } - private class Sample2 { + private class Bar { - private String userNameWhatever; - private FooBar user; - } + private FooBar user; + private Set users; + private Map userMap; + private FooBar[] userArray; + } + + private class FooBar { + + private String name; + } + + private class Sample { + + private String userName; + private FooBar user; + private Bar bar; + } + + private class Sample2 { + + private String userNameWhatever; + private FooBar user; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/AbstractEntityInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/AbstractEntityInformationUnitTests.java index 56f04a315..9de1a5fee 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/AbstractEntityInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/AbstractEntityInformationUnitTests.java @@ -25,54 +25,54 @@ import org.junit.Test; /** * Unit tests for {@link AbstractEntityInformation}. - * + * * @author Oliver Gierke */ public class AbstractEntityInformationUnitTests { - @Test(expected = IllegalArgumentException.class) - public void rejectsNullDomainClass() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullDomainClass() throws Exception { - new DummyAbstractEntityInformation(null); - } + new DummyAbstractEntityInformation(null); + } - @Test - public void considersEntityNewIfGetIdReturnsNull() throws Exception { + @Test + public void considersEntityNewIfGetIdReturnsNull() throws Exception { - EntityInformation metadata = - new DummyAbstractEntityInformation(Object.class); - assertThat(metadata.isNew(null), is(true)); - assertThat(metadata.isNew(new Object()), is(false)); - } + EntityInformation metadata = + new DummyAbstractEntityInformation(Object.class); + assertThat(metadata.isNew(null), is(true)); + assertThat(metadata.isNew(new Object()), is(false)); + } - private static class DummyAbstractEntityInformation extends - AbstractEntityInformation { + private static class DummyAbstractEntityInformation extends + AbstractEntityInformation { - public DummyAbstractEntityInformation(Class domainClass) { + public DummyAbstractEntityInformation(Class domainClass) { - super(domainClass); - } + super(domainClass); + } - /* - * (non-Javadoc) - * - * @see - * org.springframework.data.repository.support.EntityMetadata#getId( - * java.lang.Object) - */ - public Serializable getId(Object entity) { + /* + * (non-Javadoc) + * + * @see + * org.springframework.data.repository.support.EntityMetadata#getId( + * java.lang.Object) + */ + public Serializable getId(Object entity) { - return entity == null ? null : entity.toString(); - } - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.EntityInformation#getIdType() - */ - public Class getIdType() { - - return Serializable.class; - } - } + return entity == null ? null : entity.toString(); + } + + /* (non-Javadoc) + * @see org.springframework.data.repository.support.EntityInformation#getIdType() + */ + public Class getIdType() { + + return Serializable.class; + } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryInformationUnitTests.java index fe17e2ccb..5d922eb47 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryInformationUnitTests.java @@ -10,54 +10,53 @@ import org.springframework.data.repository.Repository; import org.springframework.data.repository.support.DefaultRepositoryMetadataUnitTests.DummyGenericRepositorySupport; /** - * * @author Oliver Gierke */ public class DefaultRepositoryInformationUnitTests { - @SuppressWarnings("rawtypes") - static final Class REPOSITORY = DummyGenericRepositorySupport.class; + @SuppressWarnings("rawtypes") + static final Class REPOSITORY = DummyGenericRepositorySupport.class; - @Test - public void discoversRepositoryBaseClassMethod() throws Exception { + @Test + public void discoversRepositoryBaseClassMethod() throws Exception { - Method method = FooDao.class.getMethod("findOne", Integer.class); - RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooDao.class); - DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, REPOSITORY); + Method method = FooDao.class.getMethod("findOne", Integer.class); + RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooDao.class); + DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, REPOSITORY); - Method reference = information.getBaseClassMethodFor(method); - assertEquals(REPOSITORY, reference.getDeclaringClass()); - assertThat(reference.getName(), is("findOne")); - } + Method reference = information.getBaseClassMethodFor(method); + assertEquals(REPOSITORY, reference.getDeclaringClass()); + assertThat(reference.getName(), is("findOne")); + } - @Test - public void discoveresNonRepositoryBaseClassMethod() throws Exception { + @Test + public void discoveresNonRepositoryBaseClassMethod() throws Exception { - Method method = FooDao.class.getMethod("findOne", Long.class); + Method method = FooDao.class.getMethod("findOne", Long.class); - RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooDao.class); - DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, Repository.class); + RepositoryMetadata metadata = new DefaultRepositoryMetadata(FooDao.class); + DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, Repository.class); - assertThat(information.getBaseClassMethodFor(method), is(method)); - } + assertThat(information.getBaseClassMethodFor(method), is(method)); + } - private static interface FooDao extends Repository { + private static interface FooDao extends Repository { - // Redeclared method - User findOne(Integer primaryKey); + // Redeclared method + User findOne(Integer primaryKey); - // Not a redeclared method - User findOne(Long primaryKey); - } + // Not a redeclared method + User findOne(Long primaryKey); + } - @SuppressWarnings("unused") - private class User { + @SuppressWarnings("unused") + private class User { - private String firstname; + private String firstname; - public String getAddress() { + public String getAddress() { - return null; - } - } + return null; + } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryMetadataUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryMetadataUnitTests.java index c0955c632..69564615c 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryMetadataUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DefaultRepositoryMetadataUnitTests.java @@ -28,118 +28,118 @@ import org.springframework.data.repository.util.ClassUtils; /** * Unit tests for {@link DefaultRepositoryMetadata}. - * + * * @author Oliver Gierke */ public class DefaultRepositoryMetadataUnitTests { - @Test - public void looksUpDomainClassCorrectly() throws Exception { + @Test + public void looksUpDomainClassCorrectly() throws Exception { - RepositoryMetadata metadata = - new DefaultRepositoryMetadata(UserRepository.class); - assertEquals(User.class, metadata.getDomainClass()); + RepositoryMetadata metadata = + new DefaultRepositoryMetadata(UserRepository.class); + assertEquals(User.class, metadata.getDomainClass()); - metadata = new DefaultRepositoryMetadata(SomeDao.class); - assertEquals(User.class, metadata.getDomainClass()); - } + metadata = new DefaultRepositoryMetadata(SomeDao.class); + assertEquals(User.class, metadata.getDomainClass()); + } - @Test - public void findsDomainClassOnExtensionOfDaoInterface() throws Exception { + @Test + public void findsDomainClassOnExtensionOfDaoInterface() throws Exception { - RepositoryMetadata metadata = - new DefaultRepositoryMetadata( - ExtensionOfUserCustomExtendedDao.class); - assertEquals(User.class, metadata.getDomainClass()); - } + RepositoryMetadata metadata = + new DefaultRepositoryMetadata( + ExtensionOfUserCustomExtendedDao.class); + assertEquals(User.class, metadata.getDomainClass()); + } - @Test - public void detectsParameterizedEntitiesCorrectly() { + @Test + public void detectsParameterizedEntitiesCorrectly() { - RepositoryMetadata metadata = - new DefaultRepositoryMetadata(GenericEntityRepository.class); - assertEquals(GenericEntity.class, metadata.getDomainClass()); - } + RepositoryMetadata metadata = + new DefaultRepositoryMetadata(GenericEntityRepository.class); + assertEquals(GenericEntity.class, metadata.getDomainClass()); + } - @Test - public void looksUpIdClassCorrectly() throws Exception { + @Test + public void looksUpIdClassCorrectly() throws Exception { - RepositoryMetadata metadata = - new DefaultRepositoryMetadata(UserRepository.class); + RepositoryMetadata metadata = + new DefaultRepositoryMetadata(UserRepository.class); - assertEquals(Integer.class, metadata.getIdClass()); - } + assertEquals(Integer.class, metadata.getIdClass()); + } - @SuppressWarnings("unused") - private class User { + @SuppressWarnings("unused") + private class User { - private String firstname; + private String firstname; - public String getAddress() { + public String getAddress() { - return null; - } - } + return null; + } + } - static interface UserRepository extends Repository { + static interface UserRepository extends Repository { - } + } - /** - * Sample interface to serve two purposes: - *
    - *
  1. Check that {@link ClassUtils#getDomainClass(Class)} skips non - * {@link GenericDao} interfaces
  2. - *
  3. Check that {@link ClassUtils#getDomainClass(Class)} traverses - * interface hierarchy
  4. - *
- * - * @author Oliver Gierke - */ - private interface SomeDao extends Serializable, UserRepository { + /** + * Sample interface to serve two purposes: + *
    + *
  1. Check that {@link ClassUtils#getDomainClass(Class)} skips non + * {@link GenericDao} interfaces
  2. + *
  3. Check that {@link ClassUtils#getDomainClass(Class)} traverses + * interface hierarchy
  4. + *
+ * + * @author Oliver Gierke + */ + private interface SomeDao extends Serializable, UserRepository { - Page findByFirstname(Pageable pageable, String firstname); - } + Page findByFirstname(Pageable pageable, String firstname); + } - /** - * Sample interface to test recursive lookup of domain class. - * - * @author Oliver Gierke - */ - static interface ExtensionOfUserCustomExtendedDao extends - UserCustomExtendedRepository { + /** + * Sample interface to test recursive lookup of domain class. + * + * @author Oliver Gierke + */ + static interface ExtensionOfUserCustomExtendedDao extends + UserCustomExtendedRepository { - } + } - static interface UserCustomExtendedRepository extends - Repository { + static interface UserCustomExtendedRepository extends + Repository { - } + } - static abstract class DummyGenericRepositorySupport - implements Repository { + static abstract class DummyGenericRepositorySupport + implements Repository { - public T findOne(ID id) { + public T findOne(ID id) { - return null; - } - } + return null; + } + } - /** - * Helper class to reproduce #256. - * - * @author Oliver Gierke - */ - static class GenericEntity { - } + /** + * Helper class to reproduce #256. + * + * @author Oliver Gierke + */ + static class GenericEntity { + } - static interface GenericEntityRepository extends - Repository, Long> { + static interface GenericEntityRepository extends + Repository, Long> { - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java index 043fe22e7..5e02e72cf 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassConverterUnitTests.java @@ -17,7 +17,9 @@ package org.springframework.data.repository.support; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.anyString; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; import java.util.HashMap; @@ -33,161 +35,158 @@ import org.mockito.runners.MockitoJUnitRunner; import org.springframework.context.ApplicationContext; import org.springframework.core.convert.ConversionService; import org.springframework.core.convert.TypeDescriptor; -import org.springframework.data.repository.support.DomainClassConverter; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; -import org.springframework.data.repository.support.RepositoryFactoryInformation; /** * Unit test for {@link DomainClassConverter}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class DomainClassConverterUnitTests { - static final User USER = new User(); + static final User USER = new User(); - DomainClassConverter converter; + DomainClassConverter converter; - TypeDescriptor sourceDescriptor; - TypeDescriptor targetDescriptor; + TypeDescriptor sourceDescriptor; + TypeDescriptor targetDescriptor; - @SuppressWarnings("rawtypes") - Map providers; + @SuppressWarnings("rawtypes") + Map providers; - @Mock - ApplicationContext context; - @Mock - UserRepository repository; - @Mock - ConversionService service; - @Mock - EntityInformation information; - @Mock - RepositoryFactoryInformation provider; + @Mock + ApplicationContext context; + @Mock + UserRepository repository; + @Mock + ConversionService service; + @Mock + EntityInformation information; + @Mock + RepositoryFactoryInformation provider; - @Before - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void setUp() { + @Before + @SuppressWarnings({"unchecked", "rawtypes"}) + public void setUp() { - converter = new DomainClassConverter(service); - providers = new HashMap(); + converter = new DomainClassConverter(service); + providers = new HashMap(); - sourceDescriptor = TypeDescriptor.valueOf(String.class); - targetDescriptor = TypeDescriptor.valueOf(User.class); + sourceDescriptor = TypeDescriptor.valueOf(String.class); + targetDescriptor = TypeDescriptor.valueOf(User.class); - Map map = getBeanAsMap(repository); - when(context.getBeansOfType(UserRepository.class)).thenReturn(map); - when(context.getBeansOfType(RepositoryFactoryInformation.class)) - .thenReturn(providers); - when(provider.getEntityInformation()).thenReturn(information); - when(provider.getRepositoryInterface()).thenReturn( - (Class) UserRepository.class); - when(information.getJavaType()).thenReturn(User.class); - when(information.getIdType()).thenReturn(Long.class); - } + Map map = getBeanAsMap(repository); + when(context.getBeansOfType(UserRepository.class)).thenReturn(map); + when(context.getBeansOfType(RepositoryFactoryInformation.class)) + .thenReturn(providers); + when(provider.getEntityInformation()).thenReturn(information); + when(provider.getRepositoryInterface()).thenReturn( + (Class) UserRepository.class); + when(information.getJavaType()).thenReturn(User.class); + when(information.getIdType()).thenReturn(Long.class); + } - @Test - public void matchFailsIfNoDaoAvailable() throws Exception { + @Test + public void matchFailsIfNoDaoAvailable() throws Exception { - converter.setApplicationContext(context); - assertMatches(false); - } + converter.setApplicationContext(context); + assertMatches(false); + } - @Test - public void matchesIfConversionInBetweenIsPossible() throws Exception { + @Test + public void matchesIfConversionInBetweenIsPossible() throws Exception { - letContextContain(provider); - converter.setApplicationContext(context); + letContextContain(provider); + converter.setApplicationContext(context); - when(service.canConvert(String.class, Long.class)).thenReturn(true); + when(service.canConvert(String.class, Long.class)).thenReturn(true); - assertMatches(true); - } + assertMatches(true); + } - @Test - public void matchFailsIfNoIntermediateConversionIsPossible() - throws Exception { + @Test + public void matchFailsIfNoIntermediateConversionIsPossible() + throws Exception { - letContextContain(provider); - converter.setApplicationContext(context); + letContextContain(provider); + converter.setApplicationContext(context); - when(service.canConvert(String.class, Long.class)).thenReturn(false); + when(service.canConvert(String.class, Long.class)).thenReturn(false); - assertMatches(false); - } + assertMatches(false); + } - private void assertMatches(boolean matchExpected) { + private void assertMatches(boolean matchExpected) { - assertThat(converter.matches(sourceDescriptor, targetDescriptor), - is(matchExpected)); - } + assertThat(converter.matches(sourceDescriptor, targetDescriptor), + is(matchExpected)); + } - @Test - public void convertsStringToUserCorrectly() throws Exception { + @Test + public void convertsStringToUserCorrectly() throws Exception { - letContextContain(provider); - converter.setApplicationContext(context); + letContextContain(provider); + converter.setApplicationContext(context); - when(service.canConvert(String.class, Long.class)).thenReturn(true); - when(service.convert(anyString(), eq(Long.class))).thenReturn(1L); - when(repository.findOne(1L)).thenReturn(USER); + when(service.canConvert(String.class, Long.class)).thenReturn(true); + when(service.convert(anyString(), eq(Long.class))).thenReturn(1L); + when(repository.findOne(1L)).thenReturn(USER); - Object user = - converter.convert("1", sourceDescriptor, targetDescriptor); - assertThat(user, is(instanceOf(User.class))); - assertThat(user, is((Object) USER)); - } + Object user = + converter.convert("1", sourceDescriptor, targetDescriptor); + assertThat(user, is(instanceOf(User.class))); + assertThat(user, is((Object) USER)); + } - private void letContextContain(Object bean) { + private void letContextContain(Object bean) { - Map beanMap = getBeanAsMap(bean); - when(context.getBeansOfType(argThat(is(subtypeOf(bean.getClass()))))) - .thenReturn(beanMap); - } + Map beanMap = getBeanAsMap(bean); + when(context.getBeansOfType(argThat(is(subtypeOf(bean.getClass()))))) + .thenReturn(beanMap); + } - private Map getBeanAsMap(T bean) { + private Map getBeanAsMap(T bean) { - Map beanMap = new HashMap(); - beanMap.put(bean.getClass().getName(), bean); - return beanMap; - } + Map beanMap = new HashMap(); + beanMap.put(bean.getClass().getName(), bean); + return beanMap; + } - private static TypeSafeMatcher> subtypeOf( - final Class type) { + private static TypeSafeMatcher> subtypeOf( + final Class type) { - return new TypeSafeMatcher>() { + return new TypeSafeMatcher>() { - public void describeTo(Description arg0) { + public void describeTo(Description arg0) { - arg0.appendText("not a subtype of"); - } + arg0.appendText("not a subtype of"); + } - @Override - public boolean matchesSafely(Class arg0) { + @Override + public boolean matchesSafely(Class arg0) { - return arg0.isAssignableFrom(type); - } - }; - } + return arg0.isAssignableFrom(type); + } + }; + } - private static class User { + private static class User { - } + } - private static interface UserRepository extends Repository { + private static interface UserRepository extends Repository { - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java index aae600371..ed3de9a6f 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorRegistrarUnitTests.java @@ -16,7 +16,8 @@ package org.springframework.data.repository.support; import static org.hamcrest.Matchers.*; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; import static org.mockito.Mockito.*; import java.io.Serializable; @@ -33,116 +34,114 @@ import org.mockito.runners.MockitoJUnitRunner; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.context.ApplicationContext; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; -import org.springframework.data.repository.support.RepositoryFactoryInformation; /** * Unit test for {@link DomainClassPropertyEditorRegistrar}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class DomainClassPropertyEditorRegistrarUnitTests { - DomainClassPropertyEditorRegistrar registrar = - new DomainClassPropertyEditorRegistrar(); - @Mock - ApplicationContext context; - @Mock - PropertyEditorRegistry registry; - @Mock - EntityRepository repository; - @Mock - EntityInformation information; - @Mock - RepositoryFactoryInformation provider; + DomainClassPropertyEditorRegistrar registrar = + new DomainClassPropertyEditorRegistrar(); + @Mock + ApplicationContext context; + @Mock + PropertyEditorRegistry registry; + @Mock + EntityRepository repository; + @Mock + EntityInformation information; + @Mock + RepositoryFactoryInformation provider; - DomainClassPropertyEditor reference; + DomainClassPropertyEditor reference; - @Before - @SuppressWarnings({ "unchecked", "rawtypes" }) - public void setup() { + @Before + @SuppressWarnings({"unchecked", "rawtypes"}) + public void setup() { - when(information.getJavaType()).thenReturn(Entity.class); - when(provider.getEntityInformation()).thenReturn(information); - when(provider.getRepositoryInterface()).thenReturn( - (Class) EntityRepository.class); - Map map = getBeanAsMap(repository); - when(context.getBeansOfType(EntityRepository.class)).thenReturn(map); + when(information.getJavaType()).thenReturn(Entity.class); + when(provider.getEntityInformation()).thenReturn(information); + when(provider.getRepositoryInterface()).thenReturn( + (Class) EntityRepository.class); + Map map = getBeanAsMap(repository); + when(context.getBeansOfType(EntityRepository.class)).thenReturn(map); - reference = - new DomainClassPropertyEditor(repository, - information, registry); - } + reference = + new DomainClassPropertyEditor(repository, + information, registry); + } - @Test - public void addsRepositoryForEntityIfAvailableInAppContext() - throws Exception { + @Test + public void addsRepositoryForEntityIfAvailableInAppContext() + throws Exception { - letContextContain(provider); - registrar.setApplicationContext(context); - registrar.registerCustomEditors(registry); + letContextContain(provider); + registrar.setApplicationContext(context); + registrar.registerCustomEditors(registry); - verify(registry).registerCustomEditor(eq(Entity.class), eq(reference)); - } + verify(registry).registerCustomEditor(eq(Entity.class), eq(reference)); + } - @Test - public void doesNotAddDaoAtAllIfNoDaosFound() throws Exception { + @Test + public void doesNotAddDaoAtAllIfNoDaosFound() throws Exception { - letContextContain(provider); - registrar.registerCustomEditors(registry); + letContextContain(provider); + registrar.registerCustomEditors(registry); - verify(registry, never()).registerCustomEditor(eq(Entity.class), - eq(reference)); - } + verify(registry, never()).registerCustomEditor(eq(Entity.class), + eq(reference)); + } - private void letContextContain(Object bean) { + private void letContextContain(Object bean) { - Map beanMap = getBeanAsMap(bean); + Map beanMap = getBeanAsMap(bean); - when(context.getBeansOfType(argThat(is(subtypeOf(bean.getClass()))))) - .thenReturn(beanMap); - } + when(context.getBeansOfType(argThat(is(subtypeOf(bean.getClass()))))) + .thenReturn(beanMap); + } - private Map getBeanAsMap(T bean) { + private Map getBeanAsMap(T bean) { - Map beanMap = new HashMap(); - beanMap.put(bean.getClass().getName(), bean); - return beanMap; - } + Map beanMap = new HashMap(); + beanMap.put(bean.getClass().getName(), bean); + return beanMap; + } - @SuppressWarnings("serial") - private static class Entity implements Serializable { + @SuppressWarnings("serial") + private static class Entity implements Serializable { - } + } - private static interface EntityRepository extends Repository { + private static interface EntityRepository extends Repository { - } + } - private static TypeSafeMatcher> subtypeOf( - final Class type) { + private static TypeSafeMatcher> subtypeOf( + final Class type) { - return new TypeSafeMatcher>() { + return new TypeSafeMatcher>() { - public void describeTo(Description arg0) { + public void describeTo(Description arg0) { - arg0.appendText("not a subtype of"); - } + arg0.appendText("not a subtype of"); + } - @Override - public boolean matchesSafely(Class arg0) { + @Override + public boolean matchesSafely(Class arg0) { - return arg0.isAssignableFrom(type); - } - }; - } + return arg0.isAssignableFrom(type); + } + }; + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java index ed82034e3..7dc6abe85 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/DomainClassPropertyEditorUnitTests.java @@ -29,152 +29,151 @@ import org.mockito.runners.MockitoJUnitRunner; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.data.domain.Persistable; import org.springframework.data.repository.Repository; -import org.springframework.data.repository.support.EntityInformation; /** * Unit test for {@link DomainClassPropertyEditor}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class DomainClassPropertyEditorUnitTests { - DomainClassPropertyEditor editor; + DomainClassPropertyEditor editor; - @Mock - PropertyEditorRegistry registry; - @Mock - UserRepository userRepository; - @Mock - EntityInformation information; + @Mock + PropertyEditorRegistry registry; + @Mock + UserRepository userRepository; + @Mock + EntityInformation information; - @Before - public void setUp() { + @Before + public void setUp() { - when(information.getIdType()).thenReturn(Integer.class); - editor = - new DomainClassPropertyEditor(userRepository, - information, registry); - } + when(information.getIdType()).thenReturn(Integer.class); + editor = + new DomainClassPropertyEditor(userRepository, + information, registry); + } - @Test - public void convertsPlainIdTypeCorrectly() throws Exception { + @Test + public void convertsPlainIdTypeCorrectly() throws Exception { - User user = new User(1); - when(information.getId(user)).thenReturn(user.getId()); - when(userRepository.findOne(1)).thenReturn(user); + User user = new User(1); + when(information.getId(user)).thenReturn(user.getId()); + when(userRepository.findOne(1)).thenReturn(user); - editor.setAsText("1"); + editor.setAsText("1"); - verify(userRepository, times(1)).findOne(1); - } + verify(userRepository, times(1)).findOne(1); + } - @Test - public void convertsEntityToIdCorrectly() throws Exception { + @Test + public void convertsEntityToIdCorrectly() throws Exception { - User user = new User(1); - editor.setValue(user); - when(information.getId(user)).thenReturn(user.getId()); - assertThat(editor.getAsText(), is("1")); - } + User user = new User(1); + editor.setValue(user); + when(information.getId(user)).thenReturn(user.getId()); + assertThat(editor.getAsText(), is("1")); + } - @Test - public void usesCustomEditorIfConfigured() throws Exception { + @Test + public void usesCustomEditorIfConfigured() throws Exception { - PropertyEditor customEditor = mock(PropertyEditor.class); - when(customEditor.getValue()).thenReturn(1); + PropertyEditor customEditor = mock(PropertyEditor.class); + when(customEditor.getValue()).thenReturn(1); - when(registry.findCustomEditor(Integer.class, null)).thenReturn( - customEditor); + when(registry.findCustomEditor(Integer.class, null)).thenReturn( + customEditor); - convertsPlainIdTypeCorrectly(); + convertsPlainIdTypeCorrectly(); - verify(customEditor, times(1)).setAsText("1"); - } + verify(customEditor, times(1)).setAsText("1"); + } - @Test - public void returnsNullIdIfNoEntitySet() throws Exception { + @Test + public void returnsNullIdIfNoEntitySet() throws Exception { - editor.setValue(null); - assertThat(editor.getAsText(), is(nullValue())); - } + editor.setValue(null); + assertThat(editor.getAsText(), is(nullValue())); + } - @Test - public void resetsValueToNullAfterEmptyStringConversion() throws Exception { + @Test + public void resetsValueToNullAfterEmptyStringConversion() throws Exception { - assertValueResetToNullAfterConverting(""); - } + assertValueResetToNullAfterConverting(""); + } - @Test - public void resetsValueToNullAfterNullStringConversion() throws Exception { + @Test + public void resetsValueToNullAfterNullStringConversion() throws Exception { - assertValueResetToNullAfterConverting(null); - } + assertValueResetToNullAfterConverting(null); + } - private void assertValueResetToNullAfterConverting(String source) - throws Exception { + private void assertValueResetToNullAfterConverting(String source) + throws Exception { - convertsPlainIdTypeCorrectly(); - assertThat(editor.getValue(), is(notNullValue())); + convertsPlainIdTypeCorrectly(); + assertThat(editor.getValue(), is(notNullValue())); - editor.setAsText(source); - assertThat(editor.getValue(), is(nullValue())); - } + editor.setAsText(source); + assertThat(editor.getValue(), is(nullValue())); + } - /** - * Sample entity. - * - * @author Oliver Gierke - */ - @SuppressWarnings("serial") - private static class User implements Persistable { + /** + * Sample entity. + * + * @author Oliver Gierke + */ + @SuppressWarnings("serial") + private static class User implements Persistable { - private Integer id; + private Integer id; - public User(Integer id) { + public User(Integer id) { - this.id = id; - } + this.id = id; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Persistable#getId() - */ - public Integer getId() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Persistable#getId() + */ + public Integer getId() { - return id; - } + return id; + } - /* - * (non-Javadoc) - * - * @see org.springframework.data.domain.Persistable#isNew() - */ - public boolean isNew() { + /* + * (non-Javadoc) + * + * @see org.springframework.data.domain.Persistable#isNew() + */ + public boolean isNew() { - return getId() != null; - } - } + return getId() != null; + } + } - /** - * Sample generic DAO interface. - * - * @author Oliver Gierke - */ - private static interface UserRepository extends Repository { + /** + * Sample generic DAO interface. + * + * @author Oliver Gierke + */ + private static interface UserRepository extends Repository { - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/PersistableEntityInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/PersistableEntityInformationUnitTests.java index 9bec1599c..289a4d22d 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/PersistableEntityInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/PersistableEntityInformationUnitTests.java @@ -28,63 +28,63 @@ import org.springframework.data.domain.Persistable; /** * Unit tests for {@link PersistableEntityMetadata}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class PersistableEntityInformationUnitTests { - @SuppressWarnings({ "rawtypes", "unchecked" }) - static final PersistableEntityInformation metadata = - new PersistableEntityInformation(Persistable.class); + @SuppressWarnings({"rawtypes", "unchecked"}) + static final PersistableEntityInformation metadata = + new PersistableEntityInformation(Persistable.class); - @Mock - Persistable persistable; + @Mock + Persistable persistable; - @Test - @SuppressWarnings("unchecked") - public void usesPersistablesGetId() throws Exception { + @Test + @SuppressWarnings("unchecked") + public void usesPersistablesGetId() throws Exception { - when(persistable.getId()).thenReturn(2L, 1L, 3L); - assertEquals(2L, metadata.getId(persistable)); - assertEquals(1L, metadata.getId(persistable)); - assertEquals(3L, metadata.getId(persistable)); - } + when(persistable.getId()).thenReturn(2L, 1L, 3L); + assertEquals(2L, metadata.getId(persistable)); + assertEquals(1L, metadata.getId(persistable)); + assertEquals(3L, metadata.getId(persistable)); + } - @Test - @SuppressWarnings("unchecked") - public void usesPersistablesIsNew() throws Exception { + @Test + @SuppressWarnings("unchecked") + public void usesPersistablesIsNew() throws Exception { - when(persistable.isNew()).thenReturn(true, false); - assertThat(metadata.isNew(persistable), is(true)); - assertThat(metadata.isNew(persistable), is(false)); - } + when(persistable.isNew()).thenReturn(true, false); + assertThat(metadata.isNew(persistable), is(true)); + assertThat(metadata.isNew(persistable), is(false)); + } - @Test - public void returnsGivenClassAsEntityType() throws Exception { + @Test + public void returnsGivenClassAsEntityType() throws Exception { - PersistableEntityInformation info = - new PersistableEntityInformation( - PersistableEntity.class); + PersistableEntityInformation info = + new PersistableEntityInformation( + PersistableEntity.class); - assertEquals(PersistableEntity.class, info.getJavaType()); - } + assertEquals(PersistableEntity.class, info.getJavaType()); + } - @SuppressWarnings("serial") - static class PersistableEntity implements Persistable { + @SuppressWarnings("serial") + static class PersistableEntity implements Persistable { - public Long getId() { + public Long getId() { - return null; - } + return null; + } - public boolean isNew() { + public boolean isNew() { - return false; - } - } + return false; + } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java index d31026acd..49e15f73d 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/QueryExecuterMethodInterceptorUnitTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.repository.support; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; import org.junit.Test; @@ -37,22 +37,22 @@ public class QueryExecuterMethodInterceptorUnitTests { RepositoryFactorySupport factory; @Mock RepositoryInformation information; - - @Test(expected=IllegalStateException.class) + + @Test(expected = IllegalStateException.class) public void rejectsRepositoryInterfaceWithQueryMethodsIfNoQueryLookupStrategyIsDefined() { - + when(information.hasCustomMethod()).thenReturn(true); when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); - + factory.new QueryExecuterMethodInterceptor(information, null, new Object()); } - + @Test public void skipsQueryLookupsIfQueryLookupStrategyIsNull() { - + when(information.hasCustomMethod()).thenReturn(false); when(factory.getQueryLookupStrategy(any(Key.class))).thenReturn(null); - + factory.new QueryExecuterMethodInterceptor(information, null, new Object()); verify(information, times(0)).getQueryMethods(); } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryFactorySupportUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryFactorySupportUnitTests.java index c5c0b6190..c93c3bfeb 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryFactorySupportUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryFactorySupportUnitTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.repository.support; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.any; import static org.mockito.Mockito.*; import java.io.Serializable; @@ -33,94 +33,94 @@ import org.springframework.data.repository.query.RepositoryQuery; /** * Unit tests for {@link RepositoryFactorySupport}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class RepositoryFactorySupportUnitTests { - RepositoryFactorySupport factory = new DummyRepositoryFactory(); + RepositoryFactorySupport factory = new DummyRepositoryFactory(); - @Mock - MyQueryCreationListener listener; - @Mock - PlainQueryCreationListener otherListener; + @Mock + MyQueryCreationListener listener; + @Mock + PlainQueryCreationListener otherListener; - @Test - public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly() - throws Exception { + @Test + public void invokesCustomQueryCreationListenerForSpecialRepositoryQueryOnly() + throws Exception { - factory.addQueryCreationListener(listener); - factory.addQueryCreationListener(otherListener); + factory.addQueryCreationListener(listener); + factory.addQueryCreationListener(otherListener); - factory.getRepository(ObjectRepository.class); + factory.getRepository(ObjectRepository.class); - verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class)); - verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class)); + verify(listener, times(1)).onCreation(any(MyRepositoryQuery.class)); + verify(otherListener, times(2)).onCreation(any(RepositoryQuery.class)); - } + } - class DummyRepositoryFactory extends RepositoryFactorySupport { - - /* (non-Javadoc) - * @see org.springframework.data.repository.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class) - */ - @Override - @SuppressWarnings("unchecked") - public EntityInformation getEntityInformation( - Class domainClass) { - - return mock(EntityInformation.class); - } + class DummyRepositoryFactory extends RepositoryFactorySupport { - @Override - protected Object getTargetRepository(RepositoryMetadata metadata) { + /* (non-Javadoc) + * @see org.springframework.data.repository.support.RepositoryFactorySupport#getEntityInformation(java.lang.Class) + */ + @Override + @SuppressWarnings("unchecked") + public EntityInformation getEntityInformation( + Class domainClass) { - return new Object(); - } + return mock(EntityInformation.class); + } + + @Override + protected Object getTargetRepository(RepositoryMetadata metadata) { + + return new Object(); + } - @Override - protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { + @Override + protected Class getRepositoryBaseClass(RepositoryMetadata metadata) { - return Object.class; - } + return Object.class; + } - @Override - protected QueryLookupStrategy getQueryLookupStrategy(Key key) { + @Override + protected QueryLookupStrategy getQueryLookupStrategy(Key key) { - MyRepositoryQuery queryOne = mock(MyRepositoryQuery.class); - RepositoryQuery queryTwo = mock(RepositoryQuery.class); + MyRepositoryQuery queryOne = mock(MyRepositoryQuery.class); + RepositoryQuery queryTwo = mock(RepositoryQuery.class); - QueryLookupStrategy strategy = mock(QueryLookupStrategy.class); - when(strategy.resolveQuery(any(Method.class), any(RepositoryMetadata.class))) - .thenReturn(queryOne, queryTwo); + QueryLookupStrategy strategy = mock(QueryLookupStrategy.class); + when(strategy.resolveQuery(any(Method.class), any(RepositoryMetadata.class))) + .thenReturn(queryOne, queryTwo); - return strategy; - } - } + return strategy; + } + } - interface ObjectRepository extends Repository { + interface ObjectRepository extends Repository { - Object findByClass(Class clazz); + Object findByClass(Class clazz); - Object findByFoo(); - } + Object findByFoo(); + } - interface PlainQueryCreationListener extends - QueryCreationListener { + interface PlainQueryCreationListener extends + QueryCreationListener { - } + } - interface MyQueryCreationListener extends - QueryCreationListener { + interface MyQueryCreationListener extends + QueryCreationListener { - } + } - interface MyRepositoryQuery extends RepositoryQuery { + interface MyRepositoryQuery extends RepositoryQuery { - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessorUnitTests.java index 0ea87639d..4012bbd2d 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/RepositoryInterfaceAwareBeanPostProcessorUnitTests.java @@ -33,94 +33,94 @@ import org.springframework.data.repository.Repository; /** * Unit tests for {@link RepositoryInterfaceAwareBeanPostProcessor}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class RepositoryInterfaceAwareBeanPostProcessorUnitTests { - private static final Class FACTORY_CLASS = - RepositoryFactoryBeanSupport.class; - private static final String BEAN_NAME = "foo"; - private static final String DAO_INTERFACE_PROPERTY = "repositoryInterface"; + private static final Class FACTORY_CLASS = + RepositoryFactoryBeanSupport.class; + private static final String BEAN_NAME = "foo"; + private static final String DAO_INTERFACE_PROPERTY = "repositoryInterface"; - private RepositoryInterfaceAwareBeanPostProcessor processor; + private RepositoryInterfaceAwareBeanPostProcessor processor; - @Mock - private ConfigurableListableBeanFactory beanFactory; - private BeanDefinition beanDefinition; + @Mock + private ConfigurableListableBeanFactory beanFactory; + private BeanDefinition beanDefinition; - @Before - public void setUp() { + @Before + public void setUp() { - BeanDefinitionBuilder builder = - BeanDefinitionBuilder - .rootBeanDefinition(FACTORY_CLASS) - .addPropertyValue(DAO_INTERFACE_PROPERTY, UserDao.class); - this.beanDefinition = builder.getBeanDefinition(); + BeanDefinitionBuilder builder = + BeanDefinitionBuilder + .rootBeanDefinition(FACTORY_CLASS) + .addPropertyValue(DAO_INTERFACE_PROPERTY, UserDao.class); + this.beanDefinition = builder.getBeanDefinition(); - when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn( - beanDefinition); + when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn( + beanDefinition); - processor = new RepositoryInterfaceAwareBeanPostProcessor(); + processor = new RepositoryInterfaceAwareBeanPostProcessor(); - } + } - @Test - public void returnsDaoInterfaceClassForFactoryBean() throws Exception { + @Test + public void returnsDaoInterfaceClassForFactoryBean() throws Exception { - processor.setBeanFactory(beanFactory); - assertEquals(UserDao.class, - processor.predictBeanType(FACTORY_CLASS, BEAN_NAME)); - } + processor.setBeanFactory(beanFactory); + assertEquals(UserDao.class, + processor.predictBeanType(FACTORY_CLASS, BEAN_NAME)); + } - @Test - public void doesNotResolveInterfaceForNonFactoryClasses() throws Exception { + @Test + public void doesNotResolveInterfaceForNonFactoryClasses() throws Exception { - processor.setBeanFactory(beanFactory); - assertNotTypeDetected(BeanFactory.class); - } + processor.setBeanFactory(beanFactory); + assertNotTypeDetected(BeanFactory.class); + } - @Test - public void doesNotResolveInterfaceForUnloadableClass() throws Exception { + @Test + public void doesNotResolveInterfaceForUnloadableClass() throws Exception { - BeanDefinitionBuilder builder = - BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS) - .addPropertyValue(DAO_INTERFACE_PROPERTY, - "com.acme.Foo"); + BeanDefinitionBuilder builder = + BeanDefinitionBuilder.rootBeanDefinition(FACTORY_CLASS) + .addPropertyValue(DAO_INTERFACE_PROPERTY, + "com.acme.Foo"); - when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn( - builder.getBeanDefinition()); + when(beanFactory.getBeanDefinition(BEAN_NAME)).thenReturn( + builder.getBeanDefinition()); - assertNotTypeDetected(FACTORY_CLASS); - } + assertNotTypeDetected(FACTORY_CLASS); + } - @Test - public void doesNotResolveTypeForPlainBeanFactory() throws Exception { + @Test + public void doesNotResolveTypeForPlainBeanFactory() throws Exception { - BeanFactory beanFactory = mock(BeanFactory.class); - processor.setBeanFactory(beanFactory); + BeanFactory beanFactory = mock(BeanFactory.class); + processor.setBeanFactory(beanFactory); - assertNotTypeDetected(FACTORY_CLASS); - } + assertNotTypeDetected(FACTORY_CLASS); + } - private void assertNotTypeDetected(Class beanClass) { + private void assertNotTypeDetected(Class beanClass) { - assertThat(processor.predictBeanType(beanClass, BEAN_NAME), - is(nullValue())); - } + assertThat(processor.predictBeanType(beanClass, BEAN_NAME), + is(nullValue())); + } - private class User { + private class User { - } + } - private interface UserDao extends Repository { + private interface UserDao extends Repository { - } + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/TransactionRepositoryProxyPostProcessorUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/TransactionRepositoryProxyPostProcessorUnitTests.java index 0d7d459c6..5d6cd7d24 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/TransactionRepositoryProxyPostProcessorUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/support/TransactionRepositoryProxyPostProcessorUnitTests.java @@ -15,7 +15,9 @@ */ package org.springframework.data.repository.support; -import static org.mockito.Matchers.*; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.eq; +import static org.mockito.Matchers.isA; import static org.mockito.Mockito.*; import java.util.HashMap; @@ -35,57 +37,57 @@ import org.springframework.transaction.interceptor.TransactionInterceptor; /** * Unit test for {@link TransactionalRepositoryProxyPostProcessor}. - * + * * @author Oliver Gierke */ @RunWith(MockitoJUnitRunner.class) public class TransactionRepositoryProxyPostProcessorUnitTests { - TransactionalRepositoryProxyPostProcessor processor; + TransactionalRepositoryProxyPostProcessor processor; - @Mock - ListableBeanFactory beanFactory; - @Mock - ProxyFactory proxyFactory; + @Mock + ListableBeanFactory beanFactory; + @Mock + ProxyFactory proxyFactory; - @Before - public void setUp() { + @Before + public void setUp() { - Map beans = - new HashMap(); - beans.put("foo", mock(PersistenceExceptionTranslator.class)); - when( - beanFactory.getBeansOfType( - eq(PersistenceExceptionTranslator.class), anyBoolean(), - anyBoolean())).thenReturn(beans); - } + Map beans = + new HashMap(); + beans.put("foo", mock(PersistenceExceptionTranslator.class)); + when( + beanFactory.getBeansOfType( + eq(PersistenceExceptionTranslator.class), anyBoolean(), + anyBoolean())).thenReturn(beans); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullBeanFactory() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullBeanFactory() throws Exception { - new TransactionalRepositoryProxyPostProcessor(null, "transactionManager"); - } + new TransactionalRepositoryProxyPostProcessor(null, "transactionManager"); + } - @Test(expected = IllegalArgumentException.class) - public void rejectsNullTxManagerName() throws Exception { + @Test(expected = IllegalArgumentException.class) + public void rejectsNullTxManagerName() throws Exception { - new TransactionalRepositoryProxyPostProcessor(beanFactory, null); - } + new TransactionalRepositoryProxyPostProcessor(beanFactory, null); + } - @Test - public void setsUpBasicInstance() throws Exception { + @Test + public void setsUpBasicInstance() throws Exception { - RepositoryProxyPostProcessor postProcessor = - new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager"); + RepositoryProxyPostProcessor postProcessor = + new TransactionalRepositoryProxyPostProcessor(beanFactory, "txManager"); - postProcessor.postProcess(proxyFactory); + postProcessor.postProcess(proxyFactory); - verify(proxyFactory).addAdvice( - isA(PersistenceExceptionTranslationInterceptor.class)); - verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class)); - } + verify(proxyFactory).addAdvice( + isA(PersistenceExceptionTranslationInterceptor.class)); + verify(proxyFactory).addAdvice(isA(TransactionInterceptor.class)); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java index 33c2dee38..25b4b0950 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/repository/util/ClassUtilsUnitTests.java @@ -30,82 +30,82 @@ import org.springframework.data.repository.Repository; /** * Unit test for {@link ClassUtils}. - * + * * @author Oliver Gierke */ public class ClassUtilsUnitTests { - @Test(expected = IllegalStateException.class) - public void rejectsInvalidReturnType() throws Exception { + @Test(expected = IllegalStateException.class) + public void rejectsInvalidReturnType() throws Exception { - assertReturnType(SomeDao.class.getMethod("findByFirstname", - Pageable.class, String.class), User.class); - } + assertReturnType(SomeDao.class.getMethod("findByFirstname", + Pageable.class, String.class), User.class); + } - @Test - public void determinesReturnType() throws Exception { + @Test + public void determinesReturnType() throws Exception { - assertEquals(User.class, - getReturnedDomainClass(SomeDao.class.getMethod( - "findByFirstname", Pageable.class, String.class))); - assertEquals(GenericType.class, - getReturnedDomainClass(SomeDao.class.getMethod("someMethod"))); - } + assertEquals(User.class, + getReturnedDomainClass(SomeDao.class.getMethod( + "findByFirstname", Pageable.class, String.class))); + assertEquals(GenericType.class, + getReturnedDomainClass(SomeDao.class.getMethod("someMethod"))); + } - @Test - public void determinesValidFieldsCorrectly() { + @Test + public void determinesValidFieldsCorrectly() { - assertTrue(hasProperty(User.class, "firstname")); - assertTrue(hasProperty(User.class, "Firstname")); - assertFalse(hasProperty(User.class, "address")); - } - - @Test - public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException { - assertEquals(Map.class, getReturnedDomainClass(SomeDao.class.getMethod("anotherMethod"))); - } + assertTrue(hasProperty(User.class, "firstname")); + assertTrue(hasProperty(User.class, "Firstname")); + assertFalse(hasProperty(User.class, "address")); + } - @SuppressWarnings("unused") - private class User { + @Test + public void handlesGenericTypeInReturnedCollectionCorrectly() throws SecurityException, NoSuchMethodException { + assertEquals(Map.class, getReturnedDomainClass(SomeDao.class.getMethod("anotherMethod"))); + } - private String firstname; + @SuppressWarnings("unused") + private class User { + + private String firstname; - public String getAddress() { + public String getAddress() { - return null; - } - } + return null; + } + } - static interface UserRepository extends Repository { + static interface UserRepository extends Repository { - } + } - /** - * Sample interface to serve two purposes: - *
    - *
  1. Check that {@link ClassUtils#getDomainClass(Class)} skips non - * {@link GenericDao} interfaces
  2. - *
  3. Check that {@link ClassUtils#getDomainClass(Class)} traverses - * interface hierarchy
  4. - *
- * - * @author Oliver Gierke - */ - private interface SomeDao extends Serializable, UserRepository { + /** + * Sample interface to serve two purposes: + *
    + *
  1. Check that {@link ClassUtils#getDomainClass(Class)} skips non + * {@link GenericDao} interfaces
  2. + *
  3. Check that {@link ClassUtils#getDomainClass(Class)} traverses + * interface hierarchy
  4. + *
+ * + * @author Oliver Gierke + */ + private interface SomeDao extends Serializable, UserRepository { - Page findByFirstname(Pageable pageable, String firstname); + Page findByFirstname(Pageable pageable, String firstname); - GenericType someMethod(); - - - List> anotherMethod(); - } + GenericType someMethod(); - private class GenericType { - } + List> anotherMethod(); + } + + private class GenericType { + + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index 5b2777dd4..424a708f9 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -13,162 +13,162 @@ import org.springframework.data.mapping.Person; /** * Unit tests for {@link ClassTypeInformation}. - * + * * @author Oliver Gierke */ public class ClassTypeInformationUnitTests { - @Test - public void discoversTypeForSimpleGenericField() { + @Test + public void discoversTypeForSimpleGenericField() { - TypeInformation discoverer = ClassTypeInformation.from(ConcreteType.class); - assertEquals(ConcreteType.class, discoverer.getType()); - TypeInformation content = discoverer.getProperty("content"); - assertEquals(String.class, content.getType()); - assertNull(content.getComponentType()); - assertNull(content.getMapValueType()); - } + TypeInformation discoverer = ClassTypeInformation.from(ConcreteType.class); + assertEquals(ConcreteType.class, discoverer.getType()); + TypeInformation content = discoverer.getProperty("content"); + assertEquals(String.class, content.getType()); + assertNull(content.getComponentType()); + assertNull(content.getMapValueType()); + } - @Test - public void discoversTypeForNestedGenericField() { + @Test + public void discoversTypeForNestedGenericField() { - TypeInformation discoverer = ClassTypeInformation.from(ConcreteWrapper.class); - assertEquals(ConcreteWrapper.class, discoverer.getType()); - TypeInformation wrapper = discoverer.getProperty("wrapped"); - assertEquals(GenericType.class, wrapper.getType()); - TypeInformation content = wrapper.getProperty("content"); + TypeInformation discoverer = ClassTypeInformation.from(ConcreteWrapper.class); + assertEquals(ConcreteWrapper.class, discoverer.getType()); + TypeInformation wrapper = discoverer.getProperty("wrapped"); + assertEquals(GenericType.class, wrapper.getType()); + TypeInformation content = wrapper.getProperty("content"); - assertEquals(String.class, content.getType()); - assertEquals(String.class, - discoverer.getProperty("wrapped").getProperty("content").getType()); - assertEquals(String.class, discoverer.getProperty("wrapped.content") - .getType()); - } + assertEquals(String.class, content.getType()); + assertEquals(String.class, + discoverer.getProperty("wrapped").getProperty("content").getType()); + assertEquals(String.class, discoverer.getProperty("wrapped.content") + .getType()); + } - @Test - @SuppressWarnings("rawtypes") - public void discoversBoundType() { + @Test + @SuppressWarnings("rawtypes") + public void discoversBoundType() { - TypeInformation information = ClassTypeInformation.from( - GenericTypeWithBound.class); - assertEquals(Person.class, information.getProperty("person").getType()); - } + TypeInformation information = ClassTypeInformation.from( + GenericTypeWithBound.class); + assertEquals(Person.class, information.getProperty("person").getType()); + } - @Test - public void discoversBoundTypeForSpecialization() { + @Test + public void discoversBoundTypeForSpecialization() { - TypeInformation information = ClassTypeInformation.from( - SpecialGenericTypeWithBound.class); - assertEquals(SpecialPerson.class, information.getProperty("person") - .getType()); - } + TypeInformation information = ClassTypeInformation.from( + SpecialGenericTypeWithBound.class); + assertEquals(SpecialPerson.class, information.getProperty("person") + .getType()); + } - @Test - @SuppressWarnings("rawtypes") - public void discoversBoundTypeForNested() { + @Test + @SuppressWarnings("rawtypes") + public void discoversBoundTypeForNested() { - TypeInformation information = ClassTypeInformation.from( - AnotherGenericType.class); - assertEquals(GenericTypeWithBound.class, information.getProperty("nested") - .getType()); - assertEquals(Person.class, information.getProperty("nested.person") - .getType()); - } - - @Test - public void discoversArraysAndCollections() { - TypeInformation information = ClassTypeInformation.from(StringCollectionContainer.class); - - TypeInformation property = information.getProperty("array"); - assertEquals(property.getComponentType().getType(), String.class); - - Class type = property.getType(); - assertEquals(String[].class, type); - assertThat(type.isArray(), is(true)); - - property = information.getProperty("foo"); - assertEquals(Collection[].class, property.getType()); - assertEquals(Collection.class, property.getComponentType().getType()); - assertEquals(String.class, property.getComponentType().getComponentType().getType()); - - property = information.getProperty("rawSet"); - assertEquals(Set.class, property.getType()); - assertEquals(Object.class, property.getComponentType().getType()); - assertNull(property.getMapValueType()); - } - - @Test - public void discoversMapValueType() { - - TypeInformation information = ClassTypeInformation.from(StringMapContainer.class); - TypeInformation genericMap = information.getProperty("genericMap"); - assertEquals(Map.class, genericMap.getType()); - assertEquals(String.class, genericMap.getMapValueType().getType()); + TypeInformation information = ClassTypeInformation.from( + AnotherGenericType.class); + assertEquals(GenericTypeWithBound.class, information.getProperty("nested") + .getType()); + assertEquals(Person.class, information.getProperty("nested.person") + .getType()); + } - TypeInformation map = information.getProperty("map"); - assertEquals(Map.class, map.getType()); - assertEquals(Calendar.class, map.getMapValueType().getType()); - } - - static class StringMapContainer extends MapContainer { - - } - - static class MapContainer { - Map genericMap; - Map map; - } + @Test + public void discoversArraysAndCollections() { + TypeInformation information = ClassTypeInformation.from(StringCollectionContainer.class); - static class StringCollectionContainer extends CollectionContainer { - - } - - @SuppressWarnings("rawtypes") - static class CollectionContainer { - - T[] array; - Collection[] foo; - Set set; - Set rawSet; - } + TypeInformation property = information.getProperty("array"); + assertEquals(property.getComponentType().getType(), String.class); - static class GenericTypeWithBound { + Class type = property.getType(); + assertEquals(String[].class, type); + assertThat(type.isArray(), is(true)); - T person; - } + property = information.getProperty("foo"); + assertEquals(Collection[].class, property.getType()); + assertEquals(Collection.class, property.getComponentType().getType()); + assertEquals(String.class, property.getComponentType().getComponentType().getType()); - static class AnotherGenericType> { - S nested; - } + property = information.getProperty("rawSet"); + assertEquals(Set.class, property.getType()); + assertEquals(Object.class, property.getComponentType().getType()); + assertNull(property.getMapValueType()); + } - static class SpecialGenericTypeWithBound extends - GenericTypeWithBound { + @Test + public void discoversMapValueType() { - } + TypeInformation information = ClassTypeInformation.from(StringMapContainer.class); + TypeInformation genericMap = information.getProperty("genericMap"); + assertEquals(Map.class, genericMap.getType()); + assertEquals(String.class, genericMap.getMapValueType().getType()); - static abstract class SpecialPerson extends Person { - protected SpecialPerson(Integer ssn, String firstName, String lastName) { - super(ssn, firstName, lastName); - } - } + TypeInformation map = information.getProperty("map"); + assertEquals(Map.class, map.getType()); + assertEquals(Calendar.class, map.getMapValueType().getType()); + } - static class GenericType { + static class StringMapContainer extends MapContainer { - Long index; - T content; - } - + } - static class ConcreteType extends GenericType { + static class MapContainer { + Map genericMap; + Map map; + } - } + static class StringCollectionContainer extends CollectionContainer { - static class GenericWrapper { + } - GenericType wrapped; - } + @SuppressWarnings("rawtypes") + static class CollectionContainer { - static class ConcreteWrapper extends GenericWrapper { + T[] array; + Collection[] foo; + Set set; + Set rawSet; + } - } + static class GenericTypeWithBound { + + T person; + } + + static class AnotherGenericType> { + S nested; + } + + static class SpecialGenericTypeWithBound extends + GenericTypeWithBound { + + } + + static abstract class SpecialPerson extends Person { + protected SpecialPerson(Integer ssn, String firstName, String lastName) { + super(ssn, firstName, lastName); + } + } + + static class GenericType { + + Long index; + T content; + } + + + static class ConcreteType extends GenericType { + + } + + static class GenericWrapper { + + GenericType wrapped; + } + + static class ConcreteWrapper extends GenericWrapper { + + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java index 7566ed170..677fb6b79 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/util/TypeDiscovererUnitTests.java @@ -9,8 +9,8 @@ import org.junit.Test; */ public class TypeDiscovererUnitTests { - @Test(expected = IllegalArgumentException.class) - public void rejectsNullType() { - new TypeDiscoverer(null, null, null); - } + @Test(expected = IllegalArgumentException.class) + public void rejectsNullType() { + new TypeDiscoverer(null, null, null); + } } diff --git a/spring-data-commons-core/src/test/java/org/springframework/data/web/PageableArgumentResolverUnitTests.java b/spring-data-commons-core/src/test/java/org/springframework/data/web/PageableArgumentResolverUnitTests.java index 83d53fee1..d79782005 100644 --- a/spring-data-commons-core/src/test/java/org/springframework/data/web/PageableArgumentResolverUnitTests.java +++ b/spring-data-commons-core/src/test/java/org/springframework/data/web/PageableArgumentResolverUnitTests.java @@ -33,161 +33,161 @@ import org.springframework.web.context.request.ServletWebRequest; /** * Unit test for {@link PageableArgumentResolver}. - * + * * @author Oliver Gierke - gierke@synyx.de */ public class PageableArgumentResolverUnitTests { - Method correctMethod; - Method failedMethod; - Method invalidQualifiers; - Method defaultsMethod; + Method correctMethod; + Method failedMethod; + Method invalidQualifiers; + Method defaultsMethod; - MockHttpServletRequest request; + MockHttpServletRequest request; - @Before - public void setUp() throws SecurityException, NoSuchMethodException { + @Before + public void setUp() throws SecurityException, NoSuchMethodException { - correctMethod = - SampleController.class.getMethod("correctMethod", - Pageable.class, Pageable.class); - failedMethod = - SampleController.class.getMethod("failedMethod", - Pageable.class, Pageable.class); - invalidQualifiers = - SampleController.class.getMethod("invalidQualifiers", - Pageable.class, Pageable.class); + correctMethod = + SampleController.class.getMethod("correctMethod", + Pageable.class, Pageable.class); + failedMethod = + SampleController.class.getMethod("failedMethod", + Pageable.class, Pageable.class); + invalidQualifiers = + SampleController.class.getMethod("invalidQualifiers", + Pageable.class, Pageable.class); - defaultsMethod = - SampleController.class.getMethod("defaultsMethod", - Pageable.class); + defaultsMethod = + SampleController.class.getMethod("defaultsMethod", + Pageable.class); - request = new MockHttpServletRequest(); + request = new MockHttpServletRequest(); - // Add pagination info for foo table - request.addParameter("foo_page.size", "50"); - request.addParameter("foo_page.sort", "foo"); - request.addParameter("foo_page.sort.dir", "asc"); + // Add pagination info for foo table + request.addParameter("foo_page.size", "50"); + request.addParameter("foo_page.sort", "foo"); + request.addParameter("foo_page.sort.dir", "asc"); - // Add pagination info for bar table - request.addParameter("bar_page.size", "60"); - } + // Add pagination info for bar table + request.addParameter("bar_page.size", "60"); + } - @Test - public void testname() throws Exception { + @Test + public void testname() throws Exception { - assertSizeForPrefix(50, new Sort(Direction.ASC, "foo"), 0); - assertSizeForPrefix(60, null, 1); - } + assertSizeForPrefix(50, new Sort(Direction.ASC, "foo"), 0); + assertSizeForPrefix(60, null, 1); + } - @Test(expected = IllegalStateException.class) - public void rejectsInvalidlyMappedPageables() throws Exception { + @Test(expected = IllegalStateException.class) + public void rejectsInvalidlyMappedPageables() throws Exception { - MethodParameter parameter = new MethodParameter(failedMethod, 0); - NativeWebRequest webRequest = new ServletWebRequest(request); + MethodParameter parameter = new MethodParameter(failedMethod, 0); + NativeWebRequest webRequest = new ServletWebRequest(request); - new PageableArgumentResolver().resolveArgument(parameter, webRequest); - } + new PageableArgumentResolver().resolveArgument(parameter, webRequest); + } - @Test(expected = IllegalStateException.class) - public void rejectsInvalidQualifiers() throws Exception { + @Test(expected = IllegalStateException.class) + public void rejectsInvalidQualifiers() throws Exception { - MethodParameter parameter = new MethodParameter(invalidQualifiers, 0); - NativeWebRequest webRequest = new ServletWebRequest(request); + MethodParameter parameter = new MethodParameter(invalidQualifiers, 0); + NativeWebRequest webRequest = new ServletWebRequest(request); - new PageableArgumentResolver().resolveArgument(parameter, webRequest); - } + new PageableArgumentResolver().resolveArgument(parameter, webRequest); + } - @Test - public void assertDefaults() throws Exception { + @Test + public void assertDefaults() throws Exception { - MethodParameter parameter = new MethodParameter(defaultsMethod, 0); - NativeWebRequest webRequest = - new ServletWebRequest(new MockHttpServletRequest()); - PageableArgumentResolver resolver = new PageableArgumentResolver(); - Object argument = resolver.resolveArgument(parameter, webRequest); + MethodParameter parameter = new MethodParameter(defaultsMethod, 0); + NativeWebRequest webRequest = + new ServletWebRequest(new MockHttpServletRequest()); + PageableArgumentResolver resolver = new PageableArgumentResolver(); + Object argument = resolver.resolveArgument(parameter, webRequest); - assertTrue(argument instanceof Pageable); + assertTrue(argument instanceof Pageable); - Pageable pageable = (Pageable) argument; - assertEquals(SampleController.DEFAULT_PAGESIZE, pageable.getPageSize()); - assertEquals(SampleController.DEFAULT_PAGENUMBER, - pageable.getPageNumber()); - } + Pageable pageable = (Pageable) argument; + assertEquals(SampleController.DEFAULT_PAGESIZE, pageable.getPageSize()); + assertEquals(SampleController.DEFAULT_PAGENUMBER, + pageable.getPageNumber()); + } - @Test - public void assertOverridesDefaults() throws Exception { + @Test + public void assertOverridesDefaults() throws Exception { - Integer sizeParam = 5; + Integer sizeParam = 5; - MethodParameter parameter = new MethodParameter(defaultsMethod, 0); - MockHttpServletRequest mockRequest = new MockHttpServletRequest(); + MethodParameter parameter = new MethodParameter(defaultsMethod, 0); + MockHttpServletRequest mockRequest = new MockHttpServletRequest(); - mockRequest.addParameter("page.page", sizeParam.toString()); - NativeWebRequest webRequest = new ServletWebRequest(mockRequest); - PageableArgumentResolver resolver = new PageableArgumentResolver(); - Object argument = resolver.resolveArgument(parameter, webRequest); + mockRequest.addParameter("page.page", sizeParam.toString()); + NativeWebRequest webRequest = new ServletWebRequest(mockRequest); + PageableArgumentResolver resolver = new PageableArgumentResolver(); + Object argument = resolver.resolveArgument(parameter, webRequest); - assertTrue(argument instanceof Pageable); + assertTrue(argument instanceof Pageable); - Pageable pageable = (Pageable) argument; - assertEquals(SampleController.DEFAULT_PAGESIZE, pageable.getPageSize()); - assertEquals(sizeParam - 1, pageable.getPageNumber()); - } + Pageable pageable = (Pageable) argument; + assertEquals(SampleController.DEFAULT_PAGESIZE, pageable.getPageSize()); + assertEquals(sizeParam - 1, pageable.getPageNumber()); + } - private void assertSizeForPrefix(int size, Sort sort, int index) - throws Exception { + private void assertSizeForPrefix(int size, Sort sort, int index) + throws Exception { - MethodParameter parameter = new MethodParameter(correctMethod, index); - NativeWebRequest webRequest = new ServletWebRequest(request); + MethodParameter parameter = new MethodParameter(correctMethod, index); + NativeWebRequest webRequest = new ServletWebRequest(request); - PageableArgumentResolver resolver = new PageableArgumentResolver(); + PageableArgumentResolver resolver = new PageableArgumentResolver(); - Object argument = resolver.resolveArgument(parameter, webRequest); - assertTrue(argument instanceof Pageable); + Object argument = resolver.resolveArgument(parameter, webRequest); + assertTrue(argument instanceof Pageable); - Pageable pageable = (Pageable) argument; - assertEquals(size, pageable.getPageSize()); + Pageable pageable = (Pageable) argument; + assertEquals(size, pageable.getPageSize()); - if (null != sort) { - assertEquals(sort, pageable.getSort()); - } - } + if (null != sort) { + assertEquals(sort, pageable.getSort()); + } + } - @SuppressWarnings("unused") - private class SampleController { + @SuppressWarnings("unused") + private class SampleController { - static final int DEFAULT_PAGESIZE = 198; - static final int DEFAULT_PAGENUMBER = 42; + static final int DEFAULT_PAGESIZE = 198; + static final int DEFAULT_PAGENUMBER = 42; - public void defaultsMethod( - @PageableDefaults(value = DEFAULT_PAGESIZE, pageNumber = DEFAULT_PAGENUMBER) Pageable pageable) { + public void defaultsMethod( + @PageableDefaults(value = DEFAULT_PAGESIZE, pageNumber = DEFAULT_PAGENUMBER) Pageable pageable) { - } + } - public void correctMethod(@Qualifier("foo") Pageable first, - @Qualifier("bar") Pageable second) { + public void correctMethod(@Qualifier("foo") Pageable first, + @Qualifier("bar") Pageable second) { - } + } - public void failedMethod(Pageable first, Pageable second) { + public void failedMethod(Pageable first, Pageable second) { - } + } - public void invalidQualifiers(@Qualifier("foo") Pageable first, - @Qualifier("foo") Pageable second) { + public void invalidQualifiers(@Qualifier("foo") Pageable first, + @Qualifier("foo") Pageable second) { - } - } + } + } } diff --git a/spring-data-commons-core/src/test/resources/log4j.xml b/spring-data-commons-core/src/test/resources/log4j.xml index 721aa73b8..30d2d8ff4 100644 --- a/spring-data-commons-core/src/test/resources/log4j.xml +++ b/spring-data-commons-core/src/test/resources/log4j.xml @@ -2,21 +2,21 @@ - - - - - + + + + + - - - + + + - - - + + + diff --git a/spring-data-commons-core/src/test/resources/mapping.xml b/spring-data-commons-core/src/test/resources/mapping.xml index 30d582a9b..dba24e9c4 100644 --- a/spring-data-commons-core/src/test/resources/mapping.xml +++ b/spring-data-commons-core/src/test/resources/mapping.xml @@ -1,7 +1,7 @@ + xmlns:p="http://www.springframework.org/schema/p" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> \ No newline at end of file diff --git a/spring-data-commons-core/src/test/resources/org/springframework/data/repository/config/type-filter-test.xml b/spring-data-commons-core/src/test/resources/org/springframework/data/repository/config/type-filter-test.xml index 6ef49f137..894eb3f08 100644 --- a/spring-data-commons-core/src/test/resources/org/springframework/data/repository/config/type-filter-test.xml +++ b/spring-data-commons-core/src/test/resources/org/springframework/data/repository/config/type-filter-test.xml @@ -1,12 +1,12 @@ - - - - - - - + + + + + + + \ No newline at end of file