body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE);
private JExpression buildQNameCompare(QName name, JExpression localPart, JExpression namespaceUri) { JExpression localInv = JExpr.lit(name.getLocalPart()).eq(localPart); String ns = name.getNamespaceURI(); JExpression nsInv = JExpr.lit(ns).eq(namespaceUri); if (ns.equals("")) { nsInv = nsInv.cor(namespaceUri.eq(JExpr._null())); } JExpression qnameCompare = localInv.cand(nsInv); return qnameCompare; }
public static JExpression nullSafe(final JExpression test, final JExpression source) { return JOp.cond(test.eq(JExpr._null()), JExpr._null(), source); }
@Override protected JExpression unwrap(JExpression source) { return JOp.cond(source.eq(JExpr._null()), JExpr._null(), source .invoke("value")); }
@Override protected JExpression unwrap(JExpression source) { return JOp.cond(source.eq(JExpr._null()), JExpr._null(), source .invoke("value")); }
@Override protected JExpression wrap(JExpression target) { return JOp.cond(target.eq(JExpr._null()), JExpr._null(), this.enumClass .staticInvoke("fromValue").arg(target)); }
@Override protected JExpression wrap(JExpression target) { return JOp.cond(target.eq(JExpr._null()), JExpr._null(), this.enumClass .staticInvoke("fromValue").arg(target)); }
JForEach loop(final JBlock block, final JExpression source, final JType sourceElementType, final JAssignmentTarget target, final JType targetElementType) { final JConditional ifNull = block._if(source.eq(JExpr._null())); ifNull._then().assign(target, JExpr._null()); ifNull._else().assign(target, JExpr._new(this.pluginContext.arrayListClass.narrow(targetElementType))); return ifNull._else().forEach(sourceElementType, BuilderGenerator.ITEM_VAR_NAME, source); }
public JForEach loop(final JBlock block, final JExpression source, final JType sourceElementType, final JAssignmentTarget target, final JType targetElementType) { final JConditional ifNull = block._if(source.eq(JExpr._null())); ifNull._then().assign(target, JExpr._null()); ifNull._else().assign(target, JExpr._new(this.arrayListClass.narrow(targetElementType))); return ifNull._else().forEach(sourceElementType, "_item", source); }
private JExpression getClassInfoCopyExpression( final FieldOutline fieldOutline, final CClassInfo type, final JBlock block, final JExpression sourceExpr, final boolean sourceMaybeNull ) { block.directStatement( "// CClassInfo: " + type.toType( fieldOutline.parent().parent(), Aspect.IMPLEMENTATION ).binaryName() ); if ( sourceMaybeNull ) { return JOp.cond( sourceExpr.eq( JExpr._null() ), JExpr._null(), sourceExpr.invoke( "clone" ) ); } else { return sourceExpr.invoke( "clone" ); } }
public void createEquals(final JDefinedClass bean, final Iterable<JFieldVar> fields) { final JMethod method = bean.method(JMod.PUBLIC, this.codeModel.BOOLEAN, "equals"); method.annotate(java.lang.Override.class); final JVar object = method.param(_type(java.lang.Object.class.getName()), "object"); final JBlock block = method.body(); block._if(JExpr._this().eq(object))._then()._return(JExpr.TRUE); block._if(object._instanceof(bean).not())._then()._return(JExpr.FALSE); JExpression result = JExpr.TRUE; final JExpression other = block.decl(bean, "other", JExpr.cast(bean, object)); final JClass objectUtilities = _classByNames(net.anwiba.commons.lang.object.ObjectUtilities.class.getName()); for (final JFieldVar field : fields) { result = result.cand(objectUtilities.staticInvoke("equals").arg(JExpr.refthis(field.name())).arg(other.ref(field))); } block._return(result); } }
private void declareEqualsMethod(JCodeModel model, TypeInfo pair) { JDefinedClass dc = pair.implClass; JDefinedClass dci = pair.interfaceClass; JFieldRef idField = JExpr.ref("id"); JMethod method = dc.method(JMod.PUBLIC, boolean.class, "equals"); JVar param = method.param(Object.class, "other"); JBlock body = method.body(); body._if(idField.ne(JExpr._null()).cand(param._instanceof(dci))) ._then()._return(idField.invoke("equals").arg( ((JExpression)JExpr.cast(dci, param)).invoke("getId"))); body._return(JExpr._this().eq(param)); }
private void createEqualsMethod(final JDefinedClass implClass, final List<JFieldVar> fields, final boolean isSuperClass) { final JMethod method = implClass.method(JMod.PUBLIC, implClass.owner().BOOLEAN, OPERATION_EQUALS); method.annotate(Override.class); final JVar vObj = method.param(Object.class, OBJ); final JConditional condMe = method.body()._if(JExpr._this().eq(vObj)); condMe._then()._return(JExpr.TRUE); final JConditional condNull = method.body()._if(vObj.eq(JExpr._null())); condNull._then()._return(JExpr.FALSE); if (isSuperClass) { final JConditional condSuper = method.body()._if(JExpr._super().invoke(OPERATION_EQUALS).arg(vObj).eq(JExpr.FALSE)); condSuper._then()._return(JExpr.FALSE); } final JVar vOther = method.body().decl(JMod.FINAL, implClass, OTHER, JExpr.cast(implClass, vObj)); final JClass objectsClass = implClass.owner().ref(Objects.class); final List<JFieldVar> clonedList = new ArrayList<JFieldVar>(fields.size()); clonedList.addAll(fields); final JFieldVar first = clonedList.remove(IDX_TO_REMOVE); final JBlock block = new JBlock(); JExpression invocation = block.staticInvoke(objectsClass, OPERATION_EQUALS).arg(JExpr.ref(first.name())).arg(vOther.ref(first.name())); for (final JFieldVar jFieldVar : clonedList) { invocation = JOp.cand(invocation, block.staticInvoke(objectsClass, OPERATION_EQUALS).arg(JExpr.ref(jFieldVar.name())).arg(vOther.ref(jFieldVar.name()))); } method.body()._return(invocation); }
private JExpression getWildcardCopyExpression( final FieldOutline fieldOutline, final CWildcardTypeInfo type, final JBlock block, final JExpression sourceExpr, final boolean sourceMaybeNull ) { block.directStatement( "// CWildcardTypeInfo: " + type.toType( fieldOutline.parent().parent(), Aspect.IMPLEMENTATION ).binaryName() ); if ( sourceMaybeNull ) { return JOp.cond( sourceExpr.eq( JExpr._null() ), JExpr._null(), JExpr.cast( fieldOutline.parent().parent().getCodeModel().ref( Element.class ), sourceExpr.invoke( "cloneNode" ).arg( JExpr.TRUE ) ) ); } else { return JExpr.cast( fieldOutline.parent().parent().getCodeModel().ref( Element.class ), sourceExpr.invoke( "cloneNode" ).arg( JExpr.TRUE ) ); } }
protected JMethod generateObject$Equals(final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod objectEquals = theClass.method(JMod.PUBLIC, theClass .owner().BOOLEAN, "equals"); { final JVar object = objectEquals.param(Object.class, "object"); final JBlock body = objectEquals.body(); body._if(JOp.not(object._instanceof(theClass)))._then()._return( JExpr.FALSE); body._if(JExpr._this().eq(object))._then()._return(JExpr.TRUE); final JVar equalsBuilder = body.decl(JMod.FINAL, theClass.owner() .ref(EqualsBuilder.class), "equalsBuilder", JExpr ._new(theClass.owner().ref(getEqualsBuilderClass()))); body.invoke("equals").arg(object).arg(equalsBuilder); body._return(equalsBuilder.invoke("isEquals")); } return objectEquals; }
protected JMethod generateObject$Equals( final ClassOutline classOutline, final JDefinedClass theClass) { final JMethod objectEquals = theClass.method(JMod.PUBLIC, theClass.owner().BOOLEAN, "equals"); { final JVar object = objectEquals.param(Object.class, "object"); final JBlock body = objectEquals.body(); body._if(JOp.not(object._instanceof(theClass)))._then()._return(JExpr.FALSE); body._if(JExpr._this().eq(object))._then()._return(JExpr.TRUE); final JVar equalsBuilder = body.decl( JMod.FINAL, theClass.owner().ref(EqualsBuilder.class), "equalsBuilder", JExpr._new(theClass.owner().ref(getEqualsBuilderClass()))); body.invoke("equals").arg(object).arg(equalsBuilder); body._return(equalsBuilder.invoke("isEquals")); } return objectEquals; }
private JExpression getNonElementCopyExpression( final FieldOutline fieldOutline, final CNonElement type, final JBlock block, final JExpression sourceExpr, final boolean sourceMaybeNull ) { final JType jType = type.toType( fieldOutline.parent().parent(), Aspect.IMPLEMENTATION ); block.directStatement( "// CNonElement: " + jType.binaryName() ); block.directStatement( "// " + WARNING_PREFIX + ": " + "The '" + jType.binaryName() + "'" ); block.directStatement( "// " + WARNING_PREFIX + ": type was not part of the compilation unit." ); block.directStatement( "// " + WARNING_PREFIX + ": " + "The CC-XJC plugin assumes that type to declare a 'public Object clone()' method." ); block.directStatement( "// " + WARNING_PREFIX + ": " + "If this warning is part of an 'if instanceof' block, the order of 'if instanceof'" ); block.directStatement( "// " + WARNING_PREFIX + ": " + "statements may be wrong and must be verified." ); this.log( Level.WARNING, "nonElementWarning", fieldOutline.parent().implClass.fullName(), fieldOutline.getPropertyInfo().getName( true ), jType.binaryName(), WARNING_PREFIX ); this.tryCatchCopyExpression = true; if ( sourceMaybeNull ) { return JOp.cond( sourceExpr.eq( JExpr._null() ), JExpr._null(), JExpr.cast( jType, sourceExpr.invoke( "clone" ) ) ); } else { return JExpr.cast( jType, sourceExpr.invoke( "clone" ) ); } }
private void appendHashCode(final JCodeModel codeModel, final JBlock block, final JVar hash, final JType type, final JExpression value, final JExpression isValueSet) { if (type.isArray()) { final JConditional _if = block._if(JExpr._null().eq(value)); _if._then().assign(hash, JOp.mul(JExpr.lit(31), hash)); final JForLoop _for = _if._else()._for(); final JVar i = _for.init(codeModel.INT, "i", JExpr.lit(0)); _for.test(JOp.lt(i, value.ref("length"))); _for.update(JOp.incr(i)); final JVar valueComponent = _for.body().decl(type.elementType(), "valueComponent", value.component(i)); _for.body().assign( hash, JOp.plus( JOp.mul(JExpr.lit(31), hash), valueHashCode(codeModel, type.elementType(), valueComponent, JOp.eq(JExpr._null(), valueComponent)))); } else { block.assign( hash, JOp.plus(JOp.mul(JExpr.lit(31), hash), valueHashCode(codeModel, type, value, isValueSet))); } }
ifNotInstanceof._then().invoke(equalsBuilder, "appendSuper").arg(JExpr.FALSE); ifNotInstanceof._then()._return(); body._if(JExpr._this().eq(object))._then()._return();
private void generateEquals() { equalsGeneric = cls().method(JMod.PUBLIC, cm.BOOLEAN, "equals"); equalsGeneric.param(cm.ref("java.lang.Object"), "other"); equalsGeneric.body().block()._if(_this().eq(ref("other"))) ._then() ._return(TRUE); equalsGeneric.body().block()._if( ref("other").eq(_null()) .cor((ref("other")._instanceof(cls()).not()))) ._then() ._return(FALSE); equalsGeneric.body().block()._return(_this().invoke("equals").arg(cast(cls(), ref("other")))); equalsTypeSpecific = cls().method(JMod.PUBLIC, cm.BOOLEAN, "equals"); equalsTypeSpecific.param(cls(), "other"); equalsTypeSpecific.body().block()._if(ref("other").eq(_null()))._then()._return(FALSE); JConditional test = equalsTypeSpecific.body().block()._if(_this().ref("value").eq(_null()).cand(ref("other").invoke("getValue").ne(_null()))); test._then().block()._return(FALSE); JConditional test2 = test._elseif(_this().ref("value").ne(_null()).cand(ref("other").invoke("getValue").eq(_null()))); test2._then().block()._return(FALSE); JConditional test3 = test2._elseif(_this().ref("value").eq(_null()).cand(ref("other").invoke("getValue").eq(_null()))); test3._then().block()._return(TRUE); test3._else().block()._return(_this().ref("value").invoke("equals").arg(ref("other").invoke("getValue"))); }