Skip to content

Commit

Permalink
Fix ConstField empty javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
patrodyne committed Feb 12, 2024
1 parent a2224db commit 5a96b3c
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
100 changes: 100 additions & 0 deletions jaxb-ri/xjc/src/test/java/com/sun/tools/xjc/CodeGenTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@
import com.sun.codemodel.JFormatter;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JType;
import com.sun.codemodel.writer.SingleStreamCodeWriter;
import com.sun.tools.xjc.api.ErrorListener;
import com.sun.tools.xjc.api.S2JJAXBModel;
import com.sun.tools.xjc.api.SchemaCompiler;
import com.sun.tools.xjc.api.XJC;

import static com.sun.tools.xjc.util.Util.getSystemProperty;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertNotEquals;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
Expand All @@ -38,6 +48,9 @@
*/
public class CodeGenTest extends TestCase {

// See com.sun.tools.xjc.api.impl.s2j.SchemaCompilerImpl
protected static final String XML_API_TEST = "xjc-api.test";

public void testGh1460_Gh1064() throws Throwable {
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName("ghbugs.b1460");
Expand Down Expand Up @@ -121,6 +134,93 @@ public void testIssue1750() throws FileNotFoundException, URISyntaxException {
}
}

/**
* Test issues #1785 for {@link com.sun.tools.xjc.generator.bean.field.ConstField}.
*
* @throws FileNotFoundException When the test schema file cannot be read.
* @throws URISyntaxException When the test {@link InputSource} cannot be parsed.
*
* @see <a href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1785">Issue #1785</a>
*/
public void testIssue1785() throws FileNotFoundException, URISyntaxException, IOException {
String schemaFileName = "/schemas/issue1785/document.xsd";
String packageName = "org.example.issue1785";
String documentName = packageName + ".Document";
String suidFieldName = "serialVersionUID";
String codeModelDestPathName = "target/generated-test-sources/issue1785";

// Parse the XML schema.
SchemaCompiler sc = XJC.createSchemaCompiler();
sc.forcePackageName(packageName);
sc.parseSchema(getInputSource(schemaFileName));

// Generate the defined model.
S2JJAXBModel model = sc.bind();
Plugin[] extensions = null;
ErrorListener errorListener = new ConsoleErrorReporter();
JCodeModel cm = model.generateCode(extensions, errorListener);

// Assert Document class is modeled.
JDefinedClass dc = cm._getClass(documentName);
assertNotNull(documentName, dc);

// Assert serialVersionUID
assertTrue(suidFieldName, dc.fields().containsKey(suidFieldName));
assertNotNull(suidFieldName + " value", dc.fields().get(suidFieldName));

// Generate the Document classes to a directory, for review.
if ( getSystemProperty(XML_API_TEST) != null ) {
File codeModelDestPath = new File(codeModelDestPathName);
if ( !codeModelDestPath.exists() )
codeModelDestPath.mkdirs();
cm.build(codeModelDestPath);
}

// Generate the Document classes to single String, for assertions.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
cm.build(new SingleStreamCodeWriter(baos));
String cmString = baos.toString(UTF_8);

// Assert non-empty javadoc blocks.
assertNonEmptyJavadocBlocks(cmString);
}

private void assertNonEmptyJavadocBlocks(String cmString) throws IOException {
int lineNo = 0;
try ( LineNumberReader lnr = new LineNumberReader(new StringReader(cmString)) ) {
StringBuilder javadocBlock = null;
String line;
while ( (line = lnr.readLine()) != null ) {
String trimLine = line.trim();
String javadoc = null;
if ( trimLine.startsWith("/**") ) {
lineNo = lnr.getLineNumber();
javadocBlock = new StringBuilder();
javadoc = trimLine.substring(3);
}
if ( javadocBlock != null ) {
if ( javadoc == null ) {
if ( trimLine.startsWith("*") && !trimLine.startsWith("*/") )
javadoc = trimLine.substring(1);
else
javadoc = trimLine;
}
int endIndex = 0;
if ( (endIndex = javadoc.lastIndexOf("*/")) != -1 ) {
javadocBlock.append(javadoc.substring(0, endIndex));
int javadocLen = javadocBlock.toString().trim().length();

// Assert current javadoc block length is not zero!
assertNotEquals("Empty javadoc at " + lineNo, 0, javadocLen);
javadocBlock = null;
}
else
javadocBlock.append(javadoc);
}
}
}
}

private InputSource getInputSource(String systemId) throws FileNotFoundException, URISyntaxException {
URL url = CodeGenTest.class.getResource(systemId);
File f = new File(url.toURI());
Expand Down
50 changes: 50 additions & 0 deletions jaxb-ri/xjc/src/test/resources/schemas/issue1785/document.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
targetNamespace="http://example.org/document"
xmlns:tns="http://example.org/document"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jaxb="https://jakarta.ee/xml/ns/jaxb"
jaxb:version="3.0"
>

<!-- Global Bindings -->

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings fixedAttributeAsConstantProperty="true">
<jaxb:serializable uid="20240201" />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>

<!-- Schema Bindings -->

<xs:element name="fv_string" type="xs:string" fixed="text"/>

<xs:element name="document">
<xs:complexType>
<xs:sequence>
<xs:element ref="tns:fv_string" />
</xs:sequence>
<xs:attribute name="fa_boolean" type="xs:boolean" fixed="true" >
<xs:annotation>
<xs:appinfo>
<jaxb:property>
<jaxb:javadoc>A tautology!</jaxb:javadoc>
</jaxb:property>
</xs:appinfo>
</xs:annotation>
</xs:attribute>
<xs:attribute name="fa_byte" type="xs:byte" fixed="+1" />
<xs:attribute name="fa_decimal" type="xs:decimal" fixed="+2.1" />
<xs:attribute name="fa_double" type="xs:double" fixed="+3.1" />
<xs:attribute name="fa_float" type="xs:float" fixed="+4.1" />
<xs:attribute name="fa_int" type="xs:int" fixed="+5" />
<xs:attribute name="fa_integer" type="xs:integer" fixed="+6" />
<xs:attribute name="fa_long" type="xs:long" fixed="+7" />
<xs:attribute name="fa_short" type="xs:short" fixed="+8" />
<xs:attribute name="fa_string" type="xs:string" fixed="text" />
</xs:complexType>
</xs:element>

</xs:schema>

0 comments on commit 5a96b3c

Please sign in to comment.