Search in sources :

Example 41 with JInvocation

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

the class BackgroundHandler method process.

@Override
public void process(Element element, EComponentHolder holder) throws Exception {
    ExecutableElement executableElement = (ExecutableElement) element;
    JMethod delegatingMethod = codeModelHelper.overrideAnnotatedMethod(executableElement, holder);
    JBlock previousMethodBody = codeModelHelper.removeBody(delegatingMethod);
    JDefinedClass anonymousTaskClass = getCodeModel().anonymousClass(BackgroundExecutor.Task.class);
    JMethod executeMethod = anonymousTaskClass.method(JMod.PUBLIC, getCodeModel().VOID, "execute");
    executeMethod.annotate(Override.class);
    // Catch exception in user code
    JTryBlock tryBlock = executeMethod.body()._try();
    tryBlock.body().add(previousMethodBody);
    JCatchBlock catchBlock = tryBlock._catch(getClasses().THROWABLE);
    JVar caughtException = catchBlock.param("e");
    IJStatement uncaughtExceptionCall = //
    getClasses().THREAD.staticInvoke(//
    "getDefaultUncaughtExceptionHandler").invoke(//
    "uncaughtException").arg(//
    getClasses().THREAD.staticInvoke("currentThread")).arg(caughtException);
    catchBlock.body().add(uncaughtExceptionCall);
    Background annotation = element.getAnnotation(Background.class);
    String id = annotation.id();
    long delay = annotation.delay();
    String serial = annotation.serial();
    AbstractJClass backgroundExecutorClass = getJClass(BackgroundExecutor.class);
    JInvocation newTask = _new(anonymousTaskClass).arg(lit(id)).arg(lit(delay)).arg(lit(serial));
    JInvocation executeCall = backgroundExecutorClass.staticInvoke("execute").arg(newTask);
    delegatingMethod.body().add(executeCall);
}
Also used : BackgroundExecutor(org.androidannotations.api.BackgroundExecutor) Background(org.androidannotations.annotations.Background) JDefinedClass(com.helger.jcodemodel.JDefinedClass) ExecutableElement(javax.lang.model.element.ExecutableElement) AbstractJClass(com.helger.jcodemodel.AbstractJClass) JInvocation(com.helger.jcodemodel.JInvocation) IJStatement(com.helger.jcodemodel.IJStatement) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Example 42 with JInvocation

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

the class BeforeTextChangeHandler 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 afterParameterPosition = -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 ("after".equals(parameterName)) {
                afterParameterPosition = 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.getBeforeTextChangedBody();
        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.getBeforeTextChangedStartParam();
                textChangeCall.arg(startParameter);
            } else if (i == countParameterPosition) {
                JVar countParameter = textWatcherHolder.getBeforeTextChangedCountParam();
                textChangeCall.arg(countParameter);
            } else if (i == afterParameterPosition) {
                JVar afterParameter = textWatcherHolder.getBeforeTextChangedAfterParam();
                textChangeCall.arg(afterParameter);
            } else if (i == charSequenceParameterPosition) {
                JVar charSequenceParam = textWatcherHolder.getBeforeTextChangedCharSequenceParam();
                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 43 with JInvocation

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

the class FullscreenHandler method process.

@Override
public void process(Element element, EActivityHolder holder) {
    JFieldRef fullScreen = getClasses().WINDOW_MANAGER_LAYOUT_PARAMS.staticRef("FLAG_FULLSCREEN");
    JInvocation setFlagsInvocation = invoke(invoke("getWindow"), "setFlags").arg(fullScreen).arg(fullScreen);
    holder.getInitBodyInjectionBlock().add(setFlagsInvocation);
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JInvocation(com.helger.jcodemodel.JInvocation)

Example 44 with JInvocation

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

the class HierarchyViewerSupportHandler method process.

@Override
public void process(Element element, EActivityHolder holder) throws Exception {
    JInvocation viewServerInvocation = getClasses().VIEW_SERVER.staticInvoke("get").arg(_this());
    holder.getOnViewChangedBodyInjectionBlock().invoke(viewServerInvocation, "addWindow").arg(_this());
    holder.getOnDestroyAfterSuperBlock().invoke(viewServerInvocation, "removeWindow").arg(_this());
    holder.getOnResumeAfterSuperBlock().invoke(viewServerInvocation, "setFocusedWindow").arg(_this());
}
Also used : JInvocation(com.helger.jcodemodel.JInvocation)

Example 45 with JInvocation

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

the class HttpsClientHandler method assignValue.

@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
    IRInnerClass rInnerClass = getEnvironment().getRClass().get(IRClass.Res.RAW);
    HttpsClient annotation = element.getAnnotation(HttpsClient.class);
    JFieldRef trustStoreRawIdRef = annotationHelper.extractOneAnnotationFieldRef(element, getTarget(), rInnerClass, false, "trustStore", "trustStoreResName");
    JFieldRef keyStoreRawIdRef = annotationHelper.extractOneAnnotationFieldRef(element, getTarget(), rInnerClass, false, "keyStore", "keyStoreResName");
    String trustStorePwd = annotation.trustStorePwd();
    String keyStorePwd = annotation.keyStorePwd();
    boolean allowAllHostnames = annotation.allowAllHostnames();
    boolean useCustomTrustStore = trustStoreRawIdRef != null;
    boolean useCustomKeyStore = keyStoreRawIdRef != null;
    ProcessHolder.Classes classes = getClasses();
    JDefinedClass jAnonClass = getCodeModel().anonymousClass(classes.DEFAULT_HTTP_CLIENT);
    JMethod method = jAnonClass.method(JMod.PROTECTED, classes.CLIENT_CONNECTION_MANAGER, "createClientConnectionManager");
    method.annotate(Override.class);
    JTryBlock jTryBlock = method.body()._try();
    JVar jVarTrusted = null;
    JVar jVarKeystore = null;
    if (useCustomKeyStore) {
        jVarKeystore = jTryBlock.body().decl(classes.KEY_STORE, "keystore");
        jVarKeystore.init(classes.KEY_STORE.staticInvoke("getInstance").arg("BKS"));
    }
    if (useCustomTrustStore || useCustomKeyStore) {
        /*
			 * use default trust store
			 */
        jVarTrusted = jTryBlock.body().decl(classes.KEY_STORE, "trusted");
        jVarTrusted.init(classes.KEY_STORE.staticInvoke("getInstance").arg("BKS"));
    }
    JVar jVarRes = null;
    JVar jVarTrstFile = null;
    JVar jVarKeyFile = null;
    if (useCustomKeyStore || useCustomTrustStore) {
        jVarRes = jTryBlock.body().decl(classes.RESOURCES, "res", invoke("getResources"));
    }
    if (useCustomKeyStore) {
        JInvocation jInvRawKey = jVarRes.invoke("openRawResource").arg(keyStoreRawIdRef);
        jVarKeyFile = jTryBlock.body().decl(classes.INPUT_STREAM, "inKeystore", jInvRawKey);
    }
    if (useCustomTrustStore) {
        JInvocation jInvRawTrust = jVarRes.invoke("openRawResource").arg(trustStoreRawIdRef);
        jVarTrstFile = jTryBlock.body().decl(classes.INPUT_STREAM, "inTrustStore", jInvRawTrust);
    } else if (useCustomKeyStore) {
        jVarTrstFile = jTryBlock.body().decl(classes.INPUT_STREAM, "inTrustStore", _new(classes.FILE_INPUT_STREAM).arg("/system/etc/security/cacerts.bks"));
    }
    // try load
    if (useCustomKeyStore || useCustomTrustStore) {
        JTryBlock jTryLoad = jTryBlock.body()._try();
        if (useCustomKeyStore) {
            jTryLoad.body().add(invoke(jVarKeystore, "load").arg(jVarKeyFile).arg(invoke(lit(keyStorePwd), "toCharArray")));
        }
        jTryLoad.body().add(invoke(jVarTrusted, "load").arg(jVarTrstFile).arg(invoke(lit(trustStorePwd), "toCharArray")));
        // finally load
        JBlock jFinally = jTryLoad._finally();
        if (useCustomKeyStore) {
            jFinally.add(invoke(jVarKeyFile, "close"));
        }
        jFinally.add(invoke(jVarTrstFile, "close"));
    }
    if (null == jVarKeystore && null == jVarTrusted) {
        JVar jVarCcm = jTryBlock.body().decl(classes.CLIENT_CONNECTION_MANAGER, "ccm");
        jVarCcm.init(_super().invoke("createClientConnectionManager"));
        if (allowAllHostnames) {
            IJExpression jCast = cast(classes.SSL_SOCKET_FACTORY, jVarCcm.invoke("getSchemeRegistry").invoke("getScheme").arg("https").invoke("getSocketFactory"));
            jTryBlock.body().add(jCast.invoke("setHostnameVerifier").arg(classes.SSL_SOCKET_FACTORY.staticRef("ALLOW_ALL_HOSTNAME_VERIFIER")));
        }
        jTryBlock.body()._return(jVarCcm);
    } else {
        JVar jVarSslFact = jTryBlock.body().decl(classes.SSL_SOCKET_FACTORY, "newSslSocketFactory");
        jVarSslFact.init(_new(classes.SSL_SOCKET_FACTORY).arg(null == jVarKeystore ? _null() : jVarKeystore).arg(keyStorePwd).arg(jVarTrusted));
        if (allowAllHostnames) {
            jTryBlock.body().add(invoke(jVarSslFact, "setHostnameVerifier").arg(classes.SSL_SOCKET_FACTORY.staticRef("ALLOW_ALL_HOSTNAME_VERIFIER")));
        }
        JVar jVarSchemeReg = jTryBlock.body().decl(classes.SCHEME_REGISTRY, "registry");
        jVarSchemeReg.init(_new(classes.SCHEME_REGISTRY));
        jTryBlock.body().add(invoke(jVarSchemeReg, "register").arg(_new(classes.SCHEME).arg("https").arg(jVarSslFact).arg(lit(443))));
        jTryBlock.body().add(invoke(jVarSchemeReg, "register").arg(_new(classes.SCHEME).arg("http").arg(classes.PLAIN_SOCKET_FACTORY.staticInvoke("getSocketFactory")).arg(lit(80))));
        JVar jVarCcm = jTryBlock.body().decl(classes.CLIENT_CONNECTION_MANAGER, "ccm");
        jVarCcm.init(_new(classes.SINGLE_CLIENT_CONN_MANAGER).arg(invoke("getParams")).arg(jVarSchemeReg));
        jTryBlock.body()._return(jVarCcm);
    }
    // catch block
    JCatchBlock jCatchBlock = jTryBlock._catch(classes.EXCEPTION);
    JVar jVarExceptionParam = jCatchBlock.param("e");
    jCatchBlock.body().add(jVarExceptionParam.invoke("printStackTrace"));
    jCatchBlock.body()._return(_super().invoke("createClientConnectionManager"));
    targetBlock.add(fieldRef.assign(_new(jAnonClass)));
}
Also used : JFieldRef(com.helger.jcodemodel.JFieldRef) JDefinedClass(com.helger.jcodemodel.JDefinedClass) ProcessHolder(org.androidannotations.internal.process.ProcessHolder) IJExpression(com.helger.jcodemodel.IJExpression) JInvocation(com.helger.jcodemodel.JInvocation) IRInnerClass(org.androidannotations.rclass.IRInnerClass) HttpsClient(org.androidannotations.annotations.HttpsClient) JBlock(com.helger.jcodemodel.JBlock) JMethod(com.helger.jcodemodel.JMethod) JTryBlock(com.helger.jcodemodel.JTryBlock) JCatchBlock(com.helger.jcodemodel.JCatchBlock) JVar(com.helger.jcodemodel.JVar)

Aggregations

JInvocation (com.helger.jcodemodel.JInvocation)71 JVar (com.helger.jcodemodel.JVar)38 JBlock (com.helger.jcodemodel.JBlock)37 AbstractJClass (com.helger.jcodemodel.AbstractJClass)31 IJExpression (com.helger.jcodemodel.IJExpression)27 JMethod (com.helger.jcodemodel.JMethod)26 ExecutableElement (javax.lang.model.element.ExecutableElement)20 VariableElement (javax.lang.model.element.VariableElement)16 JFieldRef (com.helger.jcodemodel.JFieldRef)14 TypeMirror (javax.lang.model.type.TypeMirror)14 JConditional (com.helger.jcodemodel.JConditional)11 JDefinedClass (com.helger.jcodemodel.JDefinedClass)9 AbstractJType (com.helger.jcodemodel.AbstractJType)5 JFieldVar (com.helger.jcodemodel.JFieldVar)5 JTryBlock (com.helger.jcodemodel.JTryBlock)5 ArrayList (java.util.ArrayList)4 DeclaredType (javax.lang.model.type.DeclaredType)4 JCatchBlock (com.helger.jcodemodel.JCatchBlock)3 PageChangeHolder (org.androidannotations.holder.PageChangeHolder)3 TextWatcherHolder (org.androidannotations.holder.TextWatcherHolder)3