下面列出了怎么用 com.sun.codemodel.JAnnotatable 的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Annotate the field according to the recipes given as {@link CPropertyInfo}.
*/
protected void annotate( JAnnotatable field ) {
assert(field!=null);
if (prop instanceof CAttributePropertyInfo) {
annotateAttribute(field);
} else if (prop instanceof CElementPropertyInfo) {
annotateElement(field);
} else if (prop instanceof CValuePropertyInfo) {
field.annotate(XmlValue.class);
} else if (prop instanceof CReferencePropertyInfo) {
annotateReference(field);
}
outline.parent().generateAdapterIfNecessary(prop,field);
}
private XmlElementWriter getXew(boolean checkWrapper, JAnnotatable field) {
XmlElementWriter xew;
if(checkWrapper) {
if(xesw==null) {
xesw = field.annotate2(XmlElementsWriter.class);
}
xew = xesw.value();
} else {
xew = field.annotate2(XmlElementWriter.class);
}
return xew;
}
/**
* Annotate the attribute property 'field'
*/
private void annotateAttribute(JAnnotatable field) {
CAttributePropertyInfo ap = (CAttributePropertyInfo) prop;
QName attName = ap.getXmlName();
// [RESULT]
// @XmlAttribute(name="foo", required=true, namespace="bar://baz")
XmlAttributeWriter xaw = field.annotate2(XmlAttributeWriter.class);
final String generatedName = attName.getLocalPart();
final String generatedNS = attName.getNamespaceURI();
// generate name property?
if(!generatedName.equals(ap.getName(false))) {
xaw.name(generatedName);
}
// generate namespace property?
if(!generatedNS.equals("")) { // assume attributeFormDefault == unqualified
xaw.namespace(generatedNS);
}
// generate required property?
if(ap.isRequired()) {
xaw.required(true);
}
}
static
private List<JAnnotationUse> getAnnotations(JAnnotatable annotatable){
try {
Class<?> clazz = annotatable.getClass();
Field field;
while(true){
try {
field = clazz.getDeclaredField("annotations");
break;
} catch(NoSuchFieldException nsfe){
clazz = clazz.getSuperclass();
if(clazz == null){
throw nsfe;
}
}
}
if(!field.isAccessible()){
field.setAccessible(true);
}
return (List)field.get(annotatable);
} catch(ReflectiveOperationException roe){
throw new RuntimeException(roe);
}
}
protected void annotate(JAnnotatable annotatable) {
this.propertyInfo
.acceptPropertyInfoVisitor(new AnnotatePropertyVisitor(
annotatable));
}
public AnnotatePropertyVisitor(final JAnnotatable annotatable) {
Validate.notNull(annotatable);
this.annotatable = annotatable;
}
protected void annotate(JAnnotatable annotatable) {
annotatable.annotate(XmlAnyAttribute.class);
}
/**
* Generate the simplest XmlElement annotation possible taking all semantic optimizations
* into account. This method is essentially equivalent to:
*
* xew.name(ctype.getTagName().getLocalPart())
* .namespace(ctype.getTagName().getNamespaceURI())
* .type(jtype)
* .defaultValue(ctype.getDefaultValue());
*
* @param field
* @param ctype
* @param jtype
* @param checkWrapper true if the method might need to generate XmlElements
*/
private void writeXmlElementAnnotation( JAnnotatable field, CTypeRef ctype, JType jtype,
boolean checkWrapper ) {
// lazily create - we don't know if we need to generate anything yet
XmlElementWriter xew = null;
// these values are used to determine how to optimize the generated annotation
XmlNsForm formDefault = parent()._package().getElementFormDefault();
String mostUsedURI = parent()._package().getMostUsedNamespaceURI();
String propName = prop.getName(false);
// generate the name property?
String generatedName = ctype.getTagName().getLocalPart();
if(!generatedName.equals(propName)) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.name(generatedName);
}
// generate the namespace property?
String generatedNS = ctype.getTagName().getNamespaceURI();
if (((formDefault == XmlNsForm.QUALIFIED) && !generatedNS.equals(mostUsedURI)) ||
((formDefault == XmlNsForm.UNQUALIFIED) && !generatedNS.equals(""))) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.namespace(generatedNS);
}
// generate the required() property?
CElementPropertyInfo ep = (CElementPropertyInfo) prop;
if(ep.isRequired() && exposedType.isReference()) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.required(true);
}
// generate the type property?
// I'm not too sure if this is the right place to handle this, but
// if the schema definition is requiring this element, we should point to a primitive type,
// not wrapper type (to correctly carry forward the required semantics.)
// if it's a collection, we can't use a primitive, however.
if(ep.isRequired() && !prop.isCollection())
jtype = jtype.unboxify();
// when generating code for 1.4, the runtime can't infer that ArrayList<Foo> derives
// from Collection<Foo> (because List isn't parameterized), so always expclitly
// generate @XmlElement(type=...)
if( !jtype.equals(exposedType) || (parent().parent().getModel().options.runtime14 && prop.isCollection())) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.type(jtype);
}
// generate defaultValue property?
final String defaultValue = ctype.getDefaultValue();
if (defaultValue!=null) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.defaultValue(defaultValue);
}
// generate the nillable property?
if (ctype.isNillable()) {
if(xew == null) xew = getXew(checkWrapper, field);
xew.nillable(true);
}
}
@Override
protected void annotate(JAnnotatable field) {
field.annotate(XmlTransient.class);
}