forked from fanruan/finekit
richie
5 years ago
3 changed files with 59 additions and 16 deletions
@ -1,16 +1,38 @@
|
||||
package com.fanruan.api.data; |
||||
|
||||
import com.fanruan.api.util.ReflectKit; |
||||
import com.fr.data.impl.Connection; |
||||
import com.fr.file.DatasourceManager; |
||||
import com.fr.file.ConnectionConfig; |
||||
import org.jetbrains.annotations.NotNull; |
||||
import org.jetbrains.annotations.Nullable; |
||||
|
||||
/** |
||||
* 数据连接相关工具类 |
||||
*/ |
||||
public class ConnectionKit { |
||||
|
||||
/** |
||||
* 获取指定名字的数据连接 |
||||
* |
||||
* @param name 名字 |
||||
* @return 数据连接 |
||||
*/ |
||||
public @Nullable Connection getConnection(@NotNull String name) { |
||||
return ConnectionConfig.getInstance().getConnection(name); |
||||
} |
||||
|
||||
/** |
||||
* 获取指定名字和指定类型的数据连接 |
||||
* |
||||
* @param name 数据连接的名字 |
||||
* @param type 类型 |
||||
* @return 数据连接 |
||||
* |
||||
*/ |
||||
public <T extends Connection> T getConnection(String name, Class<? extends Connection> type) { |
||||
return DatasourceManager.getInstance().getConnection(name, type); |
||||
public <T extends Connection> @Nullable T getConnection(@NotNull String name, Class<? extends Connection> type) { |
||||
Connection connection = getConnection(name); |
||||
if (ReflectKit.objectInstanceOf(connection, type)) { |
||||
return (T) connection; |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
@ -1,16 +1,32 @@
|
||||
package com.fanruan.api.util; |
||||
|
||||
import com.fr.stable.StableUtils; |
||||
|
||||
public class ReflectKit { |
||||
|
||||
/** |
||||
* 判断一个类是否是另一个类的子类 |
||||
* @param current 当前类 |
||||
* @param target 目标类 |
||||
* @return 如果当前类是目标类的子类,则返回true,否则返回false |
||||
* 判读指定的类是否是另一个类的子类 |
||||
* |
||||
* @param current 指定的类 |
||||
* @param targetClass 另一个类 |
||||
* @return 如果当前类是目标类的子类则返回true,否则返回false |
||||
*/ |
||||
public static boolean classInstanceOf(Class current, Class target) { |
||||
return StableUtils.classInstanceOf(current, target); |
||||
public static boolean classInstanceOf(Class<?> current, Class<?> targetClass) { |
||||
if (current == null || targetClass == null) { |
||||
return false; |
||||
} |
||||
return targetClass.isAssignableFrom(current); |
||||
} |
||||
|
||||
/** |
||||
* 判读指定的类是否是另一个类的子类 |
||||
* |
||||
* @param object 指定的类 |
||||
* @param clazz 另一个类 |
||||
* @return 如果指定类是另一个类的子类则返回true,否则返回false |
||||
*/ |
||||
public static boolean objectInstanceOf(Object object, Class clazz) { |
||||
if (object == null || clazz == null) { |
||||
return false; |
||||
} |
||||
return clazz.isInstance(object); |
||||
} |
||||
} |
||||
|
@ -1,14 +1,19 @@
|
||||
package com.fanruan.api.util; |
||||
|
||||
import org.junit.Assert; |
||||
import org.junit.Test; |
||||
|
||||
import static org.junit.Assert.*; |
||||
|
||||
public class ReflectKitTest { |
||||
|
||||
@Test |
||||
public void classInstanceOf() { |
||||
assertEquals(ReflectKit.classInstanceOf(Integer.class,Object.class),true); |
||||
assertEquals(ReflectKit.classInstanceOf(Object.class,Integer.class),false); |
||||
Assert.assertTrue(ReflectKit.classInstanceOf(Integer.class, Object.class)); |
||||
Assert.assertFalse(ReflectKit.classInstanceOf(Object.class, Integer.class)); |
||||
} |
||||
|
||||
@Test |
||||
public void objectInstanceOf() { |
||||
Assert.assertTrue(ReflectKit.objectInstanceOf(1, Integer.class)); |
||||
Assert.assertFalse(ReflectKit.objectInstanceOf(1.0, Integer.class)); |
||||
} |
||||
} |
Loading…
Reference in new issue