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 = 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);
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()) {
TypeMirror typeMirror = fieldDeclaration.asType();
if (!(typeMirror instanceof DeclaredType)) {
throw new EnunciateException(String.format("%s: %s cannot be JSON unwrapped.", fieldDeclaration, typeMirror));
}
aggregatePotentialAccessors(bag, (DecoratedTypeElement) ((DeclaredType) typeMirror).asElement(), 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);
}
}
Jackson1PropertySpec propertySpec = new Jackson1PropertySpec(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);
}
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class RubyJSONClientModule method readResource.
/**
* Reads a resource into string form.
*
* @param resource The resource to read.
* @return The string form of the resource.
*/
protected String readResource(String resource, Map<String, Object> model) {
model.put("sample_resource", findExampleResourceMethod());
URL res = RubyJSONClientModule.class.getResource(resource);
try {
return processTemplate(res, model);
} catch (TemplateException 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 ComplexTypeExampleImpl method getBody.
@Override
public String getBody() {
try {
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
DocumentBuilder domBuilder = builderFactory.newDocumentBuilder();
Document document = domBuilder.newDocument();
String rootName = Character.toLowerCase(this.typeDefinition.getSimpleName().charAt(0)) + "-----";
String rootNamespace = this.typeDefinition.getNamespace();
ElementDeclaration element = typeDefinition.getContext().findElementDeclaration(typeDefinition);
if (element != null) {
rootName = element.getName();
rootNamespace = element.getNamespace();
}
Element rootElement = document.createElementNS(rootNamespace, rootName);
Element outer = rootElement;
for (DataTypeReference.ContainerType container : this.containers) {
Element containerEl = document.createElementNS("", container.name());
containerEl.appendChild(outer);
outer = containerEl;
}
document.appendChild(outer);
Context context = new Context();
context.stack = new LinkedList<String>();
build(rootElement, this.typeDefinition, document, context);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
DOMSource source = new DOMSource(document);
StringWriter value = new StringWriter();
transformer.transform(source, new StreamResult(value));
return value.toString();
} catch (ParserConfigurationException e) {
throw new EnunciateException(e);
} catch (TransformerException e) {
throw new EnunciateException(e);
}
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class EnumTypeDefinition method loadEnumValues.
protected List<EnumValue> loadEnumValues() {
List<VariableElement> enumConstants = enumValues();
List<EnumValue> enumValues = new ArrayList<EnumValue>();
HashSet<String> enumValueNames = new HashSet<String>(enumConstants.size());
for (VariableElement enumConstant : enumConstants) {
if (isIgnored(enumConstant)) {
continue;
}
String value = enumConstant.getSimpleName().toString();
XmlEnumValue enumValue = enumConstant.getAnnotation(XmlEnumValue.class);
if (enumValue != null) {
value = enumValue.value();
}
if (!enumValueNames.add(value)) {
throw new EnunciateException(getQualifiedName() + ": duplicate enum value: " + value);
}
enumValues.add(new EnumValue(this, enumConstant, enumConstant.getSimpleName().toString(), value));
}
return enumValues;
}
use of com.webcohesion.enunciate.EnunciateException in project enunciate by stoicflame.
the class PHPJSONClientModule method readResource.
/**
* Reads a resource into string form.
*
* @param resource The resource to read.
* @return The string form of the resource.
*/
protected String readResource(String resource, Map<String, Object> model) {
model.put("sample_resource", findExampleResourceMethod());
URL res = PHPJSONClientModule.class.getResource(resource);
try {
return processTemplate(res, model);
} catch (TemplateException e) {
throw new EnunciateException(e);
} catch (IOException e) {
throw new EnunciateException(e);
}
}
Aggregations