use of com.helger.jcodemodel.IJStatement in project androidannotations by androidannotations.
the class APTCodeModelHelper method replaceSuperCall.
public void replaceSuperCall(JMethod method, JBlock replacement) {
String superCallStart = "super." + method.name() + "(";
JBlock oldBody = removeBody(method);
JBlock newBody = method.body();
for (Object content : oldBody.getContents()) {
StringWriter writer = new StringWriter();
JFormatter formatter = new JFormatter(writer);
IJStatement statement = (IJStatement) content;
statement.state(formatter);
String statementString = writer.getBuffer().toString();
if (statementString.startsWith(superCallStart)) {
newBody.add(replacement);
} else {
newBody.add(statement);
}
}
}
use of com.helger.jcodemodel.IJStatement in project androidannotations by androidannotations.
the class BeanHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentHolder holder, Element element, Element param) {
TypeMirror typeMirror = annotationHelper.extractAnnotationClassParameter(element);
if (typeMirror == null) {
typeMirror = param.asType();
typeMirror = getProcessingEnvironment().getTypeUtils().erasure(typeMirror);
}
String typeQualifiedName = typeMirror.toString();
AbstractJClass injectedClass = getJClass(annotationHelper.generatedClassQualifiedNameFromQualifiedName(typeQualifiedName));
JInvocation beanInstance = injectedClass.staticInvoke(EBeanHolder.GET_INSTANCE_METHOD_NAME).arg(holder.getContextRef());
TypeElement declaredEBean = getProcessingEnvironment().getElementUtils().getTypeElement(typeQualifiedName);
if (declaredEBean != null) {
EBean annotation = declaredEBean.getAnnotation(EBean.class);
if (annotation.scope() != EBean.Scope.Singleton && annotation.scope() != EBean.Scope.Activity) {
beanInstance.arg(holder.getRootFragmentRef());
}
}
IJStatement assignment = fieldRef.assign(beanInstance);
if (param.getKind() == ElementKind.FIELD) {
boolean hasNonConfigurationInstanceAnnotation = element.getAnnotation(NonConfigurationInstance.class) != null;
if (hasNonConfigurationInstanceAnnotation) {
JConditional conditional = targetBlock._if(fieldRef.eq(_null()));
conditional._then().add(assignment);
assignment = conditional;
}
}
targetBlock.add(assignment);
}
use of com.helger.jcodemodel.IJStatement 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);
}
Aggregations