private void generate( ClassOutlineImpl outline, CPropertyInfo prop ) { // add isSetXXX and unsetXXX. MethodWriter writer = outline.createMethodWriter(); JCodeModel codeModel = outline.parent().getCodeModel(); FieldAccessor acc = core.create(JExpr._this()); if( generateIsSetMethod ) { // [RESULT] boolean isSetXXX() JExpression hasSetValue = acc.hasSetValue(); if( hasSetValue==null ) { // this field renderer doesn't support the isSet/unset methods generation. // issue an error throw new UnsupportedOperationException(); } writer.declareMethod(codeModel.BOOLEAN,"isSet"+this.prop.getName(true)) .body()._return( hasSetValue ); } if( generateUnSetMethod ) { // [RESULT] void unsetXXX() acc.unsetValues( writer.declareMethod(codeModel.VOID,"unset"+this.prop.getName(true)).body() ); } }
/** * return a JFieldVar that represents the QName field for the given information. * * if it doesn't exist, create a static field in the class and store a new JFieldVar. */ private JExpression getQNameInvocation(CElementInfo ei) { QName name = ei.getElementName(); if(qnameMap.containsKey(name)) { return qnameMap.get(name); } if(qnameMap.size()>1024) // stop gap measure to avoid 'code too large' error in javac. return createQName(name); // [RESULT] // private static final QName _XYZ_NAME = new QName("uri", "local"); JFieldVar qnameField = objectFactory.field( JMod.PRIVATE | JMod.STATIC | JMod.FINAL, QName.class, '_' + ei.getSqueezedName() + "_QNAME", createQName(name)); qnameMap.put(name, qnameField); return qnameField; }
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { JCodeModel cm = outline().getCodeModel(); JClass elementType = ei.toType(outline(),EXPOSED).boxify(); // [RESULT] // $t = new ArrayList(); // for( Type e : $var ) { // $var.add(new JAXBElement(e)); // } // [core.fromRawValue] JClass col = cm.ref(ArrayList.class).narrow(elementType); JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col)); JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t); loop.body().invoke($var,"add").arg(createJAXBElement(loop.var())); acc.fromRawValue(block, uniqueName, $t); }
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); }
public JExpression generate( JCodeModel codeModel, JExpression literal ) { // WhitespaceProcessor.replace(<literal>); if( literal instanceof JStringLiteral ) // optimize return JExpr.lit( WhiteSpaceProcessor.replace(((JStringLiteral)literal).str) ); else return codeModel.ref(WhiteSpaceProcessor.class) .staticInvoke("replace").arg(literal); }
/** * Case from {@link #exposedType} to {@link #implType} if necessary. */ protected final JExpression castToImplType( JExpression exp ) { if(implType==exposedType) return exp; else return JExpr.cast(implType,exp); }
public FieldAccessor create(JExpression targetObject) { return new Accessor(targetObject) { public void unsetValues( JBlock body ) { // you can't unset a value } public JExpression hasSetValue() { return JExpr.TRUE; } }; }
public JExpression createConstant(Outline outline, XmlString literal) { // correctly identifying which constant it maps to is hard, so // here I'm cheating JClass type = toType(outline,Aspect.EXPOSED); for (CEnumConstant mem : members) { if(mem.getLexicalValue().equals(literal.value)) return type.staticRef(mem.getName()); } return null; }
/** * Creates a new {@link CDefaultValue} that computes the default value * by applying a lexical representation to a {@link TypeUse}. */ public static CDefaultValue create(final TypeUse typeUse, final XmlString defaultValue) { return new CDefaultValue() { public JExpression compute(Outline outline) { return typeUse.createConstant(outline,defaultValue); } }; }
ElementOutlineImpl(BeanGenerator parent, CElementInfo ei) { super(ei, parent.getClassFactory().createClass( parent.getContainer( ei.parent, Aspect.EXPOSED ), ei.shortName(), ei.getLocator() )); this.parent = parent; parent.elements.put(ei,this); JCodeModel cm = parent.getCodeModel(); implClass._extends( cm.ref(JAXBElement.class).narrow( target.getContentInMemoryType().toType(parent,Aspect.EXPOSED).boxify())); if(ei.hasClass()) { JType implType = ei.getContentInMemoryType().toType(parent,Aspect.IMPLEMENTATION); JExpression declaredType = JExpr.cast(cm.ref(Class.class),implType.boxify().dotclass()); // why do we have to cast? JClass scope=null; if(ei.getScope()!=null) scope = parent.getClazz(ei.getScope()).implRef; JExpression scopeClass = scope==null?JExpr._null():scope.dotclass(); JFieldVar valField = implClass.field(JMod.PROTECTED|JMod.FINAL|JMod.STATIC,QName.class,"NAME",createQName(cm,ei.getElementName())); // take this opportunity to generate a constructor in the element class JMethod cons = implClass.constructor(JMod.PUBLIC); cons.body().invoke("super") .arg(valField) .arg(declaredType) .arg(scopeClass) .arg(cons.param(implType,"value")); // generate no-arg constructor in the element class (bug #391; section 5.6.2 in JAXB spec 2.1) JMethod noArgCons = implClass.constructor(JMod.PUBLIC); noArgCons.body().invoke("super") .arg(valField) .arg(declaredType) .arg(scopeClass) .arg(JExpr._null()); } }
public JExpression generate( JCodeModel codeModel, JExpression literal ) { // WhitespaceProcessor.replace(<literal>); if( literal instanceof JStringLiteral ) // optimize return JExpr.lit( WhiteSpaceProcessor.collapse(((JStringLiteral)literal).str) ); else return codeModel.ref(WhiteSpaceProcessor.class) .staticInvoke("collapse").arg(literal); }
/** * Wraps a type value into a {@link JAXBElement}. */ protected final JInvocation createJAXBElement(JExpression $var) { JCodeModel cm = codeModel(); return JExpr._new(cm.ref(JAXBElement.class)) .arg(JExpr._new(cm.ref(QName.class)) .arg(ei.getElementName().getNamespaceURI()) .arg(ei.getElementName().getLocalPart())) .arg(getRawType().boxify().erasure().dotclass()) .arg($var); }
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { // [RESULT] // bean.getLIST().addAll($<var>); JVar $list = block.decl(listT,uniqueName+'l',$target.invoke($get)); block.invoke($list,"addAll").arg($var); }
public JExpression generate( JCodeModel codeModel, JExpression literal ) { return literal; }
public FieldAccessor create(JExpression targetObject) { return new Accessor(targetObject); }
Accessor( JExpression $target ) { super($target); this.core = IsSetField.this.core.create($target); }
public JExpression hasSetValue() { return $ref.ne( JExpr._null() ); }
public Accessor create(JExpression targetObject) { return new Accessor(targetObject); }
protected Accessor( JExpression $target ) { super($target); }
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { block.invoke($target,$setAll).arg($var); }
@Override public JExpression hasSetValue() { return field.ne(JExpr._null()).cand(field.ref("length").gt(JExpr.lit(0))); }
/** * Case from {@link #exposedType} to array of {@link #implType} . */ protected final JExpression castToImplTypeArray( JExpression exp ) { return JExpr.cast(implType.array(), exp); }
Accessor( JExpression $target ) { super($target); }
public JExpression createConstant(Outline outline, XmlString lexical) { return JExpr.lit(DatatypeConverter.parseBoolean(lexical.value)); }
protected Accessor( JExpression $target ) { super($target); field = $target.ref(AbstractListField.this.field); }
protected final JExpression box( JExpression exp ) { if(primitiveType==null) return exp; else return primitiveType.wrap(exp); }
public JExpression count() { return JOp.cond( field.eq(JExpr._null()), JExpr.lit(0), field.invoke("size") ); }
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) { throw new UnsupportedOperationException(); }