-
Notifications
You must be signed in to change notification settings - Fork 168
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
Handle xjc XmlEnumValue in DynamicJAXB enum #2273
Open
timtatt
wants to merge
1
commit into
eclipse-ee4j:master
Choose a base branch
from
timtatt:fix/enum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -223,7 +223,7 @@ protected void addMethods(ClassWriter cw, String parentClassType) { | |
|
||
protected byte[] createEnum(EnumInfo enumInfo) { | ||
|
||
String[] enumValues = enumInfo.getLiteralLabels(); | ||
var enumValues = enumInfo.getEnumValues(); | ||
String className = enumInfo.getClassName(); | ||
|
||
String internalClassName = className.replace('.', '/'); | ||
|
@@ -232,8 +232,10 @@ protected byte[] createEnum(EnumInfo enumInfo) { | |
cw.visit(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_SUPER + Opcodes.ACC_ENUM, internalClassName, null, "java/lang/Enum", null); | ||
|
||
// Add the individual enum values | ||
for (String enumValue : enumValues) { | ||
cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_ENUM, enumValue, "L" + internalClassName + ";", null, null); | ||
for (var enumValue : enumValues.entrySet()) { | ||
var fv = cw.visitField(Opcodes.ACC_PUBLIC + Opcodes.ACC_FINAL + Opcodes.ACC_STATIC + Opcodes.ACC_ENUM, enumValue.getKey(), "L" + internalClassName + ";", null, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var av = fv.visitAnnotation("Ljakarta/xml/bind/annotation/XmlEnumValue;", true); | ||
av.visit("value", enumValue.getValue()); | ||
} | ||
|
||
// add the synthetic "$VALUES" field | ||
|
@@ -270,8 +272,9 @@ protected byte[] createEnum(EnumInfo enumInfo) { | |
mv = cw.visitMethod(Opcodes.ACC_STATIC, CLINIT, "()V", null, null); | ||
|
||
int lastCount = 0; | ||
for (int i = 0; i < enumValues.length; i++) { | ||
String enumValue = enumValues[i]; | ||
var enumLiterals = enumInfo.getLiteralLabels(); | ||
for (int i = 0; i < enumLiterals.length; i++) { | ||
String enumValue = enumLiterals[i]; | ||
mv.visitTypeInsn(Opcodes.NEW, internalClassName); | ||
mv.visitInsn(Opcodes.DUP); | ||
mv.visitLdcInsn(enumValue); | ||
|
@@ -296,8 +299,8 @@ protected byte[] createEnum(EnumInfo enumInfo) { | |
} | ||
mv.visitTypeInsn(Opcodes.ANEWARRAY, internalClassName); | ||
|
||
for (int i = 0; i < enumValues.length; i++) { | ||
String enumValue = enumValues[i]; | ||
for (int i = 0; i < enumLiterals.length; i++) { | ||
String enumValue = enumLiterals[i]; | ||
mv.visitInsn(Opcodes.DUP); | ||
if (i <= 5) { | ||
mv.visitInsn(ICONST[i]); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,17 +17,17 @@ | |
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.*; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use specific imports instead of |
||
import java.util.stream.Collectors; | ||
|
||
import com.sun.codemodel.*; | ||
import jakarta.xml.bind.JAXBException; | ||
|
||
import javax.xml.transform.Source; | ||
import javax.xml.transform.TransformerException; | ||
import javax.xml.transform.stream.StreamResult; | ||
|
||
import jakarta.xml.bind.annotation.XmlEnumValue; | ||
import org.eclipse.persistence.dynamic.DynamicClassLoader; | ||
import org.eclipse.persistence.jaxb.dynamic.DynamicJAXBContextFactory; | ||
import org.eclipse.persistence.jaxb.javamodel.JavaClass; | ||
|
@@ -45,11 +45,6 @@ | |
import org.xml.sax.InputSource; | ||
import org.xml.sax.SAXParseException; | ||
|
||
import com.sun.codemodel.ClassType; | ||
import com.sun.codemodel.JCodeModel; | ||
import com.sun.codemodel.JDefinedClass; | ||
import com.sun.codemodel.JEnumConstant; | ||
import com.sun.codemodel.JPackage; | ||
import com.sun.tools.xjc.Plugin; | ||
import com.sun.tools.xjc.api.ErrorListener; | ||
import com.sun.tools.xjc.api.S2JJAXBModel; | ||
|
@@ -59,6 +54,7 @@ | |
public class SchemaMetadata extends Metadata { | ||
|
||
private static final String DEFAULT_SYSTEM_ID = "sysid"; | ||
private static final String XML_ENUM_VALUE_VALUE = "value"; | ||
|
||
private SchemaCompiler schemaCompiler; | ||
|
||
|
@@ -216,9 +212,7 @@ private JavaClass[] createClassModelFromXJC(ArrayList<JDefinedClass> xjcClasses, | |
// If this is an enum, trigger a dynamic class generation, because we won't | ||
// be creating a descriptor for it | ||
if (definedClass.getClassType().equals(ClassType.ENUM)) { | ||
Map<String, JEnumConstant> enumConstants = definedClass.enumConstants(); | ||
Object[] enumValues = enumConstants.keySet().toArray(); | ||
dynamicClassLoader.addEnum(definedClass.fullName(), enumValues); | ||
dynamicClassLoader.addEnum(definedClass.fullName(), getEnumValues(definedClass)); | ||
} | ||
} | ||
|
||
|
@@ -228,6 +222,23 @@ private JavaClass[] createClassModelFromXJC(ArrayList<JDefinedClass> xjcClasses, | |
} | ||
} | ||
|
||
private Map<String, String> getEnumValues(JDefinedClass definedClass) { | ||
return definedClass.enumConstants() | ||
.entrySet() | ||
.stream() | ||
.collect(Collectors.toMap(Map.Entry::getKey, entry -> | ||
entry.getValue() | ||
.annotations() | ||
.stream() | ||
.filter(annotation -> XmlEnumValue.class.getName().equals(annotation.getAnnotationClass().binaryName())) | ||
.map(annotation -> annotation.getAnnotationMembers().get(XML_ENUM_VALUE_VALUE)) | ||
.filter(value -> value instanceof JAnnotationStringValue) | ||
.map(Object::toString) | ||
.findFirst() | ||
.orElse(entry.getKey())) | ||
); | ||
} | ||
|
||
private static InputSource createInputSourceFromSource(Source aSource) { | ||
ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
StreamResult result = new StreamResult(baos); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
....moxy/src/test/resources/org/eclipse/persistence/testing/jaxb/dynamic/xmlenum-numbers.xsd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
|
||
Copyright (c) 2024, 2024 Oracle and/or its affiliates. All rights reserved. | ||
|
||
This program and the accompanying materials are made available under the | ||
terms of the Eclipse Public License v. 2.0 which is available at | ||
http://www.eclipse.org/legal/epl-2.0, | ||
or the Eclipse Distribution License v. 1.0 which is available at | ||
http://www.eclipse.org/org/documents/edl-v10.php. | ||
|
||
SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause | ||
|
||
--> | ||
|
||
<xs:schema targetNamespace="myNamespace" xmlns:myns="myNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" | ||
attributeFormDefault="qualified" elementFormDefault="qualified"> | ||
|
||
<xs:element name="tax-record"> | ||
<xs:complexType> | ||
<xs:sequence> | ||
<xs:element name="period" type="myns:period"/> | ||
</xs:sequence> | ||
</xs:complexType> | ||
</xs:element> | ||
|
||
<!-- This test contains 4 enums with digits to test value conversion for xjc enums, i.e. QTR1 is converted to enum QTR_1 --> | ||
<xs:simpleType name="period"> | ||
<xs:restriction base="xs:string"> | ||
<xs:enumeration value="QTR1"/> | ||
<xs:enumeration value="QTR2"/> | ||
<xs:enumeration value="QTR3"/> | ||
<xs:enumeration value="QTR4"/> | ||
</xs:restriction> | ||
</xs:simpleType> | ||
|
||
</xs:schema> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use specific type instead of
var
.