diff --git a/build.gradle b/build.gradle index 1b16f91..00cd1c5 100644 --- a/build.gradle +++ b/build.gradle @@ -17,7 +17,7 @@ buildscript { subprojects { group 'com.synebula' - version '1.1.0' + version '1.1.2' buildscript { repositories { diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt index 5aa6bce..3920630 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/autoconfig/service/ServiceProxy.kt @@ -47,7 +47,7 @@ class ServiceProxy( */ override fun exec(proxy: Any, method: Method, args: Array): Any? { try { - val proxyMethod = this.service.javaClass.getDeclaredMethod(method.name, *method.parameterTypes) + val proxyMethod = this.service.javaClass.getMethod(method.name, *method.parameterTypes) return proxyMethod.invoke(this.service, *args) } catch (ex: NoSuchMethodException) { throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.service::class.java}], you must implements interface [${this.supertype.name}] ") diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoProxy.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoProxy.kt index e1c64a3..30e926b 100644 --- a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoProxy.kt +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/autoconfig/MongodbRepoProxy.kt @@ -1,59 +1,41 @@ package com.synebula.gaea.mongodb.autoconfig import com.synebula.gaea.domain.repository.IRepository -import com.synebula.gaea.log.ILogger import com.synebula.gaea.mongodb.query.MongodbQuery import com.synebula.gaea.mongodb.repository.MongodbRepository import com.synebula.gaea.query.IQuery +import com.synebula.gaea.reflect.getGenericInterface import com.synebula.gaea.spring.autoconfig.Proxy import org.springframework.beans.factory.BeanFactory import org.springframework.data.mongodb.core.MongoTemplate -import java.io.InvalidClassException import java.lang.reflect.Method -import java.lang.reflect.ParameterizedType class MongodbRepoProxy( private var supertype: Class<*>, private var beanFactory: BeanFactory, implementBeanNames: Array = arrayOf() ) : Proxy() { - private var repo: IRepository<*, *>? = null - - private var query: IQuery<*, *>? = null + private var mongodbRepo: Any init { + // 判断接口类型 + val clazz: Class<*> // 代理服务类型 + val interfaceClazz: Class<*> // 代理服务接口 if (this.supertype.interfaces.any { it == IRepository::class.java }) { - // 如果是IRepository子接口 - if (implementBeanNames.isEmpty()) { - val genericInterfaces = this.supertype.genericInterfaces.find { - it.typeName.startsWith(IRepository::class.java.typeName) - }!! - val constructor = MongodbRepository::class.java.getConstructor( - Class::class.java, MongoTemplate::class.java - ) - this.repo = constructor.newInstance( - (genericInterfaces as ParameterizedType).actualTypeArguments[0], - this.beanFactory.getBean(MongoTemplate::class.java) - ) - } else { - this.repo = this.beanFactory.getBean(implementBeanNames[0]) as IRepository<*, *> - } + clazz = MongodbRepository::class.java + interfaceClazz = IRepository::class.java } else { - // 否则是IQuery子接口 - if (implementBeanNames.isEmpty()) { - val genericInterfaces = this.supertype.genericInterfaces.find { - it.typeName.startsWith(IQuery::class.java.typeName) - }!! - val constructor = MongodbQuery::class.java.getConstructor( - Class::class.java, MongoTemplate::class.java, ILogger::class.java - ) - this.query = constructor.newInstance( - (genericInterfaces as ParameterizedType).actualTypeArguments[0], - this.beanFactory.getBean(MongoTemplate::class.java), - this.beanFactory.getBean(ILogger::class.java), - ) - } else { - this.query = this.beanFactory.getBean(implementBeanNames[0]) as IQuery<*, *> - } + clazz = MongodbQuery::class.java + interfaceClazz = IQuery::class.java + } + + if (implementBeanNames.isEmpty()) { + val constructor = clazz.getConstructor(Class::class.java, MongoTemplate::class.java) + this.mongodbRepo = constructor.newInstance( + this.supertype.getGenericInterface(interfaceClazz)!!.actualTypeArguments[0], + this.beanFactory.getBean(MongoTemplate::class.java) + ) + } else { + this.mongodbRepo = this.beanFactory.getBean(implementBeanNames[0]) } } @@ -66,18 +48,11 @@ class MongodbRepoProxy( * @return 方法执行结果 */ override fun exec(proxy: Any, method: Method, args: Array): Any? { - val proxyClazz = if (this.repo != null) { - this.repo!!.javaClass - } else if (this.query != null) { - this.query!!.javaClass - } else - throw InvalidClassException("class [${this.supertype.name}] proxy object property [repo] and [query] are both null") - try { - val proxyMethod: Method = proxyClazz.getDeclaredMethod(method.name, *method.parameterTypes) - return proxyMethod.invoke(this.repo, *args) + val proxyMethod: Method = this.mongodbRepo.javaClass.getMethod(method.name, *method.parameterTypes) + return proxyMethod.invoke(this.mongodbRepo, *args) } catch (ex: NoSuchMethodException) { - throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${proxyClazz}], you must implements interface [${this.supertype.name}] ") + throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.mongodbRepo.javaClass}], you must implements interface [${this.supertype.name}] ") } } diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQuery.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQuery.kt index 93bff5b..27cabbf 100644 --- a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQuery.kt +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbQuery.kt @@ -2,7 +2,6 @@ package com.synebula.gaea.mongodb.query import com.synebula.gaea.ext.fieldNames import com.synebula.gaea.ext.firstCharLowerCase -import com.synebula.gaea.log.ILogger import com.synebula.gaea.mongodb.order import com.synebula.gaea.mongodb.select import com.synebula.gaea.mongodb.where @@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query * @param template MongodbRepo对象 */ -open class MongodbQuery(override var clazz: Class, var template: MongoTemplate, var logger: ILogger) : +open class MongodbQuery(override var clazz: Class, var template: MongoTemplate) : IQuery { /** @@ -85,18 +84,13 @@ open class MongodbQuery(override var clazz: Class, var templat /** * 获取collection */ - protected fun collection(clazz: Class): String { - val table: Table? = clazz.getDeclaredAnnotation( - Table::class.java - ) - return if (table != null) return table.name + fun collection(clazz: Class): String { + val table = clazz.getDeclaredAnnotation(Table::class.java) + return if (table != null) table.name else { - this.logger.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合") val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase() if (!validViewCollection || this.template.collectionExists(name)) name - else { - throw RuntimeException("找不到名为[${clazz.name}]的集合") - } + else throw RuntimeException("找不到名为[${clazz.name}]的集合") } } } diff --git a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbUniversalQuery.kt b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbUniversalQuery.kt index f88dfbc..823963e 100644 --- a/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbUniversalQuery.kt +++ b/src/gaea.mongodb/src/main/kotlin/com/synebula/gaea/mongodb/query/MongodbUniversalQuery.kt @@ -3,7 +3,6 @@ package com.synebula.gaea.mongodb.query import com.synebula.gaea.ext.fieldNames import com.synebula.gaea.ext.firstCharLowerCase -import com.synebula.gaea.log.ILogger import com.synebula.gaea.mongodb.order import com.synebula.gaea.mongodb.select import com.synebula.gaea.mongodb.where @@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query * 实现IQuery的Mongodb查询类 * @param template MongodbRepo对象 */ -open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogger) : IUniversalQuery { +open class MongodbUniversalQuery(var template: MongoTemplate) : IUniversalQuery { /** * 使用View解析是collection时是否校验存在,默认不校验 @@ -85,19 +84,12 @@ open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogge * 获取collection */ fun collection(clazz: Class): String { - val table: Table? = clazz.getDeclaredAnnotation( - Table::class.java - ) - return if (table != null) - return table.name + val table = clazz.getDeclaredAnnotation(Table::class.java) + return if (table != null) table.name else { - this.logger.info(this, "视图类没有标记[Collection]注解,无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合") val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase() - if (!validViewCollection || this.template.collectionExists(name)) - name - else { - throw RuntimeException("找不到名为[${clazz.name}]的集合") - } + if (!validViewCollection || this.template.collectionExists(name)) name + else throw RuntimeException("找不到名为[${clazz.name}]的集合") } } } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/bus/SubscriberRegistry.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/bus/SubscriberRegistry.kt index 05138f0..00b7244 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/bus/SubscriberRegistry.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/bus/SubscriberRegistry.kt @@ -13,7 +13,7 @@ */ package com.synebula.gaea.bus -import com.synebula.gaea.reflect.Types +import com.synebula.gaea.reflect.supertypes import java.lang.reflect.Method import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArraySet @@ -184,7 +184,7 @@ open class SubscriberRegistry(private val bus: IBus) { fun flattenHierarchy(clazz: Class<*>): Set> { var supertypes = flattenHierarchyCache[clazz] if (supertypes == null) { - supertypes = Types.supertypes(clazz) + supertypes = clazz.supertypes() flattenHierarchyCache[clazz] = supertypes } return supertypes diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt index 264a258..1856b12 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/Service.kt @@ -11,7 +11,7 @@ import com.synebula.gaea.domain.repository.IRepository * 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象 * * @param clazz 聚合根类对象 - * @param repo 仓储对象 + * @param repository 仓储对象 * @param mapper 对象转换组件 * @author alex * @version 0.1 @@ -19,11 +19,10 @@ import com.synebula.gaea.domain.repository.IRepository */ open class Service, ID>( protected open var clazz: Class, - protected open var repo: IRepository, + protected open var repository: IRepository, protected open var mapper: IObjectMapper, ) : IService { - /** * 增加对象 * @@ -32,7 +31,7 @@ open class Service, ID>( override fun add(command: ICommand): DataMessage { val msg = DataMessage() val root = this.map(command) - this.repo.add(root) + this.repository.add(root) msg.data = root.id return msg } @@ -44,7 +43,7 @@ open class Service, ID>( */ override fun add(commands: List) { val roots = commands.map { this.map(it) } - this.repo.add(roots) + this.repository.add(roots) } /** @@ -56,7 +55,7 @@ open class Service, ID>( override fun update(id: ID, command: ICommand) { val root = this.map(command) root.id = id - this.repo.update(root) + this.repository.update(root) } /** @@ -66,7 +65,7 @@ open class Service, ID>( */ override fun update(commands: List) { val roots = commands.map { this.map(it) } - this.repo.update(roots) + this.repository.update(roots) } /** @@ -74,7 +73,7 @@ open class Service, ID>( * @param id 对象ID */ override fun remove(id: ID) { - this.repo.remove(id) + this.repository.remove(id) } /** diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt index 28a137e..95e1dce 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt @@ -1,21 +1,28 @@ package com.synebula.gaea.reflect -object Types { - /** - * 获取类的所有父类型。 - */ - fun supertypes(clazz: Class<*>): Set> { - val supertypes = mutableSetOf>() - supertypes.add(clazz) - if (clazz.interfaces.isNotEmpty()) { - supertypes.addAll(clazz.interfaces.map { supertypes(it) }.reduce { r, c -> - val all = r.toMutableSet() - all.addAll(c) - all - }) - } - if (clazz.superclass != null) - supertypes.addAll(supertypes(clazz.superclass)) - return supertypes +import java.lang.reflect.ParameterizedType + +/** + * 获取类的所有父类型。 + */ +fun Class<*>.supertypes(): Set> { + val supertypes = mutableSetOf>() + supertypes.add(this) + if (this.interfaces.isNotEmpty()) { + supertypes.addAll(this.interfaces.map { it.supertypes() }.reduce { r, c -> + val all = r.toMutableSet() + all.addAll(c) + all + }) } -} \ No newline at end of file + if (this.superclass != null) + supertypes.addAll(this.superclass.supertypes()) + return supertypes +} + +fun Class<*>.getGenericInterface(interfaceClazz: Class<*>): ParameterizedType? { + val type = this.genericInterfaces.find { it.typeName.startsWith(interfaceClazz.typeName) } + return if (type == null) null + else type as ParameterizedType + +}