JVar otherObject = equals.param(Object.class, "other"); JBlock body = equals.body(); JVar rhsVar = body.decl(jclass, "rhs").init(JExpr.cast(jclass, otherObject)); JExpression result = JExpr.lit(true); result = result.cand(JExpr._super().invoke("equals").arg(rhsVar)); 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())) { JClass floatClass = jclass.owner().ref(Float.class); fieldEquals = floatClass.staticInvoke("floatToIntBits").arg(thisFieldRef).eq( floatClass.staticInvoke("floatToIntBits").arg(otherFieldRef)); } else { fieldEquals = thisFieldRef.eq(otherFieldRef); 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);
private void createOnCreateOptionsMenu() { JMethod onCreateOptionsMenu = null; JClass menu = !appDetails.isActionBarSherlock() ? ref.menu() : ref.sMenu(); onCreateOptionsMenu = jClass.method(JMod.PUBLIC, jCodeModel.BOOLEAN, "onCreateOptionsMenu"); JVar menuVar = onCreateOptionsMenu.param(menu, "menu"); onCreateOptionsMenu.annotate(ref.override()); JBlock onCreateOptionsMenuBody = onCreateOptionsMenu.body(); String getMenuInflater = appDetails.isActionBarSherlock() ? "getSupportMenuInflater" : "getMenuInflater"; JFieldRef rMenuMain = ref.r().staticRef("menu").ref("activity_main"); onCreateOptionsMenuBody.invoke(getMenuInflater).// invoke("inflate"). // arg(rMenuMain). // arg(menuVar); onCreateOptionsMenuBody._return(TRUE); }
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)); 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)); body.assign(result, result.mul(JExpr.lit(31)).plus(JExpr._super().invoke("hashCode"))); body._return(result); hashCode.annotate(Override.class);
Set<String> excludes = new HashSet<>(Arrays.asList(ruleFactory.getGenerationConfig().getToStringExcludes())); JBlock body = toString.body(); JClass stringBuilderClass = jclass.owner().ref(StringBuilder.class); JVar sb = body.decl(stringBuilderClass, "sb", JExpr._new(stringBuilderClass)); 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(); contentStart.gte(JExpr.lit(0)).cand(contentEnd.gt(contentStart))); superToStringInnerConditional._then().add( superToStringInnerConditional._else().add(sb.invoke("append").arg(superString)); JExpr.refthis(fieldVar.name()).eq(JExpr._null()), JExpr.refthis(fieldVar.name()).eq(JExpr._null()), toString.annotate(Override.class);
if ( m.name().equals( methodName ) && m.hasSignature( signature ) ) final JVar source = m.param( JMod.FINAL, field.getRawType(), "source" ); final JVar target = field.getRawType().isArray() ? null : m.param( JMod.FINAL, field.getRawType(), "target" ); m.body().directStatement( "// " + getMessage( "title" ) ); m.javadoc().append( "Copies all values of property {@code " + field.getPropertyInfo().getName( true ) m.annotate( SuppressWarnings.class ).param( "value", "unchecked" ); final JBlock body = new JBlock( false, false ); sourceNotEmpty = body._if( source.ne( JExpr._null() ).cand( source.ref( "length" ).gt( JExpr.lit( 0 ) ) ) ); copy = sourceNotEmpty._then().decl( JMod.FINAL, source.type(), "copy", JExpr.cast( source.type(), array.staticInvoke( "newInstance" ). arg( source.invoke( "getClass" ).invoke( "getComponentType" ) ).arg( source.ref( "length" ) ) ) ); copyLoop = sourceNotEmpty._then()._for(); it = copyLoop.init( field.parent().parent().getCodeModel().INT, "i", source.ref( "length" ).minus( JExpr.lit( 1 ) ) ); copyLoop.test( it.gte( JExpr.lit( 0 ) ) ); sourceNotEmpty = body._if( source.ne( JExpr._null() ).cand( JExpr.invoke( source, "isEmpty" ).not() ) ); copyLoop = sourceNotEmpty._then()._for(); it = copyLoop.init( JMod.FINAL, field.parent().parent().getCodeModel().ref( Iterator.class ). narrow( field.parent().parent().getCodeModel().wildcard() ), "it", source.invoke( "iterator" ) );
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 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; }
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 { 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 generateToString() { JMethod toString1 = cls().method(JMod.PUBLIC, cm.ref("java.lang.String"), "toString"); toString1.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().decl(cm.ref("java.lang.String"), "rc", lit("")); for (int i = 0; i < amqpFields.size(); i++) { Attribute attr = amqpFields.get(i); if ( attr.attribute.type().isArray() ) { JBlock block = toString2.body()._if(_this().ref(attr.attribute).ne(_null()))._then().block(); block._if(ref("rc").invoke("length").ne(lit(0)))._then().assignPlus(ref("rc"), lit(",\n")); block.assignPlus(ref("rc"), ref("indent").plus(lit(" " + attr.attribute.name() + ":").plus(cm.ref("java.util.Arrays").staticInvoke("toString").arg(_this().ref(attr.attribute))))); } else { JExpression value = _this().ref(attr.attribute); if ( generator.getMapping().get(attr.type) == null ) { value = value.invoke("toString").arg(ref("indent").plus(lit(" "))); } JBlock block = toString2.body()._if(_this().ref(attr.attribute).ne(_null()))._then().block(); block._if(ref("rc").invoke("length").ne(lit(0)))._then().assignPlus(ref("rc"), lit(",\n")); block.assignPlus(ref("rc"), ref("indent").plus(lit(" " + attr.attribute.name() + ":").plus(value))); } } toString2.body()._if(ref("rc").invoke("length").ne(lit(0)))._then(). assign(ref("rc"), lit(", {\n").plus(ref("rc").plus(lit("}")))); toString2.body()._return(lit("[" + toJavaClassName(type.getName())).plus(ref("rc")).plus(lit("]"))); }
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"))); }
protected void fix(final JBlock body) { // final JFieldRef wrappedPropertyField = field; // // final JFieldRef wrappingPropertyField = itemsField; body._if(wrappingPropertyField.eq(JExpr._null()))._then().assign( wrappingPropertyField, JExpr._new(codeModel.ref(ArrayList.class).narrow( wrappingPropertyExposedType.boxify()))); final JClass utilsClass = (wrappedProperty instanceof CReferencePropertyInfo && ((CReferencePropertyInfo) wrappedProperty) .isMixed()) ? codeModel.ref(MixedItemUtils.class) : codeModel .ref(ItemUtils.class); body._if( utilsClass.staticInvoke("shouldBeWrapped").arg( wrappedPropertyField))._then().assign( wrappedPropertyField, utilsClass.staticInvoke("wrap").arg(wrappedPropertyField).arg( wrappingPropertyField).arg( wrappingPropertyExposedType.boxify().dotclass())); }
JConditional booleanJC = jc._else()._if(castedOI.invoke("get").arg(returnValue)); m.directClass(ByteObjectInspector.class.getCanonicalName()), "castOI", JExpr._null()); castedOI.invoke("get").arg(returnValue)); return block; castedOI.invoke("get").arg(returnValue)); return block; .invoke("setBytes").arg(JExpr.lit(0)).arg(data)); jnullif._else().add(returnValueHolder.ref("buffer").invoke("setIndex").arg(JExpr.lit(0)).arg(data.ref("length"))); .invoke("setBytes").arg(JExpr.lit(0)).arg(data)); jnullif._else().add(returnValueHolder.ref("buffer").invoke("setIndex").arg(JExpr.lit(0)).arg(data.ref("length"))); .invoke("setBytes").arg(JExpr.lit(0)).arg(data)); jnullif._else().add(returnValueHolder.ref("buffer").invoke("setIndex").arg(JExpr.lit(0)).arg(data.ref("length"))); .invoke("setBytes").arg(JExpr.lit(0)).arg(data)); jnullif._else().add(returnValueHolder.ref("buffer").invoke("setIndex").arg(JExpr.lit(0)).arg(data.ref("length")));
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")));
private void fillInSizeMethod() { size().body()._return(invoke("sizeOfConstructor").plus(invoke("sizeOfBody"))); sizeOfConstructor().body()._return(ref("CONSTRUCTOR").invoke("size")); JMethod sizeOfFields = cls().method(JMod.PRIVATE, cm.LONG, "sizeOfFields"); sizeOfFields.body().decl(cm.LONG, "fieldSize", lit(0L)); if ( isComposite() ) { sizeOfFields.body().decl(cm.INT, "count", _this().invoke(count)); } for ( Attribute attribute : amqpFields ) { if ( isComposite() ) { sizeOfFields.body().assign(ref("count"), ref("count").minus(lit(1))); JBlock ifBody = sizeOfFields.body()._if(ref("count").gte(lit(0)))._then(); addFieldSize(attribute, ifBody); } else { addFieldSize(attribute, sizeOfFields.body()); } } sizeOfFields.body()._return(ref("fieldSize")); if ( isComposite() ) { sizeOfBody().body()._return(cm.ref(generator.getMarshaller() + ".DescribedTypeSupport").staticInvoke("fullSizeOfList").arg(_this().invoke("sizeOfFields")).arg(_this().invoke("count"))); } else { sizeOfBody().body()._return(_this().invoke("sizeOfFields")); } }
private void replaceGetter(final ClassOutline co, final JFieldVar field, final JType setType) { // Create the method name final String methodName = OPERATION_GET_PREFIX + capitalizeFirstLetter(field.name()); // Find and remove Old Getter! final JMethod oldGetter = co.implClass.getMethod(methodName, new JType[0]); co.implClass.methods().remove(oldGetter); final JMethod getter = co.implClass.method(JMod.PUBLIC, setType, methodName); if (checkNull()) { final JType classType = co.parent().getCodeModel().ref(getImplementationClass()); getter.body()._if(JExpr.ref(field.name()).eq(JExpr._null()))._then().assign(field, JExpr._new(classType)); } getter.body()._return(JExpr.ref(field.name())); }
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)); }
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 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; }
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); JFieldRef rArrayLocations = ref.r().staticRef("array").ref("locations"); 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());