GenericTypeAwarePropertyDescriptor implements equals/hashCode for proper lookups on IBM JVM 6

Issue: SPR-12185
This commit is contained in:
Juergen Hoeller
2014-09-16 23:40:10 +02:00
parent 824c90d2bf
commit 5cd59d044e
4 changed files with 328 additions and 436 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-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.
@@ -776,7 +776,7 @@ public class ExtendedBeanInfoTests {
@Test
public void propertyDescriptorComparator() throws IntrospectionException {
PropertyDescriptorComparator c = new PropertyDescriptorComparator();
ExtendedBeanInfo.PropertyDescriptorComparator c = new ExtendedBeanInfo.PropertyDescriptorComparator();
assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("a", null, null)), equalTo(0));
assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abc", null, null)), equalTo(0));
@@ -793,53 +793,6 @@ public class ExtendedBeanInfoTests {
assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null)), greaterThan(0));
}
private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
return pd.getWriteMethod() != null;
}
}
return false;
}
private boolean hasReadMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
return pd.getReadMethod() != null;
}
}
return false;
}
private boolean hasIndexedWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
if (!(pd instanceof IndexedPropertyDescriptor)) {
return false;
}
return ((IndexedPropertyDescriptor)pd).getIndexedWriteMethod() != null;
}
}
return false;
}
private boolean hasIndexedReadMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
if (!(pd instanceof IndexedPropertyDescriptor)) {
return false;
}
return ((IndexedPropertyDescriptor)pd).getIndexedReadMethod() != null;
}
}
return false;
}
private boolean trueUntilJdk17() {
return JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_17;
}
@Test
public void reproSpr8806() throws IntrospectionException {
// does not throw
@@ -923,20 +876,71 @@ public class ExtendedBeanInfoTests {
}
private boolean hasWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
return pd.getWriteMethod() != null;
}
}
return false;
}
private boolean hasReadMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
return pd.getReadMethod() != null;
}
}
return false;
}
private boolean hasIndexedWriteMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
if (!(pd instanceof IndexedPropertyDescriptor)) {
return false;
}
return ((IndexedPropertyDescriptor)pd).getIndexedWriteMethod() != null;
}
}
return false;
}
private boolean hasIndexedReadMethodForProperty(BeanInfo beanInfo, String propertyName) {
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (pd.getName().equals(propertyName)) {
if (!(pd instanceof IndexedPropertyDescriptor)) {
return false;
}
return ((IndexedPropertyDescriptor)pd).getIndexedReadMethod() != null;
}
}
return false;
}
private boolean trueUntilJdk17() {
return JdkVersion.getMajorJavaVersion() < JdkVersion.JAVA_17;
}
interface Spr9453<T> {
T getProp();
}
interface Book {
}
interface TextBook extends Book {
}
interface LawBook extends TextBook {
}
interface BookOperations {
Book getBook();
@@ -944,12 +948,14 @@ public class ExtendedBeanInfoTests {
void setBook(Book book);
}
interface TextBookOperations extends BookOperations {
@Override
TextBook getBook();
}
abstract class Library {
public Book getBook() {
@@ -960,6 +966,7 @@ public class ExtendedBeanInfoTests {
}
}
class LawLibrary extends Library implements TextBookOperations {
@Override
@@ -968,6 +975,7 @@ public class ExtendedBeanInfoTests {
}
}
static class WithStaticWriteMethod {
public static void setProp1(String prop1) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-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.
@@ -19,7 +19,6 @@ package org.springframework.beans;
import java.beans.IndexedPropertyDescriptor;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import org.junit.Test;
@@ -28,9 +27,6 @@ import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
/**
* Unit tests for {@link SimplePropertyDescriptor} and
* {@link SimpleIndexedPropertyDescriptor}.
*
* @author Chris Beams
* @see ExtendedBeanInfoTests
*/
@@ -39,7 +35,7 @@ public class SimplePropertyDescriptorTests {
@Test
public void toStringOutput() throws IntrospectionException, SecurityException, NoSuchMethodException {
{
Object pd = new SimplePropertyDescriptor("foo", null, null);
Object pd = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null);
assertThat(pd.toString(), containsString(
"PropertyDescriptor[name=foo, propertyType=null, readMethod=null"));
}
@@ -49,14 +45,14 @@ public class SimplePropertyDescriptorTests {
public Object setFoo(String foo) { return null; }
}
Method m = C.class.getMethod("setFoo", String.class);
Object pd = new SimplePropertyDescriptor("foo", null, m);
Object pd = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, m);
assertThat(pd.toString(), allOf(
containsString("PropertyDescriptor[name=foo"),
containsString("propertyType=class java.lang.String"),
containsString("readMethod=null, writeMethod=public java.lang.Object")));
}
{
Object pd = new SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
Object pd = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
assertThat(pd.toString(), containsString(
"PropertyDescriptor[name=foo, propertyType=null, indexedPropertyType=null"));
}
@@ -66,7 +62,7 @@ public class SimplePropertyDescriptorTests {
public Object setFoo(int i, String foo) { return null; }
}
Method m = C.class.getMethod("setFoo", int.class, String.class);
Object pd = new SimpleIndexedPropertyDescriptor("foo", null, null, null, m);
Object pd = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, m);
assertThat(pd.toString(), allOf(
containsString("PropertyDescriptor[name=foo, propertyType=null"),
containsString("indexedPropertyType=class java.lang.String"),
@@ -76,10 +72,10 @@ public class SimplePropertyDescriptorTests {
@Test
public void nonIndexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException {
Object pd1 = new SimplePropertyDescriptor("foo", null, null);
Object pd1 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null);
assertThat(pd1, equalTo(pd1));
Object pd2 = new SimplePropertyDescriptor("foo", null, null);
Object pd2 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null);
assertThat(pd1, equalTo(pd2));
assertThat(pd2, equalTo(pd1));
@@ -89,12 +85,12 @@ public class SimplePropertyDescriptorTests {
public String getFoo() { return null; }
}
Method wm1 = C.class.getMethod("setFoo", String.class);
Object pd3 = new SimplePropertyDescriptor("foo", null, wm1);
Object pd3 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, wm1);
assertThat(pd1, not(equalTo(pd3)));
assertThat(pd3, not(equalTo(pd1)));
Method rm1 = C.class.getMethod("getFoo");
Object pd4 = new SimplePropertyDescriptor("foo", rm1, null);
Object pd4 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", rm1, null);
assertThat(pd1, not(equalTo(pd4)));
assertThat(pd4, not(equalTo(pd1)));
@@ -113,10 +109,10 @@ public class SimplePropertyDescriptorTests {
@Test
public void indexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException {
Object pd1 = new SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
Object pd1 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
assertThat(pd1, equalTo(pd1));
Object pd2 = new SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
Object pd2 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, null);
assertThat(pd1, equalTo(pd2));
assertThat(pd2, equalTo(pd1));
@@ -126,12 +122,12 @@ public class SimplePropertyDescriptorTests {
public String getFoo(int i) { return null; }
}
Method wm1 = C.class.getMethod("setFoo", int.class, String.class);
Object pd3 = new SimpleIndexedPropertyDescriptor("foo", null, null, null, wm1);
Object pd3 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, null, wm1);
assertThat(pd1, not(equalTo(pd3)));
assertThat(pd3, not(equalTo(pd1)));
Method rm1 = C.class.getMethod("getFoo", int.class);
Object pd4 = new SimpleIndexedPropertyDescriptor("foo", null, null, rm1, null);
Object pd4 = new ExtendedBeanInfo.SimpleIndexedPropertyDescriptor("foo", null, null, rm1, null);
assertThat(pd1, not(equalTo(pd4)));
assertThat(pd4, not(equalTo(pd1)));