private JMethod addPublicSetMethod(JDefinedClass jclass, JMethod internalSetMethod) { JMethod method = jclass.method(PUBLIC, jclass.owner().VOID, SETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar valueParam = method.param(Object.class, "value"); JBlock body = method.body(); JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then(); // if we have additional properties, then put value. JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {}); if (getAdditionalProperties != null) { JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1); notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam) .arg(cast(additionalPropertiesType, valueParam))); } // else throw exception. else { notFound._throw(illegalArgumentInvocation(jclass, nameParam)); } return method; }
private JMethod addPublicWithMethod(JDefinedClass jclass, JMethod internalSetMethod) { JMethod method = jclass.method(PUBLIC, jclass, BUILDER_NAME); JVar nameParam = method.param(String.class, "name"); JVar valueParam = method.param(Object.class, "value"); JBlock body = method.body(); JBlock notFound = body._if(JOp.not(invoke(internalSetMethod).arg(nameParam).arg(valueParam)))._then(); // if we have additional properties, then put value. JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {}); if (getAdditionalProperties != null) { JType additionalPropertiesType = ((JClass) (getAdditionalProperties.type())).getTypeParameters().get(1); notFound.add(invoke(getAdditionalProperties).invoke("put").arg(nameParam) .arg(cast(additionalPropertiesType, valueParam))); } // else throw exception. else { notFound._throw(illegalArgumentInvocation(jclass, nameParam)); } body._return(_this()); return method; }
@Override public void caseAQmarkExpressionNoName(AQmarkExpressionNoName node) { ExpressionAdapter eaCond = new ExpressionAdapter(new JExprParent(), context); node.getCond().apply(eaCond); ExpressionAdapter eaTrue = new ExpressionAdapter(new JExprParent(), context); node.getTrue().apply(eaTrue); ExpressionAdapter eaFalse = new ExpressionAdapter(new JExprParent(), context); node.getFalse().apply(eaFalse); expr = JOp.cond(eaCond.expr, eaTrue.expr, eaFalse.expr); }
@Override public void onBoolean(HashCodeArguments arguments, JBlock block, boolean isAlwaysSet) { ifHasSetValueAssignPlusValueHashCode(arguments, block, JOp.cond(arguments.value(), JExpr.lit(1231), JExpr.lit(1237)), isAlwaysSet, true); }
@Override public void onDouble(HashCodeArguments arguments, JBlock block, boolean isAlwaysSet) { // long bits = doubleToLongBits(value); final JVar bits = block.decl(JMod.FINAL, getCodeModel().LONG, arguments .value().name() + "Bits", getCodeModel().ref(Double.class) .staticInvoke("doubleToLongBits").arg(arguments.value())); // return (int)(bits ^ (bits >>> 32)); final JExpression valueHashCode = JExpr.cast(getCodeModel().INT, JOp.xor(bits, JOp.shrz(bits, JExpr.lit(32)))); ifHasSetValueAssignPlusValueHashCode(arguments, block, valueHashCode, isAlwaysSet, true); }
@Override public void onDouble(EqualsArguments arguments, JBlock block, boolean isAlwaysSet) { final JClass Double$class = getCodeModel().ref(Double.class); final JExpression leftValueLongBits = Double$class.staticInvoke( "doubleToLongBits").arg(arguments.leftValue()); final JExpression rightValueLongBits = Double$class.staticInvoke( "doubleToLongBits").arg(arguments.rightValue()); returnFalseIfNotEqualsCondition(arguments, block, isAlwaysSet, JOp.ne(leftValueLongBits, rightValueLongBits)); }
@Override public void onFloat(EqualsArguments arguments, JBlock block, boolean isAlwaysSet) { final JClass Float$class = getCodeModel().ref(Float.class); final JExpression leftValueLongBits = Float$class.staticInvoke( "floatToIntBits").arg(arguments.leftValue()); final JExpression rightValueLongBits = Float$class.staticInvoke( "floatToIntBits").arg(arguments.rightValue()); returnFalseIfNotEqualsCondition(arguments, block, isAlwaysSet, JOp.ne(leftValueLongBits, rightValueLongBits)); }
@Override public JExpression unwrapCondifiton(JExpression source) { final JType type = getTypeRef().getTarget().toType(outline.parent(), Aspect.EXPOSED); return JOp._instanceof(source, type); }
@Override public JExpression unwrapCondifiton(JExpression source) { final CReferencePropertyInfo core = (CReferencePropertyInfo) this.core; JExpression predicate = null; if (core.getElements().isEmpty()) { predicate = null; } else { for (CElement element : core.getElements()) { if (element instanceof CElementInfo) { CElementInfo elementinfo = (CElementInfo) element; final SingleWrappingReferenceElementInfoField field = new SingleWrappingReferenceElementInfoField( outline, prop, core, elementinfo); final JExpression condition = field .unwrapCondifiton(source); predicate = (predicate == null) ? condition : JOp.cor( predicate, condition); } else { // TODO Other cases currently not supported. } } } final JExpression isElement = codeModel.ref(JAXBContextUtils.class) .staticInvoke("isElement").arg(contextPath).arg(source); return predicate == null ? isElement : JOp.cand(JOp.not(predicate), isElement); }
@Override public JExpression visit(Expression.BinaryOp op, JExpression left, JExpression right) { if (op.op.javaMethod == "cmp") { final JExpression cmp_res = left.invoke("compareTo").arg(right); final JExpression cond; switch(op.op){ case EQ: cond = cmp_res.eq(JExpr.lit(0)); break; case NE: cond = cmp_res.ne(JExpr.lit(0)); break; case LT: cond = cmp_res.lt(JExpr.lit(0)); break; case GT: cond = cmp_res.gt(JExpr.lit(0)); break; case LE: cond = cmp_res.lte(JExpr.lit(0)); break; case GE: cond = cmp_res.gte(JExpr.lit(0)); break; default: throw new RuntimeException(); } return JOp.cond(cond, TypeInt_t.staticRef("True"), TypeInt_t.staticRef("False")); } else { return left.invoke(op.op.javaMethod).arg(right); } }
@Override public JExpression visit(Expression.LogicalBinaryOp op, JExpression left) { JExpression right = op.right.accept(this); switch (op.op) { case LOG_AND: return JOp.cond(left.invoke("isTrue"), right, left); case LOG_OR: return JOp.cond(left.invoke("isTrue"), left, right); default: throw new RuntimeException(); } }
@Override public JExpression visit(Expression.BinaryOp op, JExpression left, JExpression right) { if (op.left.type instanceof TypeInt && op.right.type instanceof TypeInt) { JExpression expr = binaryOp_int_int(op, left, right); if (expr != null) return expr; } if (op.op.javaMethod == "cmp") { final JExpression cmp_res = left.invoke("compareTo").arg(right); final JExpression cond; switch(op.op){ case EQ: cond = cmp_res.eq(JExpr.lit(0)); break; case NE: cond = cmp_res.ne(JExpr.lit(0)); break; case LT: cond = cmp_res.lt(JExpr.lit(0)); break; case GT: cond = cmp_res.gt(JExpr.lit(0)); break; case LE: cond = cmp_res.lte(JExpr.lit(0)); break; case GE: cond = cmp_res.gte(JExpr.lit(0)); break; default: throw new RuntimeException(); } JClass int_t = this.model.ref(TypeInt.class); return JOp.cond(cond, int_t.staticRef("True"), int_t.staticRef("False")); } else { return left.invoke(op.op.javaMethod).arg(right); } }
public TreeVarGenerator(final JBlock body, final String fieldName) { this.fieldPathVar = body.decl(JMod.FINAL, PartialCopyGenerator.this.pluginContext.codeModel._ref(PropertyTree.class), fieldName + "PropertyTree", JOp.cond(PartialCopyGenerator.this.propertyTreeParam.eq(JExpr._null()), JExpr._null(),PartialCopyGenerator.this.propertyTreeParam.invoke("get").arg(JExpr.lit(fieldName))) ); }
private JExpression getIncludeCondition(final JVar fieldPathVar) { return JOp.cond( PartialCopyGenerator.this.propertyTreeUseParam.eq(PartialCopyGenerator.this.pluginContext.includeConst), fieldPathVar.ne(JExpr._null()), fieldPathVar.eq(JExpr._null()).cor(fieldPathVar.invoke("isLeaf").not()) ); }
public void generateMetaFields() { if(this.classOutline.getSuperClass() != null) { this.selectorClass._extends(this.selectorGenerator.getInfoClass(this.classOutline.getSuperClass().implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } for (final FieldOutline fieldOutline : this.classOutline.getDeclaredFields()) { final JFieldVar definedField = PluginUtil.getDeclaredField(fieldOutline); if (definedField != null) { final JType elementType = PluginUtil.getElementType(fieldOutline); if (elementType.isReference()) { final ClassOutline modelClass = this.selectorGenerator.getPluginContext().getClassOutline(elementType); final JClass returnType; if (modelClass != null) { returnType = this.selectorGenerator.getInfoClass(modelClass.implClass).selectorClass.narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } else { returnType = this.selectorGenerator.getPluginContext().codeModel.ref(this.selectorGenerator.selectorBaseClass).narrow(this.rootTypeParam).narrow(this.selectorClass.narrow(this.rootTypeParam).narrow(this.parentTypeParam)); } final JFieldVar includeField = this.selectorClass.field(JMod.PRIVATE, returnType, definedField.name(), JExpr._null()); final JFieldRef fieldRef = JExpr._this().ref(includeField); final JMethod includeMethod = this.selectorClass.method(JMod.PUBLIC, returnType, definedField.name()); if(this.selectorGenerator.selectorParamName != null) { final JVar includeParam = includeMethod.param(JMod.FINAL, this.selectorGenerator.getSelectorParamType(this.classOutline.implClass, elementType), this.selectorGenerator.selectorParamName); includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name())).arg(includeParam)), fieldRef)); } else { includeMethod.body()._return(JOp.cond(fieldRef.eq(JExpr._null()), fieldRef.assign(JExpr._new(returnType).arg(JExpr._this().ref("_root")).arg(JExpr._this()).arg(JExpr.lit(definedField.name()))), fieldRef)); } this.buildChildrenMethod.body()._if(fieldRef.ne(JExpr._null()))._then().add(this.productMapVar.invoke("put").arg(JExpr.lit(definedField.name())).arg(fieldRef.invoke("init"))); } } } this.buildChildrenMethod.body()._return(this.productMapVar); }
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); } }
/** * Update getters to use Java List. For example: * ArrayOfInvoices getInvoices() -> List<Invoice> getInvoices() */ private void updateArrayOfGetters(ClassOutline co, JCodeModel model) { JDefinedClass implClass = co.implClass; List<JMethod> removedMethods = new ArrayList<>(); Iterator<JMethod> iter = implClass.methods().iterator(); while (iter.hasNext()) { JMethod method = iter.next(); if (method.type().name().startsWith("ArrayOf")) { removedMethods.add(method); iter.remove(); } } for (JMethod removed : removedMethods) { // Parse the old code to get the variable name StringWriter oldWriter = new StringWriter(); removed.body().state(new JFormatter(oldWriter)); String oldBody = oldWriter.toString(); String varName = oldBody.substring(oldBody.indexOf("return ") + "return ".length(), oldBody.indexOf(";")); // Build the new method JClass newReturnType = (JClass) ((JDefinedClass) removed.type()).fields().values().iterator().next().type(); JMethod newMethod = implClass.method(removed.mods().getValue(), newReturnType, removed.name()); JFieldVar field = implClass.fields().get(varName); JClass typeParameter = newReturnType.getTypeParameters().get(0); String fieldName = model._getClass(field.type().fullName()).fields().keySet().iterator().next(); newMethod.body()._return( JOp.cond(field.eq(JExpr._null()), JExpr._new(model.ref("java.util.ArrayList").narrow(typeParameter)), field.invoke("get" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1)))); } }
/** * JavaType -> Item * * @param getter * @param type * (return-)type of {@code getter} * @param f * corresponds to (field of) {@code getter} * @return an {@link JExpression} that evaluates to a * {@link FieldValuesUpdate} containing the return value of * {@code getter}. If the field type is not supported, returns * {@code null}. * @throws JClassAlreadyExistsException */ private JExpression createFieldValuesUpdate(JMethod getter, PodioType type, ApplicationField f) throws JClassAlreadyExistsException { switch (type) { case TEXT: return JExpr ._new(jCodeModel.ref(FieldValuesUpdate.class)) .arg(f.getExternalId()) .arg("value") .arg(JOp.cond(JExpr.invoke(getter).invoke("length").eq(JExpr.lit(0)), JExpr.lit(" "), JExpr.invoke(getter))); case NUMBER: case DURATION: case PROGRESS: return JExpr._new(jCodeModel.ref(FieldValuesUpdate.class)).arg(f.getExternalId()).arg("value") .arg(JExpr.invoke(getter)); // all PodioField implementations are treated equally here: case DATE: case MONEY: return JExpr.invoke(getter).invoke("getFieldValuesUpdate").arg(JExpr.lit(f.getExternalId())); case CATEGORY_SINGLE: // new FieldValuesUpdate("status", "value", // customer.getPowerStatus().getId()) return JExpr._new(jCodeModel.ref(FieldValuesUpdate.class)).arg(f.getExternalId()).arg("value") .arg(JExpr.invoke(getter).invoke("getPodioId")); case CATEGORY_MULTI: return JExpr.invoke("getFielddValuesUpdateFromMultiCategory").arg(JExpr.invoke(getter)) .arg(f.getExternalId()); case CONTACT: return JExpr.invoke("getFieldValuesUpdateFromContacts").arg(JExpr.invoke(getter)).arg(f.getExternalId()); case EMBED: return JExpr.invoke("getFieldValuesUpdateFromEmbeds").arg(JExpr.invoke(getter)).arg(f.getExternalId()); case APP: return JExpr.invoke("getFieldValuesUpdateFromApp").arg(JExpr.invoke(getter)).arg(f.getExternalId()); default: return null; } }
protected HoldingContainer generateEvalBody(ClassGenerator<?> g, CompleteType resolvedOutput, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars) { g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", registeredNames[0])); JBlock sub = new JBlock(true, true); JBlock topSub = sub; HoldingContainer out = null; // add outside null handling if it is defined. if (nullHandling == NullHandling.NULL_IF_NULL) { JExpression e = null; for (HoldingContainer v : inputVariables) { final JExpression isNullExpr; if (v.isReader()) { isNullExpr = JOp.cond(v.getHolder().invoke("isSet"), JExpr.lit(1), JExpr.lit(0)); } else { isNullExpr = v.getIsSet(); } if (e == null) { e = isNullExpr; } else { e = e.mul(isNullExpr); } } if (e != null) { // if at least one expression must be checked, set up the conditional. out = g.declare(resolvedOutput); e = e.eq(JExpr.lit(0)); JConditional jc = sub._if(e); jc._then().assign(out.getIsSet(), JExpr.lit(0)); sub = jc._else(); } } if (out == null) { out = g.declare(resolvedOutput); } // add the subblock after the out declaration. g.getEvalBlock().add(topSub); JVar internalOutput = sub.decl(JMod.FINAL, resolvedOutput.getHolderType(g.getModel()), getReturnName(), JExpr._new(resolvedOutput.getHolderType(g.getModel()))); addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false); if (sub != topSub || inputVariables.length == 0) { sub.assign(internalOutput.ref("isSet"), JExpr.lit(1));// Assign null if NULL_IF_NULL mode } sub.assign(out.getHolder(), internalOutput); g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", registeredNames[0])); return out; }
protected HoldingContainer generateEvalBody(ClassGenerator<?> g, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FieldReference ref) { g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0])); JBlock sub = new JBlock(true, true); JBlock topSub = sub; HoldingContainer out = null; MajorType returnValueType = getReturnType(); // add outside null handling if it is defined. if (getNullHandling() == NullHandling.NULL_IF_NULL) { JExpression e = null; for (HoldingContainer v : inputVariables) { if (v.isOptional()) { JExpression isNullExpr; if (v.isReader()) { isNullExpr = JOp.cond(v.getHolder().invoke("isSet"), JExpr.lit(1), JExpr.lit(0)); } else { isNullExpr = v.getIsSet(); } if (e == null) { e = isNullExpr; } else { e = e.mul(isNullExpr); } } } if (e != null) { // if at least one expression must be checked, set up the conditional. returnValueType = getReturnType().toBuilder().setMode(DataMode.OPTIONAL).build(); out = g.declare(returnValueType); e = e.eq(JExpr.lit(0)); JConditional jc = sub._if(e); jc._then().assign(out.getIsSet(), JExpr.lit(0)); sub = jc._else(); } } if (out == null) { out = g.declare(returnValueType); } // add the subblock after the out declaration. g.getEvalBlock().add(topSub); JVar internalOutput = sub.decl(JMod.FINAL, g.getHolderType(returnValueType), getReturnValue().getName(), JExpr._new(g.getHolderType(returnValueType))); addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false); if (sub != topSub) { sub.assign(internalOutput.ref("isSet"),JExpr.lit(1));// Assign null if NULL_IF_NULL mode } sub.assign(out.getHolder(), internalOutput); if (sub != topSub) { sub.assign(internalOutput.ref("isSet"),JExpr.lit(1));// Assign null if NULL_IF_NULL mode } g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0])); return out; }
public JExpression _instanceof(JType type) { return JOp.cand(leftValue()._instanceof(type), rightValue() ._instanceof(type)); }
private void returnFalseIfNe(EqualsArguments arguments, JBlock block, boolean isAlwaysSet) { returnFalseIfNotEqualsCondition(arguments, block, isAlwaysSet, JOp.ne(arguments.leftValue(), arguments.rightValue())); }
@Override public JExpression unwrapCondifiton(JExpression source) { return JOp._instanceof(source, _class); }
@Override public JExpression wrapCondifiton(JExpression source) { return JOp.ne(source, JExpr._null()); }
public JExpression count() { return JOp.cond( field.eq(JExpr._null()), JExpr.lit(0), field.invoke("size") ); }
public JExpression hasSetValue() { return JOp.ne(field, JExpr._null()); }
@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 public JExpression visit(Expression.Ternary op, JExpression cond) { return JOp.cond(cond.invoke("isTrue"), op.a.accept(this), op.b.accept(this)); }
public static JExpression nullSafe(final JExpression test, final JExpression source) { return JOp.cond(test.eq(JExpr._null()), JExpr._null(), source); }