Skip to content

Commit

Permalink
Fix #1785 ConstField empty javadoc (#1786)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrodyne authored Feb 13, 2024
1 parent a2224db commit 13402b7
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1783">#1783</link>: Dependency Exclusion for stax-ex in jaxb-bom is incompatible with dependency:analyze-dep-mgt
</para></listitem>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1785">#1785</link>: XJC ConstField generates empty javadoc comment blocks
</para></listitem>
<listitem><para>
<link xlink:href="https://github.com/eclipse-ee4j/jaxb-dtd-parser/issues/39">dtd-parser #39</link>: Parser parses #IMPLIED use attributes as NORMAL (and vice versa)
</para></listitem>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 1997, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand Down 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
101 changes: 100 additions & 1 deletion jaxb-ri/xjc/src/test/java/com/sun/tools/xjc/CodeGenTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
Expand All @@ -16,19 +16,28 @@
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 com.sun.tools.xjc.util.Util;

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;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;

import junit.framework.TestCase;
import org.junit.Assert;
import org.xml.sax.InputSource;

/**
Expand All @@ -38,6 +47,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 +133,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 ( Util.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(StandardCharsets.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!
Assert.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
63 changes: 63 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,63 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright (c) 2024 Oracle and/or its affiliates. All rights reserved.
This program and the accompanying materials are made available under the
terms of the Eclipse Distribution License v. 1.0, which is available at
http://www.eclipse.org/org/documents/edl-v10.php.
SPDX-License-Identifier: BSD-3-Clause
-->

<!-- See https://github.com/eclipse-ee4j/jaxb-ri/issues/1785 -->
<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 13402b7

Please sign in to comment.