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

Throw Exception when adding @AutomapConstructor to multiple constructors #2627

Expand Up @@ -710,12 +710,14 @@ private Optional<Constructor<?>> findConstructorForAutomapping(final Class<?> re
if (constructors.length == 1) {
return Optional.of(constructors[0]);
}
for (final Constructor<?> constructor : constructors) {
if (constructor.isAnnotationPresent(AutomapConstructor.class)) {
return Optional.of(constructor);
}
}
if (configuration.isArgNameBasedConstructorAutoMapping()) {
Optional<Constructor<?>> annotated = Arrays.stream(constructors)
.filter(x -> x.isAnnotationPresent(AutomapConstructor.class))
.reduce((x, y) -> {
throw new ExecutorException("@AutomapConstructor should be used in only one constructor.");
});
if (annotated.isPresent()) {
return annotated;
} else if (configuration.isArgNameBasedConstructorAutoMapping()) {
// Finding-best-match type implementation is possible,
// but using @AutomapConstructor seems sufficient.
throw new ExecutorException(MessageFormat.format(
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2021 the original author or authors.
* Copyright 2009-2022 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.
Expand Down Expand Up @@ -29,6 +29,9 @@ public interface AutoConstructorMapper {
@Select("SELECT * FROM subject")
List<AnnotatedSubject> getAnnotatedSubjects();

@Select("SELECT * FROM subject")
List<BadAnnotatedSubject> getBadAnnotatedSubjects();

@Select("SELECT * FROM subject")
List<BadSubject> getBadSubjects();

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2009-2021 the original author or authors.
* Copyright 2009-2022 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.
Expand All @@ -15,6 +15,7 @@
*/
package org.apache.ibatis.autoconstructor;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

Expand All @@ -23,6 +24,7 @@

import org.apache.ibatis.BaseDataTest;
import org.apache.ibatis.exceptions.PersistenceException;
import org.apache.ibatis.executor.ExecutorException;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
Expand Down Expand Up @@ -71,6 +73,16 @@ void annotatedSubject() {
}
}

@Test
void badMultipleAnnotatedSubject() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
final AutoConstructorMapper mapper = sqlSession.getMapper(AutoConstructorMapper.class);
final PersistenceException ex = assertThrows(PersistenceException.class, mapper::getBadAnnotatedSubjects);
final ExecutorException cause = (ExecutorException) ex.getCause();
assertEquals("@AutomapConstructor should be used in only one constructor.", cause.getMessage());
}
}

@Test
void badSubject() {
try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
Expand Down
@@ -0,0 +1,44 @@
/*
* Copyright 2009-2022 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.autoconstructor;

import org.apache.ibatis.annotations.AutomapConstructor;

public class BadAnnotatedSubject {
private final int id;
private final String name;
private final int age;
private final int height;
private final int weight;

@AutomapConstructor
public BadAnnotatedSubject(final int id, final String name, final int age, final int height, final int weight) {
this.id = id;
this.name = name;
this.age = age;
this.height = height;
this.weight = weight;
}

@AutomapConstructor
public BadAnnotatedSubject(final int id, final String name, final int age) {
this.id = id;
this.name = name;
this.age = age;
this.height = 0;
this.weight = 0;
}
}