From a0cfba5dd986c919631a9263c7b90df01a8a0890 Mon Sep 17 00:00:00 2001 From: alex Date: Wed, 27 Jul 2022 15:23:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9ID=E6=B3=9B=E5=9E=8B=E5=90=8D?= =?UTF-8?q?=E7=A7=B0=EF=BC=8C=E9=87=8D=E6=9E=84Serialization=E5=86=85?= =?UTF-8?q?=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/synebula/gaea/app/Application.kt | 7 +-- ...azyApplication.kt => SimpleApplication.kt} | 22 ++++++--- .../com/synebula/gaea/app/cmd/CommandApp.kt | 6 +-- .../com/synebula/gaea/app/cmd/ICommandApp.kt | 8 +-- ...LazyCommandApp.kt => ISimpleCommandApp.kt} | 10 ++-- ...{LazyCommandApp.kt => SimpleCommandApp.kt} | 8 +-- .../gaea/app/component/DozerConverter.kt | 6 +-- .../synebula/gaea/app/component/poi/Excel.kt | 4 +- .../app/component/security/WebSecurity.kt | 49 ++++++++++--------- .../com/synebula/gaea/app/query/IQueryApp.kt | 4 +- .../com/synebula/gaea/app/query/QueryApp.kt | 4 +- .../com/synebula/gaea/mongo/MongoExt.kt | 2 +- .../synebula/gaea/mongo/query/MongodbQuery.kt | 2 +- .../mongo/repository/MongodbRepository.kt | 19 ++++--- .../synebula/gaea/data/IObjectConverter.kt | 21 -------- .../synebula/gaea/data/message/DataMessage.kt | 2 +- .../synebula/gaea/data/message/IMessage.kt | 16 ++++++ .../message/{Message.kt => StatusMessage.kt} | 2 +- .../gaea/data/serialization/IDeserializer.kt | 7 +-- .../gaea/data/serialization/IObjectMapper.kt | 10 ++++ .../gaea/data/serialization/ISerializer.kt | 8 +-- .../serialization/json/IJsonDeserializer.kt | 13 ++--- .../serialization/json/IJsonSerializer.kt | 13 ++--- .../gaea/domain/model/AggregateRoot.kt | 2 +- .../com/synebula/gaea/domain/model/Entity.kt | 2 +- .../gaea/domain/model/IAggregateRoot.kt | 2 +- .../com/synebula/gaea/domain/model/IEntity.kt | 6 +-- .../com/synebula/gaea/domain/record/Record.kt | 2 +- .../gaea/domain/repository/IRepository.kt | 10 ++-- .../domain/repository/ISpecificRepository.kt | 15 ++---- .../domain/repository/context/IContext.kt | 6 +-- .../synebula/gaea/domain/service/IService.kt | 12 ++--- .../{ILazyService.kt => ISimpleService.kt} | 12 ++--- .../synebula/gaea/domain/service/Service.kt | 40 +++++++-------- .../{LazyService.kt => SimpleService.kt} | 22 ++++----- .../kotlin/com/synebula/gaea/query/IQuery.kt | 2 +- .../kotlin/com/synebula/gaea/reflect/Types.kt | 23 +++++++++ 37 files changed, 210 insertions(+), 189 deletions(-) rename src/gaea.app/src/main/kotlin/com/synebula/gaea/app/{LazyApplication.kt => SimpleApplication.kt} (50%) rename src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/{ILazyCommandApp.kt => ISimpleCommandApp.kt} (78%) rename src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/{LazyCommandApp.kt => SimpleCommandApp.kt} (71%) delete mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/data/IObjectConverter.kt create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/data/message/IMessage.kt rename src/gaea/src/main/kotlin/com/synebula/gaea/data/message/{Message.kt => StatusMessage.kt} (92%) create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IObjectMapper.kt rename src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/{ILazyService.kt => ISimpleService.kt} (68%) rename src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/{LazyService.kt => SimpleService.kt} (78%) create mode 100644 src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/Application.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/Application.kt index 3340151..759905e 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/Application.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/Application.kt @@ -13,17 +13,18 @@ import javax.annotation.Resource * 联合服务,同时实现了ICommandApp和IQueryApp接口 * * @param name 业务名称 + * @param clazz 试图业务对象 * @param service 业务domain服务 * @param query 业务查询服务 * @param logger 日志组件 */ -open class Application( +open class Application( override var name: String, override var clazz: Class, - override var service: IService, + override var service: IService, override var query: IQuery, override var logger: ILogger, -) : ICommandApp, IQueryApp { +) : ICommandApp, IQueryApp { @Resource override var jsonSerializer: IJsonSerializer? = null diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/SimpleApplication.kt similarity index 50% rename from src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt rename to src/gaea.app/src/main/kotlin/com/synebula/gaea/app/SimpleApplication.kt index 59c127b..de5dc96 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/LazyApplication.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/SimpleApplication.kt @@ -1,30 +1,38 @@ package com.synebula.gaea.app -import com.synebula.gaea.app.cmd.ILazyCommandApp +import com.google.common.reflect.TypeToken +import com.synebula.gaea.app.cmd.ISimpleCommandApp import com.synebula.gaea.app.query.IQueryApp import com.synebula.gaea.data.serialization.json.IJsonSerializer +import com.synebula.gaea.domain.model.AggregateRoot import com.synebula.gaea.domain.model.IAggregateRoot -import com.synebula.gaea.domain.service.ILazyService +import com.synebula.gaea.domain.service.ISimpleService import com.synebula.gaea.log.ILogger import com.synebula.gaea.query.IQuery import javax.annotation.Resource /** - * 联合服务,同时实现了ILazyCommandApp和IQueryApp接口 + * 简单的服务, 取消了Command对象 * * @param name 业务名称 + * @param clazz 业务对象类型 * @param service 业务domain服务 * @param query 业务查询服务 * @param logger 日志组件 */ -open class LazyApplication, TKey>( +open class SimpleApplication, ID>( override var name: String, - override var clazz: Class, //view class type - override var service: ILazyService, + override var clazz: Class, + override var service: ISimpleService, override var query: IQuery, override var logger: ILogger, -) : ILazyCommandApp, IQueryApp { +) : ISimpleCommandApp, IQueryApp { @Resource override var jsonSerializer: IJsonSerializer? = null +} + +fun main() { + val rawType = TypeToken.of(AggregateRoot::class.java).types.rawTypes() + println(rawType) } \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/CommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/CommandApp.kt index b461115..516dd1a 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/CommandApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/CommandApp.kt @@ -13,11 +13,11 @@ import javax.annotation.Resource * @param service 业务domain服务 * @param logger 日志组件 */ -open class CommandApp( +open class CommandApp( override var name: String, - override var service: IService, + override var service: IService, override var logger: ILogger, -) : ICommandApp { +) : ICommandApp { @Resource override var jsonSerializer: IJsonSerializer? = null } \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt index 9976caf..d92374d 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ICommandApp.kt @@ -16,10 +16,10 @@ import org.springframework.web.bind.annotation.* * @version 0.1 * @since 2020-05-15 */ -interface ICommandApp : IApplication { +interface ICommandApp : IApplication { var jsonSerializer: IJsonSerializer? - var service: IService + var service: IService @PostMapping @MethodName("添加") @@ -29,14 +29,14 @@ interface ICommandApp : IApplication { @PutMapping("/{id:.+}") @MethodName("更新") - fun update(@PathVariable id: TKey, @RequestBody command: TCommand): HttpMessage { + fun update(@PathVariable id: ID, @RequestBody command: TCommand): HttpMessage { this.service.update(id, command) return HttpMessage() } @DeleteMapping("/{id:.+}") @MethodName("删除") - fun remove(@PathVariable id: TKey): HttpMessage { + fun remove(@PathVariable id: ID): HttpMessage { val msg = HttpMessage() try { msg.data = this.service.remove(id) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ISimpleCommandApp.kt similarity index 78% rename from src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt rename to src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ISimpleCommandApp.kt index b3e1aec..bc4451b 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ILazyCommandApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/ISimpleCommandApp.kt @@ -6,7 +6,7 @@ import com.synebula.gaea.app.struct.HttpMessage import com.synebula.gaea.data.message.Status import com.synebula.gaea.data.serialization.json.IJsonSerializer import com.synebula.gaea.domain.model.IAggregateRoot -import com.synebula.gaea.domain.service.ILazyService +import com.synebula.gaea.domain.service.ISimpleService import org.springframework.web.bind.annotation.* /** @@ -16,10 +16,10 @@ import org.springframework.web.bind.annotation.* * @version 0.1 * @since 2020-05-15 */ -interface ILazyCommandApp, TKey> : IApplication { +interface ISimpleCommandApp, ID> : IApplication { var jsonSerializer: IJsonSerializer? - var service: ILazyService + var service: ISimpleService @PostMapping @MethodName("添加") @@ -29,14 +29,14 @@ interface ILazyCommandApp, TKey> : IApplication { @PutMapping("/{id:.+}") @MethodName("更新") - fun update(@PathVariable id: TKey, @RequestBody entity: TRoot): HttpMessage { + fun update(@PathVariable id: ID, @RequestBody entity: TRoot): HttpMessage { this.service.update(id, entity) return HttpMessage() } @DeleteMapping("/{id:.+}") @MethodName("删除") - fun remove(@PathVariable id: TKey): HttpMessage { + fun remove(@PathVariable id: ID): HttpMessage { val msg = HttpMessage() try { msg.data = this.service.remove(id) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/SimpleCommandApp.kt similarity index 71% rename from src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt rename to src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/SimpleCommandApp.kt index 79f5138..84c881f 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/LazyCommandApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/cmd/SimpleCommandApp.kt @@ -2,7 +2,7 @@ package com.synebula.gaea.app.cmd import com.synebula.gaea.data.serialization.json.IJsonSerializer import com.synebula.gaea.domain.model.IAggregateRoot -import com.synebula.gaea.domain.service.ILazyService +import com.synebula.gaea.domain.service.ISimpleService import com.synebula.gaea.log.ILogger import javax.annotation.Resource @@ -13,11 +13,11 @@ import javax.annotation.Resource * @param service 业务domain服务 * @param logger 日志组件 */ -open class LazyCommandApp, TKey>( +open class SimpleCommandApp, ID>( override var name: String, - override var service: ILazyService, + override var service: ISimpleService, override var logger: ILogger, -) : ILazyCommandApp { +) : ISimpleCommandApp { @Resource override var jsonSerializer: IJsonSerializer? = null } \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/DozerConverter.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/DozerConverter.kt index a2f7421..66dd9f5 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/DozerConverter.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/DozerConverter.kt @@ -1,12 +1,12 @@ package com.synebula.gaea.app.component -import com.synebula.gaea.data.IObjectConverter +import com.synebula.gaea.data.serialization.IObjectMapper import org.dozer.DozerBeanMapper import org.springframework.stereotype.Component @Component -class DozerConverter : IObjectConverter { +class DozerConverter : IObjectMapper { private val converter = DozerBeanMapper() - override fun convert(src: Any, dest: Class): T = converter.map(src, dest) + override fun deserialize(src: Any, targetClass: Class): T = converter.map(src, targetClass) } \ No newline at end of file diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt index b8e2626..923dbd0 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/poi/Excel.kt @@ -45,7 +45,7 @@ object Excel { //声明列对象 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制 var row = sheet.createRow(0) - row.height = 25 * 20 + row.height = (25 * 20).toShort() var cell: HSSFCell //创建标题 for (col in data.columnNames.indices) { @@ -69,7 +69,7 @@ object Excel { for (i in data.data.indices) { try { row = sheet.createRow(i + 1) - row.height = 20 * 20 + row.height = (20 * 20).toShort() col = 0 while (col < data.data[i].size) { cell = row.createCell(col) diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/security/WebSecurity.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/security/WebSecurity.kt index c0f6f3c..fa6208f 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/security/WebSecurity.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/component/security/WebSecurity.kt @@ -4,11 +4,11 @@ import com.synebula.gaea.app.struct.HttpMessage import com.synebula.gaea.data.message.Status import org.springframework.beans.factory.annotation.Autowired import org.springframework.context.annotation.Bean +import org.springframework.security.authentication.AuthenticationManager import org.springframework.security.config.annotation.web.builders.HttpSecurity -import org.springframework.security.config.annotation.web.builders.WebSecurity -import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity -import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter +import org.springframework.security.config.annotation.web.configuration.WebSecurityCustomizer import org.springframework.security.config.http.SessionCreationPolicy +import org.springframework.security.web.SecurityFilterChain import org.springframework.stereotype.Component import org.springframework.web.cors.CorsConfiguration import org.springframework.web.cors.CorsConfigurationSource @@ -16,38 +16,41 @@ import org.springframework.web.cors.UrlBasedCorsConfigurationSource @Component -@EnableWebSecurity -class WebSecurity : WebSecurityConfigurerAdapter() { +class WebSecurity { @Autowired lateinit var tokenManager: TokenManager + @Autowired + lateinit var authenticationManager: AuthenticationManager + /** * 安全配置 */ @Throws(Exception::class) - override fun configure(http: HttpSecurity) { + fun filterChain(http: HttpSecurity): SecurityFilterChain { // 跨域共享 http.cors() - .and().csrf().disable() // 跨域伪造请求限制无效 - .authorizeRequests() - .anyRequest().authenticated()// 资源任何人都可访问 - .and() - .addFilter(WebAuthorization(authenticationManager(), tokenManager))// 添加JWT鉴权拦截器 - .sessionManagement() - .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 设置Session的创建策略为:Spring Security永不创建HttpSession 不使用HttpSession来获取SecurityContext - .and() - .exceptionHandling() - .authenticationEntryPoint { _, response, _ -> - response.status = Status.Success - response.characterEncoding = "utf-8" - response.contentType = "text/javascript;charset=utf-8" - response.writer.print(HttpMessage(Status.Unauthorized, "用户未登录,请重新登录后尝试!")) - } + .and().csrf().disable() // 跨域伪造请求限制无效 + .authorizeRequests() + .anyRequest().authenticated()// 资源任何人都可访问 + .and() + .addFilter(WebAuthorization(authenticationManager, tokenManager))// 添加JWT鉴权拦截器 + .sessionManagement() + .sessionCreationPolicy(SessionCreationPolicy.STATELESS) // 设置Session的创建策略为:Spring Security永不创建HttpSession 不使用HttpSession来获取SecurityContext + .and() + .exceptionHandling() + .authenticationEntryPoint { _, response, _ -> + response.status = Status.Success + response.characterEncoding = "utf-8" + response.contentType = "text/javascript;charset=utf-8" + response.writer.print(HttpMessage(Status.Unauthorized, "用户未登录,请重新登录后尝试!")) + } + return http.build() } @Throws(Exception::class) - override fun configure(web: WebSecurity) { - web.ignoring().antMatchers("/sign/**") + fun filterChain(): WebSecurityCustomizer { + return WebSecurityCustomizer { web -> web.ignoring().antMatchers("/sign/**") } } /** diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt index ff5ded0..dda9478 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/IQueryApp.kt @@ -9,7 +9,7 @@ import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestParam -interface IQueryApp : IApplication { +interface IQueryApp : IApplication { /** * 查询服务 */ @@ -22,7 +22,7 @@ interface IQueryApp : IApplication { @MethodName("获取数据") @GetMapping("/{id:.+}") - fun get(@PathVariable id: TKey): HttpMessage { + fun get(@PathVariable id: ID): HttpMessage { val data = this.query.get(id, clazz) val msg = HttpMessage() msg.data = data diff --git a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt index fc18d1c..729f48c 100644 --- a/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt +++ b/src/gaea.app/src/main/kotlin/com/synebula/gaea/app/query/QueryApp.kt @@ -10,9 +10,9 @@ import com.synebula.gaea.query.IQuery * @param query 业务查询服务 * @param logger 日志组件 */ -open class QueryApp( +open class QueryApp( override var name: String, override var clazz: Class, override var query: IQuery, override var logger: ILogger, -) : IQueryApp \ No newline at end of file +) : IQueryApp \ No newline at end of file diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt index c75d204..ecf65cd 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/MongoExt.kt @@ -116,7 +116,7 @@ fun Query.where(params: Map?, clazz: Class<*>): Query { * * @param id 业务ID */ -fun whereId(id: TKey): Query = Query.query(Criteria.where("_id").`is`(id)) +fun whereId(id: ID): Query = Query.query(Criteria.where("_id").`is`(id)) /** * 获取排序对象 diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongodbQuery.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongodbQuery.kt index b610d41..fe117cb 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongodbQuery.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/query/MongodbQuery.kt @@ -27,7 +27,7 @@ open class MongodbQuery(var template: MongoTemplate, var logger: ILogger) : IQue */ var validViewCollection = false - override fun get(id: TKey, clazz: Class): TView? { + override fun get(id: ID, clazz: Class): TView? { return this.template.findOne(whereId(id), clazz, this.collection(clazz)) } diff --git a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongodbRepository.kt b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongodbRepository.kt index fedd944..4c2b469 100644 --- a/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongodbRepository.kt +++ b/src/gaea.mongo/src/main/kotlin/com/synebula/gaea/mongo/repository/MongodbRepository.kt @@ -13,29 +13,32 @@ import org.springframework.data.mongodb.core.query.Query */ open class MongodbRepository(private var repo: MongoTemplate) : IRepository { - override fun , TKey> remove(id: TKey, clazz: Class) { + override fun , ID> remove(id: ID, clazz: Class) { this.repo.remove(whereId(id), clazz) } - override fun , TKey> get( - id: TKey, + override fun , ID> get( + id: ID, clazz: Class, ): TAggregateRoot? { return this.repo.findOne(whereId(id), clazz) } - override fun , TKey> update( - obj: TAggregateRoot, - clazz: Class + override fun , ID> update( + obj: TAggregateRoot, + clazz: Class, ) { this.repo.save(obj) } - override fun , TKey> add(obj: TAggregateRoot, clazz: Class) { + override fun , ID> add(obj: TAggregateRoot, clazz: Class) { this.repo.save(obj) } - override fun , TKey> add(obj: List, clazz: Class) { + override fun , ID> add( + obj: List, + clazz: Class, + ) { this.repo.insert(obj, clazz) } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/IObjectConverter.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/IObjectConverter.kt deleted file mode 100644 index 3deb5f2..0000000 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/IObjectConverter.kt +++ /dev/null @@ -1,21 +0,0 @@ -package com.synebula.gaea.data - -/** - * 对象转换器,支持对象之间的转换。 - * - * @author alex - * @version 0.1 - * @since 2020-05-15 - */ -interface IObjectConverter { - - /** - * 转换源对象到目标对象。 - * - * @param src 源对象。 - * @param dest 目标对象。 - * @param 目标对象类型。 - * @return 目标对象 - */ - fun convert(src: Any, dest: Class): T -} diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/DataMessage.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/DataMessage.kt index 96bd46e..ef129e1 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/DataMessage.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/DataMessage.kt @@ -8,7 +8,7 @@ import java.util.* * * @param T 消息数据类型 */ -open class DataMessage() : Message() { +open class DataMessage() : StatusMessage() { /** * 传递的业务数据 diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/IMessage.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/IMessage.kt new file mode 100644 index 0000000..dbf332d --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/IMessage.kt @@ -0,0 +1,16 @@ +package com.synebula.gaea.data.message + +/** + * 消息结构 + */ +interface IMessage { + /** + * 命令载荷, 实际的业务数据 + */ + var message: String + + /** + * 时间戳。 + */ + var timestamp: Long +} \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Message.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/StatusMessage.kt similarity index 92% rename from src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Message.kt rename to src/gaea/src/main/kotlin/com/synebula/gaea/data/message/StatusMessage.kt index c003dca..a0a960b 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/Message.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/message/StatusMessage.kt @@ -1,6 +1,6 @@ package com.synebula.gaea.data.message -open class Message { +open class StatusMessage { /** * 状态。200成功,400错误,500异常 */ diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IDeserializer.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IDeserializer.kt index e0d170d..d6efe44 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IDeserializer.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IDeserializer.kt @@ -2,16 +2,17 @@ package com.synebula.gaea.data.serialization /** * 序列化器 + * @param 源数据类型 */ -interface IDeserializer { +interface IDeserializer { /** * 反序列化 * - * @param 源数据类型 * @param 目标数据类型 * @param src 源数据 + * @param targetClass 目标对象。 * @return 目标数据 */ - fun deserialize(src: S): T + fun deserialize(src: S, targetClass: Class): T } \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IObjectMapper.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IObjectMapper.kt new file mode 100644 index 0000000..4ddbb65 --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/IObjectMapper.kt @@ -0,0 +1,10 @@ +package com.synebula.gaea.data.serialization + +/** + * 对象映射器,支持对象之间的转换。 + * + * @author alex + * @version 0.1 + * @since 2020-05-15 + */ +interface IObjectMapper : IDeserializer diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/ISerializer.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/ISerializer.kt index 925c518..e3ca229 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/ISerializer.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/ISerializer.kt @@ -2,16 +2,16 @@ package com.synebula.gaea.data.serialization /** * 序列化器 + * @param 目标数据类型 */ -interface ISerializer { +interface ISerializer { /** * 序列化 - * * @param 源数据类型 - * @param 目标数据类型 + * * @param src 源数据 * @return 目标数据 */ - fun serialize(src: S): T + fun serialize(src: S): T } \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonDeserializer.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonDeserializer.kt index da7c13b..09acb01 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonDeserializer.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonDeserializer.kt @@ -1,15 +1,8 @@ package com.synebula.gaea.data.serialization.json +import com.synebula.gaea.data.serialization.IDeserializer + /** * 序列化器 */ -interface IJsonDeserializer { - /** - * 反序列化 - * - * @param 目标数据类型 - * @param src Json字符串数据 - * @return 目标数据 - */ - fun deserialize(src: String): T -} \ No newline at end of file +interface IJsonDeserializer : IDeserializer \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonSerializer.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonSerializer.kt index e154644..9130125 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonSerializer.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/data/serialization/json/IJsonSerializer.kt @@ -1,15 +1,8 @@ package com.synebula.gaea.data.serialization.json +import com.synebula.gaea.data.serialization.ISerializer + /** * 序列化器 */ -interface IJsonSerializer { - /** - * 序列化 - * - * @param 源数据类型 - * @param src 源数据 - * @return Json字符串 - */ - fun serialize(src: S): String -} \ No newline at end of file +interface IJsonSerializer : ISerializer \ No newline at end of file diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/AggregateRoot.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/AggregateRoot.kt index c1bb7fb..3f7962f 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/AggregateRoot.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/AggregateRoot.kt @@ -1,5 +1,5 @@ package com.synebula.gaea.domain.model -abstract class AggregateRoot : Entity(), IAggregateRoot { +abstract class AggregateRoot : Entity(), IAggregateRoot { override var alive: Boolean = true } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/Entity.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/Entity.kt index 95d3fe8..8aa77b7 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/Entity.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/Entity.kt @@ -1,3 +1,3 @@ package com.synebula.gaea.domain.model -abstract class Entity : IEntity +abstract class Entity : IEntity diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IAggregateRoot.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IAggregateRoot.kt index b442100..2222405 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IAggregateRoot.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IAggregateRoot.kt @@ -5,7 +5,7 @@ package com.synebula.gaea.domain.model * * @author alex **/ -interface IAggregateRoot : IEntity { +interface IAggregateRoot : IEntity { /** * 实体对象是否有效。 diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IEntity.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IEntity.kt index 3016f06..ea99e25 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IEntity.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/model/IEntity.kt @@ -5,13 +5,13 @@ package com.synebula.gaea.domain.model * * @author alex * - * @param 主键的类型。 + * @param 主键的类型。 **/ -interface IEntity { +interface IEntity { /** * 实体ID */ - var id: TKey? + var id: ID? } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/record/Record.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/record/Record.kt index 61ad642..b6ae3c2 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/record/Record.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/record/Record.kt @@ -7,7 +7,7 @@ import java.util.* * 记录信息 * 添加了创建和修改的人\时间信息 */ -abstract class Record { +abstract class Record { //记录增加信息 var creator: String? = null diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt index 8c653ef..67d4ea2 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/IRepository.kt @@ -13,7 +13,7 @@ interface IRepository { * @param obj 需要插入的对象。 * @return 返回原对象,如果对象ID为自增,则补充自增ID。 */ - fun , TKey> add(obj: TAggregateRoot, clazz: Class) + fun , ID> add(obj: TAggregateRoot, clazz: Class) /** * 插入多个个对象。 @@ -21,7 +21,7 @@ interface IRepository { * @param obj 需要插入的对象。 * @return 返回原对象,如果对象ID为自增,则补充自增ID。 */ - fun , TKey> add(obj: List, clazz: Class) + fun , ID> add(obj: List, clazz: Class) /** * 更新对象。 @@ -29,7 +29,7 @@ interface IRepository { * @param obj 需要更新的对象。 * @return */ - fun , TKey> update(obj: TAggregateRoot, clazz: Class) + fun , ID> update(obj: TAggregateRoot, clazz: Class) /** * 通过id删除该条数据 @@ -37,7 +37,7 @@ interface IRepository { * @param id id * @param clazz 操作数据的类型 */ - fun , TKey> remove(id: TKey, clazz: Class) + fun , ID> remove(id: ID, clazz: Class) /** * 根据ID获取对象。 @@ -46,7 +46,7 @@ interface IRepository { * @param clazz 操作数据的类型 * @return 聚合根 */ - fun , TKey> get(id: TKey, clazz: Class): TAggregateRoot? + fun , ID> get(id: ID, clazz: Class): TAggregateRoot? /** diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt index 06e8564..d15ec31 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/ISpecificRepository.kt @@ -10,7 +10,7 @@ import com.synebula.gaea.domain.model.IAggregateRoot * @param this T is the parameter * @author alex */ -interface ISpecificRepository, TKey> { +interface ISpecificRepository, ID> { /** * 仓储的对象类 @@ -39,7 +39,7 @@ interface ISpecificRepository, TKey> { * @param id * @return */ - fun remove(id: TKey) + fun remove(id: ID) /** * 根据ID获取对象。 @@ -47,16 +47,7 @@ interface ISpecificRepository, TKey> { * @param id 对象ID。 * @return */ - fun get(id: TKey): TAggregateRoot - - /** - * 根据ID获取对象。 - * - * @param id id - * @param clazz 操作数据的类型 - * @return 聚合根 - */ - fun , TKey> get(id: TKey, clazz: Class): T + fun get(id: ID): TAggregateRoot /** diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/context/IContext.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/context/IContext.kt index 9c4cb5a..f585580 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/context/IContext.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/repository/context/IContext.kt @@ -12,19 +12,19 @@ interface IContext : IUnitOfWork { * 将指定的聚合根标注为“新建”状态。 * @param obj */ - fun , TKey> add(obj: TType) + fun , ID> add(obj: TType) /** * 将指定的聚合根标注为“更改”状态。 * * @param obj */ - fun , TKey> update(obj: TType) + fun , ID> update(obj: TType) /** * 将指定的聚合根标注为“删除”状态。 * * @param obj */ - fun , TKey> remove(obj: TType) + fun , ID> remove(obj: TType) } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt index 46d39a1..6cabe08 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/IService.kt @@ -1,7 +1,7 @@ package com.synebula.gaea.domain.service import com.synebula.gaea.data.message.DataMessage -import com.synebula.gaea.data.message.Message +import com.synebula.gaea.data.message.StatusMessage import com.synebula.gaea.log.ILogger @@ -11,24 +11,24 @@ import com.synebula.gaea.log.ILogger * @version 0.0.1 * @since 2016年9月18日 下午2:23:15 */ -interface IService { +interface IService { /** * 日志组件。 */ var logger: ILogger - fun add(command: ICommand): DataMessage + fun add(command: ICommand): DataMessage - fun update(id: TKey, command: ICommand) + fun update(id: ID, command: ICommand) - fun remove(id: TKey) + fun remove(id: ID) /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) + fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) /** * 移除一个删除对象前执行监听器。 diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ISimpleService.kt similarity index 68% rename from src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt rename to src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ISimpleService.kt index 2d2efec..6d8fbff 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ILazyService.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/ISimpleService.kt @@ -1,7 +1,7 @@ package com.synebula.gaea.domain.service import com.synebula.gaea.data.message.DataMessage -import com.synebula.gaea.data.message.Message +import com.synebula.gaea.data.message.StatusMessage import com.synebula.gaea.domain.model.IAggregateRoot import com.synebula.gaea.log.ILogger @@ -12,24 +12,24 @@ import com.synebula.gaea.log.ILogger * @version 0.0.1 * @since 2016年9月18日 下午2:23:15 */ -interface ILazyService, TKey> { +interface ISimpleService, ID> { /** * 日志组件。 */ var logger: ILogger - fun add(root: TAggregateRoot): DataMessage + fun add(root: TAggregateRoot): DataMessage - fun update(id: TKey, root: TAggregateRoot) + fun update(id: ID, root: TAggregateRoot) - fun remove(id: TKey) + fun remove(id: ID) /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) + fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) /** * 移除一个删除对象前执行监听器。 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 c68a1f1..eee85ec 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 @@ -1,8 +1,8 @@ package com.synebula.gaea.domain.service -import com.synebula.gaea.data.IObjectConverter import com.synebula.gaea.data.message.DataMessage -import com.synebula.gaea.data.message.Message +import com.synebula.gaea.data.message.StatusMessage +import com.synebula.gaea.data.serialization.IObjectMapper import com.synebula.gaea.domain.model.IAggregateRoot import com.synebula.gaea.domain.repository.IRepository import com.synebula.gaea.log.ILogger @@ -14,30 +14,30 @@ import com.synebula.gaea.log.ILogger * * @param repository 仓储对象 * @param clazz 聚合根类对象 - * @param converter 对象转换组件 + * @param deserializer 对象转换组件 * @param logger 日志组件 * @author alex * @version 0.1 * @since 2020-05-17 */ -open class Service, TKey>( - protected open var clazz: Class, - protected open var repository: IRepository, - protected open var converter: IObjectConverter, - override var logger: ILogger -) : IService { +open class Service, ID>( + protected open var clazz: Class, + protected open var repository: IRepository, + protected open var deserializer: IObjectMapper, + override var logger: ILogger, +) : IService { /** * 删除对象前执行监听器。 */ - protected val beforeRemoveListeners = mutableMapOf Message>() + protected val beforeRemoveListeners = mutableMapOf StatusMessage>() /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) { + override fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) { this.beforeRemoveListeners[key] = func } @@ -49,23 +49,23 @@ open class Service, TKey>( this.beforeRemoveListeners.remove(key) } - override fun add(command: ICommand): DataMessage { - val msg = DataMessage() - val root = this.convert(command) + override fun add(command: ICommand): DataMessage { + val msg = DataMessage() + val root = this.deserialize(command) this.repository.add(root, this.clazz) msg.data = root.id return msg } - override fun update(id: TKey, command: ICommand) { - val root = this.convert(command) + override fun update(id: ID, command: ICommand) { + val root = this.deserialize(command) root.id = id this.repository.update(root, this.clazz) } - override fun remove(id: TKey) { + override fun remove(id: ID) { val functions = this.beforeRemoveListeners.values - var msg: Message + var msg: StatusMessage for (func in functions) { msg = func(id) if (!msg.success()) { @@ -81,9 +81,9 @@ open class Service, TKey>( * @param command 需要转换的命令 * @return 聚合根 */ - protected open fun convert(command: ICommand): TAggregateRoot { + protected open fun deserialize(command: ICommand): TAggregateRoot { try { - return converter.convert(command, this.clazz) + return deserializer.deserialize(command, this.clazz) } catch (ex: Exception) { throw RuntimeException("command not match aggregate root", ex) } diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SimpleService.kt similarity index 78% rename from src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt rename to src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SimpleService.kt index 2e49652..1a216bc 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/LazyService.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/domain/service/SimpleService.kt @@ -1,7 +1,7 @@ package com.synebula.gaea.domain.service import com.synebula.gaea.data.message.DataMessage -import com.synebula.gaea.data.message.Message +import com.synebula.gaea.data.message.StatusMessage import com.synebula.gaea.domain.model.IAggregateRoot import com.synebula.gaea.domain.repository.IRepository import com.synebula.gaea.log.ILogger @@ -18,23 +18,23 @@ import com.synebula.gaea.log.ILogger * @version 0.1 * @since 2020-05-17 */ -open class LazyService, TKey>( +open class SimpleService, ID>( protected open var clazz: Class, protected open var repository: IRepository, - override var logger: ILogger -) : ILazyService { + override var logger: ILogger, +) : ISimpleService { /** * 删除对象前执行监听器。 */ - protected val beforeRemoveListeners = mutableMapOf Message>() + protected val beforeRemoveListeners = mutableMapOf StatusMessage>() /** * 添加一个删除对象前执行监听器。 * @param key 监听器标志。 * @param func 监听方法。 */ - override fun addBeforeRemoveListener(key: String, func: (id: TKey) -> Message) { + override fun addBeforeRemoveListener(key: String, func: (id: ID) -> StatusMessage) { this.beforeRemoveListeners[key] = func } @@ -46,21 +46,21 @@ open class LazyService, TKey>( this.beforeRemoveListeners.remove(key) } - override fun add(root: TAggregateRoot): DataMessage { - val msg = DataMessage() + override fun add(root: TAggregateRoot): DataMessage { + val msg = DataMessage() this.repository.add(root, this.clazz) msg.data = root.id return msg } - override fun update(id: TKey, root: TAggregateRoot) { + override fun update(id: ID, root: TAggregateRoot) { root.id = id this.repository.update(root, this.clazz) } - override fun remove(id: TKey) { + override fun remove(id: ID) { val functions = this.beforeRemoveListeners.values - var msg: Message + var msg: StatusMessage for (func in functions) { msg = func(id) if (!msg.success()) { diff --git a/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt index debcd0a..adce2a8 100644 --- a/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/query/IQuery.kt @@ -12,7 +12,7 @@ interface IQuery { * @param id 对象Key。 * @return 视图结果 */ - fun get(id: TKey, clazz: Class): TView? + fun get(id: ID, clazz: Class): TView? /** * 根据实体类条件查询所有符合条件记录 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 new file mode 100644 index 0000000..75ac061 --- /dev/null +++ b/src/gaea/src/main/kotlin/com/synebula/gaea/reflect/Types.kt @@ -0,0 +1,23 @@ +package com.synebula.gaea.reflect + +import com.synebula.gaea.bus.SubscriberRegistry + +object Types { + /** + * 获取类的所有父类型。 + */ + fun supertypes(clazz: Class<*>): Set> { + val supertypes = mutableSetOf>() + supertypes.add(clazz) + if (clazz.superclass != null) + supertypes.addAll(SubscriberRegistry.flattenHierarchy(clazz.superclass)) + if (clazz.interfaces.isNotEmpty()) { + supertypes.addAll(clazz.interfaces.map { SubscriberRegistry.flattenHierarchy(it) }.reduce { r, c -> + val all = r.toMutableSet() + all.addAll(c) + all + }) + } + return supertypes + } +} \ No newline at end of file