From e740748957b6fe7d66a86443a66d18937e7c4dec Mon Sep 17 00:00:00 2001 From: richie Date: Thu, 29 Aug 2019 20:07:39 +0800 Subject: [PATCH] =?UTF-8?q?=E6=95=B0=E6=8D=AE=E8=BF=9E=E6=8E=A5API?= =?UTF-8?q?=E4=BB=A5=E5=8F=8A=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/fanruan/api/data/ConnectionKit.java | 30 ++++++++++++++--- .../java/com/fanruan/api/util/ReflectKit.java | 32 ++++++++++++++----- .../com/fanruan/api/util/ReflectKitTest.java | 13 +++++--- 3 files changed, 59 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/fanruan/api/data/ConnectionKit.java b/src/main/java/com/fanruan/api/data/ConnectionKit.java index e670505..3b4c052 100644 --- a/src/main/java/com/fanruan/api/data/ConnectionKit.java +++ b/src/main/java/com/fanruan/api/data/ConnectionKit.java @@ -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 getConnection(String name, Class type) { - return DatasourceManager.getInstance().getConnection(name, type); + public @Nullable T getConnection(@NotNull String name, Class type) { + Connection connection = getConnection(name); + if (ReflectKit.objectInstanceOf(connection, type)) { + return (T) connection; + } + return null; } } diff --git a/src/main/java/com/fanruan/api/util/ReflectKit.java b/src/main/java/com/fanruan/api/util/ReflectKit.java index ef9d4b9..abe7fda 100644 --- a/src/main/java/com/fanruan/api/util/ReflectKit.java +++ b/src/main/java/com/fanruan/api/util/ReflectKit.java @@ -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 targetClass) { + if (current == null || targetClass == null) { + return false; + } + return targetClass.isAssignableFrom(current); + } + + /** + * 判读指定的类是否是另一个类的子类 + * + * @param object 指定的类 + * @param clazz 另一个类 + * @return 如果指定类是另一个类的子类则返回true,否则返回false */ - public static boolean classInstanceOf(Class current, Class target) { - return StableUtils.classInstanceOf(current, target); + public static boolean objectInstanceOf(Object object, Class clazz) { + if (object == null || clazz == null) { + return false; + } + return clazz.isInstance(object); } } diff --git a/src/test/java/com/fanruan/api/util/ReflectKitTest.java b/src/test/java/com/fanruan/api/util/ReflectKitTest.java index 1c88dfb..aac7662 100644 --- a/src/test/java/com/fanruan/api/util/ReflectKitTest.java +++ b/src/test/java/com/fanruan/api/util/ReflectKitTest.java @@ -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)); } } \ No newline at end of file