From ab37869a1a154ca2fbc41596f797f5869acf1ccf Mon Sep 17 00:00:00 2001 From: Oliver Gierke Date: Sun, 23 Feb 2014 13:37:08 +0100 Subject: [PATCH] DATACMNS-446 - Added toString() methods to TypeInformation types. --- .../data/util/ClassTypeInformation.java | 11 ++++++++++- .../data/util/GenericArrayTypeInformation.java | 13 +++++++++++-- .../data/util/ParameterizedTypeInformation.java | 14 +++++++++++++- .../data/util/ParentTypeAwareTypeInformation.java | 4 +++- .../springframework/data/util/TypeDiscoverer.java | 3 +++ .../data/util/TypeVariableTypeInformation.java | 15 ++++++++++++++- .../data/util/ClassTypeInformationUnitTests.java | 10 ++++++++++ .../data/util/ParameterizedTypeUnitTests.java | 14 ++++++++++++-- 8 files changed, 76 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/springframework/data/util/ClassTypeInformation.java b/src/main/java/org/springframework/data/util/ClassTypeInformation.java index 266a9d73a..319e4aad2 100644 --- a/src/main/java/org/springframework/data/util/ClassTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ClassTypeInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -124,4 +124,13 @@ public class ClassTypeInformation extends TypeDiscoverer { public boolean isAssignableFrom(TypeInformation target) { return getType().isAssignableFrom(target.getType()); } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return type.getName(); + } } diff --git a/src/main/java/org/springframework/data/util/GenericArrayTypeInformation.java b/src/main/java/org/springframework/data/util/GenericArrayTypeInformation.java index 1ee62fc24..1eeacf7f0 100644 --- a/src/main/java/org/springframework/data/util/GenericArrayTypeInformation.java +++ b/src/main/java/org/springframework/data/util/GenericArrayTypeInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -36,6 +36,7 @@ class GenericArrayTypeInformation extends ParentTypeAwareTypeInformation { * @param parent */ protected GenericArrayTypeInformation(GenericArrayType type, TypeDiscoverer parent) { + super(type, parent, parent.getTypeVariableMap()); this.type = type; } @@ -47,7 +48,6 @@ class GenericArrayTypeInformation extends ParentTypeAwareTypeInformation { @Override @SuppressWarnings("unchecked") public Class getType() { - return (Class) Array.newInstance(resolveType(type.getGenericComponentType()), 0).getClass(); } @@ -61,4 +61,13 @@ class GenericArrayTypeInformation extends ParentTypeAwareTypeInformation { Type componentType = type.getGenericComponentType(); return createInfo(componentType); } + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return type.toString(); + } } diff --git a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java index 42d750ae7..4dd87781e 100644 --- a/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import org.springframework.core.GenericTypeResolver; +import org.springframework.util.StringUtils; /** * Base class for all types that include parameterization of some kind. Crucial as we have to take note of the parent @@ -183,6 +184,17 @@ class ParameterizedTypeInformation extends ParentTypeAwareTypeInformation return super.hashCode() + (isResolvedCompletely() ? this.type.hashCode() : 0); } + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + + return String.format("%s<%s>", getType().getName(), + StringUtils.collectionToCommaDelimitedString(getTypeArguments())); + } + private boolean isResolvedCompletely() { Type[] types = type.getActualTypeArguments(); diff --git a/src/main/java/org/springframework/data/util/ParentTypeAwareTypeInformation.java b/src/main/java/org/springframework/data/util/ParentTypeAwareTypeInformation.java index 1043387df..0411b1e69 100644 --- a/src/main/java/org/springframework/data/util/ParentTypeAwareTypeInformation.java +++ b/src/main/java/org/springframework/data/util/ParentTypeAwareTypeInformation.java @@ -1,5 +1,5 @@ /* - * Copyright 2011-2013 the original author or authors. + * Copyright 2011-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -39,7 +39,9 @@ public abstract class ParentTypeAwareTypeInformation extends TypeDiscoverer parent, Map map) { + super(type, map); + this.parent = parent; } diff --git a/src/main/java/org/springframework/data/util/TypeDiscoverer.java b/src/main/java/org/springframework/data/util/TypeDiscoverer.java index b20f8382e..34fbb7df6 100644 --- a/src/main/java/org/springframework/data/util/TypeDiscoverer.java +++ b/src/main/java/org/springframework/data/util/TypeDiscoverer.java @@ -466,6 +466,7 @@ class TypeDiscoverer implements TypeInformation { */ @Override public boolean equals(Object obj) { + if (obj == this) { return true; } @@ -494,8 +495,10 @@ class TypeDiscoverer implements TypeInformation { public int hashCode() { int result = 17; + result += nullSafeHashCode(type); result += nullSafeHashCode(typeVariableMap); + return result; } } diff --git a/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java b/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java index 4aea74b05..d210fd425 100644 --- a/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java +++ b/src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java @@ -98,6 +98,7 @@ class TypeVariableTypeInformation extends ParentTypeAwareTypeInformation { */ @Override public boolean equals(Object obj) { + if (!super.equals(obj)) { return false; } @@ -113,9 +114,21 @@ class TypeVariableTypeInformation extends ParentTypeAwareTypeInformation { */ @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 + + /* + * (non-Javadoc) + * @see java.lang.Object#toString() + */ + @Override + public String toString() { + return variable.getName(); + } +} diff --git a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java index e9b87faf5..9459797f8 100644 --- a/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java +++ b/src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java @@ -317,6 +317,16 @@ public class ClassTypeInformationUnitTests { assertThat(nestedValueType.getComponentType().getType(), is((Object) Person.class)); } + /** + * @see DATACMNS-446 + */ + @Test + public void createsToStringRepresentation() { + + assertThat(from(SpecialPerson.class).toString(), + is("org.springframework.data.util.ClassTypeInformationUnitTests$SpecialPerson")); + } + static class StringMapContainer extends MapContainer { } diff --git a/src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java b/src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java index 1063323d4..de1363d8e 100644 --- a/src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java +++ b/src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java @@ -18,6 +18,7 @@ package org.springframework.data.util; import static org.hamcrest.Matchers.*; import static org.junit.Assert.*; import static org.mockito.Mockito.*; +import static org.springframework.data.util.ClassTypeInformation.*; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; @@ -38,8 +39,7 @@ import org.mockito.runners.MockitoJUnitRunner; @RunWith(MockitoJUnitRunner.class) public class ParameterizedTypeUnitTests { - @Mock - ParameterizedType one; + @Mock ParameterizedType one; @Before public void setUp() { @@ -85,6 +85,16 @@ public class ParameterizedTypeUnitTests { assertThat(propertyType.getMapValueType().getType(), is(typeCompatibleWith(Locale.class))); } + /** + * @see DATACMNS-446 + */ + @Test + public void createsToStringRepresentation() { + + assertThat(from(Foo.class).getProperty("param").toString(), + is("org.springframework.data.util.ParameterizedTypeUnitTests$Localized")); + } + @SuppressWarnings("serial") class Localized extends HashMap { S value;