|
| 1 | +package dev.flexmodel.common.config.nativeimage; |
| 2 | + |
| 3 | +import io.quarkus.deployment.annotations.BuildStep; |
| 4 | +import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem; |
| 5 | + |
| 6 | +/** |
| 7 | + * Flexmodel 原生镜像反射配置处理器。 |
| 8 | + * <p> |
| 9 | + * 替代传统的 reachability-metadata.json,通过 Quarkus BuildStep |
| 10 | + * 在构建时自动注册所需类的反射信息,无需维护庞大的 JSON 文件。 |
| 11 | + * <p> |
| 12 | + * 分类策略: |
| 13 | + * <ul> |
| 14 | + * <li>Codegen 实体和枚举 — 运行时数据映射需要全部方法和字段</li> |
| 15 | + * <li>DTO 和模型对象 — Jackson 序列化需要全部方法和字段</li> |
| 16 | + * <li>Jackson MixIn 类 — 多态反序列化需要构造函数</li> |
| 17 | + * <li>第三方库 — 最小化反射配置</li> |
| 18 | + * </ul> |
| 19 | + * |
| 20 | + * @author cjbi |
| 21 | + */ |
| 22 | +public class FlexmodelNativeProcessor { |
| 23 | + |
| 24 | + /** |
| 25 | + * 注册 Codegen 生成的实体和枚举。 |
| 26 | + * 这些类在运行时被 Flexmodel 数据引擎通过反射进行属性映射。 |
| 27 | + */ |
| 28 | + @BuildStep |
| 29 | + ReflectiveClassBuildItem registerCodegenEntities() { |
| 30 | + return ReflectiveClassBuildItem.builder( |
| 31 | + "dev.flexmodel.codegen.entity.**", |
| 32 | + "dev.flexmodel.codegen.enumeration.**" |
| 33 | + ) |
| 34 | + .methods() |
| 35 | + .fields() |
| 36 | + .build(); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * 注册所有 DTO 类。 |
| 41 | + * DTO 用于 REST 端点,Jackson 序列化/反序列化需要反射访问。 |
| 42 | + */ |
| 43 | + @BuildStep |
| 44 | + ReflectiveClassBuildItem registerDtos() { |
| 45 | + return ReflectiveClassBuildItem.builder( |
| 46 | + "dev.flexmodel.auth.dto.**", |
| 47 | + "dev.flexmodel.project.dto.**", |
| 48 | + "dev.flexmodel.scheduling.dto.**", |
| 49 | + "dev.flexmodel.functions.dto.**", |
| 50 | + "dev.flexmodel.api.dto.**", |
| 51 | + "dev.flexmodel.flow.dto.**", |
| 52 | + "dev.flexmodel.common.dto.**", |
| 53 | + "dev.flexmodel.metrics.dto.**", |
| 54 | + "dev.flexmodel.storage.dto.**" |
| 55 | + ) |
| 56 | + .methods() |
| 57 | + .fields() |
| 58 | + .build(); |
| 59 | + } |
| 60 | + |
| 61 | + /** |
| 62 | + * 注册引擎模块中的模型定义类。 |
| 63 | + * 这些类使用 Jackson 多态反序列化(@JsonSubTypes / @JsonTypeInfo)。 |
| 64 | + */ |
| 65 | + @BuildStep |
| 66 | + ReflectiveClassBuildItem registerModelDefinitions() { |
| 67 | + return ReflectiveClassBuildItem.builder( |
| 68 | + "dev.flexmodel.model.**", |
| 69 | + "dev.flexmodel.model.field.**", |
| 70 | + "dev.flexmodel.condition.**", |
| 71 | + "dev.flexmodel.event.**", |
| 72 | + "dev.flexmodel.event.impl.**" |
| 73 | + ) |
| 74 | + .methods() |
| 75 | + .fields() |
| 76 | + .build(); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * 注册 Jackson MixIn 和序列化支持类。 |
| 81 | + * 包含 TypedFieldMixIn、ModelMixIn、IndexMixIn 等类型映射配置。 |
| 82 | + */ |
| 83 | + @BuildStep |
| 84 | + ReflectiveClassBuildItem registerJacksonSupport() { |
| 85 | + return ReflectiveClassBuildItem.builder( |
| 86 | + "dev.flexmodel.supports.jackson.**", |
| 87 | + "dev.flexmodel.common.config.web.json.jackson.**", |
| 88 | + "dev.flexmodel.common.config.web.json.jackson.mixin.**" |
| 89 | + ) |
| 90 | + .methods() |
| 91 | + .fields() |
| 92 | + .build(); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * 注册流程引擎核心类。 |
| 97 | + * 包含执行器、验证器、服务类等运行时通过 CDI/反射实例化的组件。 |
| 98 | + */ |
| 99 | + @BuildStep |
| 100 | + ReflectiveClassBuildItem registerFlowEngineClasses() { |
| 101 | + return ReflectiveClassBuildItem.builder( |
| 102 | + "dev.flexmodel.flow.executor.**", |
| 103 | + "dev.flexmodel.flow.validator.**", |
| 104 | + "dev.flexmodel.flow.service.**", |
| 105 | + "dev.flexmodel.flow.common.**", |
| 106 | + "dev.flexmodel.flow.config.**", |
| 107 | + "dev.flexmodel.flow.plugin.**", |
| 108 | + "dev.flexmodel.flow.processor.**" |
| 109 | + ) |
| 110 | + .methods() |
| 111 | + .fields() |
| 112 | + .build(); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * 注册业务组件类。 |
| 117 | + * 包含调度配置、认证提供者等含 @JsonSubTypes 多态类型的类。 |
| 118 | + */ |
| 119 | + @BuildStep |
| 120 | + ReflectiveClassBuildItem registerBusinessComponents() { |
| 121 | + return ReflectiveClassBuildItem.builder( |
| 122 | + "dev.flexmodel.scheduling.config.**", |
| 123 | + "dev.flexmodel.scheduling.job.**", |
| 124 | + "dev.flexmodel.projectauth.provider.**", |
| 125 | + "dev.flexmodel.storage.config.**", |
| 126 | + "dev.flexmodel.sql.dialect.**" |
| 127 | + ) |
| 128 | + .methods() |
| 129 | + .fields() |
| 130 | + .build(); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * 注册数据层和服务层类。 |
| 135 | + * Repository、Service、Resource 等运行时组件。 |
| 136 | + */ |
| 137 | + @BuildStep |
| 138 | + ReflectiveClassBuildItem registerDataAndServiceClasses() { |
| 139 | + return ReflectiveClassBuildItem.builder( |
| 140 | + "dev.flexmodel.auth.repository.**", |
| 141 | + "dev.flexmodel.auth.service.**", |
| 142 | + "dev.flexmodel.flow.repository.**", |
| 143 | + "dev.flexmodel.data.**", |
| 144 | + "dev.flexmodel.scheduling.**", |
| 145 | + "dev.flexmodel.functions.**", |
| 146 | + "dev.flexmodel.settings.**", |
| 147 | + "dev.flexmodel.storage.**", |
| 148 | + "dev.flexmodel.modeling.**", |
| 149 | + "dev.flexmodel.api.**", |
| 150 | + "dev.flexmodel.project.**", |
| 151 | + "dev.flexmodel.projectauth.**", |
| 152 | + "dev.flexmodel.mcp.**", |
| 153 | + "dev.flexmodel.metrics.**", |
| 154 | + "dev.flexmodel.realtime.**", |
| 155 | + "dev.flexmodel.audit.**" |
| 156 | + ) |
| 157 | + .methods() |
| 158 | + .fields() |
| 159 | + .build(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * 注册第三方库中不含 Quarkus 扩展的类。 |
| 164 | + * 这些类在原生镜像中需要显式声明反射访问。 |
| 165 | + */ |
| 166 | + @BuildStep |
| 167 | + ReflectiveClassBuildItem registerThirdPartyClasses() { |
| 168 | + return ReflectiveClassBuildItem.builder( |
| 169 | + // Auth0 JWT — 无 Quarkus 扩展 |
| 170 | + "com.auth0.jwt.exceptions.JWTVerificationException", |
| 171 | + // MySQL JDBC — 原生镜像需要 DataSource 反射 |
| 172 | + "com.mysql.cj.jdbc.MysqlDataSource", |
| 173 | + "com.mysql.cj.jdbc.Driver", |
| 174 | + // SQLite JDBC — 原生镜像需要 DataSource 反射 |
| 175 | + "org.sqlite.SQLiteDataSource", |
| 176 | + // Agroal 连接池 — 原生镜像需要反射访问 DataSource 接口实现 |
| 177 | + "io.agroal.pool.DataSource", |
| 178 | + // Caffeine 缓存 — 内部类需反射 |
| 179 | + "com.github.benmanes.caffeine.cache.UnboundedLocalCache", |
| 180 | + // GraphQL — 执行结果类 |
| 181 | + "graphql.ExecutionResult", |
| 182 | + "graphql.ExecutionResultImpl", |
| 183 | + // Import map |
| 184 | + "io.mvnpm.importmap.model.Imports" |
| 185 | + ) |
| 186 | + .methods() |
| 187 | + .fields() |
| 188 | + .build(); |
| 189 | + } |
| 190 | + |
| 191 | + /** |
| 192 | + * 注册需要运行时重新初始化的类。 |
| 193 | + * 这些类在构建期触发了不安全的初始化,必须在运行时重新初始化。 |
| 194 | + */ |
| 195 | + @BuildStep |
| 196 | + ReflectiveClassBuildItem registerRuntimeReinitializedClasses() { |
| 197 | + return ReflectiveClassBuildItem.builder( |
| 198 | + // MySQL 连接清理线程 |
| 199 | + "com.mysql.cj.jdbc.AbandonedConnectionCleanupThread" |
| 200 | + ) |
| 201 | + .methods() |
| 202 | + .build(); |
| 203 | + } |
| 204 | +} |
0 commit comments