public EnunciateContext getContext() { return jacksonContext == null ? jackson1Context.getContext() : jacksonContext.getContext(); }
@Override public boolean isEmpty() { return this.context.getTypeDefinitions().isEmpty(); }
JsonSubTypes subTypes = declaration.getAnnotation(JsonSubTypes.class); if (subTypes != null) { Elements elementUtils = getContext().getProcessingEnvironment().getElementUtils(); Types typeUtils = getContext().getProcessingEnvironment().getTypeUtils(); JsonSubTypes.Type[] types = subTypes.value(); for (JsonSubTypes.Type type : types) { stack.push(elementUtils.getTypeElement(JsonSubTypes.class.getName())); Class clazz = type.value(); add(createTypeDefinition(elementUtils.getTypeElement(clazz.getName())), stack); Element element = typeUtils.asElement(mirror); if (element instanceof TypeElement) { add(createTypeDefinition((TypeElement) element), stack); Element element = typeUtils.asElement(mirror); if (element instanceof TypeElement) { add(createTypeDefinition((TypeElement) element), stack); Elements elementUtils = getContext().getProcessingEnvironment().getElementUtils(); Types typeUtils = getContext().getProcessingEnvironment().getTypeUtils(); stack.push(elementUtils.getTypeElement(JsonSeeAlso.class.getName())); try { Class[] classes = seeAlso.value(); for (Class clazz : classes) { add(createTypeDefinition(elementUtils.getTypeElement(clazz.getName())), stack); Element element = typeUtils.asElement(mirror); if (element instanceof TypeElement) {
protected void addPotentialJacksonElement(Element declaration, LinkedList<Element> contextStack) { if (declaration instanceof TypeElement) { if (!this.jacksonContext.isKnownTypeDefinition((TypeElement) declaration) && isExplicitTypeDefinition(declaration, this.jacksonContext.isHonorJaxb())) { OneTimeLogMessage.JACKSON_1_DEPRECATED.log(this.context); this.jacksonContext.add(this.jacksonContext.createTypeDefinition((TypeElement) declaration), contextStack); } } }
protected void addReferencedTypeDefinitions(Accessor accessor, LinkedList<Element> stack) { stack.push(accessor); try { addSeeAlsoTypeDefinitions(accessor, stack); TypeMirror enumRef = accessor.getQNameEnumRef(); if (enumRef != null) { addReferencedTypeDefinitions(enumRef, stack); } } finally { stack.pop(); } }
@Override public void addDataTypeDefinitions(TypeMirror type, Set<String> declaredMediaTypes, LinkedList<Element> contextStack) { if (MediaTypeUtils.isJsonCompatible(declaredMediaTypes)) { type = this.jacksonContext.resolveSyntheticType((DecoratedTypeMirror) type); this.jacksonContext.addReferencedTypeDefinitions(type, contextStack); } else { debug("Element %s is NOT to be added as a Jackson data type because %s doesn't seem to include JSON.", type, declaredMediaTypes); } }
public static List<String> findConflictingAccessorNamingErrors(EnunciateJackson1Context context) { List<String> errors = (List<String>) context.getContext().getProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY); if (errors == null) { errors = new ArrayList<String>(); context.getContext().setProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY, errors); for (TypeDefinition typeDefinition : context.getTypeDefinitions()) { Map<String, Accessor> accessorsBySimpleName = new HashMap<String, Accessor>(); for (Accessor accessor : typeDefinition.getAllAccessors()) { String name = accessor.getClientSimpleName(); Accessor conflict = accessorsBySimpleName.get(name); if (conflict != null) { errors.add(String.format("%s: accessor \"%s\" conflicts with accessor \"%s\" of %s: both are named \"%s\".", typeDefinition.getQualifiedName(), accessor, conflict, conflict.getTypeDefinition().getQualifiedName(), name)); } else { accessorsBySimpleName.put(name, accessor); } } } } return errors; } }
public void add(TypeDefinition typeDef, LinkedList<Element> stack) { if (findTypeDefinition(typeDef) == null && !isKnownType(typeDef)) { this.typeDefinitions.put(typeDef.getQualifiedName().toString(), typeDef); warn("Added %s as a Jackson type definition even though is was supposed to be excluded according to configuration. It was referenced from %s%s, so it had to be included to prevent broken references.", typeDef.getQualifiedName(), stack.size() > 0 ? stack.get(0) : "an unknown location", stack.size() > 1 ? " of " + stack.get(1) : ""); debug("Added %s as a Jackson type definition.", typeDef.getQualifiedName()); if (getContext().getProcessingEnvironment().findSourcePosition(typeDef) == null) { OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext()); debug("Unable to find source file for %s.", typeDef.getQualifiedName()); stack.push(typeDef); addSeeAlsoTypeDefinitions(typeDef, stack); TypeMirror hint = TypeHintUtils.getTypeHint(hintInfo, context.getProcessingEnvironment(), null); if (hint != null) { addReferencedTypeDefinitions(hint, stack); addReferencedTypeDefinitions(member, stack); addReferencedTypeDefinitions(value, stack); if (!typeDef.isBaseObject() && superclass != null && superclass.getKind() != TypeKind.NONE && !isCollapseTypeHierarchy()) { addReferencedTypeDefinitions(superclass, stack);
public AdapterType(DeclaredType adapterType, EnunciateJackson1Context context) { super(adapterType, context.getContext().getProcessingEnvironment()); if (context.isHonorJaxb()) { DeclaredType adaptorInterfaceType = findXmlAdapterType(adapterType, new TypeVariableContext(), context.getContext().getProcessingEnvironment()); if (adaptorInterfaceType == null) { throw new EnunciateException(adapterType + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter."); } List<? extends TypeMirror> adaptorTypeArgs = adaptorInterfaceType.getTypeArguments(); if ((adaptorTypeArgs == null) || (adaptorTypeArgs.size() != 2)) { throw new EnunciateException(adapterType + " must specify both a value type and a bound type."); } this.adaptingType = adaptorTypeArgs.get(0); this.adaptedType = context.getContext().getProcessingEnvironment().getTypeUtils().erasure(adaptorTypeArgs.get(1)); } else { throw new EnunciateException(adapterType + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter."); } }
/** * The type of the accessor. * * @return The type of the accessor. */ public DecoratedTypeMirror getAccessorType() { DecoratedTypeMirror accessorType = (DecoratedTypeMirror) asType(); accessorType = OptionalUtils.stripOptional(accessorType, this.context.getContext().getProcessingEnvironment()); accessorType = this.context.resolveSyntheticType(accessorType); DecoratedDeclaredType normalizedCollection = JacksonUtil.getNormalizedCollection(accessorType, this.context.getContext().getProcessingEnvironment()); if (normalizedCollection != null) { accessorType = normalizedCollection; } else { MapType mapType = MapType.findMapType(accessorType, this.context); if (mapType != null) { accessorType = mapType; } } return accessorType; }
protected TypeDefinition(TypeElement delegate, EnunciateJackson1Context context) { super(delegate, context.getContext().getProcessingEnvironment()); this.context = context; boolean alphabetical = context.isPropertiesAlphabetical(); if (context.isHonorJaxb()) { XmlType typeInfo = getAnnotation(XmlType.class); if (typeInfo != null) { this.value = value; this.wildcardMember = wildcardMember; this.facets.addAll(Facet.gatherFacets(delegate, context.getContext())); this.facets.addAll(Facet.gatherFacets(this.env.getElementUtils().getPackageOf(delegate), context.getContext()));
@Override public List<DataType> findDataTypes(String name) { if (name != null && !name.isEmpty()) { TypeElement typeElement = this.context.getContext().getProcessingEnvironment().getElementUtils().getTypeElement(name); if (typeElement != null) { TypeDefinition typeDefinition = this.context.findTypeDefinition(typeElement); if (typeDefinition instanceof ObjectTypeDefinition) { return Collections.singletonList((DataType) new ObjectDataTypeImpl((ObjectTypeDefinition) typeDefinition, registrationContext)); } else if (typeDefinition instanceof EnumTypeDefinition) { return Collections.singletonList((DataType) new EnumDataTypeImpl((EnumTypeDefinition) typeDefinition, registrationContext)); } } } return Collections.emptyList(); }
TypeElement declaration = (TypeElement) declaredType.asElement(); if (declaration.getKind() == ElementKind.ENUM) { if (!isKnownTypeDefinition(declaration)) { add(createTypeDefinition(declaration), context.referenceStack); if (!isKnownTypeDefinition(declaration) && !isIgnored(declaration) && declaration.getKind() == ElementKind.CLASS && !((DecoratedDeclaredType) declaredType).isCollection() && !((DecoratedDeclaredType) declaredType).isInstanceOf(JAXBElement.class)) { add(createTypeDefinition(declaration), context.referenceStack);
/** * Add the type definition(s) referenced by the given value. * * @param value The value. * @param stack The context stack. */ protected void addReferencedTypeDefinitions(Value value, LinkedList<Element> stack) { addReferencedTypeDefinitions((Accessor) value, stack); stack.push(value); try { if (value.isAdapted()) { addReferencedTypeDefinitions(value.getAdapterType(), stack); } else if (value.getQNameEnumRef() == null) { addReferencedTypeDefinitions(value.getAccessorType(), stack); } } finally { stack.pop(); } }
@Override public void call(EnunciateContext context) { this.jacksonContext = new EnunciateJackson1Context(context, isHonorJaxbAnnotations(), getDateFormat(), isCollapseTypeHierarchy(), getMixins(), getExternalExamples(), getDefaultVisibility(), isDisableExamples(), isWrapRootValue(), getPropertyNamingStrategy(), isPropertiesAlphabetical()); DataTypeDetectionStrategy detectionStrategy = getDataTypeDetectionStrategy(); switch (detectionStrategy) { case aggressive: for (Element declaration : context.getApiElements()) { addPotentialJacksonElement(declaration, new LinkedList<Element>()); } break; case local: for (Element declaration : context.getLocalApiElements()) { addPotentialJacksonElement(declaration, new LinkedList<Element>()); } //no break, add explicit includes: default: if (context.hasExplicitIncludes()) { //if we're not aggressive, we only want to add the api elements if they've been explicitly included for (Element declaration : context.getApiElements()) { addPotentialJacksonElement(declaration, new LinkedList<Element>()); } } } }
public static List<String> findConflictingAccessorNamingErrors(EnunciateJackson1Context context) { List<String> errors = (List<String>) context.getContext().getProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY); if (errors == null) { errors = new ArrayList<String>(); context.getContext().setProperty(CONFLICTING_JAXB_ACCESSOR_NAMING_ERRORS_PROPERTY, errors); for (TypeDefinition typeDefinition : context.getTypeDefinitions()) { Map<String, Accessor> accessorsBySimpleName = new HashMap<String, Accessor>(); for (Accessor accessor : typeDefinition.getAllAccessors()) { String name = accessor.getClientSimpleName(); Accessor conflict = accessorsBySimpleName.get(name); if (conflict != null) { errors.add(String.format("%s: accessor \"%s\" conflicts with accessor \"%s\" of %s: both are named \"%s\".", typeDefinition.getQualifiedName(), accessor, conflict, conflict.getTypeDefinition().getQualifiedName(), name)); } else { accessorsBySimpleName.put(name, accessor); } } } } return errors; } }
public void add(TypeDefinition typeDef, LinkedList<Element> stack) { if (findTypeDefinition(typeDef) == null && !isKnownType(typeDef)) { this.typeDefinitions.put(typeDef.getQualifiedName().toString(), typeDef); warn("Added %s as a Jackson type definition even though is was supposed to be excluded according to configuration. It was referenced from %s%s, so it had to be included to prevent broken references.", typeDef.getQualifiedName(), stack.size() > 0 ? stack.get(0) : "an unknown location", stack.size() > 1 ? " of " + stack.get(1) : ""); debug("Added %s as a Jackson type definition.", typeDef.getQualifiedName()); if (getContext().getProcessingEnvironment().findSourcePosition(typeDef) == null) { OneTimeLogMessage.SOURCE_FILES_NOT_FOUND.log(getContext()); debug("Unable to find source file for %s.", typeDef.getQualifiedName()); stack.push(typeDef); addSeeAlsoTypeDefinitions(typeDef, stack); TypeMirror hint = TypeHintUtils.getTypeHint(hintInfo, context.getProcessingEnvironment(), null); if (hint != null) { addReferencedTypeDefinitions(hint, stack); addReferencedTypeDefinitions(member, stack); addReferencedTypeDefinitions(value, stack); if (!typeDef.isBaseObject() && superclass != null && superclass.getKind() != TypeKind.NONE && !isCollapseTypeHierarchy()) { addReferencedTypeDefinitions(superclass, stack);
public AdapterType(DeclaredType adapterType, EnunciateJackson1Context context) { super(adapterType, context.getContext().getProcessingEnvironment()); if (context.isHonorJaxb()) { DeclaredType adaptorInterfaceType = findXmlAdapterType(adapterType, new TypeVariableContext(), context.getContext().getProcessingEnvironment()); if (adaptorInterfaceType == null) { throw new EnunciateException(adapterType + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter."); } List<? extends TypeMirror> adaptorTypeArgs = adaptorInterfaceType.getTypeArguments(); if ((adaptorTypeArgs == null) || (adaptorTypeArgs.size() != 2)) { throw new EnunciateException(adapterType + " must specify both a value type and a bound type."); } this.adaptingType = adaptorTypeArgs.get(0); this.adaptedType = context.getContext().getProcessingEnvironment().getTypeUtils().erasure(adaptorTypeArgs.get(1)); } else { throw new EnunciateException(adapterType + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter."); } }
/** * The type of the accessor. * * @return The type of the accessor. */ public DecoratedTypeMirror getAccessorType() { DecoratedTypeMirror accessorType = (DecoratedTypeMirror) asType(); accessorType = OptionalUtils.stripOptional(accessorType, this.context.getContext().getProcessingEnvironment()); accessorType = this.context.resolveSyntheticType(accessorType); DecoratedDeclaredType normalizedCollection = JacksonUtil.getNormalizedCollection(accessorType, this.context.getContext().getProcessingEnvironment()); if (normalizedCollection != null) { accessorType = normalizedCollection; } else { MapType mapType = MapType.findMapType(accessorType, this.context); if (mapType != null) { accessorType = mapType; } } return accessorType; }
protected TypeDefinition(TypeElement delegate, EnunciateJackson1Context context) { super(delegate, context.getContext().getProcessingEnvironment()); this.context = context; boolean alphabetical = context.isPropertiesAlphabetical(); if (context.isHonorJaxb()) { XmlType typeInfo = getAnnotation(XmlType.class); if (typeInfo != null) { this.value = value; this.wildcardMember = wildcardMember; this.facets.addAll(Facet.gatherFacets(delegate, context.getContext())); this.facets.addAll(Facet.gatherFacets(this.env.getElementUtils().getPackageOf(delegate), context.getContext()));