下面列出了怎么用javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter的API类实例代码及写法,或者点击链接到github查看源代码。
@SuppressWarnings("unchecked")
public static Object useAdapter(Object obj,
XmlJavaTypeAdapter typeAdapter,
boolean marshal,
Object defaultValue) {
if (typeAdapter != null) {
try {
@SuppressWarnings("rawtypes")
XmlAdapter xmlAdapter = typeAdapter.value().newInstance();
if (marshal) {
return xmlAdapter.marshal(obj);
}
return xmlAdapter.unmarshal(obj);
} catch (Exception ex) {
LOG.log(Level.INFO, "(un)marshalling failed, using defaultValue", ex);
}
}
return defaultValue;
}
/**
* Checks if the given adapter is applicable to the declared property type.
*/
private boolean isApplicable(XmlJavaTypeAdapter jta, T declaredType ) {
if(jta==null) return false;
T type = reader().getClassValue(jta,"type");
if(nav().isSameType(declaredType, type))
return true; // for types explicitly marked in XmlJavaTypeAdapter.type()
T ad = reader().getClassValue(jta,"value");
T ba = nav().getBaseClass(ad, nav().asDecl(XmlAdapter.class));
if(!nav().isParameterizedType(ba))
return true; // can't check type applicability. assume Object, which means applicable to any.
T inMemType = nav().getTypeArgument(ba, 1);
return nav().isSubClassOf(declaredType,inMemType);
}
public static List<Annotation> getJaxbAnnotations(Method method) {
List<Annotation> jaxbAnnotation = new ArrayList<>();
Annotation ann = method.getAnnotation(XmlAttachmentRef.class);
if (ann != null) {
jaxbAnnotation.add(ann);
}
ann = method.getAnnotation(XmlMimeType.class);
if (ann != null) {
jaxbAnnotation.add(ann);
}
ann = method.getAnnotation(XmlJavaTypeAdapter.class);
if (ann != null) {
jaxbAnnotation.add(ann);
}
ann = method.getAnnotation(XmlList.class);
if (ann != null) {
jaxbAnnotation.add(ann);
}
return jaxbAnnotation;
}
/**
* Checks if the given adapter is applicable to the declared property type.
*/
private boolean isApplicable(XmlJavaTypeAdapter jta, T declaredType ) {
if(jta==null) return false;
T type = reader().getClassValue(jta,"type");
if(nav().isSameType(declaredType, type))
return true; // for types explicitly marked in XmlJavaTypeAdapter.type()
T ad = reader().getClassValue(jta,"value");
T ba = nav().getBaseClass(ad, nav().asDecl(XmlAdapter.class));
if(!nav().isParameterizedType(ba))
return true; // can't check type applicability. assume Object, which means applicable to any.
T inMemType = nav().getTypeArgument(ba, 1);
return nav().isSubClassOf(declaredType,inMemType);
}
public static Class<?> getValueTypeFromAdapter(Class<?> expectedBoundType,
Class<?> defaultClass,
Annotation[] anns) {
try {
XmlJavaTypeAdapter adapter = getAdapter(expectedBoundType, anns);
if (adapter != null) {
Class<?> boundType = JAXBUtils.getTypeFromAdapter(adapter, null, true);
if (boundType != null && boundType.isAssignableFrom(expectedBoundType)) {
return JAXBUtils.getTypeFromAdapter(adapter, null, false);
}
}
} catch (Throwable ex) {
// ignore
}
return defaultClass;
}
@XmlJavaTypeAdapter(TimeAdapter.class)
@ApiModelProperty(
value = "The current time on the system.",
dataType = "string",
readOnly = true
)
public Date getCurrentTime() {
return currentTime;
}
/**
* Invoked by JAXB for fetching the unique identifier unique for the XML document.
*
* @see org.apache.sis.metadata.iso.ISOMetadata#getID()
*/
@XmlID
@XmlAttribute // Defined in "gco" as unqualified attribute.
@XmlSchemaType(name = "ID")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
private String getID() {
return isNullOrEmpty(identifiers) ? null : MetadataUtilities.getObjectID(this);
}
public J2SJAXBModel bind(
Collection<Reference> rootClasses,
Map<QName,Reference> additionalElementDecls,
String defaultNamespaceRemap,
ProcessingEnvironment env) {
ModelBuilder<TypeMirror, TypeElement, VariableElement, ExecutableElement> builder =
new ModelBuilder<TypeMirror, TypeElement, VariableElement, ExecutableElement>(
InlineAnnotationReaderImpl.theInstance,
new ApNavigator(env),
Collections.<TypeElement, TypeElement>emptyMap(),
defaultNamespaceRemap );
builder.setErrorHandler(new ErrorHandlerImpl(env.getMessager()));
for ( Reference ref : rootClasses ) {
TypeMirror t = ref.type;
XmlJavaTypeAdapter xjta = ref.annotations.getAnnotation(XmlJavaTypeAdapter.class);
XmlList xl = ref.annotations.getAnnotation(XmlList.class);
builder.getTypeInfo(new Ref<TypeMirror, TypeElement>(builder, t, xjta, xl));
}
TypeInfoSet<TypeMirror, TypeElement, VariableElement, ExecutableElement> r = builder.link();
if(r==null) return null;
if(additionalElementDecls==null)
additionalElementDecls = Collections.emptyMap();
else {
// fool proof check
for (Map.Entry<QName, ? extends Reference> e : additionalElementDecls.entrySet()) {
if(e.getKey()==null)
throw new IllegalArgumentException("nulls in additionalElementDecls");
}
}
return new JAXBModelImpl(r, builder.reader, rootClasses, new HashMap<QName, Reference>(additionalElementDecls));
}
public Adapter(
XmlJavaTypeAdapter spec,
AnnotationReader<TypeT,ClassDeclT,?,?> reader,
Navigator<TypeT,ClassDeclT,?,?> nav) {
this( nav.asDecl(reader.getClassValue(spec,"value")), nav );
}
@XmlElement
@XmlJavaTypeAdapter(LinkAdapter.class)
@ApiModelProperty(value = "The WebLink to view the metadata about the extensions contained in the extension bundle.",
dataType = "org.apache.nifi.registry.link.JaxbLink", readOnly = true)
public Link getExtensionsLink() {
return extensionsLink;
}
/**
* @return When this bulletin was generated as a formatted string
*/
@XmlJavaTypeAdapter(TimeAdapter.class)
@ApiModelProperty(
value = "When this bulletin was generated.",
dataType = "string"
)
public Date getTimestamp() {
return timestamp;
}
/**
* @return timestamp of this node
*/
@XmlJavaTypeAdapter(TimestampAdapter.class)
@ApiModelProperty(
value = "The timestamp of the node formatted.",
dataType = "string"
)
public Date getTimestamp() {
return timestamp;
}
/**
* Gets whether a role membership with the given details is applicable for the given qualification.
*
* @param qualification the qualification. cannot be null.
* @param roleMemberships the list of roleMemberships to check for matches. cannot be null.
* @return an immutable list of matched roleMemberships. will not return null.
* @throws RiceIllegalArgumentException if the qualification or roleMemberships is null.
*/
@WebMethod(operationName="getMatchingRoleMemberships")
@XmlElementWrapper(name = "roleMemberships", required = true)
@XmlElement(name = "roleMembership", required = false)
@WebResult(name = "roleMemberships")
List<RoleMembership> getMatchingRoleMemberships(@WebParam(name = "qualification")
@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
Map<String, String> qualification,
@WebParam(name = "roleMemberships")
List<RoleMembership> roleMemberships) throws RiceIllegalArgumentException;
public Adapter(
XmlJavaTypeAdapter spec,
AnnotationReader<TypeT,ClassDeclT,?,?> reader,
Navigator<TypeT,ClassDeclT,?,?> nav) {
this( nav.asDecl(reader.getClassValue(spec,"value")), nav );
}
/**
* This method gets all the members, then traverses down into members of type role and group to obtain the nested principal ids
*
* @param namespaceCode the namespace code of the role.
* @param roleName the name of the role
* @param qualification the qualifications for the roleIds.
* @return a list of role member principalIds for the given roleIds and qualifications, or an empty list if none found.
* @throws RiceIllegalArgumentException if namespaceCode, or roleName is null or blank.
*/
@WebMethod(operationName = "getRoleMemberPrincipalIds")
@XmlElementWrapper(name = "principalIds", required = true)
@XmlElement(name = "principalId", required = false)
@WebResult(name = "principalIds")
@Cacheable(value= RoleMember.Cache.NAME,
key="'namespaceCode=' + #p0 + '|' + 'roleName=' + #p1 + '|' + 'qualification=' + T(org.kuali.rice.core.api.cache.CacheKeyUtils).mapKey(#p2)",
condition="!T(org.kuali.rice.kim.api.cache.KimCacheUtils).isDynamicMembshipRoleByNamespaceAndName(#p0, #p1)" )
Collection<String> getRoleMemberPrincipalIds(@WebParam(name="namespaceCode") String namespaceCode,
@WebParam(name="roleName") String roleName,
@WebParam(name="qualification")
@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
Map<String, String> qualification)
throws RiceIllegalArgumentException;
@XmlJavaTypeAdapter(TimeAdapter.class)
@ApiModelProperty(
value = "The time the flow for the process group was last refreshed.",
dataType = "string"
)
public Date getLastRefreshed() {
return lastRefreshed;
}
/**
* @return time the event occurred
*/
@XmlJavaTypeAdapter(TimestampAdapter.class)
@ApiModelProperty(
value = "The timestamp of the event.",
dataType = "string"
)
public Date getEventTime() {
return eventTime;
}
private NonElement<TypeMirror, TypeElement> getXmlType(Reference r) {
if(r==null)
throw new IllegalArgumentException();
XmlJavaTypeAdapter xjta = r.annotations.getAnnotation(XmlJavaTypeAdapter.class);
XmlList xl = r.annotations.getAnnotation(XmlList.class);
Ref<TypeMirror, TypeElement> ref = new Ref<TypeMirror, TypeElement>(
reader,types.getNavigator(),r.type,xjta,xl);
return types.getTypeInfo(ref);
}
/**
* Assigns the principal with the given id to the role with the specified
* namespace code and name with the supplied set of qualifications.
*
* @param principalId the principalId
* @param namespaceCode the namespaceCode of the Role
* @param roleName the name of the role
* @param qualifications the qualifications for the principalId to be assigned to the role
* @return newly created/assigned RoleMember.
* @throws RiceIllegalArgumentException if princialId, namespaceCode or roleName is null or blank.
*/
@WebMethod(operationName = "assignPrincipalToRole")
@WebResult(name = "roleMember")
@CacheEvict(value={Role.Cache.NAME, Permission.Cache.NAME, Responsibility.Cache.NAME, RoleMembership.Cache.NAME, RoleMember.Cache.NAME, DelegateMember.Cache.NAME, RoleResponsibility.Cache.NAME, DelegateType.Cache.NAME }, allEntries = true)
RoleMember assignPrincipalToRole(@WebParam(name="principalId") String principalId,
@WebParam(name="namespaceCode")
String namespaceCode,
@WebParam(name="roleName")
String roleName,
@WebParam(name="qualifications")
@XmlJavaTypeAdapter(value = MapStringStringAdapter.class)
Map<String, String> qualifications)
throws RiceIllegalArgumentException;
/**
* When the status for this process group was calculated.
*
* @return The the status was calculated
*/
@XmlJavaTypeAdapter(TimeAdapter.class)
@ApiModelProperty(
value = "The time the status for the process group was last refreshed.",
dataType = "string"
)
public Date getStatsLastRefreshed() {
return statsLastRefreshed;
}
@XmlElement
@XmlJavaTypeAdapter(LinkAdapter.class)
@ApiModelProperty(value = "An WebLink to this entity.",
dataType = "org.apache.nifi.registry.link.JaxbLink", readOnly = true)
public Link getLink() {
return link;
}
/**
* @return time the query was submitted
*/
@XmlJavaTypeAdapter(TimestampAdapter.class)
@ApiModelProperty(
value = "The timestamp when the query was submitted.",
dataType = "string"
)
public Date getSubmissionTime() {
return submissionTime;
}
/**
* @return the timezone of the NiFi instance
*/
@XmlJavaTypeAdapter(TimezoneAdapter.class)
@ApiModelProperty(
value = "The timezone of the NiFi instance.",
readOnly = true,
dataType = "string"
)
public Date getTimezone() {
return timezone;
}
@XmlJavaTypeAdapter(StringMapAdapter.class)
@XmlElement(name = "order-details")
public Map<String, String> getDetails() {
if (this.details == null) {
this.details = new HashMap<>();
}
return this.details;
}
@XmlJavaTypeAdapter(CarrierSlaMapAdapter.class)
@XmlElement(name = "carrier-sla-ids")
public Map<String, Long> getCarrierSlaId() {
if (this.carrierSlaId == null) {
this.carrierSlaId = new HashMap<>();
}
return carrierSlaId;
}
/**
* @return action's timestamp
*/
@XmlJavaTypeAdapter(DateTimeAdapter.class)
@ApiModelProperty(
value = "The timestamp of the action.",
dataType = "string"
)
public Date getTimestamp() {
return timestamp;
}
/**
* @return timestamp of the node event
*/
@XmlJavaTypeAdapter(DateTimeAdapter.class)
@ApiModelProperty(
value = "The timestamp of the node event.",
dataType = "string"
)
public Date getTimestamp() {
return timestamp;
}
private NonElement<Type,Class> getXmlType(RuntimeTypeInfoSet tis, TypeReference tr) {
if(tr==null)
throw new IllegalArgumentException();
XmlJavaTypeAdapter xjta = tr.get(XmlJavaTypeAdapter.class);
XmlList xl = tr.get(XmlList.class);
Ref<Type,Class> ref = new Ref<Type,Class>(annotationReader, tis.getNavigator(), tr.type, xjta, xl );
return tis.getTypeInfo(ref);
}
private NonElement<TypeMirror, TypeElement> getXmlType(Reference r) {
if(r==null)
throw new IllegalArgumentException();
XmlJavaTypeAdapter xjta = r.annotations.getAnnotation(XmlJavaTypeAdapter.class);
XmlList xl = r.annotations.getAnnotation(XmlList.class);
Ref<TypeMirror, TypeElement> ref = new Ref<TypeMirror, TypeElement>(
reader,types.getNavigator(),r.type,xjta,xl);
return types.getTypeInfo(ref);
}
/**
* Returns the type of relation between the resources.
*
* @return type of relation between the resources.
*/
@XmlElement(name = "associationType", required = true)
@XmlJavaTypeAdapter(DS_AssociationTypeCode.Since2014.class)
@UML(identifier="associationType", obligation=MANDATORY, specification=ISO_19115)
public AssociationType getAssociationType() {
return associationType;
}