Search in sources :

Example 46 with JVar

use of com.helger.jcodemodel.JVar in project androidannotations by androidannotations.

the class SeekBarProgressChangeHandler method process.

@Override
public void process(Element element, EComponentWithViewSupportHolder holder) throws Exception {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int seekBarViewParameterPosition = -1;
    int progressParameterPosition = -1;
    int fromUserParameterPosition = -1;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        TypeMirror parameterType = parameter.asType();
        if (CanonicalNameConstants.SEEKBAR.equals(parameterType.toString())) {
            seekBarViewParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            progressParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.BOOLEAN || CanonicalNameConstants.BOOLEAN.equals(parameterType.toString())) {
            fromUserParameterPosition = i;
        }
    }
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        OnSeekBarChangeListenerHolder onSeekBarChangeListenerHolder = holder.getOnSeekBarChangeListenerHolder(idRef);
        JBlock methodBody = onSeekBarChangeListenerHolder.getOnProgressChangedBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation textChangeCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == seekBarViewParameterPosition) {
                JVar seekBarViewParameter = onSeekBarChangeListenerHolder.getOnProgressChangedSeekBarParam();
                textChangeCall.arg(seekBarViewParameter);
            } else if (i == progressParameterPosition) {
                JVar progressParameter = onSeekBarChangeListenerHolder.getOnProgressChangedProgressParam();
                textChangeCall.arg(progressParameter);
            } else if (i == fromUserParameterPosition) {
                JVar fromUserParameter = onSeekBarChangeListenerHolder.getOnProgressChangedFromUserParam();
                textChangeCall.arg(fromUserParameter);
            }
        }
    }
}
Also used : OnSeekBarChangeListenerHolder(org.androidannotations.holder.OnSeekBarChangeListenerHolder) JFieldRef(com.helger.jcodemodel.JFieldRef) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 47 with JVar

use of com.helger.jcodemodel.JVar 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 48 with JVar

use of com.helger.jcodemodel.JVar in project androidannotations by androidannotations.

the class OnActivityResultHandler method process.

@Override
public void process(Element element, HasOnActivityResult holder) throws Exception {
    String methodName = element.getSimpleName().toString();
    ExecutableElement executableElement = (ExecutableElement) element;
    List<? extends VariableElement> parameters = executableElement.getParameters();
    int requestCode = executableElement.getAnnotation(OnActivityResult.class).value();
    JBlock onResultBlock = holder.getOnActivityResultCaseBlock(requestCode).blockSimple();
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation onResultInvocation = JExpr.invoke(activityRef, methodName);
    JVar intent = holder.getOnActivityResultDataParam();
    JVar extras = null;
    for (VariableElement parameter : parameters) {
        TypeMirror parameterType = parameter.asType();
        if (parameter.getAnnotation(OnActivityResult.Extra.class) != null) {
            if (extras == null) {
                extras = onResultBlock.decl(getClasses().BUNDLE, "extras_", JOp.cond(intent.ne(_null()).cand(intent.invoke("getExtras").ne(_null())), intent.invoke("getExtras"), _new(getClasses().BUNDLE)));
            }
            IJExpression extraParameter = extraHandler.getExtraValue(parameter, extras, onResultBlock, holder);
            onResultInvocation.arg(extraParameter);
        } else if (CanonicalNameConstants.INTENT.equals(parameterType.toString())) {
            onResultInvocation.arg(intent);
        } else if (// 
        parameterType.getKind().equals(TypeKind.INT) || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            onResultInvocation.arg(holder.getOnActivityResultResultCodeParam());
        }
    }
    onResultBlock.add(onResultInvocation);
}
Also used : HasOnActivityResult(org.androidannotations.holder.HasOnActivityResult) OnActivityResult(org.androidannotations.annotations.OnActivityResult) TypeMirror(javax.lang.model.type.TypeMirror) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 49 with JVar

use of com.helger.jcodemodel.JVar in project androidannotations by androidannotations.

the class ItemSelectHandler method processParameters.

@Override
protected void processParameters(EComponentWithViewSupportHolder holder, JMethod listenerMethod, JInvocation itemSelectedCall, List<? extends VariableElement> parameters) {
    AbstractJClass narrowAdapterViewClass = getClasses().ADAPTER_VIEW.narrow(getCodeModel().wildcard());
    JVar onItemClickParentParam = listenerMethod.param(narrowAdapterViewClass, "parent");
    listenerMethod.param(getClasses().VIEW, "view");
    JVar onItemClickPositionParam = listenerMethod.param(getCodeModel().INT, "position");
    listenerMethod.param(getCodeModel().LONG, "id");
    itemSelectedCall.arg(JExpr.TRUE);
    boolean hasItemParameter = parameters.size() == 2;
    boolean secondParameterIsInt = false;
    String secondParameterQualifiedName = null;
    if (hasItemParameter) {
        VariableElement secondParameter = parameters.get(1);
        TypeMirror secondParameterType = secondParameter.asType();
        secondParameterQualifiedName = secondParameterType.toString();
        secondParameterIsInt = secondParameterType.getKind() == TypeKind.INT;
    }
    if (hasItemParameter) {
        if (secondParameterIsInt) {
            itemSelectedCall.arg(onItemClickPositionParam);
        } else {
            itemSelectedCall.arg(JExpr.cast(getJClass(secondParameterQualifiedName), invoke(onItemClickParentParam, "getAdapter").invoke("getItem").arg(onItemClickPositionParam)));
        }
    }
    onNothingSelectedMethod.param(narrowAdapterViewClass, "parent");
    IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
    JInvocation nothingSelectedCall = invoke(activityRef, getMethodName());
    onNothingSelectedMethod.body().add(nothingSelectedCall);
    nothingSelectedCall.arg(JExpr.FALSE);
    if (hasItemParameter) {
        if (secondParameterIsInt) {
            nothingSelectedCall.arg(lit(-1));
        } else {
            nothingSelectedCall.arg(_null());
        }
    }
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) VariableElement(javax.lang.model.element.VariableElement) JVar(com.helger.jcodemodel.JVar)

Example 50 with JVar

use of com.helger.jcodemodel.JVar in project androidannotations by androidannotations.

the class ViewNotifierHelper method implementHasViewsInHolder.

private void implementHasViewsInHolder() {
    holder.getGeneratedClass()._implements(HasViews.class);
    JCodeModel codeModel = environment.getCodeModel();
    JDirectClass genericType = codeModel.directClass("T");
    JMethod findViewById = holder.getGeneratedClass().method(PUBLIC, genericType, "internalFindViewById");
    findViewById.generify("T", environment.getClasses().VIEW);
    findViewById.annotate(Override.class);
    JVar idParam = findViewById.param(codeModel.INT, "id");
    IJExpression findViewByIdExpression = holder.getFindViewByIdExpression(idParam);
    findViewById.body()._return(JExpr.cast(genericType, findViewByIdExpression));
}
Also used : JDirectClass(com.helger.jcodemodel.JDirectClass) IJExpression(com.helger.jcodemodel.IJExpression) JMethod(com.helger.jcodemodel.JMethod) JCodeModel(com.helger.jcodemodel.JCodeModel) JVar(com.helger.jcodemodel.JVar)

Aggregations

JVar (com.helger.jcodemodel.JVar)126 JBlock (com.helger.jcodemodel.JBlock)86 JMethod (com.helger.jcodemodel.JMethod)61 AbstractJClass (com.helger.jcodemodel.AbstractJClass)53 JInvocation (com.helger.jcodemodel.JInvocation)39 IJExpression (com.helger.jcodemodel.IJExpression)33 VariableElement (javax.lang.model.element.VariableElement)25 ExecutableElement (javax.lang.model.element.ExecutableElement)19 JFieldRef (com.helger.jcodemodel.JFieldRef)16 TypeMirror (javax.lang.model.type.TypeMirror)16 JDefinedClass (com.helger.jcodemodel.JDefinedClass)15 JFieldVar (com.helger.jcodemodel.JFieldVar)15 JConditional (com.helger.jcodemodel.JConditional)9 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JCatchBlock (com.helger.jcodemodel.JCatchBlock)5 AbstractJType (com.helger.jcodemodel.AbstractJType)4 ArrayList (java.util.ArrayList)4 BundleHelper (org.androidannotations.helper.BundleHelper)4 JTypeVar (com.helger.jcodemodel.JTypeVar)3 VariableDeclaration (com.github.sviperll.adt4j.model.config.VariableDeclaration)2