Search in sources :

Example 46 with EnunciateException

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;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) QName(javax.xml.namespace.QName) Callable(java.util.concurrent.Callable) EnunciateException(com.webcohesion.enunciate.EnunciateException) DecoratedDeclaredType(com.webcohesion.enunciate.javac.decorations.type.DecoratedDeclaredType) DeclaredType(javax.lang.model.type.DeclaredType)

Example 47 with EnunciateException

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);
    }
}
Also used : ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EnunciateException(com.webcohesion.enunciate.EnunciateException) JsonNode(com.fasterxml.jackson.databind.JsonNode) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 48 with EnunciateException

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);
    }
}
Also used : ApiRegistrationContext(com.webcohesion.enunciate.api.ApiRegistrationContext) DataTypeReference(com.webcohesion.enunciate.api.datatype.DataTypeReference) ObjectNode(org.codehaus.jackson.node.ObjectNode) EnunciateException(com.webcohesion.enunciate.EnunciateException) JsonNode(org.codehaus.jackson.JsonNode) ArrayNode(org.codehaus.jackson.node.ArrayNode) IOException(java.io.IOException) JsonProcessingException(org.codehaus.jackson.JsonProcessingException)

Example 49 with EnunciateException

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;
}
Also used : XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) EnunciateException(com.webcohesion.enunciate.EnunciateException) XmlQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) XmlUnknownQNameEnumValue(com.webcohesion.enunciate.metadata.qname.XmlUnknownQNameEnumValue) ArrayList(java.util.ArrayList) VariableElement(javax.lang.model.element.VariableElement) HashSet(java.util.HashSet)

Example 50 with EnunciateException

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);
        }
    }
}
Also used : JsonDeserialize(com.fasterxml.jackson.databind.annotation.JsonDeserialize) JsonSerialize(com.fasterxml.jackson.databind.annotation.JsonSerialize) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) EnunciateException(com.webcohesion.enunciate.EnunciateException)

Aggregations

EnunciateException (com.webcohesion.enunciate.EnunciateException)53 URL (java.net.URL)21 TemplateException (freemarker.template.TemplateException)19 IOException (java.io.IOException)16 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)12 File (java.io.File)12 FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)11 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)8 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)7 TypeElement (javax.lang.model.element.TypeElement)7 TypeDefinition (com.webcohesion.enunciate.modules.jaxb.model.TypeDefinition)6 FindRootElementMethod (com.webcohesion.enunciate.modules.jaxb.util.FindRootElementMethod)6 IsFacetExcludedMethod (com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod)6 VariableElement (javax.lang.model.element.VariableElement)6 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)5 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)5 SchemaInfo (com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo)5 AccessorOverridesAnotherMethod (com.webcohesion.enunciate.modules.jaxb.util.AccessorOverridesAnotherMethod)5 FileDirective (com.webcohesion.enunciate.util.freemarker.FileDirective)5