Skip to content

Commit

Permalink
Refactor the AccessibleObjectStrategy for reflection mappers
Browse files Browse the repository at this point in the history
fixes jdbi#2310
  • Loading branch information
hgschmie committed Apr 12, 2023
1 parent 5d57a80 commit dad666a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AccessibleObject> {
/**
* 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) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<ReflectionMappers> {

private static final Consumer<AccessibleObject> FORCE_MAKE_ACCESSIBLE = accessibleObject -> accessibleObject.setAccessible(true);
private static final Consumer<AccessibleObject> DO_NOT_MAKE_ACCESSIBLE = accessibleObject -> {};

private List<ColumnNameMatcher> columnNameMatchers;
private boolean strictMatching;
private UnaryOperator<String> caseChange;
Expand Down Expand Up @@ -129,6 +129,10 @@ public ReflectionMappers setCaseChange(UnaryOperator<String> 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<AccessibleObject>} instance that implements the strategy.
* @see AccessibleObjectStrategy
*
*/
@Alpha
public ReflectionMappers setAccessibleObjectStrategy(Consumer<AccessibleObject> makeAccessible) {
Expand Down

0 comments on commit dad666a

Please sign in to comment.