XJC生成另一种字段类型(对get方法的影响)

发布于 2024-10-19 16:55:39 字数 637 浏览 6 评论 0原文

使用最新的 JAXB (Metro) 并使用 XJC 生成 Java...

想要(正如其他用户所要求的那样)生成 java.util.Set 作为表示无界序列的字段的类型。看起来该类型的字段被 XJC 捕获为 UntypedListField,默认行为是生成 java.util.List(仅 getter)。如果我执行类似于 collection-setter-injector 插件的操作并调整字段的类型,例如

 public boolean run(Outline model, Options opt, ErrorHandler errorHandler) {
    for (ClassOutline co : model.getClasses()) {
       FieldOutline[] fo = co.getDeclaredFields();

       for ...
          if ((fo[i] instanceof UntypedListField)) {
            --> DO SOMETHING WITH THIS FIELD
          }
    }
 }

人们如何调整类型,或者构建新字段是否更容易,然后将其替换为类大纲中声明的字段集?弄乱字段的类型如何影响属性上 get 方法的生成?

Using the latest JAXB (Metro) and generating Java with XJC....

Want to (as other users have asked) generate java.util.Set as a type for fields that represent unbounded sequences. Looks like that type of field is captured by XJC as a UntypedListField and the default behavior is to generate java.util.List (only the getter). If I do something similar to the collection-setter-injector plugin and adjust the field's type like

 public boolean run(Outline model, Options opt, ErrorHandler errorHandler) {
    for (ClassOutline co : model.getClasses()) {
       FieldOutline[] fo = co.getDeclaredFields();

       for ...
          if ((fo[i] instanceof UntypedListField)) {
            --> DO SOMETHING WITH THIS FIELD
          }
    }
 }

How do people adjust the type or is it easier to construct a new field then replace it in the set of declared fields in the class outline? How does messing with the field's type effect the generation of the get method on the property?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狂之美人 2024-10-26 16:55:39

看来您要使用自己的 XJC 插件。这就是你需要做的。替换您的 -->使用此字段执行以下操作 行并执行以下操作。

首先,弄清楚 fo[i] 的参数化类型是什么(我称之为 f)。
然后,创建 Set JType。最后将f的类型设置为setType

JType inner = ((JClass)f.type()).getTypeParameters().get(0);
JType setType = co.parent().getCodeModel().ref(Set.class).narrow(inner);
f.type(setType);

narrow()方法用于设置参数化类型。

到目前为止看起来不错,但问题是该插件将在 XJC 生成类完成后运行。这意味着吸气剂已经存在。所以我们需要更换它。

这是 replaceGetter() 方法

private void replaceGetter(ClassOutline co, JFieldVar f, JType inner) {
    //Create the method name
    String get = "get";
    String name  = f.name().substring(0, 1).toUpperCase() 
            + f.name().substring(1);
    String methodName = get+name;

    //Create HashSet JType
    JType hashSetType = co.parent().getCodeModel().ref(HashSet.class).narrow(inner);

    //Find and remove Old Getter!
    JMethod oldGetter = co.implClass.getMethod(methodName, new JType[0]);
    co.implClass.methods().remove(oldGetter);

    //Create New Getter
    JMethod getter = co.implClass.method(JMod.PUBLIC, f.type(), methodName);

    //Create Getter Body -> {if (f = null) f = new HashSet(); return f;}
    getter.body()._if(JExpr.ref(f.name()).eq(JExpr._null()))._then()
    .assign(f, JExpr._new(hashSetType));

    getter.body()._return(JExpr.ref(f.name()));
}

希望您觉得这有帮助。

Looks like you're going for your own XJC Plugin. So here's what you need to do. Replace your --> DO SOMETHING WITH THIS FIELD line with the following.

First, figure out what's the parameterization type of fo[i] (which I'm calling f).
Then, create the Set JType. And finally set the type of f to setType:

JType inner = ((JClass)f.type()).getTypeParameters().get(0);
JType setType = co.parent().getCodeModel().ref(Set.class).narrow(inner);
f.type(setType);

The method narrow() is used to set the parametrization type.

Looks good so far, the problem however is that the plugin will run after XJC is done generating the classes. Which means that the getter is already there. So we need to replace it.

And here's the replaceGetter() method

private void replaceGetter(ClassOutline co, JFieldVar f, JType inner) {
    //Create the method name
    String get = "get";
    String name  = f.name().substring(0, 1).toUpperCase() 
            + f.name().substring(1);
    String methodName = get+name;

    //Create HashSet JType
    JType hashSetType = co.parent().getCodeModel().ref(HashSet.class).narrow(inner);

    //Find and remove Old Getter!
    JMethod oldGetter = co.implClass.getMethod(methodName, new JType[0]);
    co.implClass.methods().remove(oldGetter);

    //Create New Getter
    JMethod getter = co.implClass.method(JMod.PUBLIC, f.type(), methodName);

    //Create Getter Body -> {if (f = null) f = new HashSet(); return f;}
    getter.body()._if(JExpr.ref(f.name()).eq(JExpr._null()))._then()
    .assign(f, JExpr._new(hashSetType));

    getter.body()._return(JExpr.ref(f.name()));
}

Hope you find this helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文