Search in sources :

Example 56 with JMethod

use of com.helger.jcodemodel.JMethod 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 57 with JMethod

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

the class WakeLockHandler method process.

@Override
public void process(Element element, EComponentHolder holder) {
    ExecutableElement executableElement = (ExecutableElement) element;
    WakeLock annotation = executableElement.getAnnotation(WakeLock.class);
    String tag = extractTag(executableElement);
    Level level = annotation.level();
    Flag[] flags = annotation.flags();
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(method);
    JBlock methodBody = method.body();
    IJExpression levelAndFlags = getClasses().POWER_MANAGER.staticRef(level.name());
    if (flags.length > 0) {
        for (Flag flag : flags) {
            levelAndFlags = levelAndFlags.bor(getClasses().POWER_MANAGER.staticRef(flag.name()));
        }
    }
    JInvocation newWakeLock = holder.getPowerManagerRef().invoke("newWakeLock").arg(levelAndFlags).arg(JExpr.lit(tag));
    JVar wakeLock = methodBody.decl(getClasses().WAKE_LOCK, "wakeLock", JExpr._null());
    JTryBlock tryBlock = methodBody._try();
    tryBlock.body().assign(wakeLock, newWakeLock);
    tryBlock.body().add(wakeLock.invoke("acquire"));
    tryBlock.body().add(previousMethodBody);
    JBlock finallyBlock = tryBlock._finally();
    JConditional ifStatement = finallyBlock._if(wakeLock.ne(JExpr._null()));
    ifStatement._then().add(wakeLock.invoke("release"));
}
Also used : WakeLock(org.androidannotations.annotations.WakeLock) ExecutableElement(javax.lang.model.element.ExecutableElement) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) Flag(org.androidannotations.annotations.WakeLock.Flag) JBlock(com.helger.jcodemodel.JBlock) Level(org.androidannotations.annotations.WakeLock.Level) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Example 58 with JMethod

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

the class ActivityIntentBuilder method addFragmentConstructor.

private JFieldVar addFragmentConstructor(AbstractJClass fragmentClass, String fieldName) {
    JFieldVar fragmentField = holder.getIntentBuilderClass().field(PRIVATE, fragmentClass, fieldName);
    IJExpression generatedClass = holder.getGeneratedClass().dotclass();
    JMethod constructor = holder.getIntentBuilderClass().constructor(JMod.PUBLIC);
    JVar constructorFragmentParam = constructor.param(fragmentClass, "fragment");
    JBlock constructorBody = constructor.body();
    constructorBody.invoke("super").arg(constructorFragmentParam.invoke("getActivity")).arg(generatedClass);
    constructorBody.assign(fragmentField, constructorFragmentParam);
    return fragmentField;
}
Also used : JFieldVar(com.helger.jcodemodel.JFieldVar) IJExpression(com.helger.jcodemodel.IJExpression) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 59 with JMethod

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

the class ActivityIntentBuilder method createAdditionalIntentMethods.

private void createAdditionalIntentMethods() {
    if (hasFragmentInClasspath()) {
        // intent() with android.app.Fragment param
        JMethod method = holder.getGeneratedClass().method(STATIC | PUBLIC, holder.getIntentBuilderClass(), "intent");
        JVar fragmentParam = method.param(getClasses().FRAGMENT, "fragment");
        method.body()._return(_new(holder.getIntentBuilderClass()).arg(fragmentParam));
    }
    if (hasFragmentSupportInClasspath()) {
        // intent() with android.support.v4.app.Fragment param
        JMethod method = holder.getGeneratedClass().method(STATIC | PUBLIC, holder.getIntentBuilderClass(), "intent");
        JVar fragmentParam = method.param(getClasses().SUPPORT_V4_FRAGMENT, "supportFragment");
        method.body()._return(_new(holder.getIntentBuilderClass()).arg(fragmentParam));
    }
}
Also used : JMethod(com.helger.jcodemodel.JMethod) JVar(com.helger.jcodemodel.JVar)

Example 60 with JMethod

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

the class TraceHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    String tag = extractTag(executableElement);
    int level = executableElement.getAnnotation(Trace.class).level();
    JMethod method = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(method);
    JBlock methodBody = method.body();
    JInvocation isLoggableInvocation = getClasses().LOG.staticInvoke("isLoggable");
    isLoggableInvocation.arg(tag).arg(logLevelFromInt(level, getClasses().LOG));
    JConditional ifStatement = methodBody._if(isLoggableInvocation);
    JInvocation currentTimeInvoke = getClasses().SYSTEM.staticInvoke("currentTimeMillis");
    JBlock thenBody = ifStatement._then();
    // Log In
    String logMethodName = logMethodNameFromLevel(level);
    JInvocation logEnterInvoke = getClasses().LOG.staticInvoke(logMethodName);
    logEnterInvoke.arg(tag);
    logEnterInvoke.arg(getEnterMessage(method, executableElement));
    thenBody.add(logEnterInvoke);
    JVar startDeclaration = thenBody.decl(getCodeModel().LONG, "traceStart" + generationSuffix(), currentTimeInvoke);
    JTryBlock tryBlock;
    JVar result = null;
    if (method.type().fullName().equals("void")) {
        tryBlock = thenBody._try();
        tryBlock.body().add(previousMethodBody);
    } else {
        JInvocation superCall = codeModelHelper.getSuperCall(holder, method);
        result = thenBody.decl(getJClass(Object.class), "traceResult" + generationSuffix(), JExpr._null());
        tryBlock = thenBody._try();
        tryBlock.body().assign(result, superCall);
        tryBlock.body()._return(JExpr.cast(boxify(method.type()), result));
    }
    JBlock finallyBlock = tryBlock._finally();
    JVar durationDeclaration = finallyBlock.decl(getCodeModel().LONG, "traceDuration" + generationSuffix(), currentTimeInvoke.minus(startDeclaration));
    JInvocation logExitInvoke = getClasses().LOG.staticInvoke(logMethodName);
    logExitInvoke.arg(tag);
    logExitInvoke.arg(getExitMessage(executableElement, method, result, durationDeclaration));
    finallyBlock.add(logExitInvoke);
    JBlock elseBlock = ifStatement._else();
    elseBlock.add(previousMethodBody);
}
Also used : Trace(org.androidannotations.annotations.Trace) ExecutableElement(javax.lang.model.element.ExecutableElement) JBlock(com.helger.jcodemodel.JBlock) JInvocation(com.helger.jcodemodel.JInvocation) JConditional(com.helger.jcodemodel.JConditional) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

JMethod (com.helger.jcodemodel.JMethod)122 JBlock (com.helger.jcodemodel.JBlock)73 JVar (com.helger.jcodemodel.JVar)56 AbstractJClass (com.helger.jcodemodel.AbstractJClass)36 JInvocation (com.helger.jcodemodel.JInvocation)26 JDefinedClass (com.helger.jcodemodel.JDefinedClass)20 IJExpression (com.helger.jcodemodel.IJExpression)15 ExecutableElement (javax.lang.model.element.ExecutableElement)13 JFieldVar (com.helger.jcodemodel.JFieldVar)10 JConditional (com.helger.jcodemodel.JConditional)8 JTryBlock (com.helger.jcodemodel.JTryBlock)7 JTypeVar (com.helger.jcodemodel.JTypeVar)7 TypeMirror (javax.lang.model.type.TypeMirror)7 VariableElement (javax.lang.model.element.VariableElement)6 JFieldRef (com.helger.jcodemodel.JFieldRef)5 JPrimitiveType (com.helger.jcodemodel.JPrimitiveType)5 ArrayList (java.util.ArrayList)5 VisitorDefinition (com.github.sviperll.adt4j.model.config.VisitorDefinition)4 GenerationProcess (com.github.sviperll.adt4j.model.util.GenerationProcess)4 AbstractJType (com.helger.jcodemodel.AbstractJType)4