Search in sources :

Example 76 with JVar

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

the class APTCodeModelHelper method findAlreadyGeneratedMethod.

private JMethod findAlreadyGeneratedMethod(ExecutableElement executableElement, GeneratedClassHolder holder) {
    JDefinedClass definedClass = holder.getGeneratedClass();
    String methodName = executableElement.getSimpleName().toString();
    List<? extends VariableElement> parameters = executableElement.getParameters();
    // TODO: refactor the nasty label jump
    method: for (JMethod method : definedClass.methods()) {
        if (method.name().equals(methodName) && method.params().size() == parameters.size()) {
            int i = 0;
            for (JVar param : method.params()) {
                String searchedParamType = typeMirrorToJClass(parameters.get(i).asType()).fullName();
                if (!param.type().fullName().equals(searchedParamType)) {
                    continue method;
                }
                i++;
            }
            return method;
        }
    }
    // CHECKSTYLE:ON
    return null;
}
Also used : JDefinedClass(com.helger.jcodemodel.JDefinedClass) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 77 with JVar

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

the class OrmLiteDaoHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    OrmLiteHolder ormLiteHolder = holder.getPluginHolder(new OrmLiteHolder(holder));
    AbstractJClass modelClass = getJClass(ormLiteHelper.getEntityType(param).toString());
    AbstractJClass idClass = getJClass(ormLiteHelper.getEntityIdType(param).toString());
    IJExpression modelClassDotClass = modelClass.dotclass();
    AbstractJClass daoClass = getJClass(OrmLiteClasses.DAO).narrow(modelClass, idClass);
    AbstractJClass daoImplClass = codeModelHelper.typeMirrorToJClass(param.asType());
    TypeMirror databaseHelperTypeMirror = annotationHelper.extractAnnotationParameter(element, "helper");
    JFieldVar databaseHelperRef = ormLiteHolder.getDatabaseHelperRef(databaseHelperTypeMirror);
    IJExpression injectExpr = databaseHelperRef.invoke("getDao").arg(modelClassDotClass);
    if (elementExtendsRuntimeExceptionDao(param)) {
        injectExpr = _new(daoImplClass).arg(cast(daoClass, injectExpr));
    }
    JTryBlock tryBlock = targetBlock._try();
    tryBlock.body().add(fieldRef.assign(injectExpr));
    JCatchBlock catchBlock = tryBlock._catch(getClasses().SQL_EXCEPTION);
    JVar exception = catchBlock.param("e");
    String fieldName = param.getSimpleName().toString();
    // 
    catchBlock.body().staticInvoke(getClasses().LOG, // 
    "e").arg(// 
    logTagForClassHolder(holder)).arg(// 
    "Could not create DAO " + fieldName).arg(exception);
}
Also used : TypeMirror(javax.lang.model.type.TypeMirror) JFieldVar(com.helger.jcodemodel.JFieldVar) OrmLiteHolder(org.androidannotations.ormlite.holder.OrmLiteHolder) IJExpression(com.helger.jcodemodel.IJExpression) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 78 with JVar

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

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

the class ServiceActionHandler method addActionToIntentBuilder.

private void addActionToIntentBuilder(EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
    JMethod method = holder.getIntentBuilderClass().method(PUBLIC, holder.getIntentBuilderClass(), methodName);
    JBlock body = method.body();
    String docComment = getProcessingEnvironment().getElementUtils().getDocComment(executableElement);
    codeModelHelper.addTrimmedDocComment(method, docComment);
    method.javadoc().addReturn().append("the IntentBuilder to chain calls");
    // setAction
    body.invoke("action").arg(actionKeyField);
    for (VariableElement param : executableElement.getParameters()) {
        String paramName = param.getSimpleName().toString();
        AbstractJClass parameterClass = codeModelHelper.typeMirrorToJClass(param.asType());
        JFieldVar paramVar = getStaticExtraField(holder, paramName);
        JVar methodParam = method.param(parameterClass, paramName);
        JInvocation putExtraInvocation = holder.getIntentBuilder().getSuperPutExtraInvocation(param.asType(), methodParam, paramVar);
        body.add(putExtraInvocation);
    }
    body._return(JExpr._this());
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) JBlock(com.helger.jcodemodel.JBlock) 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 80 with JVar

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

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