DATACMNS-446 - Added toString() methods to TypeInformation types.

This commit is contained in:
Oliver Gierke
2014-02-23 13:37:08 +01:00
parent 71169ca160
commit ab37869a1a
8 changed files with 76 additions and 8 deletions

View File

@@ -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<S> extends TypeDiscoverer<S> {
public boolean isAssignableFrom(TypeInformation<?> target) {
return getType().isAssignableFrom(target.getType());
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return type.getName();
}
}

View File

@@ -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<S> extends ParentTypeAwareTypeInformation<S> {
* @param parent
*/
protected GenericArrayTypeInformation(GenericArrayType type, TypeDiscoverer<?> parent) {
super(type, parent, parent.getTypeVariableMap());
this.type = type;
}
@@ -47,7 +48,6 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
@Override
@SuppressWarnings("unchecked")
public Class<S> getType() {
return (Class<S>) Array.newInstance(resolveType(type.getGenericComponentType()), 0).getClass();
}
@@ -61,4 +61,13 @@ class GenericArrayTypeInformation<S> extends ParentTypeAwareTypeInformation<S> {
Type componentType = type.getGenericComponentType();
return createInfo(componentType);
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return type.toString();
}
}

View File

@@ -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<T> extends ParentTypeAwareTypeInformation<T>
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();

View File

@@ -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<S> extends TypeDiscoverer<S
*/
@SuppressWarnings("rawtypes")
protected ParentTypeAwareTypeInformation(Type type, TypeDiscoverer<?> parent, Map<TypeVariable, Type> map) {
super(type, map);
this.parent = parent;
}

View File

@@ -466,6 +466,7 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
*/
@Override
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
@@ -494,8 +495,10 @@ class TypeDiscoverer<S> implements TypeInformation<S> {
public int hashCode() {
int result = 17;
result += nullSafeHashCode(type);
result += nullSafeHashCode(typeVariableMap);
return result;
}
}

View File

@@ -98,6 +98,7 @@ class TypeVariableTypeInformation<T> extends ParentTypeAwareTypeInformation<T> {
*/
@Override
public boolean equals(Object obj) {
if (!super.equals(obj)) {
return false;
}
@@ -113,9 +114,21 @@ class TypeVariableTypeInformation<T> extends ParentTypeAwareTypeInformation<T> {
*/
@Override
public int hashCode() {
int result = super.hashCode();
result += 31 * nullSafeHashCode(this.owningType);
result += 31 * nullSafeHashCode(this.variable);
return result;
}
}
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return variable.getName();
}
}

View File

@@ -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<String> {
}

View File

@@ -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<java.lang.String>"));
}
@SuppressWarnings("serial")
class Localized<S> extends HashMap<Locale, S> {
S value;