use of jakarta.xml.bind.annotation.XmlAttribute in project glassfish-hk2 by eclipse-ee4j.
the class ModelImpl method getJavaNameFromKey.
public synchronized String getJavaNameFromKey(String key, ClassReflectionHelper reflectionHelper) {
if (keyToJavaNameMap == null) {
keyToJavaNameMap = new LinkedHashMap<String, String>();
}
String result = keyToJavaNameMap.get(key);
if (result != null)
return result;
if (reflectionHelper == null)
return null;
Class<?> originalInterface = getOriginalInterfaceAsClass();
for (MethodWrapper wrapper : reflectionHelper.getAllMethods(originalInterface)) {
Method m = wrapper.getMethod();
String xmlName;
XmlElement element = m.getAnnotation(XmlElement.class);
if (element == null) {
XmlAttribute attribute = m.getAnnotation(XmlAttribute.class);
if (attribute == null)
continue;
xmlName = attribute.name();
} else {
xmlName = element.name();
}
String keyName;
String javaName = getJavaNameFromGetterOrSetter(m, reflectionHelper);
if ("##default".equals(xmlName)) {
keyName = javaName;
} else {
keyName = xmlName;
}
if (!key.equals(keyName))
continue;
// Found it!
keyToJavaNameMap.put(key, javaName);
return javaName;
}
return null;
}
use of jakarta.xml.bind.annotation.XmlAttribute in project eclipselink by eclipse-ee4j.
the class BeanValidationBindingsTestCase method testAllFacetsAndAnnotations.
/**
* Tests that the XJC detects all facets and generates their respective
* annotations correctly.
*/
public void testAllFacetsAndAnnotations() throws Throwable {
pkg = "rs";
Class<?>[] c = roundTrip(RICH_SCHEMA_PATH, pkg);
Class<?> Main = c[0];
Class<?> Numbers = c[1];
Class<?> NumberWithHiddenValueAttribute = c[2];
Class<?> Strings = c[4];
XmlElement xmlElement;
Size size;
DecimalMax decimalMax;
DecimalMin decimalMin;
Pattern.List patternList;
Pattern pattern;
/* Main.class */
Field numbers = Main.getDeclaredField("numbers");
assertNotNull(numbers.getAnnotation(Valid.class));
assertNotNull(numbers.getAnnotation(NotNull.class));
xmlElement = numbers.getAnnotation(XmlElement.class);
assertFalse(xmlElement.nillable());
Field strings = Main.getDeclaredField("strings");
size = strings.getAnnotation(Size.class);
assertTrue(size.min() == 1 && size.max() == 2);
assertNotNull(strings.getAnnotation(Valid.class));
assertNotNull(strings.getAnnotation(NotNull.class));
xmlElement = strings.getAnnotation(XmlElement.class);
assertFalse(xmlElement.nillable());
Field unsignedByte = Main.getDeclaredField("unsignedByte");
decimalMax = unsignedByte.getAnnotation(DecimalMax.class);
assertEquals(decimalMax.value(), "255");
assertTrue(decimalMax.inclusive());
decimalMin = unsignedByte.getAnnotation(DecimalMin.class);
assertEquals(decimalMin.value(), "0");
assertTrue(decimalMin.inclusive());
Field byteArray = Main.getDeclaredField("byteArray");
size = byteArray.getAnnotation(Size.class);
assertTrue(size.max() == 18);
Field someCollection = Main.getDeclaredField("someCollection");
size = someCollection.getAnnotation(Size.class);
assertTrue(size.min() == 1);
assertNotNull(someCollection.getAnnotation(Valid.class));
assertNotNull(someCollection.getAnnotation(NotNull.class));
Field optionalElement = Main.getDeclaredField("optionalElement");
size = optionalElement.getAnnotation(Size.class);
assertTrue(size.min() == 0);
assertNotNull(optionalElement.getAnnotation(Valid.class));
assertNull(optionalElement.getAnnotation(NotNull.class));
Field optionalElementWithSimpleType = Main.getDeclaredField("optionalElementWithSimpleType");
assertNull(optionalElementWithSimpleType.getAnnotation(NotNull.class));
decimalMin = optionalElementWithSimpleType.getAnnotation(DecimalMin.class);
assertEquals(decimalMin.value(), "1");
decimalMax = optionalElementWithSimpleType.getAnnotation(DecimalMax.class);
assertEquals(decimalMax.value(), "31");
/* Numbers.class */
Field minInclusive = Numbers.getDeclaredField("minInclusive");
decimalMin = minInclusive.getAnnotation(DecimalMin.class);
assertEquals(decimalMin.value(), "1000");
assertTrue(decimalMin.inclusive());
Field maxInclusive = Numbers.getDeclaredField("maxInclusive");
decimalMax = maxInclusive.getAnnotation(DecimalMax.class);
assertEquals(decimalMax.value(), "1000");
assertTrue(decimalMax.inclusive());
Field minExclusive = Numbers.getDeclaredField("minExclusive");
decimalMin = minExclusive.getAnnotation(DecimalMin.class);
assertEquals(decimalMin.value(), "0");
assertFalse(decimalMin.inclusive());
Field maxExclusive = Numbers.getDeclaredField("maxExclusive");
decimalMax = maxExclusive.getAnnotation(DecimalMax.class);
assertEquals(decimalMax.value(), "1000");
assertFalse(decimalMax.inclusive());
Field minMaxExclusive = Numbers.getDeclaredField("minMaxExclusive");
decimalMax = minMaxExclusive.getAnnotation(DecimalMax.class);
assertEquals(decimalMax.value(), "9223372");
assertFalse(decimalMax.inclusive());
decimalMin = minMaxExclusive.getAnnotation(DecimalMin.class);
assertEquals(decimalMin.value(), "0");
assertFalse(decimalMin.inclusive());
/* NumberWithHiddenValueAttribute.class */
Field value = NumberWithHiddenValueAttribute.getDeclaredField("value");
assertNotNull(value.getAnnotation(XmlValue.class));
size = value.getAnnotation(Size.class);
assertTrue(size.min() == 1 && size.max() == 5);
Field code = NumberWithHiddenValueAttribute.getDeclaredField("code");
assertNotNull(code.getAnnotation(NotNull.class));
Field whatNumber = NumberWithHiddenValueAttribute.getDeclaredField("whatNumber");
XmlAttribute xmlAttribute = whatNumber.getAnnotation(XmlAttribute.class);
assertTrue(xmlAttribute.required());
/* Strings.class */
Field regexShorthands = Strings.getDeclaredField("regexShorthands");
patternList = regexShorthands.getAnnotation(Pattern.List.class);
Pattern[] patterns = patternList.value();
assertEquals("[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[0].regexp());
assertEquals("[^:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[1].regexp());
assertEquals("[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[2].regexp());
assertEquals("[^-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]", patterns[3].regexp());
assertEquals("\\p{Nd}", patterns[4].regexp());
assertEquals("\\P{Nd}", patterns[5].regexp());
assertEquals("[\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[6].regexp());
assertEquals("[^\\u0009-\\u000D\\u0020\\u0085\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u2028\\u2029\\u202F\\u205F\\u3000]", patterns[7].regexp());
assertEquals("[\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000-\\u200A\\u202F\\u205F\\u3000]", patterns[8].regexp());
assertEquals("[^\\u0009\\u0020\\u00A0\\u1680\\u180E\\u2000\\u2001-\\u200A\\u202F\\u205F\\u3000]", patterns[9].regexp());
assertEquals("[^\\u000A-\\u000D\\u0085\\u2028\\u2029]", patterns[10].regexp());
assertEquals("(?:(?>\\u000D\\u000A)|[\\u000A\\u000B\\u000C\\u000D\\u0085\\u2028\\u2029])", patterns[11].regexp());
Field idType = Strings.getDeclaredField("idType");
pattern = idType.getAnnotation(Pattern.class);
assertEquals(pattern.regexp(), "[[:A-Z_a-z\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u02FF\\u0370-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]][[-.0-9:A-Z_a-z\\u00B7\\u00C0-\\u00D6\\u00D8-\\u00F6\\u00F8-\\u037D\\u037F-\\u1FFF\\u200C-\\u200D\\u203F\\u2040\\u2070-\\u218F\\u2C00-\\u2FEF\\u3001-\\uD7FF\\uF900-\\uFDCF\\uFDF0-\\uFFFD]-[:]]*");
assertTrue(idType.getAnnotation(Size.class).max() == 100);
Field genericString = Strings.getDeclaredField("genericString");
assertTrue(genericString.getAnnotation(Size.class).min() == 0);
assertTrue(genericString.getAnnotation(Size.class).max() == 1024);
Field maxLength = Strings.getDeclaredField("maxLength");
assertTrue(maxLength.getAnnotation(Size.class).max() == 1024);
Field minLength = Strings.getDeclaredField("minLength");
assertTrue(minLength.getAnnotation(Size.class).min() == 0);
}
use of jakarta.xml.bind.annotation.XmlAttribute in project jpmml-converter by jpmml.
the class AttributeCleaner method visit.
@Override
public VisitorAction visit(PMMLObject object) {
Map<Field, Method> getterMethods = ReflectionUtil.getGetterMethods(object.getClass());
Collection<Map.Entry<Field, Method>> entries = getterMethods.entrySet();
for (Map.Entry<Field, Method> entry : entries) {
Field field = entry.getKey();
Method getterMethod = entry.getValue();
XmlAttribute attribute = field.getAnnotation(XmlAttribute.class);
if (attribute == null || attribute.required()) {
continue;
}
Object fieldValue = ReflectionUtil.getFieldValue(field, object);
if (fieldValue != null) {
Object getterMethodValue = ReflectionUtil.getGetterMethodValue(getterMethod, object);
if (Objects.equals(fieldValue, getterMethodValue)) {
ReflectionUtil.setFieldValue(field, object, null);
Object defaultGetterMethodValue = ReflectionUtil.getGetterMethodValue(getterMethod, object);
if (defaultGetterMethodValue == null || !Objects.equals(fieldValue, defaultGetterMethodValue)) {
ReflectionUtil.setFieldValue(field, object, fieldValue);
}
}
}
}
return super.visit(object);
}
use of jakarta.xml.bind.annotation.XmlAttribute in project glassfish-hk2 by eclipse-ee4j.
the class GeneratorUtilities method getXmlNameMap.
public static NameInformation getXmlNameMap(AltClass convertMe) {
Map<String, XmlElementData> xmlNameMap = new LinkedHashMap<String, XmlElementData>();
Set<String> unmappedNames = new LinkedHashSet<String>();
Map<String, String> addMethodToVariableMap = new LinkedHashMap<String, String>();
Map<String, String> removeMethodToVariableMap = new LinkedHashMap<String, String>();
Map<String, String> lookupMethodToVariableMap = new LinkedHashMap<String, String>();
Set<String> referenceSet = new LinkedHashSet<String>();
Map<String, List<XmlElementData>> aliasMap = new LinkedHashMap<String, List<XmlElementData>>();
XmlElementData valueData = null;
XmlElementData xmlAnyAttributeData = null;
boolean hasAnElement = false;
for (AltMethod originalMethod : convertMe.getMethods()) {
String originalMethodName = originalMethod.getName();
String setterVariable = Utilities.isSetter(originalMethod);
if (setterVariable == null) {
setterVariable = Utilities.isGetter(originalMethod);
if (setterVariable == null)
continue;
}
if (isSpecifiedReference(originalMethod)) {
referenceSet.add(setterVariable);
}
AltAnnotation pluralOf = null;
AltAnnotation xmlElement = originalMethod.getAnnotation(XmlElement.class.getName());
AltAnnotation xmlElements = originalMethod.getAnnotation(XmlElements.class.getName());
AltAnnotation xmlElementWrapper = originalMethod.getAnnotation(XmlElementWrapper.class.getName());
AltAnnotation xmlAttribute = originalMethod.getAnnotation(XmlAttribute.class.getName());
AltAnnotation xmlValue = originalMethod.getAnnotation(XmlValue.class.getName());
AltAnnotation xmlAnyAttribute = originalMethod.getAnnotation(XmlAnyAttribute.class.getName());
String xmlElementWrapperName = (xmlElementWrapper == null) ? null : xmlElementWrapper.getStringValue("name");
if (xmlElementWrapperName != null && xmlElementWrapperName.isEmpty()) {
xmlElementWrapperName = setterVariable;
}
checkOnlyOne(convertMe, originalMethod, xmlElement, xmlElements);
checkOnlyOne(convertMe, originalMethod, xmlElement, xmlAttribute);
checkOnlyOne(convertMe, originalMethod, xmlElements, xmlAttribute);
checkOnlyOne(convertMe, originalMethod, xmlElement, xmlValue);
checkOnlyOne(convertMe, originalMethod, xmlElements, xmlValue);
checkOnlyOne(convertMe, originalMethod, xmlAttribute, xmlValue);
checkOnlyOne(convertMe, originalMethod, xmlElement, xmlAnyAttribute);
checkOnlyOne(convertMe, originalMethod, xmlElements, xmlAnyAttribute);
checkOnlyOne(convertMe, originalMethod, xmlAttribute, xmlAnyAttribute);
checkOnlyOne(convertMe, originalMethod, xmlValue, xmlAnyAttribute);
if (xmlElements != null) {
hasAnElement = true;
// First add the actual method so it is known to the system
pluralOf = originalMethod.getAnnotation(PluralOf.class.getName());
String defaultValue = Generator.JAXB_DEFAULT_DEFAULT;
xmlNameMap.put(setterVariable, new XmlElementData("", setterVariable, setterVariable, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, false, originalMethodName));
String aliasName = setterVariable;
AltAnnotation[] allXmlElements = xmlElements.getAnnotationArrayValue("value");
List<XmlElementData> aliases = new ArrayList<XmlElementData>(allXmlElements.length);
aliasMap.put(setterVariable, aliases);
for (AltAnnotation allXmlElement : allXmlElements) {
defaultValue = allXmlElement.getStringValue("defaultValue");
String allXmlElementNamespace = allXmlElement.getStringValue("namespace");
String allXmlElementName = allXmlElement.getStringValue("name");
boolean allXmlElementRequired = allXmlElement.getBooleanValue("required");
AltClass allXmlElementType = (AltClass) allXmlElement.getAnnotationValues().get("type");
String allXmlElementTypeName = (allXmlElementType == null) ? null : allXmlElementType.getName();
boolean allXmlElementTypeInterface = (allXmlElementType == null) ? true : allXmlElementType.isInterface();
if (Generator.JAXB_DEFAULT_STRING.equals(allXmlElementName)) {
throw new IllegalArgumentException("The name field of an XmlElement inside an XmlElements must have a specified name");
} else {
aliases.add(new XmlElementData(allXmlElementNamespace, allXmlElementName, aliasName, defaultValue, Format.ELEMENT, allXmlElementTypeName, allXmlElementTypeInterface, xmlElementWrapperName, allXmlElementRequired, originalMethodName));
}
}
} else if (xmlElement != null) {
hasAnElement = true;
// Get the pluralOf from the method
pluralOf = originalMethod.getAnnotation(PluralOf.class.getName());
String defaultValue = xmlElement.getStringValue("defaultValue");
String namespace = xmlElement.getStringValue("namespace");
String name = xmlElement.getStringValue("name");
boolean required = xmlElement.getBooleanValue("required");
if (Generator.JAXB_DEFAULT_STRING.equals(name)) {
xmlNameMap.put(setterVariable, new XmlElementData(namespace, setterVariable, setterVariable, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, required, originalMethodName));
} else {
xmlNameMap.put(setterVariable, new XmlElementData(namespace, name, name, defaultValue, Format.ELEMENT, null, true, xmlElementWrapperName, required, originalMethodName));
}
} else if (xmlAttribute != null) {
String namespace = xmlAttribute.getStringValue("namespace");
String name = xmlAttribute.getStringValue("name");
boolean required = xmlAttribute.getBooleanValue("required");
if (Generator.JAXB_DEFAULT_STRING.equals(name)) {
xmlNameMap.put(setterVariable, new XmlElementData(namespace, setterVariable, setterVariable, Generator.JAXB_DEFAULT_DEFAULT, Format.ATTRIBUTE, null, true, xmlElementWrapperName, required, originalMethodName));
} else {
xmlNameMap.put(setterVariable, new XmlElementData(namespace, name, name, Generator.JAXB_DEFAULT_DEFAULT, Format.ATTRIBUTE, null, true, xmlElementWrapperName, required, originalMethodName));
}
} else if (xmlValue != null) {
if (valueData != null) {
throw new IllegalArgumentException("There may be only one XmlValue method on " + convertMe);
}
valueData = new XmlElementData(XmlService.DEFAULT_NAMESPACE, XML_VALUE_LOCAL_PART, XML_VALUE_LOCAL_PART, null, Format.VALUE, null, false, xmlElementWrapperName, true, originalMethodName);
xmlNameMap.put(setterVariable, valueData);
} else if (xmlAnyAttribute != null) {
if (xmlAnyAttributeData != null) {
throw new IllegalArgumentException("There may be only one XmlAnyAttribute method on " + convertMe);
}
xmlAnyAttributeData = new XmlElementData(XmlService.DEFAULT_NAMESPACE, XML_ANY_ATTRIBUTE_LOCAL_PART, XML_ANY_ATTRIBUTE_LOCAL_PART, null, Format.ATTRIBUTE, null, false, xmlElementWrapperName, false, originalMethodName);
xmlNameMap.put(setterVariable, xmlAnyAttributeData);
} else {
unmappedNames.add(setterVariable);
}
if (pluralOf == null)
pluralOf = new AnnotationAltAnnotationImpl(new PluralOfDefault(), null);
String unDecapitalizedVariable = originalMethod.getName().substring(3);
addMethodToVariableMap.put(getMethodName(MethodType.ADD, unDecapitalizedVariable, pluralOf), setterVariable);
removeMethodToVariableMap.put(getMethodName(MethodType.REMOVE, unDecapitalizedVariable, pluralOf), setterVariable);
lookupMethodToVariableMap.put(getMethodName(MethodType.LOOKUP, unDecapitalizedVariable, pluralOf), setterVariable);
}
if (valueData != null && hasAnElement) {
throw new IllegalArgumentException("A bean cannot both have XmlElements and XmlValue methods in " + convertMe);
}
Set<String> noXmlElementNames = new LinkedHashSet<String>();
for (String unmappedName : unmappedNames) {
if (!xmlNameMap.containsKey(unmappedName)) {
noXmlElementNames.add(unmappedName);
}
}
return new NameInformation(xmlNameMap, noXmlElementNames, addMethodToVariableMap, removeMethodToVariableMap, lookupMethodToVariableMap, referenceSet, aliasMap, valueData);
}
use of jakarta.xml.bind.annotation.XmlAttribute in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method getQNameForProperty.
public QName getQNameForProperty(Property property, String defaultName, JavaHasAnnotations element, NamespaceInfo namespaceInfo, TypeInfo info) {
String uri = info.getClassNamespace();
String name = XMLProcessor.DEFAULT;
String namespace = XMLProcessor.DEFAULT;
QName qName = null;
if (property.isMap()) {
isDefaultNamespaceAllowed = false;
}
if (helper.isAnnotationPresent(element, XmlAttribute.class)) {
XmlAttribute xmlAttribute = (XmlAttribute) helper.getAnnotation(element, XmlAttribute.class);
name = xmlAttribute.name();
namespace = xmlAttribute.namespace();
if (name.equals(XMLProcessor.DEFAULT)) {
name = defaultName;
try {
name = info.getXmlNameTransformer().transformAttributeName(name);
} catch (Exception ex) {
throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(name, info.getXmlNameTransformer().getClass().getName(), ex);
}
}
if (!namespace.equals(XMLProcessor.DEFAULT)) {
qName = new QName(namespace, name);
isDefaultNamespaceAllowed = false;
} else {
if (namespaceInfo.isAttributeFormQualified()) {
qName = new QName(uri, name);
isDefaultNamespaceAllowed = false;
} else {
qName = new QName(name);
}
}
} else {
if (helper.isAnnotationPresent(element, XmlElement.class)) {
XmlElement xmlElement = (XmlElement) helper.getAnnotation(element, XmlElement.class);
name = xmlElement.name();
namespace = xmlElement.namespace();
}
if (property.isMap() && helper.isAnnotationPresent(element, XmlElementWrapper.class)) {
XmlElementWrapper xmlElementWrapper = (XmlElementWrapper) helper.getAnnotation(element, XmlElementWrapper.class);
name = xmlElementWrapper.name();
namespace = xmlElementWrapper.namespace();
}
if (name.equals(XMLProcessor.DEFAULT)) {
name = defaultName;
try {
name = info.getXmlNameTransformer().transformElementName(name);
} catch (Exception ex) {
throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(name, info.getXmlNameTransformer().getClass().getName(), ex);
}
}
if (!namespace.equals(XMLProcessor.DEFAULT)) {
qName = new QName(namespace, name);
if (namespace.equals(Constants.EMPTY_STRING)) {
isDefaultNamespaceAllowed = false;
}
} else {
if (namespaceInfo.isElementFormQualified()) {
qName = new QName(uri, name);
} else {
qName = new QName(name);
}
}
}
return qName;
}
Aggregations