Search in sources :

Example 31 with IJExpression

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

the class RoboGuiceHandler method fireEvent.

private void fireEvent(JFieldVar eventManager, JBlock body, AbstractJClass eventClass, IJExpression... eventArguments) {
    AbstractJClass actualEventClass = eventClass;
    if (eventClass.fullName().startsWith("roboguice.context.event")) {
        actualEventClass = eventClass.narrow(getClasses().ACTIVITY);
    }
    JInvocation newEvent = _new(actualEventClass);
    newEvent.arg(_this());
    for (IJExpression eventArgument : eventArguments) {
        newEvent.arg(eventArgument);
    }
    body.invoke(eventManager, "fire").arg(newEvent);
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation)

Example 32 with IJExpression

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

the class ReceiverHandler method createReceiverField.

private JFieldVar createReceiverField(HasReceiverRegistration holder, String receiverName, String methodName, ExecutableElement executableElement) {
    JDefinedClass anonymousReceiverClass = getCodeModel().anonymousClass(getClasses().BROADCAST_RECEIVER);
    JMethod onReceiveMethod = anonymousReceiverClass.method(PUBLIC, getCodeModel().VOID, "onReceive");
    JVar contextVar = onReceiveMethod.param(getClasses().CONTEXT, "context");
    JVar intentVar = onReceiveMethod.param(getClasses().INTENT, "intent");
    JBlock body = onReceiveMethod.body();
    IJExpression receiverRef = holder.getGeneratedClass().staticRef("this");
    JInvocation methodCall = receiverRef.invoke(methodName);
    JVar extras = null;
    List<? extends VariableElement> methodParameters = executableElement.getParameters();
    for (VariableElement param : methodParameters) {
        AbstractJClass extraParamClass = codeModelHelper.typeMirrorToJClass(param.asType());
        if (extraParamClass.equals(getClasses().CONTEXT)) {
            methodCall.arg(contextVar);
        } else if (extraParamClass.equals(getClasses().INTENT) && param.getAnnotation(Receiver.Extra.class) == null) {
            methodCall.arg(intentVar);
        } else if (param.getAnnotation(Receiver.Extra.class) != null) {
            if (extras == null) {
                extras = body.decl(getClasses().BUNDLE, "extras_", JOp.cond(intentVar.invoke("getExtras").ne(_null()), intentVar.invoke("getExtras"), _new(getClasses().BUNDLE)));
            }
            methodCall.arg(extraHandler.getExtraValue(param, extras, body, onReceiveMethod, anonymousReceiverClass));
        }
    }
    body.add(methodCall);
    IJExpression receiverInit = _new(anonymousReceiverClass);
    return holder.getGeneratedClass().field(PRIVATE | FINAL, getClasses().BROADCAST_RECEIVER, receiverName, receiverInit);
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) 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)

Example 33 with IJExpression

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

the class RootFragmentHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EBeanHolder holder, Element element, Element param) {
    TypeMirror elementType = param.asType();
    String typeQualifiedName = elementType.toString();
    IJExpression rootFragmentRef = holder.getRootFragmentRef();
    AbstractJClass extendingContextClass = getEnvironment().getJClass(typeQualifiedName);
    JConditional cond = targetBlock._if(rootFragmentRef._instanceof(extendingContextClass));
    cond._then().add(fieldRef.assign(cast(extendingContextClass, rootFragmentRef)));
    JInvocation warningInvoke = getClasses().LOG.staticInvoke("w");
    warningInvoke.arg(logTagForClassHolder(holder));
    warningInvoke.arg(lit("Due to class ").plus(rootFragmentRef.invoke("getClass").invoke("getSimpleName")).plus(lit(", the @RootFragment " + extendingContextClass.name() + " won't be populated")));
    JInvocation warningInvokeIfNull = getClasses().LOG.staticInvoke("w");
    warningInvokeIfNull.arg(logTagForClassHolder(holder));
    warningInvokeIfNull.arg(lit("Due to not having a rootFragment reference the @RootFragment " + extendingContextClass.name() + " won't be populated"));
    JConditional ifNotNull = cond._elseif(rootFragmentRef.ne(_null()));
    ifNotNull._then().add(warningInvoke);
    ifNotNull._else().add(warningInvokeIfNull);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JConditional(com.helger.jcodemodel.JConditional) JInvocation(com.helger.jcodemodel.JInvocation)

Example 34 with IJExpression

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

the class TextChangeHandler 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 startParameterPosition = -1;
    int countParameterPosition = -1;
    int beforeParameterPosition = -1;
    int charSequenceParameterPosition = -1;
    int viewParameterPosition = -1;
    TypeMirror viewParameterType = null;
    for (int i = 0; i < parameters.size(); i++) {
        VariableElement parameter = parameters.get(i);
        String parameterName = parameter.toString();
        TypeMirror parameterType = parameter.asType();
        if (CanonicalNameConstants.CHAR_SEQUENCE.equals(parameterType.toString())) {
            charSequenceParameterPosition = i;
        } else if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
            if ("start".equals(parameterName)) {
                startParameterPosition = i;
            } else if ("count".equals(parameterName)) {
                countParameterPosition = i;
            } else if ("before".equals(parameterName)) {
                beforeParameterPosition = i;
            }
        } else {
            TypeMirror textViewType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.TEXT_VIEW).asType();
            if (annotationHelper.isSubtype(parameterType, textViewType)) {
                viewParameterPosition = i;
                viewParameterType = parameterType;
            }
        }
    }
    List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
    for (JFieldRef idRef : idsRefs) {
        TextWatcherHolder textWatcherHolder = holder.getTextWatcherHolder(idRef, viewParameterType);
        JBlock methodBody = textWatcherHolder.getOnTextChangedBody();
        IJExpression activityRef = holder.getGeneratedClass().staticRef("this");
        JInvocation textChangeCall = methodBody.invoke(activityRef, methodName);
        for (int i = 0; i < parameters.size(); i++) {
            if (i == startParameterPosition) {
                JVar startParameter = textWatcherHolder.getOnTextChangedStartParam();
                textChangeCall.arg(startParameter);
            } else if (i == countParameterPosition) {
                JVar countParameter = textWatcherHolder.getOnTextChangedCountParam();
                textChangeCall.arg(countParameter);
            } else if (i == beforeParameterPosition) {
                JVar beforeParameter = textWatcherHolder.getOnTextChangedBeforeParam();
                textChangeCall.arg(beforeParameter);
            } else if (i == charSequenceParameterPosition) {
                JVar charSequenceParam = textWatcherHolder.getOnTextChangedCharSequenceParam();
                textChangeCall.arg(charSequenceParam);
            } else if (i == viewParameterPosition) {
                JVar viewParameter = textWatcherHolder.getTextViewVariable();
                textChangeCall.arg(viewParameter);
            }
        }
    }
}
Also used : 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) TextWatcherHolder(org.androidannotations.holder.TextWatcherHolder) TypeMirror(javax.lang.model.type.TypeMirror) JBlock(com.helger.jcodemodel.JBlock) JVar(com.helger.jcodemodel.JVar)

Example 35 with IJExpression

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

the class TraceHandler method getEnterMessage.

private IJExpression getEnterMessage(JMethod method, ExecutableElement element) {
    String methodName = getMethodName(element);
    List<JVar> params = method.params();
    if (params.isEmpty()) {
        // early exit if the method has no parameters
        return JExpr.lit("Entering [" + methodName + "()]");
    }
    AbstractJClass arraysClass = getJClass(Arrays.class);
    StringBuilder paramStr = new StringBuilder();
    List<IJExpression> paramExpressions = new ArrayList<>();
    for (int i = 0; i < params.size(); i++) {
        if (i > 0) {
            paramStr.append(", ");
        }
        JVar var = params.get(i);
        paramStr.append(var.name()).append(" = %s");
        if (var.type().isArray()) {
            paramExpressions.add(arraysClass.staticInvoke("toString").arg(var));
        } else {
            paramExpressions.add(var);
        }
    }
    JInvocation format = getJClass(String.class).staticInvoke("format");
    format.arg(JExpr.lit("Entering [" + methodName + "(" + paramStr + ")]"));
    for (IJExpression expr : paramExpressions) {
        format.arg(expr);
    }
    return format;
}
Also used : IJExpression(com.helger.jcodemodel.IJExpression) ArrayList(java.util.ArrayList) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) JVar(com.helger.jcodemodel.JVar)

Aggregations

IJExpression (com.helger.jcodemodel.IJExpression)56 JVar (com.helger.jcodemodel.JVar)33 JBlock (com.helger.jcodemodel.JBlock)31 JInvocation (com.helger.jcodemodel.JInvocation)28 AbstractJClass (com.helger.jcodemodel.AbstractJClass)24 TypeMirror (javax.lang.model.type.TypeMirror)21 JMethod (com.helger.jcodemodel.JMethod)16 ExecutableElement (javax.lang.model.element.ExecutableElement)16 JFieldRef (com.helger.jcodemodel.JFieldRef)15 VariableElement (javax.lang.model.element.VariableElement)12 JConditional (com.helger.jcodemodel.JConditional)7 JFieldVar (com.helger.jcodemodel.JFieldVar)7 JDefinedClass (com.helger.jcodemodel.JDefinedClass)5 BundleHelper (org.androidannotations.helper.BundleHelper)5 JTryBlock (com.helger.jcodemodel.JTryBlock)4 JCatchBlock (com.helger.jcodemodel.JCatchBlock)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3 IJAssignmentTarget (com.helger.jcodemodel.IJAssignmentTarget)2 OnSeekBarChangeListenerHolder (org.androidannotations.holder.OnSeekBarChangeListenerHolder)2