Skip to content

Commit

Permalink
Merge pull request mybatis#1246 from kazuki43zoo/type-safe-for-Langua…
Browse files Browse the repository at this point in the history
…geDriver

Support type-safe on @lang#value
  • Loading branch information
kazuki43zoo committed Apr 12, 2018
2 parents 455b8c6 + f6f45e4 commit 305265c
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 23 deletions.
6 changes: 4 additions & 2 deletions src/main/java/org/apache/ibatis/annotations/Lang.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2016 the original author or authors.
* Copyright 2009-2018 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,8 @@
*/
package org.apache.ibatis.annotations;

import org.apache.ibatis.scripting.LanguageDriver;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand All @@ -28,5 +30,5 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Lang {
Class<?> value();
Class<? extends LanguageDriver> value();
}
6 changes: 3 additions & 3 deletions src/main/java/org/apache/ibatis/builder/BaseBuilder.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2015 the original author or authors.
* Copyright 2009-2018 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 @@ -108,7 +108,7 @@ protected Object createInstance(String alias) {
}
}

protected Class<?> resolveClass(String alias) {
protected <T> Class<? extends T> resolveClass(String alias) {
if (alias == null) {
return null;
}
Expand Down Expand Up @@ -145,7 +145,7 @@ protected TypeHandler<?> resolveTypeHandler(Class<?> javaType, Class<? extends T
return handler;
}

protected Class<?> resolveAlias(String alias) {
protected <T> Class<? extends T> resolveAlias(String alias) {
return typeAliasRegistry.resolveAlias(alias);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 @@ -472,7 +472,7 @@ public ResultMapping buildResultMapping(
nestedResultMap, notNullColumn, columnPrefix, typeHandler, flags, null, null, configuration.isLazyLoadingEnabled());
}

public LanguageDriver getLanguageDriver(Class<?> langClass) {
public LanguageDriver getLanguageDriver(Class<? extends LanguageDriver> langClass) {
if (langClass != null) {
configuration.getLanguageRegistry().register(langClass);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ void parseStatement(Method method) {

private LanguageDriver getLanguageDriver(Method method) {
Lang lang = method.getAnnotation(Lang.class);
Class<?> langClass = null;
Class<? extends LanguageDriver> langClass = null;
if (lang != null) {
langClass = lang.value();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 @@ -192,7 +192,7 @@ private boolean databaseIdMatchesCurrent(String id, String databaseId, String re
}

private LanguageDriver getLanguageDriver(String lang) {
Class<?> langClass = null;
Class<? extends LanguageDriver> langClass = null;
if (lang != null) {
langClass = resolveClass(lang);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 @@ -23,17 +23,17 @@
*/
public class LanguageDriverRegistry {

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

private Class<?> defaultDriverClass;
private Class<? extends LanguageDriver> defaultDriverClass;

public void register(Class<?> cls) {
public void register(Class<? extends LanguageDriver> cls) {
if (cls == null) {
throw new IllegalArgumentException("null is not a valid Language Driver");
}
if (!LANGUAGE_DRIVER_MAP.containsKey(cls)) {
try {
LANGUAGE_DRIVER_MAP.put(cls, (LanguageDriver) cls.newInstance());
LANGUAGE_DRIVER_MAP.put(cls, cls.newInstance());
} catch (Exception ex) {
throw new ScriptingException("Failed to load language driver for " + cls.getName(), ex);
}
Expand All @@ -44,25 +44,25 @@ public void register(LanguageDriver instance) {
if (instance == null) {
throw new IllegalArgumentException("null is not a valid Language Driver");
}
Class<?> cls = instance.getClass();
Class<? extends LanguageDriver> cls = instance.getClass();
if (!LANGUAGE_DRIVER_MAP.containsKey(cls)) {
LANGUAGE_DRIVER_MAP.put(cls, instance);
}
}

public LanguageDriver getDriver(Class<?> cls) {
public LanguageDriver getDriver(Class<? extends LanguageDriver> cls) {
return LANGUAGE_DRIVER_MAP.get(cls);
}

public LanguageDriver getDefaultDriver() {
return getDriver(getDefaultDriverClass());
}

public Class<?> getDefaultDriverClass() {
public Class<? extends LanguageDriver> getDefaultDriverClass() {
return defaultDriverClass;
}

public void setDefaultDriverClass(Class<?> defaultDriverClass) {
public void setDefaultDriverClass(Class<? extends LanguageDriver> defaultDriverClass) {
register(defaultDriverClass);
this.defaultDriverClass = defaultDriverClass;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/apache/ibatis/session/Configuration.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 @@ -520,7 +520,7 @@ public LanguageDriverRegistry getLanguageRegistry() {
return languageRegistry;
}

public void setDefaultScriptingLanguage(Class<?> driver) {
public void setDefaultScriptingLanguage(Class<? extends LanguageDriver> driver) {
if (driver == null) {
driver = XMLLanguageDriver.class;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/**
* Copyright 2009-2017 the original author or authors.
* Copyright 2009-2018 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 @@ -56,7 +56,7 @@ public void registerByTypeSameType() {

@Test
public void registerByTypeNull() {
when(registry).register((Class<?>) null);
when(registry).register((Class<? extends LanguageDriver>) null);
then(caughtException()).isInstanceOf(IllegalArgumentException.class)
.hasMessage("null is not a valid Language Driver");
}
Expand Down

0 comments on commit 305265c

Please sign in to comment.