/** * Narrows the existing declaration down to its adapting declaration, if it's being adapted. Otherwise, the original declaration will be returned. * * @param declaration The declaration to narrow. * @return The narrowed declaration. */ protected TypeElement narrowToAdaptingType(TypeElement declaration) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this); if (adapterType != null) { TypeMirror adaptingType = adapterType.getAdaptingType(); if (adaptingType.getKind() != TypeKind.DECLARED) { return declaration; } else { TypeElement adaptingDeclaration = (TypeElement) ((DeclaredType) adaptingType).asElement(); if (adaptingDeclaration == null) { throw new EnunciateException(String.format("Class %s is being adapted by a type (%s) that doesn't seem to be on the classpath.", declaration.getQualifiedName(), adaptingType)); } return adaptingDeclaration; } } return declaration; }
return new AdapterType((DecoratedDeclaredType) adapterTypeMirror, context);
/** * Finds the interface type that declares that the specified declaration implements XmlAdapter. * * @param declaredType The declaration. * @return The interface type, or null if none found. */ private static DeclaredType findXmlAdapterType(DeclaredType declaredType, TypeVariableContext variableContext, DecoratedProcessingEnvironment env) { TypeElement element = (TypeElement) declaredType.asElement(); if (element == null) { return null; } else if (Object.class.getName().equals(element.getQualifiedName().toString())) { return null; } else if (XmlAdapter.class.getName().equals(element.getQualifiedName().toString())) { return (DeclaredType) variableContext.resolveTypeVariables(declaredType, env); } else { DeclaredType superclass = (DeclaredType) element.getSuperclass(); if (superclass == null || superclass.getKind() == TypeKind.NONE) { return null; } else { return findXmlAdapterType(superclass, variableContext.push(element.getTypeParameters(), declaredType.getTypeArguments()), env); } } }
/** * Get the adapting type for the specified type. This method differs from {@link #getAdaptingType()} because it takes * into account whether the adapted is an array or collection. * * @param adaptedType The type. * @return The adapting type, or null if not adaptable. */ public TypeMirror getAdaptingType(DecoratedTypeMirror adaptedType, EnunciateContext context) { TypeMirror componentType = null; if (adaptedType.isCollection()) { List<? extends TypeMirror> itemTypes = ((DeclaredType) adaptedType).getTypeArguments(); if (itemTypes.isEmpty()) { componentType = TypeMirrorUtils.objectType(context.getProcessingEnvironment()); } else { componentType = itemTypes.get(0); } } else if (adaptedType instanceof ArrayType) { componentType = ((ArrayType) adaptedType).getComponentType(); } if (componentType != null && canAdapt(componentType, context)) { //if we can adapt the component type, then the adapting type is the collection of the declared adapting type. return context.getProcessingEnvironment().getTypeUtils().getDeclaredType((TypeElement) TypeMirrorUtils.collectionType(context.getProcessingEnvironment()).asElement(), componentType); } else { return getAdaptingType(); } }
/** * Whether this adapter can adapt the specified type. * * @param type The type. * @return Whether this adapter can adapt the specified type. */ public boolean canAdapt(TypeMirror type, EnunciateContext context) { return context.getProcessingEnvironment().getTypeUtils().isAssignable(type, getAdaptedType()); }
/** * Get the adapting type for the specified type. This method differs from {@link #getAdaptingType()} because it takes * into account whether the adapted is an array or collection. * * @param adaptedType The type. * @return The adapting type, or null if not adaptable. */ public TypeMirror getAdaptingType(DecoratedTypeMirror adaptedType, EnunciateContext context) { TypeMirror componentType = null; if (adaptedType.isCollection()) { List<? extends TypeMirror> itemTypes = ((DeclaredType) adaptedType).getTypeArguments(); if (itemTypes.isEmpty()) { componentType = TypeMirrorUtils.objectType(context.getProcessingEnvironment()); } else { componentType = itemTypes.get(0); } } else if (adaptedType instanceof ArrayType) { componentType = ((ArrayType) adaptedType).getComponentType(); } if (componentType != null && canAdapt(componentType, context)) { //if we can adapt the component type, then the adapting type is the collection of the declared adapting type. return context.getProcessingEnvironment().getTypeUtils().getDeclaredType((TypeElement) TypeMirrorUtils.collectionType(context.getProcessingEnvironment()).asElement(), componentType); } else { return getAdaptingType(); } }
/** * Whether this adapter can adapt the specified type. * * @param type The type. * @return Whether this adapter can adapt the specified type. */ public boolean canAdapt(TypeMirror type, EnunciateContext context) { return context.getProcessingEnvironment().getTypeUtils().isAssignable(type, getAdaptedType()); }
/** * Narrows the existing declaration down to its adapting declaration, if it's being adapted. Otherwise, the original declaration will be returned. * * @param declaration The declaration to narrow. * @return The narrowed declaration. */ protected TypeElement narrowToAdaptingType(TypeElement declaration) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this); if (adapterType != null) { TypeMirror adaptingType = adapterType.getAdaptingType(); if (adaptingType.getKind() != TypeKind.DECLARED) { return declaration; } else { TypeElement adaptingDeclaration = (TypeElement) ((DeclaredType) adaptingType).asElement(); if (adaptingDeclaration == null) { throw new EnunciateException(String.format("Class %s is being adapted by a type (%s) that doesn't seem to be on the classpath.", declaration.getQualifiedName(), adaptingType)); } return adaptingDeclaration; } } return declaration; }
/** * Finds the interface type that declares that the specified declaration implements XmlAdapter. * * @param declaredType The declaration. * @return The interface type, or null if none found. */ private static DeclaredType findXmlAdapterType(DeclaredType declaredType, TypeVariableContext variableContext, DecoratedProcessingEnvironment env) { TypeElement element = (TypeElement) declaredType.asElement(); if (element == null) { return null; } else if (Object.class.getName().equals(element.getQualifiedName().toString())) { return null; } else if (XmlAdapter.class.getName().equals(element.getQualifiedName().toString())) { return (DeclaredType) variableContext.resolveTypeVariables(declaredType, env); } else { DeclaredType superclass = (DeclaredType) element.getSuperclass(); if (superclass == null || superclass.getKind() == TypeKind.NONE) { return null; } else { return findXmlAdapterType(superclass, variableContext.push(element.getTypeParameters(), declaredType.getTypeArguments()), env); } } }
return new AdapterType((DecoratedDeclaredType) adapterTypeMirror, context);
public TypeMirror findAdaptingType(TypeElement declaration) { if (this.jacksonContext != null) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext); if (adapterType != null) { return adapterType.getAdaptingType(); } } if (this.jackson1Context != null) { com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType otherAdapterType = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context); if (otherAdapterType != null) { return otherAdapterType.getAdaptingType(); } } return null; }
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."); } }
public TypeMirror findAdaptingType(TypeElement declaration) { if (this.jacksonContext != null) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext); if (adapterType != null) { return adapterType.getAdaptingType(); } } if (this.jackson1Context != null) { com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType otherAdapterType = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context); if (otherAdapterType != null) { return otherAdapterType.getAdaptingType(); } } return null; }
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."); } }
public TypeMirror findAdaptingType(TypeElement declaration) { if (this.jacksonContext != null) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext); if (adapterType != null) { return adapterType.getAdaptingType(); } } if (this.jackson1Context != null) { com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType otherAdapterType = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context); if (otherAdapterType != null) { return otherAdapterType.getAdaptingType(); } } return null; }
public static DeclaredType findMapTypeDeclaration(TypeMirror typeMirror, EnunciateJackson1Context context) { if (!(typeMirror instanceof DeclaredType)) { return null; } DeclaredType declaredType = (DeclaredType) typeMirror; TypeElement element = (TypeElement) declaredType.asElement(); String fqn = element.getQualifiedName().toString(); if (Map.class.getName().equals(fqn)) { return declaredType; } AdapterType adapterType = JacksonUtil.findAdapterType(element, context); if (adapterType != null) { return findMapTypeDeclaration(adapterType.getAdaptingType(), context); } DeclaredType mapType = null; Types typeUtils = context.getContext().getProcessingEnvironment().getTypeUtils(); List<? extends TypeMirror> supers = typeUtils.directSupertypes(declaredType); for (TypeMirror superInterface : supers) { mapType = findMapTypeDeclaration(superInterface, context); if (mapType != null) { break; } } return mapType; }
/** * Get the resolved accessor type for this accessor. * * @return the resolved accessor type for this accessor. */ public TypeMirror getResolvedAccessorType() { DecoratedTypeMirror accessorType = getAccessorType(); if (isAdapted()) { accessorType = (DecoratedTypeMirror) getAdapterType().getAdaptingType(accessorType, this.context.getContext()); } return accessorType; }
/** * Get the resolved accessor type for this accessor. * * @return the resolved accessor type for this accessor. */ public TypeMirror getResolvedAccessorType() { DecoratedTypeMirror accessorType = getAccessorType(); if (isAdapted()) { accessorType = (DecoratedTypeMirror) getAdapterType().getAdaptingType(accessorType, this.context.getContext()); } return accessorType; }
public static DeclaredType findMapTypeDeclaration(TypeMirror typeMirror, EnunciateJackson1Context context) { if (!(typeMirror instanceof DeclaredType)) { return null; } DeclaredType declaredType = (DeclaredType) typeMirror; TypeElement element = (TypeElement) declaredType.asElement(); String fqn = element.getQualifiedName().toString(); if (Map.class.getName().equals(fqn)) { return declaredType; } AdapterType adapterType = JacksonUtil.findAdapterType(element, context); if (adapterType != null) { return findMapTypeDeclaration(adapterType.getAdaptingType(), context); } DeclaredType mapType = null; Types typeUtils = context.getContext().getProcessingEnvironment().getTypeUtils(); List<? extends TypeMirror> supers = typeUtils.directSupertypes(declaredType); for (TypeMirror superInterface : supers) { mapType = findMapTypeDeclaration(superInterface, context); if (mapType != null) { break; } } return mapType; }
@Override public String convert(TypeElement declaration) throws TemplateModelException { String fqn = declaration.getQualifiedName().toString(); if (classConversions.containsKey(fqn)) { return classConversions.get(fqn); } else if (declaration.getKind() == ElementKind.ENUM) { return "String"; } else if (isCollection(declaration) || isMap(declaration)) { return "Array"; } if (this.jacksonContext != null) { AdapterType adapterType = JacksonUtil.findAdapterType(declaration, this.jacksonContext); if (adapterType != null) { return convert(adapterType.getAdaptingType()); } } if (this.jackson1Context != null) { com.webcohesion.enunciate.modules.jackson1.model.adapters.AdapterType adapter1Type = com.webcohesion.enunciate.modules.jackson1.model.util.JacksonUtil.findAdapterType(declaration, this.jackson1Context); if (adapter1Type != null) { return convert(adapter1Type.getAdaptingType()); } } String convertedPackage = convertPackage(this.context.getProcessingEnvironment().getElementUtils().getPackageOf(declaration)); ClientName specifiedName = declaration.getAnnotation(ClientName.class); String simpleName = specifiedName == null ? declaration.getSimpleName().toString() : specifiedName.value(); return convertedPackage + getPackageSeparator() + simpleName; }