【发布时间】:2018-01-17 14:34:23
【问题描述】:
我的问题是关于编写 JAXB 插件,尤其是 ClassOutline internals。
com.sun.tools.xjc.outline.ClassOutline中有字段:
- 目标
- 参考
- implClass
- implRef
代码:
/**
* This {@link ClassOutline} holds information about this {@link CClassInfo}.
*/
public final @NotNull CClassInfo target;
/**
* The exposed aspect of the a bean.
*
* implClass is always assignable to this type.
* <p>
* Usually this is the public content interface, but
* it could be the same as the implClass.
*/
public final @NotNull JDefinedClass ref;
/**
* The implementation aspect of a bean.
* The actual place where fields/methods should be generated into.
*/
public final @NotNull JDefinedClass implClass;
/**
* The implementation class that shall be used for reference.
* <p>
* Usually this field holds the same value as the {@link #implClass} method,
* but sometimes it holds the user-specified implementation class
* when it is specified.
* <p>
* This is the type that needs to be used for generating fields.
*/
public final @NotNull JClass implRef;
据我所知 (SO Answer):
-
target- 在Model中保存信息,表示已解析和分析的架构文件 (.xsd) -
ref通常等于implClass并且两者都持有Code Model -
implClass是放置新生成的字段、方法等的正确位置。 -
implRef- 这是什么?
我想为ClassOutline描述的类添加新字段,所以代码如下:
JDefinedClass dstClass = classOutline.ref;
JFieldVar dstField = dstClass.field(srcField.mods().getValue(),
srcField.type(), srcField.name());
它工作得很好,直到在执行上述代码并使用com.sun.tools.xjc.outline.ClassOutline.getDeclaredFields() 方法后有另一个插件工作。
想象一下 - Plugin1 创建新字段,然后执行 CopyablePlugin 并希望添加 clone() 方法,该方法复制每个字段。但是CopyablePlugin 看不到Plugin1 新生成的字段-因为要从ClassOutline 检索所有字段,CopyablePlugin 使用com.sun.tools.xjc.outline.ClassOutline.getDeclaredFields() 方法,如下所示:
/**
* Gets all the {@link FieldOutline}s newly declared
* in this class.
*/
public final FieldOutline[] getDeclaredFields() {
List<CPropertyInfo> props = target.getProperties();
// ...
请注意,getDeclaredFields() 从 ClassOutline.target 字段(这是 Model - 已解析的 XSD 架构)检索属性并完全忽略生成到 ClassOutline.implClass 的代码。
这是错误还是功能?
现在我找到了解决方法。同样的字段也作为属性添加到target:
classOutline.target.addProperty(prop);
问题
- 能否解释一下,
ref/implClass/implRef的作用是什么? - 我应该在哪里生成全新的字段/方法?转入
ref/implClass? - 是否需要保持
ref/implClass和target之间的一致性?添加到implClass的新字段应该也添加到target,对吧? -
com.sun.tools.xjc.outline.ClassOutline.getDeclaredFields()正确吗?或者如何正确地从 ClassOutline 中检索所有字段?也许这应该是target和implClass内容的联合?
【问题讨论】:
标签: java jaxb xjc sun-codemodel jcodemodel