Refine search
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) { JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar valueParam = method.param(Object.class, "value"); JBlock body = method.body(); JConditional propertyConditional = null; if (propertiesNode != null) { JsonNode node = property.getValue(); String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node); JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition) : propertyConditional._elseif(condition); JBlock callSite = propertyConditional._then(); addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node); callSite._return(TRUE); JClass extendsType = jclass._extends(); JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else(); JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) }); lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam)); } else { lastBlock._return(FALSE);
private void addSetProperty(JDefinedClass jclass, JBlock callSite, String propertyName, JType propertyType, JVar valueVar, JsonNode node) { JMethod propertySetter = jclass.getMethod(getSetterName(propertyName, node), new JType[] { propertyType }); JConditional isInstance = callSite._if(valueVar._instanceof(propertyType.boxify().erasure())); isInstance._then() .invoke(propertySetter).arg(cast(propertyType.boxify(), valueVar)); isInstance._else() ._throw(illegalArgumentInvocation(jclass, propertyName, propertyType, valueVar)); }
private JMethod addPublicGetMethod(JDefinedClass jclass, JMethod internalGetMethod, JFieldRef notFoundValue) { JMethod method = jclass.method(PUBLIC, jclass.owner()._ref(Object.class), GETTER_NAME); JTypeVar returnType = method.generify("T"); method.type(returnType); Models.suppressWarnings(method, "unchecked"); JVar nameParam = method.param(String.class, "name"); JBlock body = method.body(); JVar valueVar = body.decl(jclass.owner()._ref(Object.class), "value", invoke(internalGetMethod).arg(nameParam).arg(notFoundValue)); JConditional found = method.body()._if(notFoundValue.ne(valueVar)); found._then()._return(cast(returnType, valueVar)); JBlock notFound = found._else(); JMethod getAdditionalProperties = jclass.getMethod("getAdditionalProperties", new JType[] {}); if (getAdditionalProperties != null) { notFound._return(cast(returnType, invoke(getAdditionalProperties).invoke("get").arg(nameParam))); } else { notFound._throw(illegalArgumentInvocation(jclass, nameParam)); } return method; }
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(_enum, fromValue); }
JBlock body = toString.body(); JClass stringBuilderClass = jclass.owner().ref(StringBuilder.class); JVar sb = body.decl(stringBuilderClass, "sb", JExpr._new(stringBuilderClass)); body.add(sb .invoke("append").arg(jclass.dotclass().invoke("getName")) .invoke("append").arg(JExpr.lit('@')) jclass.owner().ref(System.class).staticInvoke("identityHashCode").arg(JExpr._this()))) JVar baseLength = body.decl(jclass.owner().INT, "baseLength", sb.invoke("length")); JBlock superToStringBlock = body._if(superString.ne(JExpr._null()))._then(); superToStringInnerConditional._then().add( superToStringInnerConditional._else().add(sb.invoke("append").arg(superString)); ._then().add(sb.invoke("append").arg(JExpr.lit(','))); trailerConditional._then().add( trailerConditional._else().add( toString.annotate(Override.class);
private void addEquals(JDefinedClass jclass, JsonNode node) { Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node); JMethod equals = jclass.method(JMod.PUBLIC, boolean.class, "equals"); JVar otherObject = equals.param(Object.class, "other"); JBlock body = equals.body(); body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE); body._if(otherObject._instanceof(jclass).eq(JExpr.FALSE))._then()._return(JExpr.FALSE); JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject)); JExpression result = JExpr.lit(true); if (!jclass._extends().fullName().equals(Object.class.getName())) { result = result.cand(JExpr._super().invoke("equals").arg(rhsVar)); if ("double".equals(fieldVar.type().name())) { JClass doubleClass = jclass.owner().ref(Double.class); fieldEquals = doubleClass.staticInvoke("doubleToLongBits").arg(thisFieldRef).eq( doubleClass.staticInvoke("doubleToLongBits").arg(otherFieldRef)); } else if ("float".equals(fieldVar.type().name())) { body._return(result); equals.annotate(Override.class);
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; }
public JStatement validate(final DatabindableDatatype datatype, final JCodeModel codeModel, final JDefinedClass theClass, final JExpression value, final JAssignmentTarget problem) { final JBlock block = newBlock(); final JExpression intValue = value.invoke("intValue"); final JConditional ifNegative = block._if(JOp.lt(intValue, JExpr.lit(0))); ifNegative._then().assign(problem, JExpr._new(codeModel.ref(org.jvnet.jaxbvalidation.problem.datatype.NegativeProblem.class)).arg(value)); final JConditional ifTooBig = ifNegative._else()._if(JOp.gt(intValue, JExpr.lit(65535))); ifTooBig._then().assign(problem, JExpr._new(codeModel.ref(GreaterProblem.class)).arg(value).arg(JExpr._new(codeModel.ref(Integer.class)).arg(JExpr.lit(65535)))); return block; }
private void writeClassLoaderResourceWSDLLocation(String className, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { JBlock staticBlock = cls.init(); staticBlock.assign(urlField, JExpr.dotclass(cm.ref(className)).invoke("getClassLoader").invoke("getResource").arg(wsdlLocation)); JVar exVar = staticBlock.decl(cm.ref(WebServiceException.class), "e", JExpr._null()); JConditional ifBlock = staticBlock._if(urlField.eq(JExpr._null())); ifBlock._then().assign(exVar, JExpr._new(cm.ref(WebServiceException.class)).arg( "Cannot find "+JExpr.quotify('\'', wsdlLocation)+" wsdl. Place the resource correctly in the classpath.")); staticBlock.assign(exField, exVar); }
public JMethod addListSetter(final JDefinedClass definedClass, final JFieldVar field) { final JMethod method = definedClass.method(JMod.PUBLIC, Void.TYPE, getSetterName(field)); method.param(field.type(), field.name()); final JFieldRef thisField = JExpr._this().ref(field.name()); final JConditional ifListIsNull = method.body()._if(thisField.eq(JExpr._null())); ifListIsNull._then().assign(JExpr._this().ref(field.name()), JExpr.ref(field.name())); final JBlock elseBlock = ifListIsNull._else(); final JVar copyVar = elseBlock.decl(field.type(), "copy", JExpr._new(getModel().ref(ArrayList.class)).arg(field)); elseBlock.invoke(JExpr._this().ref(field.name()), "clear"); elseBlock.invoke(JExpr._this().ref(field.name()), "addAll").arg(copyVar); return method; }
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; }
switch(poi.getPrimitiveCategory()) { case BOOLEAN:{ JConditional jc = block._if(returnValue.eq(JExpr._null())); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(BooleanObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._else().assign(castedOI, JConditional booleanJC = jc._else()._if(castedOI.invoke("get").arg(returnValue)); booleanJC._then().assign(returnValueHolder.ref("value"), JExpr.lit(1)); booleanJC._else().assign(returnValueHolder.ref("value"), JExpr.lit(0)); jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(ByteObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._else().assign(castedOI, jc._else().assign(returnValueHolder.ref("value"), castedOI.invoke("get").arg(returnValue)); return block; jc._then().assign(returnValueHolder.ref("isSet"), JExpr.lit(0)); jc._else().assign(returnValueHolder.ref("isSet"), JExpr.lit(1)); JVar castedOI = jc._else().decl( m.directClass(ShortObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); jc._else().assign(castedOI,
JBlock sub = new JBlock(true, true); sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i))); JBlock conditionalBlock = new JBlock(false, false); JConditional jc = conditionalBlock._if(inputVariables[i].getIsSet().ne(JExpr.lit(0))); jc._then().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder()); jc._else().assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), JExpr._null()); sub.add(conditionalBlock); } else { JTryBlock udfEvalTry = sub._try(); udfEvalTry.body().assign(retVal, workspaceJVars[1].invoke("evaluate").arg(workspaceJVars[3])); udfEvalCatch.body() ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())) .arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));
private void createAsPrismContainerValue(JDefinedClass definedClass, JVar containerValueVar) { JMethod getContainer = definedClass.method(JMod.PUBLIC, CLASS_MAP.get(PrismContainerValue.class), METHOD_AS_PRISM_CONTAINER_VALUE); // getContainer.annotate(CLASS_MAP.get(XmlTransient.class)); //create method body JBlock body = getContainer.body(); body._if(containerValueVar.eq(JExpr._null())). // if (_containerValue == null) { _then() // .assign(containerValueVar, // _containerValue = JExpr._new(CLASS_MAP.get(PrismContainerValueImpl.class).narrow(new JClass[0])) // new PrismContainerValueImpl<>( .arg(JExpr._this()) // this) ); body._return(containerValueVar); }
private void writeGetWsdlLocation(JType retType, JDefinedClass cls, JFieldVar urlField, JFieldVar exField) { JMethod m = cls.method(JMod.PRIVATE|JMod.STATIC , retType, "__getWsdlLocation"); JConditional ifBlock = m.body()._if(exField.ne(JExpr._null())); ifBlock._then()._throw(exField); m.body()._return(urlField); }
private void generate_migrate(ClassHolder classHolder) { JMethod method = classHolder.definedClass.method(JMod.PUBLIC, Void.TYPE, "migrate"); method.annotate(Override.class); JVar assistant = method.param(CodeModelTypes.MIGRATION_ASSISTANT.narrow(classHolder.entityClass), "assistant"); JVar sourceRevision = method.param(CodeModelTypes.LONG_PRIMITIVE, "sourceRevision"); JVar targetRevision = method.param(CodeModelTypes.LONG_PRIMITIVE, "targetRevision"); JBlock elseBlock = conditional._else(); Field bracesRequired = JBlock.class.getDeclaredField("bracesRequired"); Field indentRequired = JBlock.class.getDeclaredField("indentRequired"); indentRequired.setAccessible(true); indentRequired.setBoolean(elseBlock, false); conditional = elseBlock._if(ifExpression); } catch (Exception e) { conditional = conditional._elseif(ifExpression); conditional._then().add(classHolder.entityClass.staticInvoke(migrationMethodName).arg(assistant)); JExpression exception = JExpr._new(CodeModelTypes.MIGRATION_EXCEPTION).arg( JExpr.lit("Unable to migrate entity! Could not find migration path from '") .plus(sourceRevision) conditional._else()._throw(exception); } else { method.body()._throw(exception);
private void generateCurieMethod(JCodeModel model, JDefinedClass dc, JVar mapField) { JClass stringBuilderClass = model.ref(StringBuilder.class); JClass namespaceClass = model.ref(Namespace.class); JMethod method = dc.method(JMod.PUBLIC | JMod.STATIC, String.class, "curie"); JVar uriVar = method.param(URI.class, "uri"); JVar nsVar = method.body().decl(namespaceClass, "ns", mapField.invoke("get").arg(uriVar.invoke("getNamespace"))); method.body()._if(JOp.eq(nsVar, JExpr._null()))._then()._return(uriVar.invoke("stringValue")); JVar builderVar = method.body().decl(stringBuilderClass, "builder", JExpr._new(stringBuilderClass)); method.body().add(builderVar.invoke("add").arg(nsVar.invoke("getPrefix"))); method.body().add(builderVar.invoke("add").arg(JExpr.lit(':'))); method.body().add(builderVar.invoke("add").arg(uriVar.invoke("getLocalName"))); method.body()._return(builderVar.invoke("toString")); }
private void createEqualsMethod(ClassOutline classOutline, String baseMethod) { JDefinedClass definedClass = classOutline.implClass; JMethod equals = definedClass.getMethod(METHOD_EQUALS, new JType[]{CLASS_MAP.get(Object.class)}); if (equals != null) { // removeOldCustomGeneratedEquals(classOutline, hasParentAnnotation(classOutline, PRISM_OBJECT)); todo can this be removed? equals = recreateMethod(equals, definedClass); } else { equals = definedClass.method(JMod.PUBLIC, boolean.class, METHOD_EQUALS); } equals.annotate(CLASS_MAP.get(Override.class)); JBlock body = equals.body(); JVar obj = equals.listParams()[0]; JBlock ifNull = body._if(obj._instanceof(definedClass).not())._then(); ifNull._return(JExpr.lit(false)); JVar other = body.decl(definedClass, "other", JExpr.cast(definedClass, obj)); JInvocation invocation = JExpr.invoke(baseMethod).invoke(METHOD_EQUIVALENT); invocation.arg(other.invoke(baseMethod)); body._return(invocation); }
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 withEquals() { JMethod equals = this.pojo.method(JMod.PUBLIC, boolean.class, "equals"); JVar otherObject = equals.param(Object.class, "other"); Class<?> equalsBuilderClass = org.apache.commons.lang3.builder.EqualsBuilder.class; if (!Config.getPojoConfig().isUseCommonsLang3()) { equalsBuilderClass = org.apache.commons.lang.builder.EqualsBuilder.class; } JBlock body = equals.body(); body._if(otherObject.eq(JExpr._null()))._then()._return(JExpr.FALSE); body._if(otherObject.eq(JExpr._this()))._then()._return(JExpr.TRUE); body._if(JExpr._this().invoke("getClass").ne(otherObject.invoke("getClass")))._then()._return(JExpr.FALSE); JVar otherObjectVar = body.decl(this.pojo, "otherObject").init(JExpr.cast(this.pojo, otherObject)); JClass equalsBuilderRef = this.pojo.owner().ref(equalsBuilderClass); JInvocation equalsBuilderInvocation = appendFieldsToEquals(getNonTransientAndNonStaticFields(), otherObjectVar, equalsBuilderRef); body._return(equalsBuilderInvocation.invoke("isEquals")); }