Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XJC ConstField generates empty javadoc comment blocks #1785

Closed
patrodyne opened this issue Feb 12, 2024 · 0 comments
Closed

XJC ConstField generates empty javadoc comment blocks #1785

patrodyne opened this issue Feb 12, 2024 · 0 comments

Comments

@patrodyne
Copy link
Contributor

patrodyne commented Feb 12, 2024

As of v4.0.4, the ConstField class generates empty Javadoc comment blocks when no <javadoc>text</javadoc> is explicitly provided in the XML schema.

Example: Empty Javadoc for a Constant Field

    /**
     * 
     * 
     */
    @XmlAttribute(name = "fa_long")
    public static final boolean FA_LONG = 7L;

A fix is to conditionally append the prop.javadoc only when it has content:

--- a/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/generator/bean/field/ConstField.java
+++ b/jaxb-ri/xjc/src/main/java/com/sun/tools/xjc/generator/bean/field/ConstField.java
@@ -54,8 +54,11 @@ final class ConstField extends AbstractField {
 
         $ref = outline.ref.field(JMod.PUBLIC|JMod.STATIC|JMod.FINAL,
             ptype!=null?ptype:implType, prop.getName(true), defaultValue );
-        $ref.javadoc().append(prop.javadoc);
         
+        // Do not append empty javadoc.
+        if ( (prop.javadoc != null) && (prop.javadoc.length() > 0) )
+            $ref.javadoc().append(prop.javadoc);
+
         annotate($ref);
     }

Rationale

Currently, when $ref.javadoc() is invoked by ConstField, the jdoc field of JFieldVar is initialized to a non-null value. Later, the declare method is invoked and the JFormatter.g(jdoc) method is triggered whenever jdoc is not null. This leads to a call to JDocComment.generate(JFormatter) which includes blank lines when @param, @return, @throws, etc. are not applicable.

The proposed fix is to prevent an unnecessary initialization of the jdoc field of JFieldVar; thus, avoiding a call to JDocComment.generate(JFormatter). I will submit a pull request with the fix and a unit test.

ConstField: Call Sequence

com.sun.codemodel.JFieldVar.javadoc()
	...
    public JDocComment javadoc()
	{
        if( jdoc == null ) 
            jdoc = new JDocComment(owner.owner());
        return jdoc;
    }
	...
com.sun.codemodel.JDefinedClass.declareBody(JFormatter)
com.sun.codemodel.JFormatter.d(JDeclaration)
com.sun.codemodel.JFieldVar.declare(JFormatter)
	...
    public void declare(JFormatter f)
	{
        if( jdoc != null )
            f.g( jdoc );
        super.declare( f );
    }
	...
com.sun.codemodel.JFormatter.g(JGenerable)
com.sun.codemodel.JDocComment.generate(JFormatter)
patrodyne added a commit to patrodyne/jaxb-ri that referenced this issue Feb 13, 2024
@lukasj lukasj closed this as completed in 13402b7 Feb 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant