コード例 #1
0
  private void addActionInOnHandleIntent(
      EIntentServiceHolder holder,
      ExecutableElement executableElement,
      String methodName,
      JFieldVar actionKeyField) {
    // If action match, call the method
    JInvocation actionCondition =
        actionKeyField.invoke("equals").arg(holder.getOnHandleIntentIntentAction());
    JBlock callActionBlock = holder.getOnHandleIntentBody()._if(actionCondition)._then();
    JInvocation callActionInvocation = JExpr._super().invoke(methodName);

    // For each method params, we get back value from extras and put it
    // in super calls
    List<? extends VariableElement> methodParameters = executableElement.getParameters();
    if (methodParameters.size() > 0) {
      // Extras
      JVar extras = callActionBlock.decl(classes().BUNDLE, "extras");
      extras.init(holder.getOnHandleIntentIntent().invoke("getExtras"));
      JBlock extrasNotNullBlock = callActionBlock._if(extras.ne(_null()))._then();

      // Extras params
      for (VariableElement param : methodParameters) {
        String paramName = param.getSimpleName().toString();
        String extraParamName = paramName + "Extra";
        JFieldVar paramVar = getStaticExtraField(holder, paramName);
        JClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType(), holder);
        BundleHelper bundleHelper = new BundleHelper(annotationHelper, param);

        JExpression getExtraExpression =
            JExpr.invoke(extras, bundleHelper.getMethodNameToRestore()).arg(paramVar);
        if (bundleHelper.restoreCallNeedCastStatement()) {
          getExtraExpression = JExpr.cast(extraParamClass, getExtraExpression);

          if (bundleHelper.restoreCallNeedsSuppressWarning()) {
            JMethod onHandleIntentMethod = holder.getOnHandleIntentMethod();
            if (onHandleIntentMethod.annotations().size() == 0) {
              onHandleIntentMethod.annotate(SuppressWarnings.class).param("value", "unchecked");
            }
          }
        }

        JVar extraField =
            extrasNotNullBlock.decl(extraParamClass, extraParamName, getExtraExpression);
        callActionInvocation.arg(extraField);
      }
      extrasNotNullBlock.add(callActionInvocation);
    } else {
      callActionBlock.add(callActionInvocation);
    }
    callActionBlock._return();
  }
コード例 #2
0
  @Override
  public void process(Element element, HasInstanceState holder) {
    String fieldName = element.getSimpleName().toString();

    JBlock saveStateBody = holder.getSaveStateMethodBody();
    JVar saveStateBundleParam = holder.getSaveStateBundleParam();
    JMethod restoreStateMethod = holder.getRestoreStateMethod();
    JBlock restoreStateBody = restoreStateMethod.body();
    JVar restoreStateBundleParam = holder.getRestoreStateBundleParam();

    AnnotationHelper annotationHelper = new AnnotationHelper(processingEnv);
    BundleHelper bundleHelper = new BundleHelper(annotationHelper, element);
    APTCodeModelHelper codeModelHelper = new APTCodeModelHelper();

    JFieldRef ref = ref(fieldName);
    saveStateBody
        .invoke(saveStateBundleParam, bundleHelper.getMethodNameToSave())
        .arg(fieldName)
        .arg(ref);

    JInvocation restoreMethodCall =
        JExpr.invoke(restoreStateBundleParam, bundleHelper.getMethodNameToRestore()).arg(fieldName);
    if (bundleHelper.restoreCallNeedCastStatement()) {

      JClass jclass = codeModelHelper.typeMirrorToJClass(element.asType(), holder);
      JExpression castStatement = JExpr.cast(jclass, restoreMethodCall);
      restoreStateBody.assign(ref, castStatement);

      if (bundleHelper.restoreCallNeedsSuppressWarning()) {
        if (restoreStateMethod.annotations().size() == 0) {
          restoreStateMethod.annotate(SuppressWarnings.class).param("value", "unchecked");
        }
      }

    } else {
      restoreStateBody.assign(ref, restoreMethodCall);
    }
  }