Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support for JDK 16 record type #2477

Merged
merged 10 commits into from
Mar 23, 2022
Next Next commit
support for JDK 16 record type
bzhou committed Mar 9, 2022
commit 9d36a47de890f0a675fec3e8622bf5cdcbb7a33a
28 changes: 25 additions & 3 deletions src/main/java/org/apache/ibatis/reflection/Reflector.java
Original file line number Diff line number Diff line change
@@ -65,9 +65,14 @@ public Reflector(Class<?> clazz) {
type = clazz;
addDefaultConstructor(clazz);
Method[] classMethods = getClassMethods(clazz);
addGetMethods(classMethods);
addSetMethods(classMethods);
addFields(clazz);
if (this.isRecordType()) {
addAccessorMethods(classMethods);
}
else {
addGetMethods(classMethods);
addSetMethods(classMethods);
addFields(clazz);
}
readablePropertyNames = getMethods.keySet().toArray(new String[0]);
writablePropertyNames = setMethods.keySet().toArray(new String[0]);
for (String propName : readablePropertyNames) {
@@ -78,6 +83,20 @@ public Reflector(Class<?> clazz) {
}
}

// java.lang.Record

private boolean isRecordType() {
Class<?> parent = this.type.getSuperclass();
return null != parent && "java.lang.Record".equals(parent.getName());
}

private void addAccessorMethods(Method[] methods) {
Arrays.stream(methods).filter(m -> m.getParameterTypes().length == 0)
.forEach(m -> addGetMethod(m.getName(), m, false));
}

// non-record

private void addDefaultConstructor(Class<?> clazz) {
Constructor<?>[] constructors = clazz.getDeclaredConstructors();
Arrays.stream(constructors).filter(constructor -> constructor.getParameterTypes().length == 0)
@@ -132,6 +151,9 @@ private void addGetMethod(String name, Method method, boolean isAmbiguous) {
getMethods.put(name, invoker);
Type returnType = TypeParameterResolver.resolveReturnType(method, type);
getTypes.put(name, typeToClass(returnType));
// if (this.isRecordType()) {
// System.out.println("record " + this.type.getSimpleName() + " addGetMethod " + name + " returning " + returnType);
// }
}

private void addSetMethods(Method[] methods) {
23 changes: 23 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/record_type/CreateDB.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--
-- Copyright 2009-2019 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.
--

drop table properties if exists;

create table properties (
item_id int,
property_id int,
value varchar(20)
);
24 changes: 24 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/record_type/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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.submitted.record_type;

import java.util.List;

public record Item(int id, List<Property> properties) {
public String idStr() {
return String.valueOf(id);
}
}
35 changes: 35 additions & 0 deletions src/test/java/org/apache/ibatis/submitted/record_type/Mapper.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright 2009-2019 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.

-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.apache.ibatis.submitted.record_type">

<update id="updateProps" parameterType="org.apache.ibatis.submitted.record_type.Item">
insert into properties (item_id, property_id, value) values
<foreach collection="properties" item="prop" close=";" separator=",">
(#{id}, #{prop.id}, #{prop.value})
</foreach>
</update>

<select id="selectProps" resultType="map">
select * from properties
</select>
</mapper>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* 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.submitted.record_type;

public record Property(int id, String value) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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.submitted.record_type;

import java.io.Reader;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class RecordTypeTest {

private static SqlSessionFactory sqlSessionFactory;

@BeforeAll
static void setUp() throws Exception {
// create a SqlSessionFactory
try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/submitted/record_type/mybatis-config.xml")) {
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}

// populate in-memory database
BaseDataTest.runScript(sqlSessionFactory.getConfiguration().getEnvironment().getDataSource(),
"org/apache/ibatis/submitted/record_type/CreateDB.sql");
}

@Test
void shouldUpdateProps() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Property p1 = new Property(11, "value11");
Property p2 = new Property(22, "value22");
List<Property> list = List.of(p1, p2);
Item item = new Item(10, list);
sqlSession.update("updateProps", item);

List<Map<String, Object>> rows = sqlSession.selectList("selectProps");
for (var row : rows) {
System.out.println(row);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--

Copyright 2009-2019 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.

-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">
<environment id="development">
<transactionManager type="JDBC">
<property name="" value="" />
</transactionManager>
<dataSource type="UNPOOLED">
<property name="driver" value="org.hsqldb.jdbcDriver" />
<property name="url" value="jdbc:hsqldb:mem:record_type" />
<property name="username" value="sa" />
</dataSource>
</environment>
</environments>

<mappers>
<mapper resource="org/apache/ibatis/submitted/record_type/Mapper.xml" />
</mappers>

</configuration>