Search in sources :

Example 1 with BundleHelper

use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.

the class ServiceActionHandler method addActionInOnHandleIntent.

private void addActionInOnHandleIntent(EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
    JMethod onHandleIntentMethod = holder.getOnHandleIntentMethod();
    // 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 intent = holder.getOnHandleIntentIntent();
        JVar extras = callActionBlock.decl(getClasses().BUNDLE, "extras");
        extras.init(intent.invoke("getExtras"));
        callActionBlock = 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);
            AbstractJClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType());
            BundleHelper bundleHelper = new BundleHelper(getEnvironment(), param.asType());
            IJExpression getExtraExpression = bundleHelper.getExpressionToRestoreFromBundle(extraParamClass, extras, paramVar, onHandleIntentMethod);
            JVar extraField = callActionBlock.decl(extraParamClass, extraParamName, getExtraExpression);
            callActionInvocation.arg(extraField);
        }
    }
    callActionBlock.add(callActionInvocation);
    callActionBlock._return();
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar) BundleHelper(org.androidannotations.helper.BundleHelper)

Example 2 with BundleHelper

use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.

the class FragmentArgHandler method createBuilderInjectMethod.

public void createBuilderInjectMethod(EFragmentHolder holder, Element element, List<ArgHelper> argHelpers) {
    JDefinedClass builderClass = holder.getBuilderClass();
    JFieldRef builderArgsField = holder.getBuilderArgsField();
    JMethod builderMethod = builderClass.method(PUBLIC, holder.narrow(builderClass), element.getSimpleName().toString());
    String docComment = getProcessingEnvironment().getElementUtils().getDocComment(element);
    codeModelHelper.addTrimmedDocComment(builderMethod, docComment);
    for (ArgHelper argHelper : argHelpers) {
        String fieldName = argHelper.param.getSimpleName().toString();
        TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, argHelper.param);
        BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
        JFieldVar argKeyStaticField = getOrCreateStaticArgField(holder, argHelper.argKey, fieldName);
        AbstractJClass paramClass = codeModelHelper.typeMirrorToJClass(actualType);
        JVar arg = builderMethod.param(paramClass, fieldName);
        builderMethod.body().add(bundleHelper.getExpressionToSaveFromField(builderArgsField, argKeyStaticField, arg));
        builderMethod.javadoc().addParam(fieldName).append("value for this Fragment argument");
    }
    builderMethod.javadoc().addReturn().append("the FragmentBuilder to chain calls");
    builderMethod.body()._return(_this());
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JDefinedClass(com.helger.jcodemodel.JDefinedClass) TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 3 with BundleHelper

use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.

the class FragmentArgHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EFragmentHolder holder, Element element, Element param) {
    String fieldName = element.getSimpleName().toString();
    String argKey = extractArgKey(element, fieldName);
    if (element.getKind() != ElementKind.PARAMETER) {
        createBuilderInjectionMethod(holder, element, new ArgHelper(param, argKey));
    }
    TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
    AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(actualType);
    BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
    JVar bundle = holder.getInjectBundleArgs();
    JMethod injectExtrasMethod = holder.getInjectArgsMethod();
    JFieldVar extraKeyStaticField = getOrCreateStaticArgField(holder, argKey, fieldName);
    IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, bundle, extraKeyStaticField, injectExtrasMethod);
    JConditional conditional = targetBlock._if(JExpr.invoke(bundle, "containsKey").arg(extraKeyStaticField));
    conditional._then().add(fieldRef.assign(restoreMethodCall));
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 4 with BundleHelper

use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.

the class ExtraHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EActivityHolder holder, Element element, Element param) {
    String fieldName = element.getSimpleName().toString();
    String extraKey = extractExtraKey(element, fieldName);
    TypeMirror actualType = codeModelHelper.getActualTypeOfEnclosingElementOfInjectedElement(holder, param);
    BundleHelper bundleHelper = new BundleHelper(getEnvironment(), actualType);
    JFieldVar extraKeyStaticField = getOrCreateStaticExtraField(holder, extraKey, fieldName);
    if (element.getKind() != ElementKind.PARAMETER) {
        holder.getIntentBuilder().getPutExtraMethod(element, new IntentBuilder.IntentExtra(param.asType(), fieldName, extraKeyStaticField));
    }
    AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(param.asType());
    JMethod injectExtrasMethod = holder.getInjectExtrasMethod();
    JVar extras = holder.getInjectExtras();
    IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, extras, extraKeyStaticField, injectExtrasMethod);
    JBlock ifContainsKey = targetBlock._if(JExpr.invoke(extras, "containsKey").arg(extraKeyStaticField))._then();
    ifContainsKey.assign(fieldRef, restoreMethodCall);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) IntentBuilder(org.androidannotations.internal.core.helper.IntentBuilder) IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) BundleHelper(org.androidannotations.helper.BundleHelper) JVar(com.helger.jcodemodel.JVar)

Example 5 with BundleHelper

use of org.androidannotations.helper.BundleHelper in project androidannotations by androidannotations.

the class InstanceStateHandler method process.

@Override
public void process(Element element, HasInstanceState holder) {
    AbstractJClass elementClass = codeModelHelper.typeMirrorToJClass(element.asType());
    String fieldName = element.getSimpleName().toString();
    JBlock saveStateBody = holder.getSaveStateMethodBody();
    JVar saveStateBundleParam = holder.getSaveStateBundleParam();
    JMethod restoreStateMethod = holder.getRestoreStateMethod();
    JBlock restoreStateBody = holder.getRestoreStateMethodBody();
    JVar restoreStateBundleParam = holder.getRestoreStateBundleParam();
    TypeMirror type = codeModelHelper.getActualType(element, holder);
    BundleHelper bundleHelper = new BundleHelper(getEnvironment(), type);
    JFieldRef ref = ref(fieldName);
    saveStateBody.add(bundleHelper.getExpressionToSaveFromField(saveStateBundleParam, JExpr.lit(fieldName), ref));
    IJExpression restoreMethodCall = bundleHelper.getExpressionToRestoreFromBundle(elementClass, restoreStateBundleParam, JExpr.lit(fieldName), restoreStateMethod);
    restoreStateBody.assign(ref, restoreMethodCall);
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar) BundleHelper(org.androidannotations.helper.BundleHelper)

Aggregations

AbstractJClass (com.helger.jcodemodel.AbstractJClass)6 BundleHelper (org.androidannotations.helper.BundleHelper)6 IJExpression (com.helger.jcodemodel.IJExpression)5 JMethod (com.helger.jcodemodel.JMethod)5 JVar (com.helger.jcodemodel.JVar)5 JFieldVar (com.helger.jcodemodel.JFieldVar)4 TypeMirror (javax.lang.model.type.TypeMirror)4 JBlock (com.helger.jcodemodel.JBlock)3 JFieldRef (com.helger.jcodemodel.JFieldRef)2 JConditional (com.helger.jcodemodel.JConditional)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JInvocation (com.helger.jcodemodel.JInvocation)1 VariableElement (javax.lang.model.element.VariableElement)1 IntentBuilder (org.androidannotations.internal.core.helper.IntentBuilder)1