From dad666add0ec95520f667d516fefd1ae32a961f1 Mon Sep 17 00:00:00 2001 From: "Henning P. Schmiedehausen" Date: Wed, 12 Apr 2023 09:57:47 -0700 Subject: [PATCH] Refactor the AccessibleObjectStrategy for reflection mappers fixes #2310 --- RELEASE_NOTES.md | 1 + .../reflect/AccessibleObjectStrategy.java | 44 +++++++++++++++++++ .../mapper/reflect/ReflectionMappers.java | 10 +++-- 3 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 core/src/main/java/org/jdbi/v3/core/mapper/reflect/AccessibleObjectStrategy.java diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 5ee7faf361..8637b033a0 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,5 +1,6 @@ # Unreleased + - rewrite `AccessibleObjectStrategy` into an enum (#2310) - fix `GenericType` creation in parameterized classes (#2305) - add `SqlStatements#setAttachAllStatementsForCleanup`. Setting this configuration flag will attach all created statements to their handles for resource cleanup. Default is `false`. (#2293, thanks @jodastephen) - add `SqlStatements#setAttachCallbackStatementsForCleanup`. Setting this configuration flag will attach all created statements within one of the `Jdbi` callback methods to the handle. This allows code that uses the `Jdbi` callback methods to delegate resource management fully to Jdbi. This flag is set by default. (#2293, thanks @jodastephen) diff --git a/core/src/main/java/org/jdbi/v3/core/mapper/reflect/AccessibleObjectStrategy.java b/core/src/main/java/org/jdbi/v3/core/mapper/reflect/AccessibleObjectStrategy.java new file mode 100644 index 0000000000..c63f0dbd2c --- /dev/null +++ b/core/src/main/java/org/jdbi/v3/core/mapper/reflect/AccessibleObjectStrategy.java @@ -0,0 +1,44 @@ +/* + * Licensed 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.jdbi.v3.core.mapper.reflect; + +import java.lang.reflect.AccessibleObject; +import java.util.function.Consumer; + +import org.jdbi.v3.meta.Alpha; + +@Alpha +public enum AccessibleObjectStrategy implements Consumer { + /** + * Force non-public methods and constructors to be accessible. Doing this + * has been discouraged since Java 9 and will be removed in a future Java + * version. While this is the current default behavior of Jdbi, it will + * change in the near future due to these changes in the Java platform. + */ + FORCE_MAKE_ACCESSIBLE { + @Override + public void accept(AccessibleObject accessibleObject) { + accessibleObject.setAccessible(true); + } + }, + + /** + * Do not make non-public methods and constructors accessible. + */ + DO_NOT_MAKE_ACCESSIBLE { + @Override + public void accept(AccessibleObject accessibleObject) { + } + } +} diff --git a/core/src/main/java/org/jdbi/v3/core/mapper/reflect/ReflectionMappers.java b/core/src/main/java/org/jdbi/v3/core/mapper/reflect/ReflectionMappers.java index ddb33fba8a..fb6726e62f 100644 --- a/core/src/main/java/org/jdbi/v3/core/mapper/reflect/ReflectionMappers.java +++ b/core/src/main/java/org/jdbi/v3/core/mapper/reflect/ReflectionMappers.java @@ -26,14 +26,14 @@ import org.jdbi.v3.meta.Alpha; import org.jdbi.v3.meta.Beta; +import static org.jdbi.v3.core.mapper.reflect.AccessibleObjectStrategy.DO_NOT_MAKE_ACCESSIBLE; +import static org.jdbi.v3.core.mapper.reflect.AccessibleObjectStrategy.FORCE_MAKE_ACCESSIBLE; + /** * Configuration class for reflective mappers. */ public class ReflectionMappers implements JdbiConfig { - private static final Consumer FORCE_MAKE_ACCESSIBLE = accessibleObject -> accessibleObject.setAccessible(true); - private static final Consumer DO_NOT_MAKE_ACCESSIBLE = accessibleObject -> {}; - private List columnNameMatchers; private boolean strictMatching; private UnaryOperator caseChange; @@ -129,6 +129,10 @@ public ReflectionMappers setCaseChange(UnaryOperator caseChange) { * Set the strategy Jdbi uses for Java accessibility rules. * The legacy default is to call {@code setAccessible(true)} in certain cases when we try to use a Constructor, Method, or Field. * In the future, this default will be changed to a no-op, to better interact with the Java module system. + * + * @param makeAccessible A {@link Consumer} instance that implements the strategy. + * @see AccessibleObjectStrategy + * */ @Alpha public ReflectionMappers setAccessibleObjectStrategy(Consumer makeAccessible) {