Skip to content

Commit

Permalink
Add constructor that pass an Annotation type instead of Object on Pro…
Browse files Browse the repository at this point in the history
…viderSqlSource

The motivation for this change is to resolve issues that prevent GraalVM support. See mybatisgh-1552
  • Loading branch information
kazuki43zoo committed Jul 20, 2019
1 parent 9107d3d commit dde90b8
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
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 All @@ -46,7 +48,8 @@ public class ProviderSqlSource implements SqlSource {
private Integer providerContextIndex;

/**
* @deprecated Please use the {@link #ProviderSqlSource(Configuration, Object, Class, Method)} instead of this.
* @deprecated Since 3.5.3, Please use the {@link #ProviderSqlSource(Configuration, Annotation, Class, Method)} instead of this.
* This constructor will remove at a future version.
*/
@Deprecated
public ProviderSqlSource(Configuration configuration, Object provider) {
Expand All @@ -55,16 +58,34 @@ public ProviderSqlSource(Configuration configuration, Object provider) {

/**
* @since 3.4.5
* @deprecated Since 3.5.3, Please use the {@link #ProviderSqlSource(Configuration, Annotation, Class, Method)} instead of this.
* This constructor will remove at a future version.
*/
@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);
}

/**
* @since 3.5.3
*/
public ProviderSqlSource(Configuration configuration, Annotation provider, Class<?> mapperType, Method mapperMethod) {
String providerMethodName;
try {
this.configuration = configuration;
this.mapperMethod = mapperMethod;
Lang lang = mapperMethod == null ? null : mapperMethod.getAnnotation(Lang.class);
this.languageDriver = configuration.getLanguageDriver(lang == null ? null : lang.value());
this.providerType = getProviderType(provider, mapperMethod);
providerMethodName = (String) provider.getClass().getMethod("method").invoke(provider);
providerMethodName = (String) provider.annotationType().getMethod("method").invoke(provider);

if (providerMethodName.length() == 0 && ProviderMethodResolver.class.isAssignableFrom(this.providerType)) {
this.providerMethod = ((ProviderMethodResolver) this.providerType.getDeclaredConstructor().newInstance())
Expand Down Expand Up @@ -192,18 +213,18 @@ private String invokeProviderMethod(Object... args) throws Exception {
return sql != null ? sql.toString() : null;
}

private Class<?> getProviderType(Object providerAnnotation, Method mapperMethod)
private Class<?> getProviderType(Annotation providerAnnotation, Method mapperMethod)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> type = (Class<?>) providerAnnotation.getClass().getMethod("type").invoke(providerAnnotation);
Class<?> value = (Class<?>) providerAnnotation.getClass().getMethod("value").invoke(providerAnnotation);
Class<?> type = (Class<?>) providerAnnotation.annotationType().getMethod("type").invoke(providerAnnotation);
Class<?> value = (Class<?>) providerAnnotation.annotationType().getMethod("value").invoke(providerAnnotation);
if (value == void.class && type == void.class) {
throw new BuilderException("Please specify either 'value' or 'type' attribute of @"
+ ((Annotation) providerAnnotation).annotationType().getSimpleName()
+ providerAnnotation.annotationType().getSimpleName()
+ " at the '" + mapperMethod.toString() + "'.");
}
if (value != void.class && type != void.class && value != type) {
throw new BuilderException("Cannot specify different class on 'value' and 'type' attribute of @"
+ ((Annotation) providerAnnotation).annotationType().getSimpleName()
+ providerAnnotation.annotationType().getSimpleName()
+ " at the '" + mapperMethod.toString() + "'.");
}
return value == void.class ? type : value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -298,12 +298,14 @@ void methodOverload() throws NoSuchMethodException {
}

@Test
@SuppressWarnings("deprecation")
void notSqlProvider() {
try {
new ProviderSqlSource(new Configuration(), new Object(), null, null);
fail();
} catch (BuilderException e) {
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider. Cause: java.lang.NoSuchMethodException: java.lang.Object.type()"));
assertTrue(e.getMessage().contains("Error creating SqlSource for SqlProvider."));
assertTrue(e.getCause().getCause().getCause().getMessage().contains("java.lang.Object.type()"));
}
}

Expand Down Expand Up @@ -648,6 +650,41 @@ 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 {
Class<?> mapperType = StaticMethodSqlProviderMapper.class;
Method mapperMethod = mapperType.getMethod("noArgument");
ProviderSqlSource sqlSource = new ProviderSqlSource(new Configuration(), (Object)mapperMethod.getAnnotation(SelectProvider.class), mapperType, mapperMethod);
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 dde90b8

Please sign in to comment.