为什么 JAXB 生成的类具有受保护的成员以及如何更改它?

发布于 2025-01-07 19:32:27 字数 195 浏览 2 评论 0原文

我一直在互联网上搜索 JAXB 生成的类具有受保护成员(所有成员,无论继承如何)的原因。

我希望成员是私人的。

我的搜索结果是空的。

我有普通的 xsd 文件,它们使用 Maven 和 JAXB 转换为 Java 类。理想情况下,生成的成员应该是私有的,但我找不到实现此目的的方法。

有没有办法修改这种默认行为?

I have been searching the internet for a reason why JAXB generated classes have protected members (all of them, regardless of inheritance).

I would like the members to be private instead.

My search has come up empty.

I have normal xsd files which are converted into Java classes using Maven and JAXB. Ideally the generated members should be private but I cannot find a way to achieve this.

Is there a way to modify this default behaviour?

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

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

发布评论

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

评论(2

星軌x 2025-01-14 19:32:28

好吧,我要回答我自己的问题。
创建插件是正确的方法。

我编写了以下插件,它似乎可以工作。

public class PrivateMemberPlugin
    extends Plugin
{

    @Override
    public String getOptionName()
    {
        return "Xpm";
    }

    @Override
    public String getUsage()
    {
        return "  -Xpm    : Change members visibility to private";
    }

    @Override
    public boolean run(Outline model, Options opt, ErrorHandler errorHandler)
        throws SAXException
    {
        for (ClassOutline co : model.getClasses())
        {

            JDefinedClass jdc = co.implClass;
            // avoid concurrent modification by copying the fields in a new list
            List<JFieldVar> fields = new ArrayList<JFieldVar>(jdc.fields().values());
            for (JFieldVar field : fields)
            {
                // never do something with serialVersionUID if it exists.
                if (!field.name().equalsIgnoreCase("serialVersionuid"))
                {
                    // only try to change members that are not private
                    if (field.mods().getValue() != JMod.PRIVATE)
                    {
                        // since there is no way to change the visibilty, remove the field an recreate it
                        jdc.removeField(field);
                        jdc.field(JMod.PRIVATE, field.type(), field.name());

                    }
                }
            }

        }
        return true;
    }

}

如果您愿意,请随意使用。

Well, I am going to respond to my own question.
Creating a plugin was the right way to go.

I wrote the following plugin and it seems to work.

public class PrivateMemberPlugin
    extends Plugin
{

    @Override
    public String getOptionName()
    {
        return "Xpm";
    }

    @Override
    public String getUsage()
    {
        return "  -Xpm    : Change members visibility to private";
    }

    @Override
    public boolean run(Outline model, Options opt, ErrorHandler errorHandler)
        throws SAXException
    {
        for (ClassOutline co : model.getClasses())
        {

            JDefinedClass jdc = co.implClass;
            // avoid concurrent modification by copying the fields in a new list
            List<JFieldVar> fields = new ArrayList<JFieldVar>(jdc.fields().values());
            for (JFieldVar field : fields)
            {
                // never do something with serialVersionUID if it exists.
                if (!field.name().equalsIgnoreCase("serialVersionuid"))
                {
                    // only try to change members that are not private
                    if (field.mods().getValue() != JMod.PRIVATE)
                    {
                        // since there is no way to change the visibilty, remove the field an recreate it
                        jdc.removeField(field);
                        jdc.field(JMod.PRIVATE, field.type(), field.name());

                    }
                }
            }

        }
        return true;
    }

}

Feel free to use this if you want.

请恋爱 2025-01-14 19:32:28

我认为实现这一点的唯一方法是开发一个 JXC 插件 自己在 google 上搜索示例。

插件可以做什么?

XJC 插件参与模式的代码生成。它
可以定义自己的定制,用户可以使用它来控制它
可以访问 JAXB RI 生成的代码,它可以生成
额外的类/方法/字段/注释/注释,它可以
还替换了编译中的一些可插拔点
流程,如XML名称-> Java 名称转换。

幸运的是,问题所有者开发并分享了该插件

I think the only way to achieve this is to develop a JXC plugin yourself, search google for samples.

What Can A Plugin Do?

An XJC plugin participates in the code generation from a schema. It
can define its own customizations that users can use to control it, it
can access the code that the JAXB RI generates, it can generate
additional classes/methods/fields/annotations/comments, and it can
also replace some of the pluggability points in the compilation
process, such as XML name -> Java name conversion.

Luckily, question owner has developed and shared the plugin.

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