cayenne-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From johnth...@apache.org
Subject [4/6] cayenne git commit: CAY-2064 Issue with BeanAccessor for classes with complex inheritance
Date Tue, 09 Aug 2016 16:30:33 GMT
CAY-2064 Issue with BeanAccessor for classes with complex inheritance


Project: http://git-wip-us.apache.org/repos/asf/cayenne/repo
Commit: http://git-wip-us.apache.org/repos/asf/cayenne/commit/b0619b49
Tree: http://git-wip-us.apache.org/repos/asf/cayenne/tree/b0619b49
Diff: http://git-wip-us.apache.org/repos/asf/cayenne/diff/b0619b49

Branch: refs/heads/ics11
Commit: b0619b49db594557110585d28c1e56c81a57ab48
Parents: 7e0f801
Author: David Feshbach <djfeshbach@gmail.com>
Authored: Thu Feb 25 16:26:24 2016 -0600
Committer: David Feshbach <djfeshbach@gmail.com>
Committed: Fri Feb 26 11:25:29 2016 -0600

----------------------------------------------------------------------
 .../apache/cayenne/reflect/BeanAccessor.java    | 67 ++++++++++++++------
 .../cayenne/reflect/BeanAccessorTest.java       | 16 +++++
 .../apache/cayenne/reflect/TstHasRelated.java   | 24 +++++++
 .../cayenne/reflect/TstJavaBeanChild.java       | 24 +++++++
 4 files changed, 113 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0619b49/cayenne-server/src/main/java/org/apache/cayenne/reflect/BeanAccessor.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/reflect/BeanAccessor.java b/cayenne-server/src/main/java/org/apache/cayenne/reflect/BeanAccessor.java
index bb5db57..ec30df0 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/reflect/BeanAccessor.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/reflect/BeanAccessor.java
@@ -53,30 +53,61 @@ public class BeanAccessor implements Accessor {
 		this.nullValue = PropertyUtils.defaultNullValueForType(propertyType);
 
 		String capitalized = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
-
-		try {
-			this.readMethod = objectClass.getMethod("get" + capitalized);
-		} catch (NoSuchMethodException e) {
-
-			// try boolean
-			try {
-				Method readMethod = objectClass.getMethod("is" + capitalized);
-				this.readMethod = (readMethod.getReturnType().equals(Boolean.TYPE)) ? readMethod : null;
-			} catch (NoSuchMethodException e1) {
-				// not readable...
+		String isGetterName = "is" + capitalized;
+		String getGetterName = "get" + capitalized;
+		String setterName = "set" + capitalized;
+
+		Method[] publicMethods = objectClass.getMethods();
+
+		Method getter = null;
+		for (Method method : publicMethods) {
+			Class<?> returnType = method.getReturnType();
+			// following Java Bean naming conventions, "is" methods are preferred over "get" methods
+			if (method.getName().equals(isGetterName) && returnType.equals(Boolean.TYPE) &&
method.getParameterTypes().length == 0) {
+				getter = method;
+				break;
+			}
+			// Find the method with the most specific return type.
+			// This is the same behavior as Class.getMethod(String, Class...) except that
+			// Class.getMethod prefers synthetic methods generated for interfaces
+			// over methods with more specific return types in a super class.
+			if (method.getName().equals(getGetterName) && method.getParameterTypes().length
== 0) {
+				if (returnType.isPrimitive()) {
+					getter = returnType.equals(Void.TYPE) ? null : method;
+					if (returnType.equals(Boolean.TYPE)) {
+						// keep looking for the "is" method
+						continue;
+					} else {
+						// nothing more specific than a primitive, so stop here
+						break;
+					}
+				}
+				if (getter == null || getter.getReturnType().isAssignableFrom(returnType)) {
+					getter = method;
+				}
 			}
 		}
 
-		if (readMethod == null) {
-			throw new IllegalArgumentException("Property '" + propertyName + "' is not readbale");
+		if (getter == null) {
+			throw new IllegalArgumentException("Property '" + propertyName + "' is not readable");
 		}
 
+		this.readMethod = getter;
+
 		// TODO: compare 'propertyType' arg with readMethod.getReturnType()
 
-		try {
-			this.writeMethod = objectClass.getMethod("set" + capitalized, readMethod.getReturnType());
-		} catch (NoSuchMethodException e) {
-			// read-only is supported...
+		for (Method method : publicMethods) {
+			if (!method.getName().equals(setterName) || !method.getReturnType().equals(Void.TYPE))
{
+				continue;
+			}
+			Class<?>[] parameterTypes = method.getParameterTypes();
+			if (parameterTypes.length != 1) {
+				continue;
+			}
+			if (getter.getReturnType().isAssignableFrom(parameterTypes[0])) {
+				this.writeMethod = method;
+				break;
+			}
 		}
 	}
 
@@ -118,7 +149,7 @@ public class BeanAccessor implements Accessor {
 		try {
 			writeMethod.invoke(object, newValue);
 		} catch (Throwable th) {
-			throw new PropertyException("Error reading property: " + propertyName, this, object, th);
+			throw new PropertyException("Error writing property: " + propertyName, this, object, th);
 		}
 	}
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0619b49/cayenne-server/src/test/java/org/apache/cayenne/reflect/BeanAccessorTest.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/reflect/BeanAccessorTest.java
b/cayenne-server/src/test/java/org/apache/cayenne/reflect/BeanAccessorTest.java
index 9f62fb5..9129aaf 100644
--- a/cayenne-server/src/test/java/org/apache/cayenne/reflect/BeanAccessorTest.java
+++ b/cayenne-server/src/test/java/org/apache/cayenne/reflect/BeanAccessorTest.java
@@ -81,4 +81,20 @@ public class BeanAccessorTest {
         assertEquals("Incorrectly set null default", 0, o1.getIntField());
     }
 
+    @Test
+    public void testInheritedCovariantProperty() {
+
+    	BeanAccessor accessor = new BeanAccessor(
+    			TstJavaBeanChild.class,
+    			"related",
+    			null);
+
+    	TstJavaBeanChild o1 = new TstJavaBeanChild();
+
+    	assertNull(o1.getRelated());
+    	accessor.setValue(o1, o1);
+    	assertSame(o1, o1.getRelated());
+    	assertSame(o1, accessor.getValue(o1));
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0619b49/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstHasRelated.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstHasRelated.java b/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstHasRelated.java
new file mode 100644
index 0000000..8ce9302
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstHasRelated.java
@@ -0,0 +1,24 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.reflect;
+
+public interface TstHasRelated {
+	Object getRelated();
+}

http://git-wip-us.apache.org/repos/asf/cayenne/blob/b0619b49/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstJavaBeanChild.java
----------------------------------------------------------------------
diff --git a/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstJavaBeanChild.java
b/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstJavaBeanChild.java
new file mode 100644
index 0000000..4a0060e
--- /dev/null
+++ b/cayenne-server/src/test/java/org/apache/cayenne/reflect/TstJavaBeanChild.java
@@ -0,0 +1,24 @@
+/*****************************************************************
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ ****************************************************************/
+
+package org.apache.cayenne.reflect;
+
+public class TstJavaBeanChild extends TstJavaBean implements TstHasRelated {
+
+}


Mime
View raw message