Search in sources :

Example 1 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition in project enunciate by stoicflame.

the class JsonTypeVisitor method visitDeclared.

@Override
public JsonType visitDeclared(DeclaredType declaredType, Context context) {
    JsonType jsonType = null;
    Element declaredElement = declaredType.asElement();
    DecoratedProcessingEnvironment env = context.getContext().getContext().getProcessingEnvironment();
    DecoratedTypeMirror decoratedTypeMirror = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(declaredType, env);
    String fqn = declaredElement instanceof TypeElement ? ((TypeElement) declaredElement).getQualifiedName().toString() : declaredType.toString();
    if (context.getStack().contains(fqn) && !decoratedTypeMirror.isCollection() && !decoratedTypeMirror.isStream()) {
        // break out of recursive loop.
        return wrapAsNeeded(KnownJsonType.OBJECT, context);
    }
    context.getStack().push(fqn);
    try {
        TypeHint typeHint = declaredElement.getAnnotation(TypeHint.class);
        if (typeHint != null) {
            TypeMirror hint = TypeHintUtils.getTypeHint(typeHint, context.getContext().getContext().getProcessingEnvironment(), null);
            if (hint != null) {
                jsonType = hint.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        final JsonSerialize serializeInfo = declaredElement.getAnnotation(JsonSerialize.class);
        if (serializeInfo != null) {
            DecoratedTypeMirror using = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.using();
                }
            }, env, JsonSerializer.None.class);
            if (using != null) {
                // custom serializer; just say it's an object.
                jsonType = KnownJsonType.OBJECT;
            }
            DecoratedTypeMirror as = Annotations.mirrorOf(new Callable<Class<?>>() {

                @Override
                public Class<?> call() throws Exception {
                    return serializeInfo.as();
                }
            }, env, Void.class);
            if (as != null) {
                jsonType = (JsonType) as.accept(this, new Context(context.context, false, false, context.stack));
            }
        }
        AdapterType adapterType = JacksonUtil.findAdapterType(declaredElement, context.getContext());
        if (adapterType != null) {
            adapterType.getAdaptingType().accept(this, new Context(context.context, false, false, context.stack));
        } else {
            MapType mapType = MapType.findMapType(declaredType, context.getContext());
            if (mapType != null) {
                JsonType keyType = mapType.getKeyType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
                JsonType valueType = mapType.getValueType().accept(this, new Context(context.getContext(), false, false, context.getStack()));
                jsonType = new JsonMapType(keyType, valueType);
            } else {
                TypeMirror componentType = getComponentType(decoratedTypeMirror, env);
                if (componentType != null) {
                    return wrapAsNeeded(componentType.accept(this, new Context(context.context, false, true, context.stack)), context);
                } else {
                    switch(declaredElement.getKind()) {
                        case ENUM:
                        case CLASS:
                        case INTERFACE:
                            JsonType knownType = context.getContext().getKnownType(declaredElement);
                            if (knownType != null) {
                                jsonType = knownType;
                            } else {
                                // type not known, not specified.  Last chance: look for the type definition.
                                TypeDefinition typeDefinition = context.getContext().findTypeDefinition(declaredElement);
                                if (typeDefinition != null) {
                                    jsonType = new JsonClassType(typeDefinition);
                                }
                            }
                            break;
                    }
                }
            }
        }
        if (jsonType == null) {
            jsonType = super.visitDeclared(declaredType, context);
        }
        return wrapAsNeeded(jsonType, context);
    } finally {
        context.getStack().pop();
    }
}
Also used : EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TypeHint(com.webcohesion.enunciate.metadata.rs.TypeHint) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror) TypeElement(javax.lang.model.element.TypeElement) Element(javax.lang.model.element.Element) TypeElement(javax.lang.model.element.TypeElement) AdapterType(com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType) JsonSerializer(org.codehaus.jackson.map.JsonSerializer) DecoratedProcessingEnvironment(com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment) MapType(com.webcohesion.enunciate.modules.jackson1.model.util.MapType) TypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition) JsonSerialize(org.codehaus.jackson.map.annotate.JsonSerialize) DecoratedTypeMirror(com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)

Example 2 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition in project enunciate by stoicflame.

the class DataTypeReferenceImpl method getExample.

@Override
public Example getExample() {
    Example example = null;
    if (this.dataType instanceof ObjectDataTypeImpl) {
        ObjectTypeDefinition typeDefinition = ((ObjectDataTypeImpl) this.dataType).typeDefinition;
        example = typeDefinition == null || typeDefinition.getContext().isDisableExamples() ? null : new DataTypeExampleImpl(typeDefinition, this.containers, registrationContext);
    } else if (this.dataType instanceof EnumDataTypeImpl) {
        String body = "...";
        List<? extends Value> values = this.dataType.getValues();
        if (values != null && !values.isEmpty()) {
            body = values.get(0).getValue();
        }
        example = new CustomExampleImpl('"' + body + '"');
    }
    return example;
}
Also used : ObjectTypeDefinition(com.webcohesion.enunciate.modules.jackson1.model.ObjectTypeDefinition) List(java.util.List) LinkedList(java.util.LinkedList)

Example 3 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition in project enunciate by stoicflame.

the class GWTJSONOverlayModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJacksonContext jacksonContext = this.jacksonModule != null ? this.jacksonModule.getJacksonContext() : null;
    EnunciateJackson1Context jackson1Context = this.jackson1Module != null ? this.jackson1Module.getJacksonContext() : null;
    MergedJsonContext jsonContext = new MergedJsonContext(jacksonContext, jackson1Context);
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(conversions, jsonContext);
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", classnameFor);
    model.put("simpleNameFor", new SimpleNameForMethod(classnameFor, jsonContext));
    model.put("isAccessorOfTypeLong", new IsAccessorOfTypeLongMethod());
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
    facetIncludes.addAll(getFacetIncludes());
    Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
    facetExcludes.addAll(getFacetExcludes());
    FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    AntPatternMatcher matcher = new AntPatternMatcher();
    matcher.setPathSeparator(".");
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the GWT JSON Overlay...");
            if (jacksonContext != null) {
                for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
                    if (!typeDefinition.isSimple() && facetFilter.accept(typeDefinition)) {
                        model.put("type", typeDefinition);
                        URL template = typeDefinition.isEnum() ? getTemplateURL("gwt-enum-type.fmt") : getTemplateURL("gwt-type.fmt");
                        processTemplate(template, model);
                    }
                }
            }
            if (jackson1Context != null) {
                for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
                    if (!typeDefinition.isSimple() && facetFilter.accept(typeDefinition)) {
                        model.put("type", typeDefinition);
                        URL template = typeDefinition.isEnum() ? getTemplateURL("gwt-enum-type.fmt") : getTemplateURL("gwt-type.fmt");
                        processTemplate(template, model);
                    }
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of GWT JSON Overlay as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) IsFacetExcludedMethod(com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod) ClientPackageForMethod(com.webcohesion.enunciate.util.freemarker.ClientPackageForMethod) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) FileDirective(com.webcohesion.enunciate.util.freemarker.FileDirective) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) IOException(java.io.IOException) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) File(java.io.File)

Example 4 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition in project enunciate by stoicflame.

the class PHPJSONClientModule method call.

@Override
public void call(EnunciateContext context) {
    if ((this.jacksonModule == null || this.jacksonModule.getJacksonContext() == null || this.jacksonModule.getJacksonContext().getTypeDefinitions().isEmpty()) && (this.jackson1Module == null || this.jackson1Module.getJacksonContext() == null || this.jackson1Module.getJacksonContext().getTypeDefinitions().isEmpty())) {
        info("No Jackson JSON data types: PHP JSON client will not be generated.");
        return;
    }
    detectAccessorNamingErrors();
    if (usesUnmappableElements()) {
        warn("Web service API makes use of elements that cannot be handled by the PHP JSON client. PHP JSON client will not be generated.");
        return;
    }
    Map<String, String> packageToNamespaceConversions = getPackageToNamespaceConversions();
    List<DecoratedTypeElement> schemaTypes = new ArrayList<DecoratedTypeElement>();
    ExtensionDepthComparator comparator = new ExtensionDepthComparator();
    EnunciateJacksonContext jacksonContext = null;
    EnunciateJackson1Context jackson1Context = null;
    if (this.jacksonModule != null) {
        jacksonContext = this.jacksonModule.getJacksonContext();
        for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
            String pckg = typeDefinition.getPackage().getQualifiedName().toString();
            if (!packageToNamespaceConversions.containsKey(pckg)) {
                packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
            }
            int position = Collections.binarySearch(schemaTypes, typeDefinition, comparator);
            if (position < 0) {
                position = -position - 1;
            }
            schemaTypes.add(position, typeDefinition);
        }
    }
    if (this.jackson1Module != null) {
        jackson1Context = this.jackson1Module.getJacksonContext();
        for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
            String pckg = typeDefinition.getPackage().getQualifiedName().toString();
            if (!packageToNamespaceConversions.containsKey(pckg)) {
                packageToNamespaceConversions.put(pckg, packageToNamespace(pckg));
            }
            schemaTypes.add(typeDefinition);
        }
    }
    File srcDir = getSourceDir();
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("schemaTypes", schemaTypes);
    model.put("namespaceFor", new ClientPackageForMethod(packageToNamespaceConversions, this.context));
    ClientClassnameForMethod classnameFor = new ClientClassnameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context);
    model.put("classnameFor", classnameFor);
    model.put("typeNameFor", new TypeNameForMethod(packageToNamespaceConversions, jacksonContext, jackson1Context));
    model.put("simpleNameFor", new SimpleNameWithParamsMethod(classnameFor));
    model.put("phpFileName", getSourceFileName());
    model.put("file", new FileDirective(srcDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
    facetIncludes.addAll(getFacetIncludes());
    Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
    facetExcludes.addAll(getFacetExcludes());
    FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    if (!isUpToDateWithSources(srcDir)) {
        debug("Generating the PHP data classes...");
        URL apiTemplate = isSingleFilePerClass() ? getTemplateURL("api-multiple-files.fmt") : getTemplateURL("api.fmt");
        try {
            processTemplate(apiTemplate, model);
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping PHP code generation because everything appears up-to-date.");
    }
    File packageDir = getPackageDir();
    packageDir.mkdirs();
    File bundle = new File(packageDir, getBundleFileName());
    boolean anyFiles = bundle.exists();
    if (!isUpToDateWithSources(packageDir)) {
        try {
            anyFiles = enunciate.zip(bundle, srcDir);
        } catch (IOException e) {
            throw new EnunciateException(e);
        }
    }
    if (anyFiles) {
        ClientLibraryArtifact artifactBundle = new ClientLibraryArtifact(getName(), "php.json.client.library", "PHP JSON Client Library");
        artifactBundle.setPlatform("PHP");
        FileArtifact sourceScript = new FileArtifact(getName(), "php.json.client", bundle);
        // binaries and sources are the same thing in php
        sourceScript.setArtifactType(ArtifactType.binaries);
        sourceScript.setPublic(false);
        // read in the description from file
        String description = readResource("library_description.fmt", model);
        artifactBundle.setDescription(description);
        artifactBundle.addArtifact(sourceScript);
        this.enunciate.addArtifact(artifactBundle);
    }
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) ClientLibraryArtifact(com.webcohesion.enunciate.artifacts.ClientLibraryArtifact) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) DecoratedTypeElement(com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) IOException(java.io.IOException) FileArtifact(com.webcohesion.enunciate.artifacts.FileArtifact) File(java.io.File)

Example 5 with TypeDefinition

use of com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition in project enunciate by stoicflame.

the class JavaJSONClientModule method generateClientSources.

protected File generateClientSources() {
    File sourceDir = getSourceDir();
    sourceDir.mkdirs();
    Map<String, Object> model = new HashMap<String, Object>();
    Map<String, String> conversions = getClientPackageConversions();
    EnunciateJacksonContext jacksonContext = this.jacksonModule != null ? this.jacksonModule.getJacksonContext() : null;
    EnunciateJackson1Context jackson1Context = this.jackson1Module != null ? this.jackson1Module.getJacksonContext() : null;
    MergedJsonContext jsonContext = new MergedJsonContext(jacksonContext, jackson1Context);
    model.put("packageFor", new ClientPackageForMethod(conversions, this.context));
    model.put("classnameFor", new ClientClassnameForMethod(conversions, jsonContext));
    model.put("simpleNameFor", new SimpleNameForMethod(new ClientClassnameForMethod(conversions, jsonContext, true), jsonContext));
    model.put("file", new FileDirective(sourceDir, this.enunciate.getLogger()));
    model.put("generatedCodeLicense", this.enunciate.getConfiguration().readGeneratedCodeLicenseFile());
    model.put("annotationValue", new AnnotationValueMethod());
    model.put("wrapRootValue", this.jacksonModule == null ? this.jackson1Module.isWrapRootValue() : this.jacksonModule.isWrapRootValue());
    Set<String> facetIncludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetIncludes());
    facetIncludes.addAll(getFacetIncludes());
    Set<String> facetExcludes = new TreeSet<String>(this.enunciate.getConfiguration().getFacetExcludes());
    facetExcludes.addAll(getFacetExcludes());
    FacetFilter facetFilter = new FacetFilter(facetIncludes, facetExcludes);
    model.put("isFacetExcluded", new IsFacetExcludedMethod(facetFilter));
    AntPatternMatcher matcher = new AntPatternMatcher();
    matcher.setPathSeparator(".");
    boolean upToDate = isUpToDateWithSources(sourceDir);
    if (!upToDate) {
        try {
            debug("Generating the Java client classes...");
            if (jacksonContext != null) {
                for (TypeDefinition typeDefinition : jacksonContext.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
            }
            if (jackson1Context != null) {
                for (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition typeDefinition : jackson1Context.getTypeDefinitions()) {
                    if (facetFilter.accept(typeDefinition)) {
                        if (useServerSide(typeDefinition, matcher)) {
                            copyServerSideType(sourceDir, typeDefinition);
                        } else {
                            model.put("type", typeDefinition);
                            URL template = typeDefinition.isEnum() ? getTemplateURL("client-enum-type.fmt") : typeDefinition.isSimple() ? getTemplateURL("client-simple-type.fmt") : getTemplateURL("client-complex-type.fmt");
                            processTemplate(template, model);
                        }
                    }
                }
            }
        } catch (IOException e) {
            throw new EnunciateException(e);
        } catch (TemplateException e) {
            throw new EnunciateException(e);
        }
    } else {
        info("Skipping generation of Java client sources as everything appears up-to-date...");
    }
    context.setProperty(LIRBARY_DESCRIPTION_PROPERTY, readLibraryDescription(model));
    return sourceDir;
}
Also used : FacetFilter(com.webcohesion.enunciate.facets.FacetFilter) URL(java.net.URL) TypeDefinition(com.webcohesion.enunciate.modules.jackson.model.TypeDefinition) EnunciateException(com.webcohesion.enunciate.EnunciateException) EnunciateJackson1Context(com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context) TemplateException(freemarker.template.TemplateException) EnunciateJacksonContext(com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext) AntPatternMatcher(com.webcohesion.enunciate.util.AntPatternMatcher) JavaFileObject(javax.tools.JavaFileObject)

Aggregations

FacetFilter (com.webcohesion.enunciate.facets.FacetFilter)6 EnunciateJackson1Context (com.webcohesion.enunciate.modules.jackson1.EnunciateJackson1Context)6 EnunciateException (com.webcohesion.enunciate.EnunciateException)5 EnunciateJacksonContext (com.webcohesion.enunciate.modules.jackson.EnunciateJacksonContext)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson.model.TypeDefinition)5 TemplateException (freemarker.template.TemplateException)5 URL (java.net.URL)5 TypeDefinition (com.webcohesion.enunciate.modules.jackson1.model.TypeDefinition)4 File (java.io.File)4 IOException (java.io.IOException)4 ClientLibraryArtifact (com.webcohesion.enunciate.artifacts.ClientLibraryArtifact)3 FileArtifact (com.webcohesion.enunciate.artifacts.FileArtifact)3 DecoratedTypeElement (com.webcohesion.enunciate.javac.decorations.element.DecoratedTypeElement)3 ObjectTypeDefinition (com.webcohesion.enunciate.modules.jackson1.model.ObjectTypeDefinition)3 DecoratedTypeMirror (com.webcohesion.enunciate.javac.decorations.type.DecoratedTypeMirror)2 AntPatternMatcher (com.webcohesion.enunciate.util.AntPatternMatcher)2 List (java.util.List)2 DecoratedProcessingEnvironment (com.webcohesion.enunciate.javac.decorations.DecoratedProcessingEnvironment)1 TypeHint (com.webcohesion.enunciate.metadata.rs.TypeHint)1 Accessor (com.webcohesion.enunciate.modules.jackson1.model.Accessor)1