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/02/07 08:36:10 UTC

[cayenne] branch STABLE-4.1 updated: CAY-2486 Customizable Accessor implementation

This is an automated email from the ASF dual-hosted git repository.

ntimofeev pushed a commit to branch STABLE-4.1
in repository https://gitbox.apache.org/repos/asf/cayenne.git


The following commit(s) were added to refs/heads/STABLE-4.1 by this push:
     new 576fd6e  CAY-2486 Customizable Accessor implementation
     new db35e67  Merge PR #363
576fd6e is described below

commit 576fd6e78f437536774d72327271571209eb17b9
Author: Nikita Timofeev <st...@gmail.com>
AuthorDate: Mon Feb 4 15:59:23 2019 +0300

    CAY-2486 Customizable Accessor implementation
---
 .../apache/cayenne/reflect/AccessorFactory.java    | 29 ++++++++++++++++++++++
 .../org/apache/cayenne/reflect/PropertyUtils.java  | 20 ++++++++++++++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/cayenne-server/src/main/java/org/apache/cayenne/reflect/AccessorFactory.java b/cayenne-server/src/main/java/org/apache/cayenne/reflect/AccessorFactory.java
new file mode 100644
index 0000000..22de4f2
--- /dev/null
+++ b/cayenne-server/src/main/java/org/apache/cayenne/reflect/AccessorFactory.java
@@ -0,0 +1,29 @@
+/*****************************************************************
+ *   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;
+
+/**
+ * @since 4.1
+ */
+public interface AccessorFactory {
+
+    Accessor createAccessor(Class<?> objectClass, String propertyName, Class<?> propertyType);
+
+}
diff --git a/cayenne-server/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java b/cayenne-server/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
index c701331..0e38bca 100644
--- a/cayenne-server/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
+++ b/cayenne-server/src/main/java/org/apache/cayenne/reflect/PropertyUtils.java
@@ -20,6 +20,7 @@
 package org.apache.cayenne.reflect;
 
 import java.util.Map;
+import java.util.Objects;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 
@@ -39,6 +40,11 @@ public class PropertyUtils {
 	private static final ConcurrentMap<String, Accessor> PATH_ACCESSORS = new ConcurrentHashMap<>();
 	private static final ConcurrentMap<Class<?>, ConcurrentMap<String, Accessor>> SEGMENT_ACCESSORS = new ConcurrentHashMap<>();
 
+    /**
+     * Factory for accessor, can be customized by {@link #installAccessorFactory(AccessorFactory)}
+     */
+	private static AccessorFactory accessorFactory = BeanAccessor::new;
+
 	/**
 	 * Compiles an accessor that can be used for fast access for the nested
 	 * property of the objects of a given class.
@@ -112,7 +118,7 @@ public class PropertyUtils {
 		if (Map.class.isAssignableFrom(objectClass)) {
 			return new MapAccessor(propertyName);
 		} else {
-			return new BeanAccessor(objectClass, propertyName, null);
+			return accessorFactory.createAccessor(objectClass, propertyName, null);
 		}
 	}
 
@@ -197,6 +203,18 @@ public class PropertyUtils {
 		return null;
 	}
 
+    /**
+     * This method installs custom accessor factory to be used by property utils.
+     * <p>
+     * A factory that produces {@link BeanAccessor} is used by default.
+     *
+     * @param accessorFactory new factory to use
+     * @since 4.1
+     */
+	public static void installAccessorFactory(AccessorFactory accessorFactory) {
+	    PropertyUtils.accessorFactory = Objects.requireNonNull(accessorFactory);
+    }
+
 	private PropertyUtils() {
 		super();
 	}