1.1.2 优化代码

This commit is contained in:
2022-08-19 17:00:09 +08:00
parent b09e0c0f03
commit 3c23c26a64
8 changed files with 68 additions and 101 deletions

View File

@@ -17,7 +17,7 @@ buildscript {
subprojects { subprojects {
group 'com.synebula' group 'com.synebula'
version '1.1.0' version '1.1.2'
buildscript { buildscript {
repositories { repositories {

View File

@@ -47,7 +47,7 @@ class ServiceProxy(
*/ */
override fun exec(proxy: Any, method: Method, args: Array<Any>): Any? { override fun exec(proxy: Any, method: Method, args: Array<Any>): Any? {
try { 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) return proxyMethod.invoke(this.service, *args)
} catch (ex: NoSuchMethodException) { } catch (ex: NoSuchMethodException) {
throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.service::class.java}], you must implements interface [${this.supertype.name}] ") throw NoSuchMethodException("method [${method.toGenericString()}] not implements in class [${this.service::class.java}], you must implements interface [${this.supertype.name}] ")

View File

@@ -1,59 +1,41 @@
package com.synebula.gaea.mongodb.autoconfig package com.synebula.gaea.mongodb.autoconfig
import com.synebula.gaea.domain.repository.IRepository 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.query.MongodbQuery
import com.synebula.gaea.mongodb.repository.MongodbRepository import com.synebula.gaea.mongodb.repository.MongodbRepository
import com.synebula.gaea.query.IQuery import com.synebula.gaea.query.IQuery
import com.synebula.gaea.reflect.getGenericInterface
import com.synebula.gaea.spring.autoconfig.Proxy import com.synebula.gaea.spring.autoconfig.Proxy
import org.springframework.beans.factory.BeanFactory import org.springframework.beans.factory.BeanFactory
import org.springframework.data.mongodb.core.MongoTemplate import org.springframework.data.mongodb.core.MongoTemplate
import java.io.InvalidClassException
import java.lang.reflect.Method import java.lang.reflect.Method
import java.lang.reflect.ParameterizedType
class MongodbRepoProxy( class MongodbRepoProxy(
private var supertype: Class<*>, private var beanFactory: BeanFactory, implementBeanNames: Array<String> = arrayOf() private var supertype: Class<*>, private var beanFactory: BeanFactory, implementBeanNames: Array<String> = arrayOf()
) : Proxy() { ) : Proxy() {
private var repo: IRepository<*, *>? = null private var mongodbRepo: Any
private var query: IQuery<*, *>? = null
init { init {
// 判断接口类型
val clazz: Class<*> // 代理服务类型
val interfaceClazz: Class<*> // 代理服务接口
if (this.supertype.interfaces.any { it == IRepository::class.java }) { if (this.supertype.interfaces.any { it == IRepository::class.java }) {
// 如果是IRepository子接口 clazz = MongodbRepository::class.java
if (implementBeanNames.isEmpty()) { interfaceClazz = IRepository::class.java
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<*, *>
}
} else { } else {
// 否则是IQuery子接口 clazz = MongodbQuery::class.java
if (implementBeanNames.isEmpty()) { interfaceClazz = IQuery::class.java
val genericInterfaces = this.supertype.genericInterfaces.find { }
it.typeName.startsWith(IQuery::class.java.typeName)
}!! if (implementBeanNames.isEmpty()) {
val constructor = MongodbQuery::class.java.getConstructor( val constructor = clazz.getConstructor(Class::class.java, MongoTemplate::class.java)
Class::class.java, MongoTemplate::class.java, ILogger::class.java this.mongodbRepo = constructor.newInstance(
) this.supertype.getGenericInterface(interfaceClazz)!!.actualTypeArguments[0],
this.query = constructor.newInstance( this.beanFactory.getBean(MongoTemplate::class.java)
(genericInterfaces as ParameterizedType).actualTypeArguments[0], )
this.beanFactory.getBean(MongoTemplate::class.java), } else {
this.beanFactory.getBean(ILogger::class.java), this.mongodbRepo = this.beanFactory.getBean(implementBeanNames[0])
)
} else {
this.query = this.beanFactory.getBean(implementBeanNames[0]) as IQuery<*, *>
}
} }
} }
@@ -66,18 +48,11 @@ class MongodbRepoProxy(
* @return 方法执行结果 * @return 方法执行结果
*/ */
override fun exec(proxy: Any, method: Method, args: Array<Any>): Any? { override fun exec(proxy: Any, method: Method, args: Array<Any>): 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 { try {
val proxyMethod: Method = proxyClazz.getDeclaredMethod(method.name, *method.parameterTypes) val proxyMethod: Method = this.mongodbRepo.javaClass.getMethod(method.name, *method.parameterTypes)
return proxyMethod.invoke(this.repo, *args) return proxyMethod.invoke(this.mongodbRepo, *args)
} catch (ex: NoSuchMethodException) { } 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}] ")
} }
} }

View File

@@ -2,7 +2,6 @@ package com.synebula.gaea.mongodb.query
import com.synebula.gaea.ext.fieldNames import com.synebula.gaea.ext.fieldNames
import com.synebula.gaea.ext.firstCharLowerCase import com.synebula.gaea.ext.firstCharLowerCase
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.mongodb.order import com.synebula.gaea.mongodb.order
import com.synebula.gaea.mongodb.select import com.synebula.gaea.mongodb.select
import com.synebula.gaea.mongodb.where import com.synebula.gaea.mongodb.where
@@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query
* @param template MongodbRepo对象 * @param template MongodbRepo对象
*/ */
open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var template: MongoTemplate, var logger: ILogger) : open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var template: MongoTemplate) :
IQuery<TView, ID> { IQuery<TView, ID> {
/** /**
@@ -85,18 +84,13 @@ open class MongodbQuery<TView, ID>(override var clazz: Class<TView>, var templat
/** /**
* 获取collection * 获取collection
*/ */
protected fun collection(clazz: Class<TView>): String { fun <TView> collection(clazz: Class<TView>): String {
val table: Table? = clazz.getDeclaredAnnotation( val table = clazz.getDeclaredAnnotation(Table::class.java)
Table::class.java return if (table != null) table.name
)
return if (table != null) return table.name
else { else {
this.logger.info(this, "视图类没有标记[Collection]注解无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase() val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.template.collectionExists(name)) name if (!validViewCollection || this.template.collectionExists(name)) name
else { else throw RuntimeException("找不到名为[${clazz.name}]的集合")
throw RuntimeException("找不到名为[${clazz.name}]的集合")
}
} }
} }
} }

View File

@@ -3,7 +3,6 @@ package com.synebula.gaea.mongodb.query
import com.synebula.gaea.ext.fieldNames import com.synebula.gaea.ext.fieldNames
import com.synebula.gaea.ext.firstCharLowerCase import com.synebula.gaea.ext.firstCharLowerCase
import com.synebula.gaea.log.ILogger
import com.synebula.gaea.mongodb.order import com.synebula.gaea.mongodb.order
import com.synebula.gaea.mongodb.select import com.synebula.gaea.mongodb.select
import com.synebula.gaea.mongodb.where import com.synebula.gaea.mongodb.where
@@ -20,7 +19,7 @@ import org.springframework.data.mongodb.core.query.Query
* 实现IQuery的Mongodb查询类 * 实现IQuery的Mongodb查询类
* @param template MongodbRepo对象 * @param template MongodbRepo对象
*/ */
open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogger) : IUniversalQuery { open class MongodbUniversalQuery(var template: MongoTemplate) : IUniversalQuery {
/** /**
* 使用View解析是collection时是否校验存在默认不校验 * 使用View解析是collection时是否校验存在默认不校验
@@ -85,19 +84,12 @@ open class MongodbUniversalQuery(var template: MongoTemplate, var logger: ILogge
* 获取collection * 获取collection
*/ */
fun <TView> collection(clazz: Class<TView>): String { fun <TView> collection(clazz: Class<TView>): String {
val table: Table? = clazz.getDeclaredAnnotation( val table = clazz.getDeclaredAnnotation(Table::class.java)
Table::class.java return if (table != null) table.name
)
return if (table != null)
return table.name
else { else {
this.logger.info(this, "视图类没有标记[Collection]注解无法获取Collection名称。尝试使用View<${clazz.name}>名称解析集合")
val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase() val name = clazz.simpleName.removeSuffix("View").firstCharLowerCase()
if (!validViewCollection || this.template.collectionExists(name)) if (!validViewCollection || this.template.collectionExists(name)) name
name else throw RuntimeException("找不到名为[${clazz.name}]的集合")
else {
throw RuntimeException("找不到名为[${clazz.name}]的集合")
}
} }
} }
} }

View File

@@ -13,7 +13,7 @@
*/ */
package com.synebula.gaea.bus package com.synebula.gaea.bus
import com.synebula.gaea.reflect.Types import com.synebula.gaea.reflect.supertypes
import java.lang.reflect.Method import java.lang.reflect.Method
import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.CopyOnWriteArraySet import java.util.concurrent.CopyOnWriteArraySet
@@ -184,7 +184,7 @@ open class SubscriberRegistry<T : Any>(private val bus: IBus<T>) {
fun flattenHierarchy(clazz: Class<*>): Set<Class<*>> { fun flattenHierarchy(clazz: Class<*>): Set<Class<*>> {
var supertypes = flattenHierarchyCache[clazz] var supertypes = flattenHierarchyCache[clazz]
if (supertypes == null) { if (supertypes == null) {
supertypes = Types.supertypes(clazz) supertypes = clazz.supertypes()
flattenHierarchyCache[clazz] = supertypes flattenHierarchyCache[clazz] = supertypes
} }
return supertypes return supertypes

View File

@@ -11,7 +11,7 @@ import com.synebula.gaea.domain.repository.IRepository
* 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象 * 该类依赖仓储接口 @see IGenericsRepository, 需要显式提供聚合根的class对象
* *
* @param clazz 聚合根类对象 * @param clazz 聚合根类对象
* @param repo 仓储对象 * @param repository 仓储对象
* @param mapper 对象转换组件 * @param mapper 对象转换组件
* @author alex * @author alex
* @version 0.1 * @version 0.1
@@ -19,11 +19,10 @@ import com.synebula.gaea.domain.repository.IRepository
*/ */
open class Service<TRoot : IAggregateRoot<ID>, ID>( open class Service<TRoot : IAggregateRoot<ID>, ID>(
protected open var clazz: Class<TRoot>, protected open var clazz: Class<TRoot>,
protected open var repo: IRepository<TRoot, ID>, protected open var repository: IRepository<TRoot, ID>,
protected open var mapper: IObjectMapper, protected open var mapper: IObjectMapper,
) : IService<ID> { ) : IService<ID> {
/** /**
* 增加对象 * 增加对象
* *
@@ -32,7 +31,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
override fun add(command: ICommand): DataMessage<ID> { override fun add(command: ICommand): DataMessage<ID> {
val msg = DataMessage<ID>() val msg = DataMessage<ID>()
val root = this.map(command) val root = this.map(command)
this.repo.add(root) this.repository.add(root)
msg.data = root.id msg.data = root.id
return msg return msg
} }
@@ -44,7 +43,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
*/ */
override fun add(commands: List<ICommand>) { override fun add(commands: List<ICommand>) {
val roots = commands.map { this.map(it) } val roots = commands.map { this.map(it) }
this.repo.add(roots) this.repository.add(roots)
} }
/** /**
@@ -56,7 +55,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
override fun update(id: ID, command: ICommand) { override fun update(id: ID, command: ICommand) {
val root = this.map(command) val root = this.map(command)
root.id = id root.id = id
this.repo.update(root) this.repository.update(root)
} }
/** /**
@@ -66,7 +65,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
*/ */
override fun update(commands: List<ICommand>) { override fun update(commands: List<ICommand>) {
val roots = commands.map { this.map(it) } val roots = commands.map { this.map(it) }
this.repo.update(roots) this.repository.update(roots)
} }
/** /**
@@ -74,7 +73,7 @@ open class Service<TRoot : IAggregateRoot<ID>, ID>(
* @param id 对象ID * @param id 对象ID
*/ */
override fun remove(id: ID) { override fun remove(id: ID) {
this.repo.remove(id) this.repository.remove(id)
} }
/** /**

View File

@@ -1,21 +1,28 @@
package com.synebula.gaea.reflect package com.synebula.gaea.reflect
object Types { import java.lang.reflect.ParameterizedType
/**
* 获取类的所有父类型。 /**
*/ * 获取类的所有父类型。
fun supertypes(clazz: Class<*>): Set<Class<*>> { */
val supertypes = mutableSetOf<Class<*>>() fun Class<*>.supertypes(): Set<Class<*>> {
supertypes.add(clazz) val supertypes = mutableSetOf<Class<*>>()
if (clazz.interfaces.isNotEmpty()) { supertypes.add(this)
supertypes.addAll(clazz.interfaces.map { supertypes(it) }.reduce { r, c -> if (this.interfaces.isNotEmpty()) {
val all = r.toMutableSet() supertypes.addAll(this.interfaces.map { it.supertypes() }.reduce { r, c ->
all.addAll(c) val all = r.toMutableSet()
all all.addAll(c)
}) all
} })
if (clazz.superclass != null)
supertypes.addAll(supertypes(clazz.superclass))
return supertypes
} }
} 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
}