OTC框架生成代码缺失BigDecimal导入语句致编译失败求助
OTC框架copy-literal命令生成代码缺失BigDecimal导入导致编译失败
问题概述
使用OTC框架的copy-literal命令将字面量值复制到BigDecimal类型的目标字段时,生成的Java源代码未自动导入java.math.BigDecimal类,触发编译错误。
复现步骤
- 创建包含copy-literal命令的OTCS脚本,指定将值
18.1复制到目标对象的payloadAttributes.version字段(类型为BigDecimal) - 运行测试项目生成对象转换源代码
- 编译生成代码时,出现"cannot find symbol: class BigDecimal"错误
相关配置与代码
OTCS脚本内容:
metadata: objectTypes: source: ar.ndc_21_3_4.IATAAirShoppingRQ target: org.iata.iata._2015._00._2018_1.airshoppingrq.AirShoppingRQ entryClassName: IATAAirShoppingRQ__AirShoppingRQ commands: - copy: id: CP1 from: values: - 18.1 to: objectPath: payloadAttributes.version factoryClassName: CP1
编译错误详情:
ar.ndc_21_3_4.IATAAirShoppingRQ_org.iata.iata._2015._00._2018_1.airshoppingrq.AirShoppingRQ.otcs 529 [main] INFO o.o.compiler.OtcCodeGeneratorImpl - Starting Source-Code generator. Please wait....... 560 [main] INFO o.o.compiler.OtcCodeGeneratorImpl - Source-Code generation completed in 30.689166 millis. 633 [main] INFO o.o.compiler.OtcsCompilerImpl - Completed 1/1 OTC compilation(s), Failed : 0/1. in 159.718292 millis. 633 [main] INFO o.o.compiler.OtcsCompilerImpl - Compiling source-code files. Please wait....... //Users/sabarishr/ARR_Projects/OTC/otc/otc-home/src/AirShopping/CP1.java:28: error: cannot find symbol payloadAttributes.setVersion(new BigDecimal("18.1")); ^ symbol: class BigDecimal location: class AirShopping.CP1 2359 [main] ERROR o.o.compiler.OtcsCompilerImpl - org.otcframework.compiler.exception.OtcCompilerException: Source code compilation failed. at org.otcframework.compiler.OtcsCompilerImpl.compileSourceCode(OtcsCompilerImpl.java:482) at org.otcframework.compiler.OtcsCompilerImpl.createCompilationUnitsAndCompile(OtcCompilerImpl.java:447) at org.otcframework.compiler.OtcsCompilerImpl.compileSourceCode(OtcsCompilerImpl.java:401) at org.otcframework.test.unit.OtcTest.compileAndRegister(OtcTest.java:137) at org.otcframework.test.unit.OtcTest.runTest(OtcTest.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.platform.commons.util.ReflectionUtils.invokeMethod(ReflectionUtils.java:628) at org.junit.jupiter.engine.execution.ExecutableInvoker.invoke(ExecutableInvoker.java:117)
解决方法
1. 框架层面修复(长期方案)
修改OTC框架的代码生成逻辑:
- 在
OtcCodeGeneratorImpl类中,找到处理copy-literal命令的代码模块 - 增加对目标字段类型的检测逻辑,当字段类型为
java.math.BigDecimal时,自动将import java.math.BigDecimal;语句添加到生成的Java类头部 - 确保导入语句不会重复添加,可通过维护一个导入集合来管理
2. 临时 workaround(快速修复)
手动编辑生成的CP1.java文件,在类定义前添加导入语句:
import java.math.BigDecimal;
之后重新执行编译流程即可。
内容的提问来源于stack exchange,提问作者iPC
相关产品推荐
相关产品推荐

