/** * Get the resolved accessor type for this accessor. * * @return the resolved accessor type for this accessor. */ public TypeMirror getResolvedAccessorType() { TypeMirror accessorType = getAccessorType(); if (isAdapted()) { accessorType = getAdapterType().getAdaptingType(accessorType); } return accessorType; }
/** * Finds the interface type that declares that the specified declaration implements XmlAdapter. * * @param declaration The declaration. * @return The interface type, or null if none found. */ private static ClassType findXmlAdapterType(ClassDeclaration declaration) { if (Object.class.getName().equals(declaration.getQualifiedName())) { return null; } ClassType superClass = declaration.getSuperclass(); if (XmlAdapter.class.getName().equals(superClass.getDeclaration().getQualifiedName())) { return superClass; } return findXmlAdapterType(superClass.getDeclaration()); }
if (componentType instanceof ReferenceType && canAdapt((ReferenceType) componentType)) { TypeMirror declaredAdapting = getAdaptingType(); while (declaredAdapting instanceof DecoratedTypeMirror) { declaredAdapting = ((DecoratedTypeMirror) declaredAdapting).getDelegate(); return getAdaptingType();
AdapterType adapterType = new AdapterType(adapterTypeMirror); if (!adaptedType.equals(adapterType.getAdaptedType())) { throw new ValidationException(referer.getPosition(), "Adapter " + adapterTypeMirror.getDeclaration().getQualifiedName() + " does not adapt " + adaptedType);
AdapterType adapterType = new AdapterType(adapterTypeMirror); if ((adaptedType instanceof DeclaredType && adapterType.canAdapt((DeclaredType)adaptedType)) || (unwrappedAdaptedType != adaptedType && unwrappedAdaptedType instanceof ReferenceType && adapterType.canAdapt((ReferenceType) unwrappedAdaptedType))) { return adapterType;
/** * Whether this adapter can adapt the specified type. * * @param type The type. * @return Whether this adapter can adapt the specified type. */ public boolean canAdapt(ReferenceType type) { DecoratedTypeMirror decorated = (DecoratedTypeMirror) TypeMirrorDecorator.decorate(type); ReferenceType adaptedType = getAdaptedType(); if (adaptedType instanceof DeclaredType) { return ((DeclaredType)adaptedType).getDeclaration() != null && decorated.isInstanceOf(((DeclaredType)adaptedType).getDeclaration().getQualifiedName()); } else if (adaptedType instanceof ArrayType) { if (decorated instanceof ArrayType) { TypeMirror adaptedComponentType = ((ArrayType) adaptedType).getComponentType(); while (adaptedComponentType instanceof DecoratedTypeMirror) { adaptedComponentType = ((DecoratedTypeMirror) adaptedComponentType).getDelegate(); } TypeMirror componentType = ((ArrayType) decorated).getComponentType(); while (componentType instanceof DecoratedTypeMirror) { componentType = ((DecoratedTypeMirror) componentType).getDelegate(); } Types typeUtils = Context.getCurrentEnvironment().getTypeUtils(); return typeUtils.isAssignable(componentType, adaptedComponentType); } } return false; }
/** * Find the specified type of the given adaptable element, if it exists. * * @param adaptable The adaptable element for which to find the specified type. * @return The specified XML type, or null if it doesn't exist. */ public static XmlType findSpecifiedType(Adaptable adaptable) throws XmlTypeException { XmlType xmlType = null; if (adaptable.isAdapted()) { xmlType = getXmlType(adaptable.getAdapterType().getAdaptingType()); } else if (adaptable instanceof Accessor) { //The XML type of accessors can be explicitly defined... xmlType = findExplicitSchemaType((Accessor) adaptable); } return xmlType; }
/** * Finds the interface type that declares that the specified declaration implements XmlAdapter. * * @param declaration The declaration. * @return The interface type, or null if none found. */ private static ClassType findXmlAdapterType(ClassDeclaration declaration) { if (Object.class.getName().equals(declaration.getQualifiedName())) { return null; } ClassType superClass = declaration.getSuperclass(); if (XmlAdapter.class.getName().equals(superClass.getDeclaration().getQualifiedName())) { return superClass; } return findXmlAdapterType(superClass.getDeclaration()); }
@Override public String convert(Accessor accessor) throws TemplateModelException { if (accessor.isAdapted()) { //if the type is adapted, the adapting type is already unwrapped. return convert(accessor.getAdapterType().getAdaptingType()); } else { return convert(accessor.getAccessorType()); } }
public AdapterType(ClassType adapterType) { super(adapterType); ClassDeclaration adapterDeclaration = adapterType.getDeclaration(); ClassType adaptorInterfaceType = findXmlAdapterType(adapterDeclaration); if (adaptorInterfaceType == null) { throw new ValidationException(adapterDeclaration.getPosition(), adapterDeclaration.getQualifiedName() + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter."); } Collection<TypeMirror> adaptorTypeArgs = adaptorInterfaceType.getActualTypeArguments(); if ((adaptorTypeArgs == null) || (adaptorTypeArgs.size() != 2)) { throw new ValidationException(adapterDeclaration.getPosition(), adapterDeclaration.getQualifiedName() + " must specify both a value type and a bound type."); } Iterator<TypeMirror> formalTypeIt = adaptorTypeArgs.iterator(); this.adaptingType = formalTypeIt.next(); TypeMirror boundTypeMirror = formalTypeIt.next(); if (!(boundTypeMirror instanceof DeclaredType)) { throw new ValidationException(adapterDeclaration.getPosition(), "Illegal XML adapter: not adapting a declared type (" + boundTypeMirror + ")."); } this.adaptedType = (DeclaredType) boundTypeMirror; }
@Override public String convert(ImplicitChildElement childElement) throws TemplateModelException { if ((childElement instanceof Adaptable) && (((Adaptable) childElement).isAdapted())) { //the adapting type is already unwrapped... return convert(((Adaptable) childElement).getAdapterType().getAdaptingType()); } else { return convert(childElement.getType()); } }
ClassType adaptorInterfaceType = findXmlAdapterType(adapterDeclaration); if (adaptorInterfaceType == null) { throw new ValidationException(adapterDeclaration.getPosition(), adapterDeclaration.getQualifiedName() + " is not an instance of javax.xml.bind.annotation.adapters.XmlAdapter.");
@Override public String convert(Accessor accessor) throws TemplateModelException { TypeMirror accessorType = accessor.getAccessorType(); if (accessor.isAdapted()) { accessorType = accessor.getAdapterType().getAdaptingType(accessor.getAccessorType()); } return convert(accessorType); }
@Override public String convert(ImplicitChildElement childElement) throws TemplateModelException { TypeMirror elementType = childElement.getType(); if ((childElement instanceof Adaptable) && (((Adaptable) childElement).isAdapted())) { elementType = (((Adaptable) childElement).getAdapterType().getAdaptingType(childElement.getType())); } return convert(elementType); }
/** * 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 ClassDeclaration narrowToAdaptingType(ClassDeclaration declaration) { AdapterType adapterType = AdapterUtil.findAdapterType(declaration); if (adapterType != null) { TypeMirror adaptingType = adapterType.getAdaptingType(); if (!(adaptingType instanceof ClassType)) { return declaration; } else { ClassDeclaration adaptingDeclaration = ((ClassType) adaptingType).getDeclaration(); if (adaptingDeclaration == null) { throw new ValidationException(declaration.getPosition(), 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; }
public void visitInterfaceType(InterfaceType interfaceType) { AdapterType adapterType = AdapterUtil.findAdapterType(interfaceType.getDeclaration()); if (adapterType != null) { adapterType.getAdaptingType().accept(this); } else { MapType mapType = MapTypeUtil.findMapType(interfaceType); if (mapType != null) { setMapXmlType(mapType); } else { this.xmlType = null; this.errorMessage = "An interface type cannot be an xml type."; } } }
@Override public String convert(Accessor accessor) throws TemplateModelException { if (accessor.isAdapted()) { //if the type is adapted, the adapting type might be already unwrapped. return convert(accessor.getAdapterType().getAdaptingType(accessor.getAccessorType())); } else { return convert(accessor.getAccessorType()); } }
@Override public String convert(ImplicitChildElement childElement) throws TemplateModelException { if ((childElement instanceof Adaptable) && (((Adaptable) childElement).isAdapted())) { //the adapting type might be already unwrapped... return convert(((Adaptable) childElement).getAdapterType().getAdaptingType(childElement.getType())); } else { return convert(childElement.getType()); } }
public void visitInterfaceType(InterfaceType interfaceType) { AdapterType adapterType = AdapterUtil.findAdapterType(interfaceType.getDeclaration()); if (adapterType != null) { adapterType.getAdaptingType().accept(this); } else { MapType mapType = MapTypeUtil.findMapType(interfaceType); if (mapType != null) { setMapXmlType(mapType); } else if (isInCollection) { this.xmlType = KnownXmlType.ANY_TYPE; } else { this.xmlType = null; this.errorMessage = "An interface type cannot be an xml type."; } } }
AdapterType adapterType = AdapterUtil.findAdapterType(declaration); if (adapterType != null) { return findMapType(adapterType.getAdaptingType());