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();
}
}
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;
}
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;
}
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);
}
}
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;
}
Aggregations