/** * Returns the setter method for a given field. * * @param fieldOutline field outline * @return setter for the specified field */ protected JMethod setter(FieldOutline fieldOutline) { JDefinedClass theClass = fieldOutline.parent().implClass; String publicName = fieldOutline.getPropertyInfo().getName(true); return theClass.getMethod("set" + publicName, new JType[] {fieldOutline.getRawType()}); } }
Accessor( JExpression $target ) { super($target); this.core = IsSetField.this.core.create($target); }
public static JFieldVar field(FieldOutline fieldOutline) { final JDefinedClass theClass = fieldOutline.parent().implClass; return theClass.fields().get( fieldOutline.getPropertyInfo().getName(false)); }
public boolean hasGetter() { for (final JMethod method : this.fieldOutline.parent().implClass.methods()) { if ((method.name().equals("get" + this.fieldOutline.getPropertyInfo().getName(true)) || method.name().equals("is" + this.fieldOutline.getPropertyInfo().getName(true))) && method.params().isEmpty()) { return true; } } return false; }
protected JMethod generateHashCode$hashCode(ClassOutline classOutline, final JDefinedClass theClass) { ClassUtils._implements(theClass, theClass.owner().ref(HashCode.class)); final JMethod hashCode$hashCode = theClass.method(JMod.PUBLIC, theClass .owner().VOID, "hashCode"); { final JVar hashCodeBuilder = hashCode$hashCode.param( HashCodeBuilder.class, "hashCodeBuilder"); final JBlock body = hashCode$hashCode.body(); if (classOutline.getSuperClass() != null) { body.invoke(JExpr._super(), "hashCode").arg(hashCodeBuilder); } final FieldOutline[] fieldOutlines = classOutline .getDeclaredFields(); for (final FieldOutline fieldOutline : fieldOutlines) { final JBlock block = body.block(); final JVar theValue = block.decl(fieldOutline.getRawType(), "the" + fieldOutline.getPropertyInfo().getName(true)); FieldAccessorFactory.createFieldAccessor(fieldOutline, JExpr._this()).toRawValue(block, theValue); block.invoke(hashCodeBuilder, "append").arg(theValue); } } return hashCode$hashCode; } }
private JMethod generateCopyConstructor(final JDefinedClass clazz, final JDefinedClass builderClass, FieldOutline[] declaredFields, FieldOutline[] superclassFields) { final JMethod ctor = createConstructor(builderClass, JMod.PUBLIC); final JVar o = ctor.param(JMod.FINAL, clazz, "o"); ctor.body()._if(o.eq(JExpr._null()))._then()._throw( JExpr._new(builderClass.owner().ref(NullPointerException.class)). arg("Cannot create a copy of '" + builderClass.name() + "' from 'null'.")); JCodeModel codeModel = clazz.owner(); String propertyName = field.getPropertyInfo().getName(false); JMethod getter = getPropertyGetter(field); if (field.getPropertyInfo().isCollection()) { JVar tmpVar = ctor.body().decl(0, getJavaType(field), "_" + propertyName, JExpr.invoke(o, getter)); JConditional conditional = ctor.body()._if(tmpVar.eq(JExpr._null())); conditional._then().assign(JExpr.refthis(propertyName), getNewCollectionExpression(codeModel, getJavaType(field))); String propertyName = field.getPropertyInfo().getName(false); if (field.getPropertyInfo().isCollection()) { JVar tmpVar = ctor.body().decl(0, getJavaType(field), "_" + propertyName, JExpr.ref(o, propertyName)); JConditional conditional = ctor.body()._if(tmpVar.eq(JExpr._null()));
private JMethod generateLazyProxyInitGetter(final ClassOutline classOutline, final FieldOutline fieldOutline) { final JCodeModel m = classOutline.parent().getCodeModel(); final JDefinedClass definedClass = classOutline.implClass; final String fieldName = fieldOutline.getPropertyInfo().getName(false); final String getterName = "get" + fieldOutline.getPropertyInfo().getName(true); final JFieldVar collectionField = definedClass.fields().get(fieldName); final JClass elementType = ((JClass) collectionField.type()).getTypeParameters().get(0); final JClass proxyFieldType = m.ref(BoundList.class).narrow(elementType); final JFieldRef collectionFieldRef = JExpr._this().ref(collectionField); final JFieldRef proxyField = JExpr._this().ref(collectionField.name() + BoundPropertiesPlugin.PROXY_SUFFIX); final JMethod oldGetter = definedClass.getMethod(getterName, new JType[0]); definedClass.methods().remove(oldGetter); final JMethod newGetter = definedClass.method(JMod.PUBLIC, proxyFieldType, getterName); newGetter.body()._if(collectionFieldRef.eq(JExpr._null()))._then().assign(collectionFieldRef, JExpr._new(m.ref(ArrayList.class).narrow(elementType))); final JBlock ifProxyNull = newGetter.body()._if(proxyField.eq(JExpr._null()))._then(); ifProxyNull.assign(proxyField, JExpr._new(m.ref(BoundListProxy.class).narrow(elementType)).arg(collectionFieldRef)); newGetter.body()._return(proxyField); return newGetter; }
private void replaceCollectionGetter(FieldOutline field, final JMethod getter) { JDefinedClass clazz = field.parent().implClass; // remove the old getter clazz.methods().remove(getter); // and create a new one JMethod newGetter = field.parent().implClass.method(getter.mods().getValue(), getter.type(), getter.name()); JBlock block = newGetter.body(); JVar ret = block.decl(getJavaType(field), "ret"); JCodeModel codeModel = field.parent().implClass.owner(); JVar param = generateMethodParameter(getter, field); JConditional conditional = block._if(param.eq(JExpr._null())); conditional._then().assign(ret, getEmptyCollectionExpression(codeModel, param)); conditional._else().assign(ret, getUnmodifiableWrappedExpression(codeModel, param)); block._return(ret); getter.javadoc().append("Returns unmodifiable collection."); }
private void updateSetter(ClassOutline co, FieldOutline fo, JDefinedClass dc) { String fieldName = fo.getPropertyInfo().getName(false); JType type = fo.getRawType(); String typeName = type.fullName(); + fo.getPropertyInfo().getName(true); JMethod method = dc.getMethod(getterName, new JType[0]); JType mtype = method.type(); String setterName = "set" + fo.getPropertyInfo().getName(true); method = dc.getMethod(setterName, new JType[] {mtype}); if (LOG.isLoggable(Level.FINE)) { LOG.fine("Updating setter: " + setterName); JDocComment doc = method.javadoc(); dc.methods().remove(method); method.param(mtype, "value"); JFieldRef fr = JExpr.ref(fieldName); method.body().assign(fr, JExpr.ref("value")); method = dc.method(mods, method.type(), "unset" + fo.getPropertyInfo().getName(true)); method.body().assign(fr, JExpr._null()); method = dc.getMethod("isSet" + fo.getPropertyInfo().getName(true), new JType[0]); if (method != null) {
boolean remapRet) { String fieldName = fo.getPropertyInfo().getName(false); JType type = fo.getRawType(); String typeName = type.fullName(); + fo.getPropertyInfo().getName(true); JMethod method = dc.getMethod(getterName, new JType[0]); JDocComment doc = method.javadoc(); int mods = method.mods().getValue(); JType mtype = method.type(); if (remapRet) { mtype = mtype.unboxify(); dc.methods().remove(method); method = dc.method(mods, mtype, getterName); method.javadoc().append(doc); JFieldRef fr = JExpr.ref(fieldName); if (dvExpr != null) { JExpression test = JOp.eq(JExpr._null(), fr); JConditional jc = method.body()._if(test); jc._then()._return(dvExpr); jc._else()._return(fr); } else { method.body()._return(fr);
private JMethod addWithIfNotNullMethod(JDefinedClass builderClass, FieldOutline field, JMethod unconditionalWithMethod) { if (field.getRawType().isPrimitive()) return null; String fieldName = field.getPropertyInfo().getName(true); JMethod method = builderClass.method(JMod.PUBLIC, builderClass, "with" + fieldName + "IfNotNull"); JVar param = generateMethodParameter(method, field); JBlock block = method.body(); JConditional conditional = block._if(param.eq(JExpr._null())); conditional._then()._return(JExpr.direct("this")); conditional._else()._return(JExpr.invoke(unconditionalWithMethod).arg(param)); return method; }
public FieldOutline generate(ClassOutlineImpl classOutline, CPropertyInfo propertyInfo) { final FieldOutline fieldOutline = new SingleField(classOutline, propertyInfo) { @Override protected String getGetterMethod() { return "get" + prop.getName(true); } protected JType getType(Aspect aspect) { return super.getType(aspect).boxify(); } }; final JClass itemClass = classOutline.implClass.owner().ref( Item.class).narrow(fieldOutline.getRawType().boxify()); classOutline.implClass._implements(itemClass); if (classOutline.parent().getModel().serializable) { classOutline.implClass._implements(Serializable.class); } final JMethod isGetter = FieldAccessorUtils .getter(fieldOutline); if (isGetter.name().startsWith("is")) { final JMethod getter = classOutline.implClass.method( JMod.PUBLIC, isGetter.type(), "get" + isGetter.name().substring(2)); getter.body()._return(JExpr._this().invoke(isGetter)); } return fieldOutline; } };
private void generateAccessors(final FieldOutline fieldOutline, final String propertyName, final JType returnType, final JDefinedClass declaringClass, final F1<JExpression, JVar> getMaker, final F3<JExpression, JBlock, JVar, JVar> setMaker) { final String constantName = getConstantName(fieldOutline); final JMethod getMethod = declaringClass.method(JMod.PUBLIC, returnType, "get"); getMethod.annotate(Override.class); final JVar instanceParam = getMethod.param(JMod.FINAL, fieldOutline.parent().implClass, "_instance_"); getMethod.body()._return(JOp.cond(instanceParam.eq(JExpr._null()), JExpr._null(), getMaker.f(instanceParam))); final JMethod setMethod = declaringClass.method(JMod.PUBLIC, void.class, "set"); setMethod.annotate(Override.class); final JVar setInstanceParam = setMethod.param(JMod.FINAL, fieldOutline.parent().implClass, "_instance_"); final JVar valueParam = setMethod.param(JMod.FINAL, returnType, "_value_"); if (constantName == null) { final JConditional ifNotNull = setMethod.body()._if(setInstanceParam.ne(JExpr._null())); setMaker.f(ifNotNull._then(), setInstanceParam, valueParam); } }
private JMethod addAddMethod(JDefinedClass builderClass, FieldOutline field) { List<JClass> typeParams = ((JClass) getJavaType(field)).getTypeParameters(); if (!typeParams.iterator().hasNext()) { return null; } JMethod method = builderClass.method(JMod.PUBLIC, builderClass, "add" + field.getPropertyInfo().getName(true)); JBlock block = method.body(); String fieldName = field.getPropertyInfo().getName(false); JVar param = method.param(JMod.FINAL, typeParams.iterator().next(), fieldName); JInvocation invocation = JExpr.refthis(fieldName).invoke("add").arg(param); block.add(invocation); block._return(JExpr.direct("this")); return method; }
JMethod m = objectFactory.method( JMod.PUBLIC, sigType, "create" + cc.target.getSqueezedName() ); m.body()._return( JExpr._new(cc.implRef) ); m.javadoc() .append("Create an instance of ") .append(cc.ref); cc.implClass.constructor(JMod.PUBLIC); JMethod m = objectFactory.method( JMod.PUBLIC, cc.ref, "create" + cc.target.getSqueezedName() ); JInvocation inv = JExpr._new(cc.implRef); m.body()._return(inv); FieldAccessor accessor = fo.create(JExpr._this()); inv.arg(m.param( fo.getRawType(), fieldName )); JVar $var = c.param( fo.getRawType(), fieldName ); accessor.fromRawValue(c.body(),'_'+fieldName,$var);
public void toRawValue(JBlock block, JVar $var) { // [RESULT] // if([core.hasSetValue]) // $var = [core.toRawValue].getValue(); // else // $var = null; JConditional cond = block._if(acc.hasSetValue()); JVar $v = cond._then().decl(core.getRawType(), "v" + hashCode());// TODO: unique value control acc.toRawValue(cond._then(),$v); cond._then().assign($var,$v.invoke("getValue")); cond._else().assign($var, JExpr._null()); }
private void generatePropertyAssignment(final JMethod method, FieldOutline fieldOutline, boolean wrapUnmodifiable) { JBlock block = method.body(); JCodeModel codeModel = fieldOutline.parent().implClass.owner(); String fieldName = fieldOutline.getPropertyInfo().getName(false); JVar param = generateMethodParameter(method, fieldOutline); if (fieldOutline.getPropertyInfo().isCollection()) { if (wrapUnmodifiable) { JConditional conditional = block._if(param.eq(JExpr._null())); conditional._then().assign(JExpr.refthis(fieldName), JExpr._null()); conditional._else().assign(JExpr.refthis(fieldName), getDefensiveCopyExpression(codeModel, getJavaType(fieldOutline), param)); } else { block.assign(JExpr.refthis(fieldName), JExpr.ref(fieldName)); } replaceCollectionGetter(fieldOutline, getGetterProperty(fieldOutline)); } else { block.assign(JExpr.refthis(fieldName), JExpr.ref(fieldName)); } }
private JMethod addBuildMethod(JDefinedClass clazz, JDefinedClass builderClass, FieldOutline[] declaredFields, FieldOutline[] superclassFields) { JMethod method = builderClass.method(JMod.PUBLIC, clazz, "build"); JInvocation constructorInvocation = JExpr._new(clazz); for (FieldOutline field : superclassFields) { constructorInvocation.arg(JExpr.ref(field.getPropertyInfo().getName(false))); } for (FieldOutline field : declaredFields) { constructorInvocation.arg(JExpr.ref(field.getPropertyInfo().getName(false))); } method.body()._return(constructorInvocation); return method; }
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() ); } }
definedClazz._implements(hasIdInterface); definedClazz._implements(hasExtensionsInterface); CPropertyInfo propertyInfo = contentField.getPropertyInfo(); definedClazz._implements(iterableInterface.narrow(elementType)); JMethod iteratorMethod = definedClazz.method(JMod.PUBLIC, iteratorInterface.narrow(elementType), "iterator"); iteratorMethod.body()._return(JExpr.invoke("get" + propertyInfo.getName(true)).invoke("iterator"));