Search in sources :

Example 1 with IRInnerClass

use of org.androidannotations.rclass.IRInnerClass in project androidannotations by androidannotations.

the class RClass method get.

@Override
public IRInnerClass get(Res res) {
    String id = res.rName();
    IRInnerClass rInnerClass = rClass.get(id);
    if (rInnerClass != null) {
        return rInnerClass;
    } else {
        return new RInnerClass(null);
    }
}
Also used : IRInnerClass(org.androidannotations.rclass.IRInnerClass) IRInnerClass(org.androidannotations.rclass.IRInnerClass)

Example 2 with IRInnerClass

use of org.androidannotations.rclass.IRInnerClass in project androidannotations by androidannotations.

the class SharedPrefHandler method createFieldMethod.

private IJExpression createFieldMethod(SharedPrefHolder holder, ExecutableElement method, Class<? extends Annotation> annotationClass, Class<? extends AbstractPrefField<?>> prefFieldClass, Object defaultValue, Res resType, String fieldHelperMethodName) {
    Annotation annotation = method.getAnnotation(annotationClass);
    IJExpression defaultValueExpr;
    Object value = null;
    if (annotation != null) {
        value = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "value");
    }
    if (annotation != null && method.getAnnotation(DefaultStringSet.class) == null) {
        defaultValueExpr = codeModelHelper.litObject(value);
    } else if (method.getAnnotation(DefaultRes.class) != null) {
        defaultValueExpr = extractResValue(holder, method, resType);
        annotationClass = DefaultRes.class;
    } else if (method.getAnnotation(DefaultStringSet.class) != null) {
        if (value != null) {
            Set<String> arrayValues = new HashSet<>(Arrays.asList((String[]) value));
            value = arrayValues;
            if (arrayValues.isEmpty()) {
                defaultValueExpr = newEmptyStringHashSet();
            } else {
                JInvocation arrayAsList = getClasses().ARRAYS.staticInvoke("asList");
                for (String arrayValue : arrayValues) {
                    arrayAsList.arg(lit(arrayValue));
                }
                defaultValueExpr = JExpr._new(getClasses().HASH_SET.narrow(getClasses().STRING)).arg(arrayAsList);
            }
        } else {
            defaultValueExpr = newEmptyStringHashSet();
        }
        annotationClass = DefaultStringSet.class;
    } else {
        defaultValueExpr = defaultValue != null ? codeModelHelper.litObject(defaultValue) : newEmptyStringHashSet();
        annotationClass = null;
    }
    Integer keyResId = ResId.DEFAULT_VALUE;
    if (annotationClass != null) {
        keyResId = annotationHelper.extractAnnotationParameter(method, annotationClass.getName(), "keyRes");
    }
    IJExpression keyExpression;
    String fieldName = method.getSimpleName().toString();
    if (keyResId == ResId.DEFAULT_VALUE) {
        keyExpression = lit(fieldName);
    } else {
        IRInnerClass idClass = getEnvironment().getRClass().get(IRClass.Res.STRING);
        JFieldRef idRef = idClass.getIdStaticRef(keyResId, getEnvironment());
        keyExpression = holder.getEditorContextField().invoke("getString").arg(idRef);
    }
    String docComment = getProcessingEnvironment().getElementUtils().getDocComment(method);
    String defaultValueStr = value == null ? null : value.toString();
    if (defaultValueStr == null) {
        defaultValueStr = defaultValue == null ? null : defaultValue.toString();
    }
    holder.createFieldMethod(prefFieldClass, keyExpression, fieldName, fieldHelperMethodName, defaultValueExpr, docComment, defaultValueStr);
    return keyExpression;
}
Also used : DefaultStringSet(org.androidannotations.annotations.sharedpreferences.DefaultStringSet) JFieldRef(com.helger.jcodemodel.JFieldRef) IJExpression(com.helger.jcodemodel.IJExpression) DefaultRes(org.androidannotations.annotations.sharedpreferences.DefaultRes) JInvocation(com.helger.jcodemodel.JInvocation) DefaultString(org.androidannotations.annotations.sharedpreferences.DefaultString) Annotation(java.lang.annotation.Annotation) HashSet(java.util.HashSet) IRInnerClass(org.androidannotations.rclass.IRInnerClass)

Example 3 with IRInnerClass

use of org.androidannotations.rclass.IRInnerClass 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

IRInnerClass (org.androidannotations.rclass.IRInnerClass)3 IJExpression (com.helger.jcodemodel.IJExpression)2 JFieldRef (com.helger.jcodemodel.JFieldRef)2 JInvocation (com.helger.jcodemodel.JInvocation)2 JBlock (com.helger.jcodemodel.JBlock)1 JCatchBlock (com.helger.jcodemodel.JCatchBlock)1 JDefinedClass (com.helger.jcodemodel.JDefinedClass)1 JMethod (com.helger.jcodemodel.JMethod)1 JTryBlock (com.helger.jcodemodel.JTryBlock)1 JVar (com.helger.jcodemodel.JVar)1 Annotation (java.lang.annotation.Annotation)1 HashSet (java.util.HashSet)1 HttpsClient (org.androidannotations.annotations.HttpsClient)1 DefaultRes (org.androidannotations.annotations.sharedpreferences.DefaultRes)1 DefaultString (org.androidannotations.annotations.sharedpreferences.DefaultString)1 DefaultStringSet (org.androidannotations.annotations.sharedpreferences.DefaultStringSet)1 ProcessHolder (org.androidannotations.internal.process.ProcessHolder)1