use of com.helger.jcodemodel.JDefinedClass 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);
}
use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.
the class ProcessHolder method put.
public void put(Element element, GeneratedClassHolder generatedClassHolder) {
JDefinedClass generatedClass = generatedClassHolder.getGeneratedClass();
String qualifiedName = generatedClass.fullName();
originatingElements.add(qualifiedName, element);
generatedClassHolders.put(element, generatedClassHolder);
}
use of com.helger.jcodemodel.JDefinedClass 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)));
}
use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method createParameterizedTypeReferenceAnonymousSubclassInstance.
public IJExpression createParameterizedTypeReferenceAnonymousSubclassInstance(TypeMirror returnType) {
AbstractJClass narrowedTypeReference = getEnvironment().getJClass(RestSpringClasses.PARAMETERIZED_TYPE_REFERENCE).narrow(codeModelHelper.typeMirrorToJClass(returnType));
JDefinedClass anonymousClass = getEnvironment().getCodeModel().anonymousClass(narrowedTypeReference);
return JExpr._new(anonymousClass);
}
use of com.helger.jcodemodel.JDefinedClass in project androidannotations by androidannotations.
the class RestAnnotationHelper method retrieveDecoratedResponseClass.
/**
* Recursive method used to find if one of the grand-parent of the
* <code>enclosingJClass</code> is {@link java.util.Map Map}, {@link Set} or
* {@link java.util.Collection Collection}.
*/
private AbstractJClass retrieveDecoratedResponseClass(DeclaredType declaredType, TypeElement typeElement, RestHolder holder) {
String classTypeBaseName = typeElement.toString();
// Looking for basic java.util interfaces to set a default
// implementation
String decoratedClassName = null;
if (typeElement.getKind() == ElementKind.INTERFACE) {
if (classTypeBaseName.equals(CanonicalNameConstants.MAP)) {
decoratedClassName = LinkedHashMap.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.SET)) {
decoratedClassName = TreeSet.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.LIST)) {
decoratedClassName = ArrayList.class.getCanonicalName();
} else if (classTypeBaseName.equals(CanonicalNameConstants.COLLECTION)) {
decoratedClassName = ArrayList.class.getCanonicalName();
}
} else {
decoratedClassName = typeElement.getQualifiedName().toString();
}
if (decoratedClassName != null) {
// Configure the super class of the final decorated class
String decoratedClassNameSuffix = "";
AbstractJClass decoratedSuperClass = getEnvironment().getJClass(decoratedClassName);
for (TypeMirror typeArgument : declaredType.getTypeArguments()) {
TypeMirror actualTypeArgument = typeArgument;
if (typeArgument instanceof WildcardType) {
WildcardType wildcardType = (WildcardType) typeArgument;
if (wildcardType.getExtendsBound() != null) {
actualTypeArgument = wildcardType.getExtendsBound();
} else if (wildcardType.getSuperBound() != null) {
actualTypeArgument = wildcardType.getSuperBound();
}
}
AbstractJClass narrowJClass = codeModelHelper.typeMirrorToJClass(actualTypeArgument);
decoratedSuperClass = decoratedSuperClass.narrow(narrowJClass);
decoratedClassNameSuffix += plainName(narrowJClass);
}
String decoratedFinalClassName = classTypeBaseName + "_" + decoratedClassNameSuffix;
decoratedFinalClassName = decoratedFinalClassName.replaceAll("\\[\\]", "s");
String packageName = holder.getGeneratedClass()._package().name();
decoratedFinalClassName = packageName + "." + decoratedFinalClassName;
JDefinedClass decoratedJClass = getEnvironment().getDefinedClass(decoratedFinalClassName);
decoratedJClass._extends(decoratedSuperClass);
return decoratedJClass;
}
// Try to find the superclass and make a recursive call to the this
// method
TypeMirror enclosingSuperJClass = typeElement.getSuperclass();
if (enclosingSuperJClass != null && enclosingSuperJClass.getKind() == TypeKind.DECLARED) {
DeclaredType declaredEnclosingSuperJClass = (DeclaredType) enclosingSuperJClass;
return retrieveDecoratedResponseClass(declaredType, (TypeElement) declaredEnclosingSuperJClass.asElement(), holder);
}
// Falling back to the current enclosingJClass if Class can't be found
return null;
}
Aggregations