Why do JAXB generated classes have protected members and how can I change this?

前端 未结 2 1921
广开言路
广开言路 2020-12-31 11:39

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 member

相关标签:
2条回答
  • 2020-12-31 12:35

    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.

    0 讨论(0)
  • 2020-12-31 12:42

    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.

    0 讨论(0)
提交回复
热议问题