Skip to content

Commit 4dbd30a

Browse files
committed
Support only annotation type at 2nd argument on deprecated constructor of ProviderSqlSource
See gh-1611
1 parent 4aa737f commit 4dbd30a

File tree

2 files changed

+6
-41
lines changed

2 files changed

+6
-41
lines changed

src/main/java/org/apache/ibatis/builder/annotation/ProviderSqlSource.java

+1-11
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,9 @@
1919
import java.lang.reflect.InvocationTargetException;
2020
import java.lang.reflect.Method;
2121
import java.lang.reflect.Modifier;
22-
import java.lang.reflect.Proxy;
2322
import java.util.Map;
2423

2524
import org.apache.ibatis.annotations.Lang;
26-
import org.apache.ibatis.annotations.SelectProvider;
2725
import org.apache.ibatis.builder.BuilderException;
2826
import org.apache.ibatis.mapping.BoundSql;
2927
import org.apache.ibatis.mapping.SqlSource;
@@ -63,15 +61,7 @@ public ProviderSqlSource(Configuration configuration, Object provider) {
6361
*/
6462
@Deprecated
6563
public ProviderSqlSource(Configuration configuration, Object provider, Class<?> mapperType, Method mapperMethod) {
66-
this(configuration, provider instanceof Annotation ? (Annotation) provider :
67-
(Annotation) Proxy.newProxyInstance(SelectProvider.class.getClassLoader(), new Class<?>[]{SelectProvider.class},
68-
(proxy, method, args) -> {
69-
if (method.getName().equals("annotationType")) {
70-
return SelectProvider.class;
71-
}
72-
return provider.getClass().getMethod(method.getName()).invoke(provider);
73-
}),
74-
mapperType, mapperMethod);
64+
this(configuration, (Annotation) provider , mapperType, mapperMethod);
7565
}
7666

7767
/**

src/test/java/org/apache/ibatis/submitted/sqlprovider/SqlProviderTest.java

+5-30
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.junit.jupiter.api.Assertions.fail;
2323

2424
import java.io.Reader;
25+
import java.lang.annotation.Annotation;
2526
import java.lang.reflect.Method;
2627
import java.util.ArrayList;
2728
import java.util.Collections;
@@ -299,13 +300,13 @@ void methodOverload() throws NoSuchMethodException {
299300

300301
@Test
301302
@SuppressWarnings("deprecation")
302-
void notSqlProvider() {
303+
void notSqlProvider() throws NoSuchMethodException {
304+
Object testAnnotation = getClass().getDeclaredMethod("notSqlProvider").getAnnotation(Test.class);
303305
try {
304-
new ProviderSqlSource(new Configuration(), new Object(), null, null);
306+
new ProviderSqlSource(new Configuration(), testAnnotation);
305307
fail();
306308
} catch (BuilderException e) {
307-
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider."));
308-
assertTrue(e.getCause().getCause().getCause().getMessage().contains("java.lang.Object.type()"));
309+
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. Cause: java.lang.NoSuchMethodException: org.junit.jupiter.api.Test.type()"));
309310
}
310311
}
311312

@@ -650,15 +651,6 @@ void providerContextAndMap() {
650651
}
651652
}
652653

653-
@Test
654-
@SuppressWarnings("deprecation")
655-
void keepBackwardCompatibilityOnDeprecatedConstructor() throws NoSuchMethodException {
656-
Class<?> mapperType = StaticMethodSqlProviderMapper.class;
657-
Method mapperMethod = mapperType.getMethod("noArgument");
658-
ProviderSqlSource sqlSource = new ProviderSqlSource(new Configuration(), new SqlProviderConfig(), mapperType, mapperMethod);
659-
assertEquals("SELECT 1", sqlSource.getBoundSql(null).getSql());
660-
}
661-
662654
@Test
663655
@SuppressWarnings("deprecation")
664656
void keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation() throws NoSuchMethodException {
@@ -668,23 +660,6 @@ void keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation() throws NoS
668660
assertEquals("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS", sqlSource.getBoundSql(null).getSql());
669661
}
670662

671-
public static class SqlProviderConfig {
672-
public Class<?> type() {
673-
return SqlProvider.class;
674-
}
675-
public Class<?> value() {
676-
return void.class;
677-
}
678-
public String method() {
679-
return "provideSql";
680-
}
681-
public static class SqlProvider {
682-
public static String provideSql() {
683-
return "SELECT 1";
684-
}
685-
}
686-
}
687-
688663
public interface ErrorMapper {
689664
@SelectProvider(type = ErrorSqlBuilder.class, method = "methodNotFound")
690665
void methodNotFound();

0 commit comments

Comments
 (0)