Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
patrodyne authored and lukasj committed Oct 6, 2023
1 parent 2269a1d commit d1c27f9
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@

package com.sun.tools.xjc.generator.bean;


import jakarta.xml.bind.JAXBElement;

import java.io.Serializable;

import javax.xml.namespace.QName;

import com.sun.codemodel.JClass;
import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JExpr;
import com.sun.codemodel.JExpression;
import com.sun.codemodel.JFieldVar;
Expand Down Expand Up @@ -54,8 +57,9 @@ public BeanGenerator parent() {
target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify()));

if(ei.hasClass()) {
serializableOption(parent, implClass);
JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION);
JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast?
JExpression declaredType = JExpr.cast(cm.ref(Class.class).narrow(implType),implType.boxify().dotclass()); // why do we have to cast?
JClass scope=null;
if(ei.getScope()!=null)
scope = parent.getClazz(ei.getScope()).implRef;
Expand All @@ -77,7 +81,37 @@ public BeanGenerator parent() {
.arg(declaredType)
.arg(scopeClass)
.arg(JExpr._null());
}
}

/**
* Serializable option to implement the {@link Serializable} interface
* and <code>serialVersionUID</code> field.
*
* @param bg The XJC bean generator.
* @param implClass The implementation class.
*
* @see <a href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1750">Issue #1750</a>
*/
private void serializableOption(BeanGenerator bg, JDefinedClass implClass)
{
if (bg.getModel().serializable)
{
JClass serRef = bg.getCodeModel().ref(Serializable.class);
if ( !serRef.isAssignableFrom(implClass) )
implClass._implements(Serializable.class);

if ( !implClass.fields().containsKey("serialVersionUID") )
{
if (bg.getModel().serialVersionUID != null)
{
implClass.field(
JMod.PRIVATE | JMod.STATIC | JMod.FINAL,
bg.getCodeModel().LONG,
"serialVersionUID",
JExpr.lit(bg.getModel().serialVersionUID));
}
}
}
}

Expand Down
48 changes: 48 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 @@ -12,8 +12,11 @@

import com.sun.codemodel.JCodeModel;
import com.sun.codemodel.JDeclaration;
import com.sun.codemodel.JDefinedClass;
import com.sun.codemodel.JFormatter;
import com.sun.codemodel.JMethod;
import com.sun.codemodel.JType;
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;
Expand All @@ -23,10 +26,13 @@
import java.io.StringWriter;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Iterator;

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

/**
* Test code generated by the {@link SchemaCompiler}.
*
* @author lukas
*/
Expand Down Expand Up @@ -75,6 +81,48 @@ public void testGh1460_Gh1064_jaxb21() throws Throwable {
assertTrue(method, method.contains("return ;"));
}

/**
* Test issues #1748 and #1750 for {@link ElementOutlineImpl}.
*
* @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/1748">Issue #1748</a>
* @see <a href="https://github.com/eclipse-ee4j/jaxb-ri/issues/1750">Issue #1750</a>
*/
public void testIssue1750() throws FileNotFoundException, URISyntaxException
{
String schemaFileName = "/schemas/issue1750/schema.xsd";
String packageName = "org.example.issue1750";
String someClassName = packageName + ".SomeJAXBElement";
String suidFieldName = "serialVersionUID";

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

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

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

// Assert Class includes narrow type
Iterator<JMethod> conIter = dc.constructors();
while ( conIter.hasNext() )
{
JMethod con = conIter.next();
assertTrue(toString(con).contains("java.lang.Class<java.lang.String>"));
}
}

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

<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<jaxb:serializable uid="20231001" />
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>

<xs:element name="someJAXBElement" type="xs:string">
<xs:annotation>
<xs:appinfo>
<jaxb:class name="SomeJAXBElement"/>
</xs:appinfo>
</xs:annotation>
</xs:element>

</xs:schema>

0 comments on commit d1c27f9

Please sign in to comment.