Пример #1
0
 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());
 }
Пример #2
0
  @Test
  public void test() throws Exception {
    final JCodeModel cm = new JCodeModel();

    final JPackage aPkg1 = cm._package("id.myapp");
    final JDefinedClass aClass_R = aPkg1._class("R");
    final JDefinedClass aClass_id = aClass_R._class(JMod.PUBLIC | JMod.STATIC, "id");
    final JFieldVar aFieldItem =
        aClass_id.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, cm.INT, "myItem", JExpr.lit(1));
    final JDefinedClass aClass_menu = aClass_R._class(JMod.PUBLIC | JMod.STATIC, "menu");
    final JFieldVar aFieldMenu =
        aClass_menu.field(JMod.PUBLIC | JMod.STATIC | JMod.FINAL, cm.INT, "myMenu", JExpr.lit(2));

    final JPackage aPkg2 = cm._package("demo");
    final JDefinedClass aClassAct = aPkg2._class("HelloAndroidActivity_");
    final JMethod aMethodCreate = aClassAct.method(JMod.PUBLIC, cm.BOOLEAN, "onCreateOptionsMenu");
    aMethodCreate
        .body()
        .add(JExpr.ref("menuInflater").invoke("inflate").arg(aFieldMenu.fieldRef()));
    final JMethod aMethodSelected =
        aClassAct.method(JMod.PUBLIC, cm.BOOLEAN, "onOptionsItemSelected");
    aMethodSelected.body()._if(JExpr.ref("itemId_").eq(aFieldItem.fieldRef()));

    // Multiple packages - print only
    CodeModelTestsHelper.printCodeModel(cm);
  }
Пример #3
0
 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);
 }
Пример #4
0
  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));
    }
  }
Пример #5
0
 public void invokeInitInConstructor() {
   JBlock constructorBody = constructor.body();
   constructorBody.invoke(getInit());
 }