Skip to content

Commit cbdc960

Browse files
authored
Merge pull request #1246 from kazuki43zoo/type-safe-for-LanguageDriver
Support type-safe on @lang#value
2 parents 7b0d7cb + adbcd98 commit cbdc960

File tree

8 files changed

+25
-23
lines changed

8 files changed

+25
-23
lines changed

src/main/java/org/apache/ibatis/annotations/Lang.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2016 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -15,6 +15,8 @@
1515
*/
1616
package org.apache.ibatis.annotations;
1717

18+
import org.apache.ibatis.scripting.LanguageDriver;
19+
1820
import java.lang.annotation.Documented;
1921
import java.lang.annotation.ElementType;
2022
import java.lang.annotation.Retention;
@@ -28,5 +30,5 @@
2830
@Retention(RetentionPolicy.RUNTIME)
2931
@Target(ElementType.METHOD)
3032
public @interface Lang {
31-
Class<?> value();
33+
Class<? extends LanguageDriver> value();
3234
}

src/main/java/org/apache/ibatis/builder/BaseBuilder.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2015 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -108,7 +108,7 @@ protected Object createInstance(String alias) {
108108
}
109109
}
110110

111-
protected Class<?> resolveClass(String alias) {
111+
protected <T> Class<? extends T> resolveClass(String alias) {
112112
if (alias == null) {
113113
return null;
114114
}
@@ -145,7 +145,7 @@ protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends T
145145
return handler;
146146
}
147147

148-
protected Class<?> resolveAlias(String alias) {
148+
protected <T> Class<? extends T> resolveAlias(String alias) {
149149
return typeAliasRegistry.resolveAlias(alias);
150150
}
151151
}

src/main/java/org/apache/ibatis/builder/MapperBuilderAssistant.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -472,7 +472,7 @@ public ResultMapping buildResultMapping(
472472
nestedResultMap, notNullColumn, columnPrefix, typeHandler, flags, null, null, configuration.isLazyLoadingEnabled());
473473
}
474474

475-
public LanguageDriver getLanguageDriver(Class<?> langClass) {
475+
public LanguageDriver getLanguageDriver(Class<? extends LanguageDriver> langClass) {
476476
if (langClass != null) {
477477
configuration.getLanguageRegistry().register(langClass);
478478
} else {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,7 @@ void parseStatement(Method method) {
383383

384384
private LanguageDriver getLanguageDriver(Method method) {
385385
Lang lang = method.getAnnotation(Lang.class);
386-
Class<?> langClass = null;
386+
Class<? extends LanguageDriver> langClass = null;
387387
if (lang != null) {
388388
langClass = lang.value();
389389
}

src/main/java/org/apache/ibatis/builder/xml/XMLStatementBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -192,7 +192,7 @@ private boolean databaseIdMatchesCurrent(String id, String databaseId, String re
192192
}
193193

194194
private LanguageDriver getLanguageDriver(String lang) {
195-
Class<?> langClass = null;
195+
Class<? extends LanguageDriver> langClass = null;
196196
if (lang != null) {
197197
langClass = resolveClass(lang);
198198
}

src/main/java/org/apache/ibatis/scripting/LanguageDriverRegistry.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -23,17 +23,17 @@
2323
*/
2424
public class LanguageDriverRegistry {
2525

26-
private final Map<Class<?>, LanguageDriver> LANGUAGE_DRIVER_MAP = new HashMap<Class<?>, LanguageDriver>();
26+
private final Map<Class<? extends LanguageDriver>, LanguageDriver> LANGUAGE_DRIVER_MAP = new HashMap<>();
2727

28-
private Class<?> defaultDriverClass;
28+
private Class<? extends LanguageDriver> defaultDriverClass;
2929

30-
public void register(Class<?> cls) {
30+
public void register(Class<? extends LanguageDriver> cls) {
3131
if (cls == null) {
3232
throw new IllegalArgumentException("null is not a valid Language Driver");
3333
}
3434
if (!LANGUAGE_DRIVER_MAP.containsKey(cls)) {
3535
try {
36-
LANGUAGE_DRIVER_MAP.put(cls, (LanguageDriver) cls.newInstance());
36+
LANGUAGE_DRIVER_MAP.put(cls, cls.newInstance());
3737
} catch (Exception ex) {
3838
throw new ScriptingException("Failed to load language driver for " + cls.getName(), ex);
3939
}
@@ -44,25 +44,25 @@ public void register(LanguageDriver instance) {
4444
if (instance == null) {
4545
throw new IllegalArgumentException("null is not a valid Language Driver");
4646
}
47-
Class<?> cls = instance.getClass();
47+
Class<? extends LanguageDriver> cls = instance.getClass();
4848
if (!LANGUAGE_DRIVER_MAP.containsKey(cls)) {
4949
LANGUAGE_DRIVER_MAP.put(cls, instance);
5050
}
5151
}
5252

53-
public LanguageDriver getDriver(Class<?> cls) {
53+
public LanguageDriver getDriver(Class<? extends LanguageDriver> cls) {
5454
return LANGUAGE_DRIVER_MAP.get(cls);
5555
}
5656

5757
public LanguageDriver getDefaultDriver() {
5858
return getDriver(getDefaultDriverClass());
5959
}
6060

61-
public Class<?> getDefaultDriverClass() {
61+
public Class<? extends LanguageDriver> getDefaultDriverClass() {
6262
return defaultDriverClass;
6363
}
6464

65-
public void setDefaultDriverClass(Class<?> defaultDriverClass) {
65+
public void setDefaultDriverClass(Class<? extends LanguageDriver> defaultDriverClass) {
6666
register(defaultDriverClass);
6767
this.defaultDriverClass = defaultDriverClass;
6868
}

src/main/java/org/apache/ibatis/session/Configuration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -520,7 +520,7 @@ public LanguageDriverRegistry getLanguageRegistry() {
520520
return languageRegistry;
521521
}
522522

523-
public void setDefaultScriptingLanguage(Class<?> driver) {
523+
public void setDefaultScriptingLanguage(Class<? extends LanguageDriver> driver) {
524524
if (driver == null) {
525525
driver = XMLLanguageDriver.class;
526526
}

src/test/java/org/apache/ibatis/scripting/LanguageDriverRegistryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* Copyright 2009-2017 the original author or authors.
2+
* Copyright 2009-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -56,7 +56,7 @@ public void registerByTypeSameType() {
5656

5757
@Test
5858
public void registerByTypeNull() {
59-
when(registry).register((Class<?>) null);
59+
when(registry).register((Class<? extends LanguageDriver>) null);
6060
then(caughtException()).isInstanceOf(IllegalArgumentException.class)
6161
.hasMessage("null is not a valid Language Driver");
6262
}

0 commit comments

Comments
 (0)