Skip to content

Commit

Permalink
Support only annotation type at 2nd argument on deprecated constructo…
Browse files Browse the repository at this point in the history
…r of ProviderSqlSource

See gh-1611
  • Loading branch information
kazuki43zoo committed Jul 22, 2019
1 parent 4aa737f commit 4dbd30a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.Proxy;
import java.util.Map;

import org.apache.ibatis.annotations.Lang;
import org.apache.ibatis.annotations.SelectProvider;
import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.SqlSource;
Expand Down Expand Up @@ -63,15 +61,7 @@ public ProviderSqlSource(Configuration configuration, Object provider) {
*/
@Deprecated
public ProviderSqlSource(Configuration configuration, Object provider, Class<?> mapperType, Method mapperMethod) {
this(configuration, provider instanceof Annotation ? (Annotation) provider :
(Annotation) Proxy.newProxyInstance(SelectProvider.class.getClassLoader(), new Class<?>[]{SelectProvider.class},
(proxy, method, args) -> {
if (method.getName().equals("annotationType")) {
return SelectProvider.class;
}
return provider.getClass().getMethod(method.getName()).invoke(provider);
}),
mapperType, mapperMethod);
this(configuration, (Annotation) provider , mapperType, mapperMethod);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.junit.jupiter.api.Assertions.fail;

import java.io.Reader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -299,13 +300,13 @@ void methodOverload() throws NoSuchMethodException {

@Test
@SuppressWarnings("deprecation")
void notSqlProvider() {
void notSqlProvider() throws NoSuchMethodException {
Object testAnnotation = getClass().getDeclaredMethod("notSqlProvider").getAnnotation(Test.class);
try {
new ProviderSqlSource(new Configuration(), new Object(), null, null);
new ProviderSqlSource(new Configuration(), testAnnotation);
fail();
} catch (BuilderException e) {
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider."));
assertTrue(e.getCause().getCause().getCause().getMessage().contains("java.lang.Object.type()"));
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. Cause: java.lang.NoSuchMethodException: org.junit.jupiter.api.Test.type()"));
}
}

Expand Down Expand Up @@ -650,15 +651,6 @@ void providerContextAndMap() {
}
}

@Test
@SuppressWarnings("deprecation")
void keepBackwardCompatibilityOnDeprecatedConstructor() throws NoSuchMethodException {
Class<?> mapperType = StaticMethodSqlProviderMapper.class;
Method mapperMethod = mapperType.getMethod("noArgument");
ProviderSqlSource sqlSource = new ProviderSqlSource(new Configuration(), new SqlProviderConfig(), mapperType, mapperMethod);
assertEquals("SELECT 1", sqlSource.getBoundSql(null).getSql());
}

@Test
@SuppressWarnings("deprecation")
void keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation() throws NoSuchMethodException {
Expand All @@ -668,23 +660,6 @@ void keepBackwardCompatibilityOnDeprecatedConstructorWithAnnotation() throws NoS
assertEquals("SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS", sqlSource.getBoundSql(null).getSql());
}

public static class SqlProviderConfig {
public Class<?> type() {
return SqlProvider.class;
}
public Class<?> value() {
return void.class;
}
public String method() {
return "provideSql";
}
public static class SqlProvider {
public static String provideSql() {
return "SELECT 1";
}
}
}

public interface ErrorMapper {
@SelectProvider(type = ErrorSqlBuilder.class, method = "methodNotFound")
void methodNotFound();
Expand Down

0 comments on commit 4dbd30a

Please sign in to comment.