Skip to content

Commit 2420632

Browse files
committed
Support anonymous enum object that implement interface method
Related with gh-1489
1 parent 73c3320 commit 2420632

File tree

5 files changed

+67
-1
lines changed

5 files changed

+67
-1
lines changed

src/main/java/org/apache/ibatis/type/TypeHandlerRegistry.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ private Map<JdbcType, TypeHandler<?>> getJdbcHandlerMap(Type type) {
242242
if (jdbcHandlerMap == null && type instanceof Class) {
243243
Class<?> clazz = (Class<?>) type;
244244
if (clazz.isEnum() || (clazz.isAnonymousClass() && clazz.getEnclosingClass().isEnum())) {
245+
clazz = clazz.isAnonymousClass() ? clazz.getEnclosingClass() : clazz;
245246
jdbcHandlerMap = getJdbcHandlerMapForEnumInterfaces(clazz, clazz);
246247
if (jdbcHandlerMap == null) {
247248
register(clazz, getInstance(clazz, defaultEnumTypeHandler));

src/test/java/org/apache/ibatis/submitted/enum_interface_type_handler/EnumInterfaceTypeHandlerTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ void shouldInsertAUser() {
6565
assertEquals(Color.BLUE, result.getColor());
6666
}
6767
}
68+
69+
@Test
70+
void shouldInsertAUserWithoutParameterTypeInXmlElement() {
71+
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
72+
XmlMapper mapper = sqlSession.getMapper(XmlMapper.class);
73+
User user = new User();
74+
user.setId(2);
75+
user.setColor(Color.BLUE);
76+
mapper.insertUser(user);
77+
User result = sqlSession.getMapper(Mapper.class).getUser(2);
78+
assertEquals(Color.BLUE, result.getColor());
79+
}
80+
}
81+
6882
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.submitted.enum_interface_type_handler;
17+
18+
public interface XmlMapper {
19+
int insertUser(User user);
20+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Copyright 2009-2019 the original author or authors.
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
18+
-->
19+
<!DOCTYPE mapper
20+
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
21+
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
22+
23+
<mapper namespace="org.apache.ibatis.submitted.enum_interface_type_handler.XmlMapper">
24+
25+
<!-- without parameterType attribute -->
26+
<insert id="insertUser">
27+
insert into users (id, color) values (#{id}, #{color})
28+
</insert>
29+
30+
</mapper>

src/test/java/org/apache/ibatis/submitted/enum_interface_type_handler/mybatis-config.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8" ?>
22
<!--
33
4-
Copyright 2009-2017 the original author or authors.
4+
Copyright 2009-2019 the original author or authors.
55
66
Licensed under the Apache License, Version 2.0 (the "License");
77
you may not use this file except in compliance with the License.
@@ -39,5 +39,6 @@
3939
</environments>
4040
<mappers>
4141
<mapper class="org.apache.ibatis.submitted.enum_interface_type_handler.Mapper" />
42+
<mapper class="org.apache.ibatis.submitted.enum_interface_type_handler.XmlMapper" />
4243
</mappers>
4344
</configuration>

0 commit comments

Comments
 (0)