use of com.sun.xml.xsom.XmlString in project jaxb-ri by eclipse-ee4j.
the class BeanGenerator method generateEnumBody.
private void generateEnumBody(EnumOutline eo) {
JDefinedClass type = eo.clazz;
CEnumLeafInfo e = eo.target;
XmlTypeWriter xtw = type.annotate2(XmlTypeWriter.class);
writeTypeName(e.getTypeName(), xtw, eo._package().getMostUsedNamespaceURI());
JCodeModel cModel = model.codeModel;
// since constant values are never null, no point in using the boxed types.
JType baseExposedType = e.base.toType(this, EXPOSED).unboxify();
JType baseImplType = e.base.toType(this, Aspect.IMPLEMENTATION).unboxify();
XmlEnumWriter xew = type.annotate2(XmlEnumWriter.class);
xew.value(baseExposedType);
boolean needsValue = e.needsValueField();
// for each member <m>,
// [RESULT]
// <EnumName>(<deserializer of m>(<value>));
// record generated field names to detect collision
Set<String> enumFieldNames = new HashSet<>();
for (CEnumConstant mem : e.members) {
String constName = mem.getName();
if (!JJavaName.isJavaIdentifier(constName)) {
// didn't produce a name.
getErrorReceiver().error(e.getLocator(), Messages.ERR_UNUSABLE_NAME.format(mem.getLexicalValue(), constName));
}
if (!enumFieldNames.add(constName)) {
getErrorReceiver().error(e.getLocator(), Messages.ERR_NAME_COLLISION.format(constName));
}
// [RESULT]
// <Const>(...)
// ASSUMPTION: datatype is outline-independent
JEnumConstant constRef = type.enumConstant(constName);
if (needsValue) {
constRef.arg(e.base.createConstant(this, new XmlString(mem.getLexicalValue())));
}
if (!mem.getLexicalValue().equals(constName)) {
constRef.annotate2(XmlEnumValueWriter.class).value(mem.getLexicalValue());
}
// set javadoc
if (mem.javadoc != null) {
constRef.javadoc().append(mem.javadoc);
}
eo.constants.add(new EnumConstantOutline(mem, constRef) {
});
}
if (needsValue) {
// [RESULT]
// final <valueType> value;
JFieldVar $value = type.field(JMod.PRIVATE | JMod.FINAL, baseExposedType, "value");
// [RESULT]
// public <valuetype> value() { return value; }
type.method(JMod.PUBLIC, baseExposedType, "value").body()._return($value);
// [RESULT]
// <constructor>(<valueType> v) {
// this.value=v;
// }
{
JMethod m = type.constructor(0);
m.body().assign($value, m.param(baseImplType, "v"));
}
// [RESULT]
// public static <Const> fromValue(<valueType> v) {
// for( <Const> c : <Const>.values() ) {
// if(c.value == v) // or equals
// return c;
// }
// throw new IllegalArgumentException(...);
// }
{
JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue");
JVar $v = m.param(baseExposedType, "v");
JForEach fe = m.body().forEach(type, "c", type.staticInvoke("values"));
JExpression eq;
if (baseExposedType.isPrimitive()) {
eq = fe.var().ref($value).eq($v);
} else {
eq = fe.var().ref($value).invoke("equals").arg($v);
}
fe.body()._if(eq)._then()._return(fe.var());
JInvocation ex = JExpr._new(cModel.ref(IllegalArgumentException.class));
JExpression strForm;
if (baseExposedType.isPrimitive()) {
strForm = cModel.ref(String.class).staticInvoke("valueOf").arg($v);
} else if (baseExposedType == cModel.ref(String.class)) {
strForm = $v;
} else {
strForm = $v.invoke("toString");
}
m.body()._throw(ex.arg(strForm));
}
} else {
// [RESULT]
// public String value() { return name(); }
type.method(JMod.PUBLIC, String.class, "value").body()._return(JExpr.invoke("name"));
// [RESULT]
// public <Const> fromValue(String v) { return valueOf(v); }
JMethod m = type.method(JMod.PUBLIC | JMod.STATIC, type, "fromValue");
m.body()._return(JExpr.invoke("valueOf").arg(m.param(String.class, "v")));
}
}
use of com.sun.xml.xsom.XmlString in project jaxb-ri by eclipse-ee4j.
the class TDTDReader method createAttribute.
protected CPropertyInfo createAttribute(String elementName, String attributeName, String attributeType, String[] enums, short attributeUse, String defaultValue) throws SAXException {
boolean required = attributeUse == USE_REQUIRED;
// get the attribute-property declaration
BIElement edecl = bindInfo.element(elementName);
BIAttribute decl = null;
if (edecl != null)
decl = edecl.attribute(attributeName);
String propName;
if (decl == null)
propName = model.getNameConverter().toPropertyName(attributeName);
else
propName = decl.getPropertyName();
QName qname = new QName("", attributeName);
// if no declaration is specified, just wrap it by
// a FieldItem and let the normalizer handle its content.
TypeUse use;
if (decl != null && decl.getConversion() != null)
use = decl.getConversion().getTransducer();
else
use = builtinConversions.get(attributeType);
CPropertyInfo r = new CAttributePropertyInfo(propName, null, null, /*TODO*/
copyLocator(), qname, use, null, required);
if (defaultValue != null)
r.defaultValue = CDefaultValue.create(use, new XmlString(defaultValue));
return r;
}
use of com.sun.xml.xsom.XmlString in project jaxb-ri by eclipse-ee4j.
the class TypeUseImpl method createConstant.
@Override
public JExpression createConstant(Outline outline, XmlString lexical) {
if (isCollection())
return null;
if (adapter == null)
return coreType.createConstant(outline, lexical);
// [RESULT] new Adapter().unmarshal(CONSTANT);
JExpression cons = coreType.createConstant(outline, lexical);
Class<? extends XmlAdapter> atype = adapter.getAdapterIfKnown();
// try to run the adapter now rather than later.
if (cons instanceof JStringLiteral && atype != null) {
JStringLiteral scons = (JStringLiteral) cons;
XmlAdapter a = ClassFactory.create(atype);
try {
Object value = a.unmarshal(scons.str);
if (value instanceof String) {
return JExpr.lit((String) value);
}
} catch (Exception e) {
// assume that we can't eagerly bind this
}
}
return JExpr._new(adapter.getAdapterClass(outline)).invoke("unmarshal").arg(cons);
}
Aggregations