Skip to content

Commit ab37869

Browse files
committed
DATACMNS-446 - Added toString() methods to TypeInformation types.
1 parent 71169ca commit ab37869

File tree

8 files changed

+76
-8
lines changed

8 files changed

+76
-8
lines changed

src/main/java/org/springframework/data/util/ClassTypeInformation.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -124,4 +124,13 @@ public Class<S> getType() {
124124
public boolean isAssignableFrom(TypeInformation<?> target) {
125125
return getType().isAssignableFrom(target.getType());
126126
}
127+
128+
/*
129+
* (non-Javadoc)
130+
* @see java.lang.Object#toString()
131+
*/
132+
@Override
133+
public String toString() {
134+
return type.getName();
135+
}
127136
}

src/main/java/org/springframework/data/util/GenericArrayTypeInformation.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -36,6 +36,7 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
3636
* @param parent
3737
*/
3838
protected GenericArrayTypeInformation(GenericArrayType type, TypeDiscoverer<?> parent) {
39+
3940
super(type, parent, parent.getTypeVariableMap());
4041
this.type = type;
4142
}
@@ -47,7 +48,6 @@ protected GenericArrayTypeInformation(GenericArrayType type, TypeDiscoverer<?> p
4748
@Override
4849
@SuppressWarnings("unchecked")
4950
public Class<S> getType() {
50-
5151
return (Class<S>) Array.newInstance(resolveType(type.getGenericComponentType()), 0).getClass();
5252
}
5353

@@ -61,4 +61,13 @@ public TypeInformation<?> getComponentType() {
6161
Type componentType = type.getGenericComponentType();
6262
return createInfo(componentType);
6363
}
64+
65+
/*
66+
* (non-Javadoc)
67+
* @see java.lang.Object#toString()
68+
*/
69+
@Override
70+
public String toString() {
71+
return type.toString();
72+
}
6473
}

src/main/java/org/springframework/data/util/ParameterizedTypeInformation.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import java.util.Set;
2626

2727
import org.springframework.core.GenericTypeResolver;
28+
import org.springframework.util.StringUtils;
2829

2930
/**
3031
* 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 @@ public int hashCode() {
183184
return super.hashCode() + (isResolvedCompletely() ? this.type.hashCode() : 0);
184185
}
185186

187+
/*
188+
* (non-Javadoc)
189+
* @see java.lang.Object#toString()
190+
*/
191+
@Override
192+
public String toString() {
193+
194+
return String.format("%s<%s>", getType().getName(),
195+
StringUtils.collectionToCommaDelimitedString(getTypeArguments()));
196+
}
197+
186198
private boolean isResolvedCompletely() {
187199

188200
Type[] types = type.getActualTypeArguments();

src/main/java/org/springframework/data/util/ParentTypeAwareTypeInformation.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2011-2013 the original author or authors.
2+
* Copyright 2011-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -39,7 +39,9 @@ public abstract class ParentTypeAwareTypeInformation<S> extends TypeDiscoverer<S
3939
*/
4040
@SuppressWarnings("rawtypes")
4141
protected ParentTypeAwareTypeInformation(Type type, TypeDiscoverer<?> parent, Map<TypeVariable, Type> map) {
42+
4243
super(type, map);
44+
4345
this.parent = parent;
4446
}
4547

src/main/java/org/springframework/data/util/TypeDiscoverer.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,7 @@ private TypeInformation<?> getTypeArgument(Class<?> bound, int index) {
466466
*/
467467
@Override
468468
public boolean equals(Object obj) {
469+
469470
if (obj == this) {
470471
return true;
471472
}
@@ -494,8 +495,10 @@ public boolean equals(Object obj) {
494495
public int hashCode() {
495496

496497
int result = 17;
498+
497499
result += nullSafeHashCode(type);
498500
result += nullSafeHashCode(typeVariableMap);
501+
499502
return result;
500503
}
501504
}

src/main/java/org/springframework/data/util/TypeVariableTypeInformation.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ private int getIndex(TypeVariable<?> variable) {
9898
*/
9999
@Override
100100
public boolean equals(Object obj) {
101+
101102
if (!super.equals(obj)) {
102103
return false;
103104
}
@@ -113,9 +114,21 @@ public boolean equals(Object obj) {
113114
*/
114115
@Override
115116
public int hashCode() {
117+
116118
int result = super.hashCode();
119+
117120
result += 31 * nullSafeHashCode(this.owningType);
118121
result += 31 * nullSafeHashCode(this.variable);
122+
119123
return result;
120124
}
121-
}
125+
126+
/*
127+
* (non-Javadoc)
128+
* @see java.lang.Object#toString()
129+
*/
130+
@Override
131+
public String toString() {
132+
return variable.getName();
133+
}
134+
}

src/test/java/org/springframework/data/util/ClassTypeInformationUnitTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,16 @@ public void detectsSpecialMapAsMapValueType() {
317317
assertThat(nestedValueType.getComponentType().getType(), is((Object) Person.class));
318318
}
319319

320+
/**
321+
* @see DATACMNS-446
322+
*/
323+
@Test
324+
public void createsToStringRepresentation() {
325+
326+
assertThat(from(SpecialPerson.class).toString(),
327+
is("org.springframework.data.util.ClassTypeInformationUnitTests$SpecialPerson"));
328+
}
329+
320330
static class StringMapContainer extends MapContainer<String> {
321331

322332
}

src/test/java/org/springframework/data/util/ParameterizedTypeUnitTests.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import static org.hamcrest.Matchers.*;
1919
import static org.junit.Assert.*;
2020
import static org.mockito.Mockito.*;
21+
import static org.springframework.data.util.ClassTypeInformation.*;
2122

2223
import java.lang.reflect.ParameterizedType;
2324
import java.lang.reflect.Type;
@@ -38,8 +39,7 @@
3839
@RunWith(MockitoJUnitRunner.class)
3940
public class ParameterizedTypeUnitTests {
4041

41-
@Mock
42-
ParameterizedType one;
42+
@Mock ParameterizedType one;
4343

4444
@Before
4545
public void setUp() {
@@ -85,6 +85,16 @@ public void resolvesMapValueTypeCorrectly() {
8585
assertThat(propertyType.getMapValueType().getType(), is(typeCompatibleWith(Locale.class)));
8686
}
8787

88+
/**
89+
* @see DATACMNS-446
90+
*/
91+
@Test
92+
public void createsToStringRepresentation() {
93+
94+
assertThat(from(Foo.class).getProperty("param").toString(),
95+
is("org.springframework.data.util.ParameterizedTypeUnitTests$Localized<java.lang.String>"));
96+
}
97+
8898
@SuppressWarnings("serial")
8999
class Localized<S> extends HashMap<Locale, S> {
90100
S value;

0 commit comments

Comments
 (0)