public void createRebindMethod() { JMethod rebindMethod = generatedClass.method(PUBLIC, getCodeModel().VOID, "rebind"); JVar contextParam = rebindMethod.param(getClasses().CONTEXT, "context"); JBlock body = rebindMethod.body(); body.assign(getContextField(), contextParam); body.invoke(getInit()); }
private void setConstructor() { constructor = generatedClass.constructor(PRIVATE); JVar constructorContextParam = constructor.param(getClasses().CONTEXT, "context"); JBlock constructorBody = constructor.body(); List<ExecutableElement> constructors = ElementFilter.constructorsIn(annotatedElement.getEnclosedElements()); ExecutableElement superConstructor = constructors.get(0); if (superConstructor.getParameters().size() == 1) { constructorBody.invoke("super").arg(constructorContextParam); } constructorBody.assign(getContextField(), constructorContextParam); }
@Test public void testRegression1() throws Exception { final JCodeModel cm = new JCodeModel(); final JPackage aPkg1 = cm._package("id.myapp.activity"); final JDefinedClass testClass = aPkg1._class("TestClass"); final JDirectClass androidR = cm.directClass("android.R"); final JDirectClass androidRId = androidR._class("id"); final JDirectClass myR = cm.directClass("id.myapp.R"); final JDirectClass myRId = myR._class("id"); final JBlock constructorBody = testClass.constructor(JMod.PUBLIC).body(); constructorBody.decl(cm.INT, "myInt", androidRId.staticRef("someId")); constructorBody.decl(cm.INT, "myInt2", myRId.staticRef("otherId")); CodeModelTestsHelper.printCodeModel(cm); CodeModelTestsHelper.parseCodeModel(cm); }
@Override public void process(Element element, EComponentWithViewSupportHolder holder) throws Exception { ActionScanner.processActions(element, holder); uniquePriorityCounter++; final ViewsHolder viewsHolder = holder.getPluginHolder(new ViewsHolder(holder, annotationHelper)); final String methodName = element.getSimpleName().toString(); JBlock block = new JBlock(); JInvocation invoke = block.invoke(methodName); ExecutableElement exeElem = (ExecutableElement) element; for (VariableElement param : exeElem.getParameters()) { final String paramName = param.getSimpleName().toString(); ParamUtils.injectParam(paramName, invoke, viewsHolder); } SharedRecords.priorityAdd(holder.getOnViewChangedBody(), block, uniquePriorityCounter); }
@Override public void assignValue( JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) { TypeMirror uiFieldTypeMirror = param.asType(); JFieldRef idRef = annotationHelper.extractOneAnnotationFieldRef(element, IRClass.Res.ID, true); AbstractJClass viewClass = codeModelHelper.typeMirrorToJClass(uiFieldTypeMirror); IJAssignmentTarget viewHolderTarget = null; if (element.getKind() == ElementKind.FIELD) { viewHolderTarget = fieldRef; } FoundViewHolder viewHolder = holder.getFoundViewHolder(idRef, viewClass, viewHolderTarget); if (!viewHolder.getRef().equals(viewHolderTarget)) { targetBlock.add(fieldRef.assign(viewHolder.getOrCastRef(viewClass))); } }
public void createFactoryMethod(boolean hasSingletonScope) { AbstractJClass narrowedGeneratedClass = codeModelHelper.narrowGeneratedClass(generatedClass, annotatedElement.asType()); JMethod factoryMethod = generatedClass.method(PUBLIC | STATIC, narrowedGeneratedClass, GET_INSTANCE_METHOD_NAME); codeModelHelper.generify(factoryMethod, annotatedElement); JVar factoryMethodContextParam = factoryMethod.param(getClasses().CONTEXT, "context"); JBlock factoryMethodBody = factoryMethod.body(); /* * Singletons are bound to the application context */ if (hasSingletonScope) { JFieldVar instanceField = generatedClass.field(PRIVATE | STATIC, generatedClass, "instance" + generationSuffix()); JBlock creationBlock = factoryMethodBody // ._if(instanceField.eq(_null())) // ._then(); JVar previousNotifier = viewNotifierHelper.replacePreviousNotifierWithNull(creationBlock); creationBlock.assign( instanceField, _new(narrowedGeneratedClass) .arg(factoryMethodContextParam.invoke("getApplicationContext"))); creationBlock.invoke(instanceField, getInit()); viewNotifierHelper.resetPreviousNotifier(creationBlock, previousNotifier); factoryMethodBody._return(instanceField); } else { factoryMethodBody._return(_new(narrowedGeneratedClass).arg(factoryMethodContextParam)); } }
public void invokeInitInConstructor() { JBlock constructorBody = constructor.body(); constructorBody.invoke(getInit()); }