use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class ElementRef method loadRef.
/**
* Load the qname of the referenced root element declaration.
*
* @return the qname of the referenced root element declaration.
*/
protected ReferencedElement loadRef() {
DecoratedTypeMirror refType = null;
if (xmlElementRef != null) {
refType = Annotations.mirrorOf(new Callable<Class<?>>() {
@Override
public Class<?> call() throws Exception {
return xmlElementRef.type();
}
}, this.env, XmlElementRef.DEFAULT.class);
}
if (refType == null) {
refType = getBareAccessorType();
}
TypeElement declaration = null;
if (refType.isDeclared()) {
declaration = (TypeElement) ((DeclaredType) refType).asElement();
}
ReferencedElement referencedElement = null;
if (refType.isInstanceOf(JAXBElement.class)) {
String localName = xmlElementRef != null && !"##default".equals(xmlElementRef.name()) ? xmlElementRef.name() : null;
String namespace = xmlElementRef != null ? xmlElementRef.namespace() : "";
if (localName == null) {
throw new EnunciateException("Member " + getName() + " of " + getTypeDefinition().getQualifiedName() + ": @XmlElementRef annotates a type JAXBElement without specifying the name of the JAXB element.");
}
QName refQname = new QName(namespace, localName);
List<? extends TypeMirror> elementTypeArguments = ((DecoratedDeclaredType) refType).getTypeArguments();
DecoratedTypeMirror elementOf = elementTypeArguments == null || elementTypeArguments.size() != 1 ? TypeMirrorUtils.objectType(context.getContext().getProcessingEnvironment()) : (DecoratedTypeMirror) elementTypeArguments.get(0);
referencedElement = new TypeReferencedElement(refQname, elementOf);
} else if (declaration != null && declaration.getAnnotation(XmlRootElement.class) != null) {
RootElementDeclaration refElement = new RootElementDeclaration(declaration, null, this.context);
referencedElement = new DeclarationReferencedElement(new QName(refElement.getNamespace(), refElement.getName()), declaration);
}
if (referencedElement == null) {
throw new EnunciateException("Member " + getSimpleName() + " of " + getTypeDefinition().getQualifiedName() + ": " + refType + " is neither JAXBElement nor a root element declaration.");
}
return referencedElement;
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class DataTypeExampleImpl method getBody.
@Override
public String getBody() {
ObjectNode node = JsonNodeFactory.instance.objectNode();
Context context = new Context();
context.stack = new LinkedList<String>();
build(node, this.type, this.type, context);
if (this.type.getContext().isWrapRootValue()) {
ObjectNode wrappedNode = JsonNodeFactory.instance.objectNode();
wrappedNode.set(this.type.getJsonRootName(), node);
node = wrappedNode;
}
if (isWrappedSubclass(this.type)) {
ObjectNode wrappedNode = JsonNodeFactory.instance.objectNode();
wrappedNode.set(this.type.getTypeIdValue(), node);
node = wrappedNode;
}
JsonNode outer = node;
for (DataTypeReference.ContainerType container : this.containers) {
switch(container) {
case array:
case collection:
case list:
ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
arrayNode.add(outer);
outer = arrayNode;
break;
case map:
ObjectNode mapNode = JsonNodeFactory.instance.objectNode();
mapNode.set("...", outer);
outer = mapNode;
break;
}
}
try {
return MAPPER.writeValueAsString(outer);
} catch (JsonProcessingException e) {
throw new EnunciateException(e);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class DataTypeExampleImpl method getBody.
@Override
public String getBody() {
ObjectNode node = JsonNodeFactory.instance.objectNode();
Context context = new Context();
context.stack = new LinkedList<String>();
build(node, this.type, this.type, context);
if (this.type.getContext().isWrapRootValue()) {
ObjectNode wrappedNode = JsonNodeFactory.instance.objectNode();
wrappedNode.put(this.type.getJsonRootName(), node);
node = wrappedNode;
}
if (isWrappedSubclass(this.type)) {
ObjectNode wrappedNode = JsonNodeFactory.instance.objectNode();
wrappedNode.put(this.type.getTypeIdValue(), node);
node = wrappedNode;
}
JsonNode outer = node;
for (DataTypeReference.ContainerType container : this.containers) {
switch(container) {
case array:
case collection:
case list:
ArrayNode arrayNode = JsonNodeFactory.instance.arrayNode();
arrayNode.add(outer);
outer = arrayNode;
break;
case map:
ObjectNode mapNode = JsonNodeFactory.instance.objectNode();
mapNode.put("...", outer);
outer = mapNode;
break;
}
}
try {
return MAPPER.writeValueAsString(outer);
} catch (JsonProcessingException e) {
throw new EnunciateException(e);
} catch (IOException e) {
throw new EnunciateException(e);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class QNameEnumTypeDefinition method loadEnumValues.
@Override
protected List<EnumValue> loadEnumValues() {
List<VariableElement> enumConstants = enumValues();
List<EnumValue> enumValueMap = new ArrayList<EnumValue>();
HashSet<String> enumValues = new HashSet<String>(enumConstants.size());
VariableElement unknownQNameConstant = null;
for (VariableElement enumConstant : enumConstants) {
if (isIgnored(enumConstant)) {
continue;
}
XmlUnknownQNameEnumValue unknownQNameEnumValue = enumConstant.getAnnotation(XmlUnknownQNameEnumValue.class);
if (unknownQNameEnumValue != null) {
if (unknownQNameConstant != null) {
throw new EnunciateException(getQualifiedName() + ": no more than two constants can be annotated with @XmlUnknownQNameEnumValue.");
}
unknownQNameConstant = enumConstant;
continue;
}
String ns = this.namespace;
String localPart = enumConstant.getSimpleName().toString();
XmlQNameEnumValue enumValueInfo = enumConstant.getAnnotation(XmlQNameEnumValue.class);
if (enumValueInfo != null) {
if (enumValueInfo.exclude()) {
continue;
}
if (!"##default".equals(enumValueInfo.namespace())) {
ns = enumValueInfo.namespace();
}
if (!"##default".equals(enumValueInfo.localPart())) {
localPart = enumValueInfo.localPart();
}
}
String uri = ns + localPart;
if (!enumValues.add(uri)) {
throw new EnunciateException(getQualifiedName() + ": duplicate qname enum value: " + uri);
}
enumValueMap.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), uri));
}
if (unknownQNameConstant != null) {
// enter the unknown qname constant as a null qname.
enumValueMap.add(new EnumValue(this, unknownQNameConstant, unknownQNameConstant.getSimpleName().toString(), null));
}
return enumValueMap;
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class TypeDefinition method aggregatePotentialAccessors.
/**
* Aggregate the potential accessor into their separate buckets for the given class declaration, recursively including transient superclasses.
*
* @param bag The collected fields and properties.
* @param clazz The class.
* @param filter The filter.
*/
protected void aggregatePotentialAccessors(AccessorBag bag, DecoratedTypeElement clazz, AccessorFilter filter, boolean inlineAccessorsOfSuperclasses) {
String fqn = clazz.getQualifiedName().toString();
if (Object.class.getName().equals(fqn) || Enum.class.getName().equals(fqn)) {
return;
}
if (bag.typeIdProperty == null) {
final JsonTypeInfo info = clazz.getAnnotation(JsonTypeInfo.class);
if (info != null && !info.property().isEmpty()) {
bag.typeIdProperty = info.property();
}
}
DecoratedTypeElement superDeclaration = clazz.getSuperclass() != null ? (DecoratedTypeElement) this.env.getTypeUtils().asElement(clazz.getSuperclass()) : null;
if (superDeclaration != null && (this.context.isIgnored(superDeclaration) || inlineAccessorsOfSuperclasses)) {
inlineAccessorsOfSuperclasses = true;
aggregatePotentialAccessors(bag, superDeclaration, filter, true);
}
TypeElement mixin = this.context.lookupMixin(clazz);
if (mixin == null) {
DeclaredType as = refineType(env, new DecoratedTypeElement(clazz, env), JsonSerialize.class, JsonSerialize::as);
if (as == null) {
as = refineType(env, new DecoratedTypeElement(clazz, env), JsonDeserialize.class, JsonDeserialize::as);
}
if (as != null) {
mixin = (TypeElement) as.asElement();
}
}
List<VariableElement> fieldElements = new ArrayList<VariableElement>(ElementFilter.fieldsIn(clazz.getEnclosedElements()));
if (mixin != null) {
// replace all mixin fields.
for (VariableElement mixinField : ElementFilter.fieldsIn(mixin.getEnclosedElements())) {
int index = indexOf(fieldElements, mixinField.getSimpleName().toString());
if (index >= 0) {
fieldElements.set(index, mixinField);
} else {
fieldElements.add(mixinField);
}
}
}
Set<String> propsIgnore = new HashSet<String>();
for (VariableElement fieldDeclaration : fieldElements) {
JsonUnwrapped unwrapped = fieldDeclaration.getAnnotation(JsonUnwrapped.class);
if (unwrapped != null && unwrapped.enabled()) {
DecoratedTypeElement element;
TypeMirror typeMirror = fieldDeclaration.asType();
switch(typeMirror.getKind()) {
case DECLARED:
element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
break;
case TYPEVAR:
typeMirror = ((TypeVariable) typeMirror).getUpperBound();
element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
break;
case WILDCARD:
TypeMirror bound = ((WildcardType) typeMirror).getExtendsBound();
if (bound == null) {
bound = ((WildcardType) typeMirror).getSuperBound();
}
if (!(bound instanceof DeclaredType)) {
bound = TypeMirrorUtils.objectType(this.env);
}
element = (DecoratedTypeElement) ((DeclaredType) bound).asElement();
break;
default:
throw new EnunciateException(String.format("%s: %s cannot be JSON unwrapped.", fieldDeclaration, typeMirror));
}
aggregatePotentialAccessors(bag, element, filter, inlineAccessorsOfSuperclasses);
// Fix issue #806
propsIgnore.add(fieldDeclaration.getSimpleName().toString());
} else if (!filter.accept((DecoratedElement) fieldDeclaration)) {
bag.fields.removeByName(fieldDeclaration);
} else {
bag.fields.addOrReplace(fieldDeclaration);
}
}
JacksonPropertySpec propertySpec = new JacksonPropertySpec(this.env);
List<PropertyElement> propertyElements = new ArrayList<PropertyElement>(clazz.getProperties(propertySpec));
if (mixin != null) {
// replace all mixin properties.
for (PropertyElement mixinProperty : ((DecoratedTypeElement) mixin).getProperties(propertySpec)) {
int index = indexOf(propertyElements, mixinProperty.getSimpleName().toString());
if (index >= 0) {
propertyElements.set(index, mixinProperty);
} else {
propertyElements.add(mixinProperty);
}
}
}
for (PropertyElement propertyDeclaration : propertyElements) {
JsonUnwrapped unwrapped = propertyDeclaration.getAnnotation(JsonUnwrapped.class);
if (unwrapped != null && unwrapped.enabled()) {
DecoratedTypeElement element;
TypeMirror typeMirror = propertyDeclaration.asType();
switch(typeMirror.getKind()) {
case DECLARED:
element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
break;
case TYPEVAR:
typeMirror = ((TypeVariable) typeMirror).getUpperBound();
element = (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement();
break;
case WILDCARD:
TypeMirror bound = ((WildcardType) typeMirror).getExtendsBound();
if (bound == null) {
bound = ((WildcardType) typeMirror).getSuperBound();
}
if (!(bound instanceof DeclaredType)) {
bound = TypeMirrorUtils.objectType(this.env);
}
element = (DecoratedTypeElement) ((DeclaredType) bound).asElement();
break;
default:
throw new EnunciateException(String.format("%s: %s cannot be JSON unwrapped.", propertyDeclaration, typeMirror));
}
aggregatePotentialAccessors(bag, element, filter, inlineAccessorsOfSuperclasses);
} else if (!filter.accept(propertyDeclaration) || indexOf(bag.fields, propertyDeclaration.getSimpleName().toString()) >= 0 || propsIgnore.contains(propertyDeclaration.getSimpleName().toString())) {
bag.properties.removeByName(propertyDeclaration);
} else {
bag.properties.addOrReplace(propertyDeclaration);
}
}
}
Aggregations