Skip to content

Commit

Permalink
WIP: Support record for JDK 16
Browse files Browse the repository at this point in the history
  • Loading branch information
kazuki43zoo committed Jun 4, 2021
1 parent 395be63 commit f8a5eda
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@
import org.apache.ibatis.mapping.ParameterMode;
import org.apache.ibatis.mapping.ResultMap;
import org.apache.ibatis.mapping.ResultMapping;
import org.apache.ibatis.reflection.Jdk;
import org.apache.ibatis.reflection.MetaClass;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.reflection.RecordUtil;
import org.apache.ibatis.reflection.ReflectorFactory;
import org.apache.ibatis.reflection.factory.ObjectFactory;
import org.apache.ibatis.session.AutoMappingBehavior;
Expand Down Expand Up @@ -396,6 +398,9 @@ private void skipRows(ResultSet rs, RowBounds rowBounds) throws SQLException {
private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, String columnPrefix) throws SQLException {
final ResultLoaderMap lazyLoader = new ResultLoaderMap();
Object rowValue = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
if (Jdk.recordExists && RecordUtil.isRecord(resultMap.getType())) {
return rowValue;
}
if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
final MetaObject metaObject = configuration.newMetaObject(rowValue);
boolean foundValues = this.useConstructorMappings;
Expand Down Expand Up @@ -424,6 +429,9 @@ private Object getRowValue(ResultSetWrapper rsw, ResultMap resultMap, CacheKey c
} else {
final ResultLoaderMap lazyLoader = new ResultLoaderMap();
rowValue = createResultObject(rsw, resultMap, lazyLoader, columnPrefix);
if (Jdk.recordExists && RecordUtil.isRecord(resultMap.getType())) {
return rowValue;
}
if (rowValue != null && !hasTypeHandlerForResultObject(rsw, resultMap.getType())) {
final MetaObject metaObject = configuration.newMetaObject(rowValue);
boolean foundValues = this.useConstructorMappings;
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/org/apache/ibatis/reflection/Jdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ public class Jdk {
optionalExists = available;
}

/**
* Whether exists 'java.lang.Record' in runtime environment.
* <p>
* If exists class return {@code true}.
* </p>
* @since 3.5.8
*/
public static final boolean recordExists;
static {
boolean available = false;
try {
Resources.classForName("java.lang.Record");
available = true;
} catch (ClassNotFoundException e) {
// ignore
}
recordExists = available;
}

private Jdk() {
super();
}
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/org/apache/ibatis/reflection/RecordUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2009-2021 the original author or authors.
*
* 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.apache.ibatis.reflection;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Utility class for java.lang.Record.
*
* @since 3.5.8
*/
public class RecordUtil {

private static final Method IS_RECORD_METHOD = findIsRecordMethod();

/**
* Whether record class or not.
*
* @param type a target type
* @return If record class, return {@code true}
*/
public static boolean isRecord(Class<?> type) {
try {
return IS_RECORD_METHOD != null && (boolean) IS_RECORD_METHOD.invoke(type);
} catch (IllegalAccessException | InvocationTargetException e) {
throw new IllegalStateException(e);
}
}

private static Method findIsRecordMethod() {
try {
return Class.class.getMethod("isRecord");
} catch (NoSuchMethodException e) {
return null;
}
}

private RecordUtil() {
// NOP
}

}

0 comments on commit f8a5eda

Please sign in to comment.