You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cayenne.apache.org by nt...@apache.org on 2019/04/11 08:40:12 UTC
[cayenne] 01/03: New BeanAccessor constructor,
allowing custom accessor method names
This is an automated email from the ASF dual-hosted git repository.
ntimofeev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cayenne.git
commit b82d0ef07fb63714b22cdd1ec6825027fe90f0ac
Author: Hugi Thordarson <hu...@karlmenn.is>
AuthorDate: Wed Mar 20 15:24:52 2019 +0000
New BeanAccessor constructor, allowing custom accessor method names
---
.../org/apache/cayenne/reflect/BeanAccessor.java | 40 ++++++++++++++++++----
1 file changed, 33 insertions(+), 7 deletions(-)
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 c6c2884..354699e 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
@@ -37,6 +37,10 @@ public class BeanAccessor implements Accessor {
protected Object nullValue;
public BeanAccessor(Class<?> objectClass, String propertyName, Class<?> propertyType) {
+ this( objectClass, propertyName, propertyType, defaultBooleanGetterName( propertyName ), defaultGetterName( propertyName ), defaultSetterName( propertyName ) );
+ }
+
+ protected BeanAccessor(Class<?> objectClass, String propertyName, Class<?> propertyType, String booleanGetterName, String getterName, String setterName ) {
if (objectClass == null) {
throw new IllegalArgumentException("Null objectClass");
}
@@ -48,22 +52,29 @@ public class BeanAccessor implements Accessor {
if (propertyName.length() == 0) {
throw new IllegalArgumentException("Empty propertyName");
}
+
+ if (booleanGetterName == null) {
+ throw new IllegalArgumentException("Null booleanGetterName");
+ }
+
+ if (getterName == null) {
+ throw new IllegalArgumentException("Null getterName");
+ }
+
+ if (setterName == null) {
+ throw new IllegalArgumentException("Null setterName");
+ }
this.propertyName = propertyName;
this.nullValue = PropertyUtils.defaultNullValueForType(propertyType);
- String capitalized = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
- 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) {
+ if (method.getName().equals(booleanGetterName) && returnType.equals(Boolean.TYPE) && method.getParameterTypes().length == 0) {
getter = method;
break;
}
@@ -71,7 +82,7 @@ public class BeanAccessor implements Accessor {
// 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 (method.getName().equals(getterName) && method.getParameterTypes().length == 0) {
if (returnType.isPrimitive()) {
getter = returnType.equals(Void.TYPE) ? null : method;
if (returnType.equals(Boolean.TYPE)) {
@@ -152,4 +163,19 @@ public class BeanAccessor implements Accessor {
throw new PropertyException("Error writing property: " + propertyName, this, object, th);
}
}
+
+ private static String defaultSetterName( String propertyName ) {
+ final String capitalized = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
+ return "set" + capitalized;
+ }
+
+ private static String defaultGetterName( String propertyName ) {
+ final String capitalized = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
+ return "get" + capitalized;
+ }
+
+ private static String defaultBooleanGetterName( String propertyName ) {
+ final String capitalized = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1);
+ return "is" + capitalized;
+ }
}