From 06116b7726883781b610ca70d60b595c208f5cbd Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Thu, 11 Jan 2018 08:59:42 +0100 Subject: [PATCH] DATACMNS-1206 - Polishing. Convert type array to string to construct the exception message. Slight Javadoc tweaks. Reduce method visibility. Simplify hasDefaultGetter check. Remove superfluous throws declaration. Strip trailing whitespaces. Ignore property descriptors without getter (e.g. indexed properties). Original pull request: #263. --- .../DefaultProjectionInformation.java | 28 ++++++++++--------- .../DefaultMethodsMetadataReader.java | 1 + ...DefaultProjectionInformationUnitTests.java | 19 ++++++++++++- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java b/src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java index 294717dfc..b32b2da1e 100644 --- a/src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java +++ b/src/main/java/org/springframework/data/projection/DefaultProjectionInformation.java @@ -117,7 +117,7 @@ class DefaultProjectionInformation implements ProjectionInformation { Method method = descriptor.getReadMethod(); - return method == null ? false : method.isDefault(); + return method != null && method.isDefault(); } /** @@ -136,10 +136,10 @@ class DefaultProjectionInformation implements ProjectionInformation { /** * Creates a new {@link PropertyDescriptorSource} for the given type. - * + * * @param type must not be {@literal null}. */ - public PropertyDescriptorSource(Class type) { + PropertyDescriptorSource(Class type) { Assert.notNull(type, "Type must not be null!"); @@ -152,7 +152,7 @@ class DefaultProjectionInformation implements ProjectionInformation { * * @return */ - public List getDescriptors() { + List getDescriptors() { return collectDescriptors().distinct().collect(StreamUtils.toUnmodifiableList()); } @@ -178,9 +178,9 @@ class DefaultProjectionInformation implements ProjectionInformation { } /** - * Returns a Stream of {@link PropertyDescriptor} ordered following the given {@link MethodsMetadata} only returning - * methods seen by the given {@link MethodsMetadata}. - * + * Returns a {@link Stream} of {@link PropertyDescriptor} ordered following the given {@link MethodsMetadata} only + * returning methods seen by the given {@link MethodsMetadata}. + * * @param source must not be {@literal null}. * @param metadata must not be {@literal null}. * @return @@ -194,13 +194,14 @@ class DefaultProjectionInformation implements ProjectionInformation { return source; } - return source.filter(descriptor -> orderedMethods.containsKey(descriptor.getReadMethod().getName())) + return source.filter(descriptor -> descriptor.getReadMethod() != null) + .filter(descriptor -> orderedMethods.containsKey(descriptor.getReadMethod().getName())) .sorted(Comparator.comparingInt(left -> orderedMethods.get(left.getReadMethod().getName()))); } /** * Returns a {@link Stream} of interfaces using the given {@link MethodsMetadata} as primary source for ordering. - * + * * @param metadata must not be {@literal null}. * @return */ @@ -209,8 +210,8 @@ class DefaultProjectionInformation implements ProjectionInformation { } /** - * Returns a Stream of interfaces using the given type as primary source for ordering. - * + * Returns a {@link Stream} of interfaces using the given type as primary source for ordering. + * * @return */ private Stream> fromType() { @@ -243,7 +244,7 @@ class DefaultProjectionInformation implements ProjectionInformation { /** * Find the type with the given name in the given array of {@link Class}. - * + * * @param name must not be {@literal null} or empty. * @param types must not be {@literal null}. * @return @@ -253,7 +254,8 @@ class DefaultProjectionInformation implements ProjectionInformation { return Arrays.stream(types) // .filter(it -> name.equals(it.getName())) // .findFirst() - .orElseThrow(() -> new IllegalStateException(String.format("Did not find type %s in %s!", name, types))); + .orElseThrow(() -> new IllegalStateException( + String.format("Did not find type %s in %s!", name, Arrays.toString(types)))); } /** diff --git a/src/main/java/org/springframework/data/type/classreading/DefaultMethodsMetadataReader.java b/src/main/java/org/springframework/data/type/classreading/DefaultMethodsMetadataReader.java index 39e249693..17404360d 100644 --- a/src/main/java/org/springframework/data/type/classreading/DefaultMethodsMetadataReader.java +++ b/src/main/java/org/springframework/data/type/classreading/DefaultMethodsMetadataReader.java @@ -43,6 +43,7 @@ import org.springframework.util.Assert; * {@link MethodsMetadataReader} implementation based on an ASM {@link org.springframework.asm.ClassReader}. * * @author Mark Paluch + * @author Oliver Gierke * @since 2.1 */ @Getter diff --git a/src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java b/src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java index 36bafbcc0..6945d04d0 100755 --- a/src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java +++ b/src/test/java/org/springframework/data/projection/DefaultProjectionInformationUnitTests.java @@ -23,6 +23,7 @@ import java.util.List; import java.util.stream.Collectors; import org.junit.Test; +import org.springframework.beans.factory.annotation.Value; /** * Unit tests for {@link DefaultProjectionInformation}. @@ -40,6 +41,14 @@ public class DefaultProjectionInformationUnitTests { assertThat(toNames(information.getInputProperties())).contains("firstname", "lastname"); } + @Test // DATACMNS-1206 + public void omitsInputPropertiesAcceptingArguments() { + + ProjectionInformation information = new DefaultProjectionInformation(ProjectionAcceptingArguments.class); + + assertThat(toNames(information.getInputProperties())).containsOnly("lastname"); + } + @Test // DATACMNS-89 public void discoversAllInputProperties() { @@ -66,7 +75,7 @@ public class DefaultProjectionInformationUnitTests { } @Test // DATACMNS-967 - public void doesNotConsiderDefaultMethodInputProperties() throws Exception { + public void doesNotConsiderDefaultMethodInputProperties() { ProjectionInformation information = new DefaultProjectionInformation(WithDefaultMethod.class); @@ -89,6 +98,14 @@ public class DefaultProjectionInformationUnitTests { String getLastname(); } + interface ProjectionAcceptingArguments { + + @Value("foo") + String getFirstname(int i); + + String getLastname(); + } + interface ExtendedProjection extends CustomerProjection { int getAge();