use of javax.xml.bind.annotation.XmlElement in project herd by FINRAOS.
the class DefinitionGenerator method processField.
/**
* Processes a Field of a model class which can be converted into a Swagger definition property. The property is added into the given model. This method may
* be called recursively.
*
* @param field the field to process.
* @param model model the model.
*
* @throws MojoExecutionException if any problems were encountered.
*/
private void processField(Field field, ModelImpl model) throws MojoExecutionException {
log.debug("Processing field \"" + field.getName() + "\".");
if (!Modifier.isStatic(field.getModifiers())) {
Property property;
Class<?> fieldClass = field.getType();
if (Collection.class.isAssignableFrom(fieldClass)) {
property = new ArrayProperty(getPropertyFromType(FieldUtils.getCollectionType(field)));
} else {
property = getPropertyFromType(fieldClass);
}
// Set the required field based on the XmlElement that comes from the XSD.
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null) {
property.setRequired(xmlElement.required());
}
if (xsdParser != null) {
property.setDescription(xsdParser.getAnnotation(model.getName(), field.getName()));
}
// Set the property on model.
model.property(field.getName(), property);
}
}
use of javax.xml.bind.annotation.XmlElement in project ibas-framework by color-coding.
the class Serializer method getSerializedElements.
private List<SchemaElement> getSerializedElements(Field[] fields) {
List<SchemaElement> elements = new ArrayList<>();
for (Field field : fields) {
Class<?> elementType = field.getType();
String elementName = field.getName();
String wrapperName = null;
XmlElementWrapper xmlWrapper = field.getAnnotation(XmlElementWrapper.class);
if (xmlWrapper != null) {
// 首先判断是否为数组元素
wrapperName = xmlWrapper.name();
}
XmlElement xmlElement = field.getAnnotation(XmlElement.class);
if (xmlElement != null) {
if (!xmlElement.name().equals("##default")) {
elementName = xmlElement.name();
}
if (xmlElement.type() != null && !xmlElement.type().getName().startsWith(XmlElement.class.getName())) {
elementType = xmlElement.type();
}
} else {
continue;
}
if (elementName == null) {
continue;
}
if (elementType == null) {
continue;
}
elements.add(new SchemaElement(elementName, wrapperName, elementType));
}
return elements;
}
use of javax.xml.bind.annotation.XmlElement in project ddf by codice.
the class AdaptedSourceResponse method getMetacard.
@XmlElement(namespace = METACARD_URI)
public List<MetacardElement> getMetacard() {
List<MetacardElement> metacards = new ArrayList<MetacardElement>();
for (Result r : delegate.getResults()) {
Metacard metacard = r.getMetacard();
if (metacard == null) {
continue;
}
MetacardElement element = new MetacardElement();
element.setId(metacard.getId());
element.setSource(metacard.getSourceId());
if (metacard.getMetacardType() != null) {
String metacardTypeName = MetacardImpl.BASIC_METACARD.getName();
if (isNotBlank(metacard.getMetacardType().getName())) {
metacardTypeName = metacard.getMetacardType().getName();
}
element.setType(metacardTypeName);
AttributeAdapter attributeAdapter = new AttributeAdapter(metacard.getMetacardType());
for (AttributeDescriptor descriptor : metacard.getMetacardType().getAttributeDescriptors()) {
try {
element.getAttributes().add(attributeAdapter.marshal(metacard.getAttribute(descriptor.getName())));
} catch (CatalogTransformerException e) {
LOGGER.info("Marshalling error with attribute", e);
}
}
}
metacards.add(element);
}
return metacards;
}
use of javax.xml.bind.annotation.XmlElement in project tomee by apache.
the class JAXBDataBinding method createWrapperHelper.
public WrapperHelper createWrapperHelper(Class<?> wrapperType, QName wrapperName, List<String> partNames, List<String> elTypeNames, List<Class<?>> partClasses) {
List<Method> getMethods = new ArrayList<>(partNames.size());
List<Method> setMethods = new ArrayList<>(partNames.size());
List<Method> jaxbMethods = new ArrayList<>(partNames.size());
List<Field> fields = new ArrayList<>(partNames.size());
Method[] allMethods = wrapperType.getMethods();
String packageName = PackageUtils.getPackageName(wrapperType);
// if wrappertype class is generated by ASM, getPackage() always return null
if (wrapperType.getPackage() != null) {
packageName = wrapperType.getPackage().getName();
}
String objectFactoryClassName = packageName + ".ObjectFactory";
Object objectFactory = null;
try {
objectFactory = wrapperType.getClassLoader().loadClass(objectFactoryClassName).newInstance();
} catch (Exception e) {
// ignore, probably won't need it
}
Method[] allOFMethods;
if (objectFactory != null) {
allOFMethods = objectFactory.getClass().getMethods();
} else {
allOFMethods = new Method[0];
}
for (int x = 0; x < partNames.size(); x++) {
String partName = partNames.get(x);
if (partName == null) {
getMethods.add(null);
setMethods.add(null);
fields.add(null);
jaxbMethods.add(null);
continue;
}
String elementType = elTypeNames.get(x);
String getAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.GETTER);
String setAccessor = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.SETTER);
Method getMethod = null;
Method setMethod = null;
Class<?> valueClass = wrapperType;
try {
getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
} catch (NoSuchMethodException ex) {
// ignore for now
}
Field elField = getElField(partName, valueClass);
if (getMethod == null && elementType != null && "boolean".equalsIgnoreCase(elementType) && (elField == null || (!Collection.class.isAssignableFrom(elField.getType()) && !elField.getType().isArray()))) {
try {
String newAcc = getAccessor.replaceFirst("get", "is");
getMethod = wrapperType.getMethod(newAcc, AbstractWrapperHelper.NO_CLASSES);
} catch (NoSuchMethodException ex) {
// ignore for now
}
}
if (getMethod == null && "return".equals(partName)) {
// RI generated code uses this
try {
getMethod = valueClass.getMethod("get_return", AbstractWrapperHelper.NO_CLASSES);
} catch (NoSuchMethodException ex) {
try {
getMethod = valueClass.getMethod("is_return", new Class[0]);
} catch (NoSuchMethodException ex2) {
// ignore for now
}
}
}
if (getMethod == null && elField != null) {
getAccessor = JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.GETTER);
setAccessor = JAXBUtils.nameToIdentifier(elField.getName(), JAXBUtils.IdentifierType.SETTER);
try {
getMethod = valueClass.getMethod(getAccessor, AbstractWrapperHelper.NO_CLASSES);
} catch (NoSuchMethodException ex) {
// ignore for now
}
}
String setAccessor2 = setAccessor;
if ("return".equals(partName)) {
// some versions of jaxb map "return" to "set_return" instead of "setReturn"
setAccessor2 = "set_return";
}
for (Method method : allMethods) {
if (method.getParameterTypes() != null && method.getParameterTypes().length == 1 && (setAccessor.equals(method.getName()) || setAccessor2.equals(method.getName()))) {
setMethod = method;
break;
}
}
getMethods.add(getMethod);
setMethods.add(setMethod);
if (setMethod != null && JAXBElement.class.isAssignableFrom(setMethod.getParameterTypes()[0])) {
Type t = setMethod.getGenericParameterTypes()[0];
Class<?> pcls = null;
if (t instanceof ParameterizedType) {
t = ((ParameterizedType) t).getActualTypeArguments()[0];
}
if (t instanceof Class) {
pcls = (Class<?>) t;
}
String methodName = "create" + wrapperType.getSimpleName() + setMethod.getName().substring(3);
for (Method m : allOFMethods) {
if (m.getName().equals(methodName) && m.getParameterTypes().length == 1 && (pcls == null || pcls.equals(m.getParameterTypes()[0]))) {
jaxbMethods.add(m);
}
}
} else {
jaxbMethods.add(null);
}
if (elField != null) {
// JAXB Type get XmlElement Annotation
XmlElement el = elField.getAnnotation(XmlElement.class);
if (el != null && (partName.equals(el.name()) || "##default".equals(el.name()))) {
ReflectionUtil.setAccessible(elField);
fields.add(elField);
} else {
if (getMethod == null && setMethod == null) {
if (el != null) {
LOG.warning("Could not create accessor for property " + partName + " of type " + wrapperType.getName() + " as the @XmlElement " + "defines the name as " + el.name());
} else {
LOG.warning("Could not create accessor for property " + partName + " of type " + wrapperType.getName());
}
}
fields.add(null);
}
} else {
fields.add(null);
}
}
return createWrapperHelper(getBus(), wrapperType, setMethods.toArray(new Method[0]), getMethods.toArray(new Method[0]), jaxbMethods.toArray(new Method[0]), fields.toArray(new Field[0]), objectFactory);
}
use of javax.xml.bind.annotation.XmlElement in project tomee by apache.
the class JAXBDataBinding method getElField.
private static Field getElField(String partName, final Class<?> wrapperType) {
String fieldName = JAXBUtils.nameToIdentifier(partName, JAXBUtils.IdentifierType.VARIABLE);
Field[] fields = ReflectionUtil.getDeclaredFields(wrapperType);
for (Field field : fields) {
XmlElement el = field.getAnnotation(XmlElement.class);
if (el != null && partName.equals(el.name())) {
return field;
}
XmlElementRef xmlElementRefAnnotation = field.getAnnotation(XmlElementRef.class);
if (xmlElementRefAnnotation != null && partName.equals(xmlElementRefAnnotation.name())) {
return field;
}
if (field.getName().equals(fieldName)) {
return field;
}
}
return null;
}
Aggregations