Refine search
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('@')) .invoke("append").arg( jclass.owner().ref(Integer.class).staticInvoke("toHexString").arg( jclass.owner().ref(System.class).staticInvoke("identityHashCode").arg(JExpr._this()))) .invoke("append").arg(JExpr.lit('['))); JVar baseLength = body.decl(jclass.owner().INT, "baseLength", sb.invoke("length")); JVar superString = body.decl(jclass.owner().ref(String.class), "superString", JExpr._super().invoke("toString")); JBlock superToStringBlock = body._if(superString.ne(JExpr._null()))._then(); superString.invoke("indexOf").arg(JExpr.lit('['))); JVar contentEnd = superToStringBlock.decl(jclass.owner().INT, "contentEnd", superString.invoke("lastIndexOf").arg(JExpr.lit(']'))); contentStart.gte(JExpr.lit(0)).cand(contentEnd.gt(contentStart))); .arg(contentStart.plus(JExpr.lit(1))) ._then().add(sb.invoke("append").arg(JExpr.lit(','))); body.add(sb.invoke("append").arg(JExpr.lit('='))); body.add(sb.invoke("append").arg(JExpr.refthis(fieldVar.name())));
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; }
private void addEquals(JDefinedClass jclass, JsonNode node) { Map<String, JFieldVar> fields = removeFieldsExcludedFromEqualsAndHashCode(jclass.fields(), node); 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)); JFieldRef thisFieldRef = JExpr.refthis(fieldVar.name()); JFieldRef otherFieldRef = JExpr.ref(rhsVar, fieldVar.name()); JExpression fieldEquals; fieldEquals = jclass.owner().ref(Arrays.class).staticInvoke("equals").arg(thisFieldRef).arg(otherFieldRef); } else { fieldEquals = thisFieldRef.eq(otherFieldRef).cor( thisFieldRef.ne(JExpr._null()) .cand(thisFieldRef.invoke("equals").arg(otherFieldRef))); body._return(result); equals.annotate(Override.class);
private JInvocation getUDFInstance(JCodeModel m) { if (isGenericUDF) { return JExpr._new(m.directClass(genericUdfClazz.getCanonicalName())); } else { return JExpr._new(m.directClass(GenericUDFBridge.class.getCanonicalName())) .arg(JExpr.lit(udfName)) .arg(JExpr.lit(false)) .arg(JExpr.lit(udfClazz.getCanonicalName().toString())); } }
private void addOverrideBuilder(JDefinedClass thisJDefinedClass, JMethod parentBuilder, JVar parentParam) { if (thisJDefinedClass.getMethod(parentBuilder.name(), new JType[] {parentParam.type()}) == null) { JMethod builder = thisJDefinedClass.method(parentBuilder.mods().getValue(), thisJDefinedClass, parentBuilder.name()); builder.annotate(Override.class); JVar param = builder.param(parentParam.type(), parentParam.name()); JBlock body = builder.body(); body.invoke(JExpr._super(), parentBuilder).arg(param); body._return(JExpr._this()); } } }
public JMethod generate(ClassContext classContext, FieldItem fieldItem, JDefinedClass theClass) { if (processedClassesMap.keySet().contains(theClass)) { return (JMethod) processedClassesMap.get(theClass); } else { final JCodeModel codeModel = getCodeModel(classContext); final JVar $codes = theClass.field(JMod.PRIVATE + JMod.STATIC, List.class, "codes"); final JMethod constructor = (JMethod) theClass.constructors().next(); constructor.body()._if(JOp.eq($codes, JExpr._null()))._then().assign( $codes, JExpr._new(codeModel.ref(ArrayList.class))); constructor.body().invoke($codes, "add").arg(JExpr.ref("v")); final JMethod values = theClass.method(JMod.PUBLIC | JMod.STATIC, codeModel .ref(String.class) .array(), "codes"); values.body()._return( JExpr.cast(codeModel.ref(String.class).array(), $codes.invoke("toArray").arg( JExpr.newArray(codeModel.ref(String.class), JExpr.invoke($codes, "size"))))); processedClassesMap.put(theClass, values); return values; } } }
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; }
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) { JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue"); JBlock body = method.body(); JConditional propertyConditional = null; JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); if (propertyConditional == null) { propertyConditional = body._if(condition); } else { propertyConditional = propertyConditional._elseif(condition); JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {}); propertyConditional._then()._return(invoke(propertyGetter)); JDefinedClass parentClass = (JDefinedClass) extendsType; JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) }); lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam)); } else { lastBlock._return(notFoundParam);
JMethod hashCode = jclass.method(JMod.PUBLIC, int.class, "hashCode"); JBlock body = hashCode.body(); JVar result = body.decl(jclass.owner().INT, "result", JExpr.lit(1)); JFieldRef fieldRef = JExpr.refthis(fieldVar.name()); fieldHash = JExpr.cast(jclass.owner().INT, fieldRef.xor(fieldRef.shrz(JExpr.lit(32)))); } else if ("boolean".equals(fieldVar.type().name())) { fieldHash = JOp.cond(fieldRef, JExpr.lit(1), JExpr.lit(0)); } else if ("int".equals(fieldVar.type().name())) { fieldHash = fieldRef; } else if ("double".equals(fieldVar.type().name())) { JClass doubleClass = jclass.owner().ref(Double.class); JExpression longField = doubleClass.staticInvoke("doubleToLongBits").arg(fieldRef); fieldHash = JExpr.cast(jclass.owner().INT, longField.xor(longField.shrz(JExpr.lit(32)))); } else if ("float".equals(fieldVar.type().name())) { fieldHash = jclass.owner().ref(Float.class).staticInvoke("floatToIntBits").arg(fieldRef); } else { fieldHash = JExpr.cast(jclass.owner().INT, fieldRef); fieldHash = jclass.owner().ref(Arrays.class).staticInvoke("hashCode").arg(fieldRef); } else { fieldHash = JOp.cond(fieldRef.eq(JExpr._null()), JExpr.lit(0), fieldRef.invoke("hashCode")); body.assign(result, result.mul(JExpr.lit(31)).plus(fieldHash)); if (!jclass._extends().fullName().equals(Object.class.getName())) { body.assign(result, result.mul(JExpr.lit(31)).plus(JExpr._super().invoke("hashCode")));
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); }
public JCodeModel generate(JCodeModel jCodeModel, RefHelper ref) throws IOException { try { jClass = jCodeModel._class(appDetails.getViewPagerAdapterPackage()); jClass._extends(ref.fragmentPagerAdapter()); if (hasLocationsField) { locationsField = jClass.field(JMod.PRIVATE, ref.string().array(), "locations"); JBlock constructorBody = constructor.body(); constructorBody.directStatement("super(fm);"); constructorBody.assign(JExpr._this().ref("locations"), locationsField); if (hasLocationsField) { getCountMethodBody._return(locationsField.ref("length")); } else { JVar fragmentVar = getItemMethodBody.decl(ref.fragment(), "fragment", JExpr._new(ref.ref(sampleFragmentPackage))); JVar bundleVar = getItemMethodBody.decl(ref.bundle(), "bundle", JExpr._new(ref.bundle())); getItemMethodBody.invoke(fragmentVar, "setArguments").arg(bundleVar);
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 { sub.assign(workspaceJVars[3].component(JExpr.lit(i)), workspaceJVars[2].component(JExpr.lit(i))); sub.assign(JExpr.ref(workspaceJVars[3].component(JExpr.lit(i)), "valueHolder"), inputVariables[i].getHolder()); JVar retVal = sub.decl(m._ref(Object.class), "ret"); JTryBlock udfEvalTry = sub._try(); udfEvalTry.body().assign(retVal, workspaceJVars[1].invoke("evaluate").arg(workspaceJVars[3])); JCatchBlock udfEvalCatch = udfEvalTry._catch(m.directClass(Exception.class.getCanonicalName())); JVar exVar = udfEvalCatch.param("ex"); udfEvalCatch.body() ._throw(JExpr._new(m.directClass(RuntimeException.class.getCanonicalName())) .arg(JExpr.lit(String.format("GenericUDF.evaluate method failed"))).arg(exVar));
public void addConstructorFromParcel(JDefinedClass jclass) { JMethod ctorFromParcel = jclass.constructor(JMod.PROTECTED); JVar in = ctorFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in"); if (extendsParcelable(jclass)) { ctorFromParcel.body().directStatement("super(in);"); } for (JFieldVar f : jclass.fields().values()) { if( (f.mods().getValue() & JMod.STATIC) == JMod.STATIC ) { continue; } if (f.type().erasure().name().equals("List")) { ctorFromParcel.body() .invoke(in, "readList") .arg(JExpr._this().ref(f)) .arg(JExpr.direct(getListType(f.type()) + ".class.getClassLoader()")); } else { ctorFromParcel.body().assign( JExpr._this().ref(f), JExpr.cast( f.type(), in.invoke("readValue").arg(JExpr.direct(f.type().erasure().name() + ".class.getClassLoader()")) ) ); } } }
private void addListNavigationConfiguration(JBlock configureActionBarBody) { jClass._implements(ref.sNavigationListener()); JMethod onNavigationItemSelectedMethod = jClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "onNavigationItemSelected"); JVar itemPositionParam = onNavigationItemSelectedMethod.param(jCodeModel.INT, "itemPosition"); onNavigationItemSelectedMethod.param(jCodeModel.LONG, "itemId"); onNavigationItemSelectedMethod.annotate(ref.override()); JBlock onNavigationItemSelectedBody = onNavigationItemSelectedMethod.body(); if (appDetails.isViewPager()) { onNavigationItemSelectedBody.invoke(pagerField, "setCurrentItem").arg(itemPositionParam); onNavigationItemSelectedBody._return(JExpr.TRUE); JInvocation getSupportActionbar = JExpr.invoke("getSupportActionBar"); JInvocation getContext = getSupportActionbar.invoke("getThemedContext"); JVar contextVar = configureActionBarBody.decl(ref.context(), "context", getContext); JExpression rLayoutSherlockSpinner = JExpr.direct("android.R.layout.simple_list_item_1"); arg(contextVar). // arg(rArrayLocations). // arg(rLayoutSherlockSpinner); configureActionBarBody.invoke(getSupportActionbar, "setListNavigationCallbacks").// arg(listVar).// arg(JExpr._this());
private void generateToString() { toString = cls().method(JMod.PUBLIC, cm.ref("java.lang.String"), "toString"); toString.body()._return(ref("this").invoke("toString").arg(lit(""))); JMethod toString2 = cls().method(JMod.PUBLIC, cm.ref("java.lang.String"), "toString"); toString2.param(String.class, "indent"); toString2.body()._if(_this().ref("value").eq(_null()))._then().block()._return(lit("null")); if ( type.getName().equals("array") ) { toString2.body()._return(cm.ref("java.util.Arrays").staticInvoke("toString").arg(_this().ref("value"))); } else { toString2.body()._return(_this().ref("value").invoke("toString")); } }
private void createAddMethod(JCodeModel model, TypeInfo declaringClass, String fieldName, JClass jClass) { String methodName = addMethodName(fieldName); JFieldRef field = JExpr.ref(fieldName); JClass listClass = model.ref(LinkedHashSet.class); listClass = listClass.narrow(jClass); JMethod method = declaringClass.implClass.method(JMod.PUBLIC, void.class, methodName); JVar param = method.param(jClass, "value"); JBlock body = method.body(); JMethod imethod = declaringClass.interfaceClass.method(JMod.NONE, void.class, methodName); imethod.param(jClass, "value"); body._if(field.eq(JExpr._null()))._then().assign(field, JExpr._new(listClass)); body.add(field.invoke("add").arg(param)); }
static private void createCopyMethod(JDefinedClass clazz){ JCodeModel codeModel = clazz.owner(); JClass reportClazz = codeModel.ref("org.jpmml.evaluator.Report"); JMethod method = clazz.method(JMod.PUBLIC, clazz, "copy"); method.annotate(Override.class); JBlock body = method.body(); JVar reportVariable = body.decl(reportClazz, "report", JExpr.invoke("getReport")); body._return(JExpr._new(clazz).arg(JExpr._super().ref("value")).arg(reportVariable.invoke("copy")).arg(JExpr._null())); }