private void addBuilder(JDefinedClass jclass, JType propertyType, JFieldVar field) { JMethod builder = jclass.method(JMod.PUBLIC, jclass, "withAdditionalProperty"); JVar nameParam = builder.param(String.class, "name"); JVar valueParam = builder.param(propertyType, "value"); JBlock body = builder.body(); JInvocation mapInvocation = body.invoke(JExpr._this().ref(field), "put"); mapInvocation.arg(nameParam); mapInvocation.arg(valueParam); body._return(JExpr._this()); }
private JMethod addSetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) { JMethod setter = c.method(JMod.PUBLIC, void.class, getSetterName(jsonPropertyName, node)); JVar param = setter.param(field.type(), field.name()); JBlock body = setter.body(); body.assign(JExpr._this().ref(field), param); return setter; }
private void addRestClient(JFieldVar textViewField) { // add annotated restClient field JFieldVar restClient = jClass.field(JMod.NONE, ref.ref(appDetails.getRestClientPackage()), "restClient"); restClient.annotate(ref.restService()); // add doSomethingElseOnUiThread method JMethod doSomethingElseOnUiThread = jClass.method(JMod.NONE, jCodeModel.VOID, "doSomethingElseOnUiThread"); doSomethingElseOnUiThread.annotate(ref.uithread()); JBlock body = doSomethingElseOnUiThread.body(); if (textViewField != null) { body.invoke(textViewField, "setText").arg("Hi!"); } else { body.directStatement("// do something on UIThread"); } // add doSomethingInBackground method JMethod doSomethingInBackground = jClass.method(JMod.NONE, jCodeModel.VOID, "doSomethingInBackground"); doSomethingInBackground.annotate(ref.background()); JBlock doSomethingInBackgroundBody = doSomethingInBackground.body(); doSomethingInBackgroundBody.invoke(restClient, "main"); doSomethingInBackgroundBody.invoke(doSomethingElseOnUiThread); }
private void addCreateFromParcel(JDefinedClass jclass, JDefinedClass creatorClass) { JMethod createFromParcel = creatorClass.method(JMod.PUBLIC, jclass, "createFromParcel"); JVar in = createFromParcel.param(jclass.owner().directClass("android.os.Parcel"), "in"); suppressWarnings(createFromParcel, "unchecked"); createFromParcel.body()._return(JExpr._new(jclass).arg(in)); }
final JDefinedClass exampleClass = codeModel._class( "com.example.ExampleClass" ); final JMethod method = exampleClass.method( JMod.PUBLIC, Object.class, "getValue" ); final JTypeVar t = method.generify( "T" ); method.type( t ); method.param( codeModel.ref( Class.class ).narrow( t ), "type" ); method.body()._return(JExpr._null());
public JCodeModel generate(JCodeModel jCodeModel, RefHelper ref) throws IOException { logger = LoggerFactory.getLogger(getClass()); try { jClass = jCodeModel._class(appDetails.getApplicationPackage()); jClass._extends(ref.application()); jClass.annotate(ref.reportsCrashes()).param("formKey", "YOUR_FORM_KEY"); JMethod onCreateMethod = jClass.method(JMod.PUBLIC, jCodeModel.VOID, "onCreate"); onCreateMethod.annotate(ref.override()); JBlock onCreateMethodBody = onCreateMethod.body(); onCreateMethodBody.staticInvoke(ref.acra(), "init").arg(JExpr._this()); onCreateMethodBody.invoke(JExpr._super(), "onCreate"); } catch (JClassAlreadyExistsException e1) { logger.error("Classname already exists", e1); } return jCodeModel; }
private void createConfigureActionBar() { if (appDetails.isActionBarSherlock() && (appDetails.isListNavigation() || appDetails.isTabNavigation())) { JMethod configureActionBar = jClass.method(JMod.PRIVATE, jCodeModel.VOID, "configureActionBar"); JBlock configureActionBarBody = configureActionBar.body(); // LIST NAVIGATION if (appDetails.isListNavigation()) { addListNavigationConfiguration(configureActionBarBody); } // TAB NAVIGATION if (appDetails.isTabNavigation()) { addTabNavigationConfiguration(configureActionBarBody); } afterViewsBody.invoke(configureActionBar); } }
injectMethod = injector.method(JMod.PUBLIC, void.class, "inject"); injectMethod.param(Dom.class, "dom"); injectMethod.param(targetType, "target"); injectMethod.body(); injectAttributeMethod = injector.method(JMod.PUBLIC,void.class,"injectAttribute"); addReinjectionParam(injectAttributeMethod); injectElementMethod = injector.method(JMod.PUBLIC,void.class,"injectElement"); addReinjectionParam(injectElementMethod);
private void addSetter(JDefinedClass jclass, JType propertyType, JFieldVar field) { JMethod setter = jclass.method(JMod.PUBLIC, void.class, "setAdditionalProperty"); ruleFactory.getAnnotator().anySetter(setter, jclass); JVar nameParam = setter.param(String.class, "name"); JVar valueParam = setter.param(propertyType, "value"); JInvocation mapInvocation = setter.body().invoke(JExpr._this().ref(field), "put"); mapInvocation.arg(nameParam); mapInvocation.arg(valueParam); }
private void addToString(JDefinedClass _enum, JFieldVar valueField) { JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString"); JBlock body = toString.body(); JExpression toReturn = JExpr._this().ref(valueField); if(!isString(valueField.type())){ toReturn = toReturn.plus(JExpr.lit("")); } body._return(toReturn); toString.annotate(Override.class); }
private JMethod addBuilder(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node) { JMethod builder = c.method(JMod.PUBLIC, c, getBuilderName(jsonPropertyName, node)); JVar param = builder.param(field.type(), field.name()); JBlock body = builder.body(); body.assign(JExpr._this().ref(field), param); body._return(JExpr._this()); return builder; }
public void addWriteToParcel(JDefinedClass jclass) { JMethod method = jclass.method(JMod.PUBLIC, void.class, "writeToParcel"); JVar dest = method.param(jclass.owner().directClass("android.os.Parcel"), "dest"); method.param(int.class, "flags"); // Call super.writeToParcel if (extendsParcelable(jclass)) { method.body().directStatement("super.writeToParcel(dest, flags);"); } for (JFieldVar f : jclass.fields().values()) { if( (f.mods().getValue() & JMod.STATIC) == JMod.STATIC ) { continue; } if (f.type().erasure().name().equals("List")) { method.body().invoke(dest, "writeList").arg(f); } else { method.body().invoke(dest, "writeValue").arg(f); } } }
private JMethod addGetter(JDefinedClass c, JFieldVar field, String jsonPropertyName, JsonNode node, boolean isRequired, boolean usesOptional) { JType type = getReturnType(c, field, isRequired, usesOptional); JMethod getter = c.method(JMod.PUBLIC, type, getGetterName(jsonPropertyName, field.type(), node)); JBlock body = getter.body(); if ((ruleFactory.getGenerationConfig().isUseOptionalForGetters() || usesOptional) && !isRequired && field.type().isReference()) { body._return(c.owner().ref("java.util.Optional").staticInvoke("ofNullable").arg(field)); } else { body._return(field); } return getter; }
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 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()); } } }