use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.
the class PageSelectedHandler 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 positionParameterPosition = -1;
int viewPagerParameterPosition = -1;
TypeMirror viewPagerParameterType = null;
for (int i = 0; i < parameters.size(); i++) {
VariableElement parameter = parameters.get(i);
TypeMirror parameterType = parameter.asType();
if (parameterType.getKind() == TypeKind.INT || CanonicalNameConstants.INTEGER.equals(parameterType.toString())) {
positionParameterPosition = i;
} else {
TypeMirror viewPagerType = annotationHelper.typeElementFromQualifiedName(CanonicalNameConstants.VIEW_PAGER).asType();
if (annotationHelper.isSubtype(parameterType, viewPagerType)) {
viewPagerParameterPosition = i;
viewPagerParameterType = parameterType;
}
}
}
List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
for (JFieldRef idRef : idsRefs) {
PageChangeHolder pageChangeHolder = holder.getPageChangeHolder(idRef, viewPagerParameterType, hasAddOnPageChangeListenerMethod());
JBlock methodBody = pageChangeHolder.getPageSelectedBody();
IJExpression thisRef = holder.getGeneratedClass().staticRef("this");
JInvocation methodCall = methodBody.invoke(thisRef, methodName);
for (int i = 0; i < parameters.size(); i++) {
if (i == positionParameterPosition) {
JVar stateParam = pageChangeHolder.getPageSelectedPositionParam();
methodCall.arg(stateParam);
} else if (i == viewPagerParameterPosition) {
JVar viewParameter = pageChangeHolder.getViewPagerVariable();
methodCall.arg(viewParameter);
}
}
}
}
use of com.helger.jcodemodel.JInvocation 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.JInvocation in project androidannotations by androidannotations.
the class RootContextHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EBeanHolder holder, Element element, Element param) {
TypeMirror elementType = param.asType();
String typeQualifiedName = elementType.toString();
IJExpression contextRef = holder.getContextRef();
if (CanonicalNameConstants.CONTEXT.equals(typeQualifiedName)) {
targetBlock.add(fieldRef.assign(contextRef));
} else {
AbstractJClass extendingContextClass = getEnvironment().getJClass(typeQualifiedName);
JConditional cond = getInvocationBlock(holder)._if(holder.getContextRef()._instanceof(extendingContextClass));
cond._then().add(fieldRef.assign(cast(extendingContextClass, holder.getContextRef())));
JInvocation warningInvoke = getClasses().LOG.staticInvoke("w");
warningInvoke.arg(logTagForClassHolder(holder));
warningInvoke.arg(lit("Due to Context class ").plus(holder.getContextRef().invoke("getClass").invoke("getSimpleName")).plus(lit(", the @RootContext " + extendingContextClass.name() + " won't be populated")));
cond._else().add(warningInvoke);
}
}
use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.
the class ServiceActionHandler method addActionToIntentBuilder.
private void addActionToIntentBuilder(EIntentServiceHolder holder, ExecutableElement executableElement, String methodName, JFieldVar actionKeyField) {
JMethod method = holder.getIntentBuilderClass().method(PUBLIC, holder.getIntentBuilderClass(), methodName);
JBlock body = method.body();
String docComment = getProcessingEnvironment().getElementUtils().getDocComment(executableElement);
codeModelHelper.addTrimmedDocComment(method, docComment);
method.javadoc().addReturn().append("the IntentBuilder to chain calls");
// setAction
body.invoke("action").arg(actionKeyField);
for (VariableElement param : executableElement.getParameters()) {
String paramName = param.getSimpleName().toString();
AbstractJClass parameterClass = codeModelHelper.typeMirrorToJClass(param.asType());
JFieldVar paramVar = getStaticExtraField(holder, paramName);
JVar methodParam = method.param(parameterClass, paramName);
JInvocation putExtraInvocation = holder.getIntentBuilder().getSuperPutExtraInvocation(param.asType(), methodParam, paramVar);
body.add(putExtraInvocation);
}
body._return(JExpr._this());
}
use of com.helger.jcodemodel.JInvocation in project androidannotations by androidannotations.
the class ActivityIntentBuilder method overrideStartForResultMethod.
private void overrideStartForResultMethod() {
AbstractJClass postActivityStarterClass = environment.getJClass(PostActivityStarter.class);
JMethod method = holder.getIntentBuilderClass().method(PUBLIC, postActivityStarterClass, "startForResult");
method.annotate(Override.class);
JVar requestCode = method.param(environment.getCodeModel().INT, "requestCode");
JBlock body = method.body();
JConditional condition = null;
if (fragmentSupportField != null) {
condition = body._if(fragmentSupportField.ne(JExpr._null()));
//
condition._then().invoke(fragmentSupportField, "startActivityForResult").arg(intentField).arg(requestCode);
}
if (fragmentField != null) {
if (condition == null) {
condition = body._if(fragmentField.ne(JExpr._null()));
} else {
condition = condition._elseif(fragmentField.ne(JExpr._null()));
}
JBlock fragmentStartForResultInvocationBlock;
if (hasActivityOptionsInFragment() && shouldGuardActivityOptions()) {
fragmentStartForResultInvocationBlock = createCallWithIfGuard(requestCode, condition._then(), fragmentField);
} else {
fragmentStartForResultInvocationBlock = condition._then();
}
JInvocation invocation = //
fragmentStartForResultInvocationBlock.invoke(fragmentField, "startActivityForResult").arg(intentField).arg(requestCode);
if (hasActivityOptionsInFragment()) {
invocation.arg(optionsField);
}
}
JBlock activityStartInvocationBlock = null;
if (condition != null) {
activityStartInvocationBlock = condition._else();
} else {
activityStartInvocationBlock = method.body();
}
JConditional activityCondition = activityStartInvocationBlock._if(contextField._instanceof(getClasses().ACTIVITY));
JBlock thenBlock = activityCondition._then();
JVar activityVar = thenBlock.decl(getClasses().ACTIVITY, "activity", JExpr.cast(getClasses().ACTIVITY, contextField));
if (hasActivityCompatInClasspath() && hasActivityOptionsInActivityCompat()) {
//
thenBlock.staticInvoke(getClasses().ACTIVITY_COMPAT, "startActivityForResult").arg(activityVar).arg(intentField).arg(requestCode).arg(optionsField);
} else if (hasActivityOptionsInFragment()) {
JBlock startForResultInvocationBlock;
if (shouldGuardActivityOptions()) {
startForResultInvocationBlock = createCallWithIfGuard(requestCode, thenBlock, activityVar);
} else {
startForResultInvocationBlock = thenBlock;
}
//
startForResultInvocationBlock.invoke(activityVar, "startActivityForResult").arg(intentField).arg(requestCode).arg(optionsField);
} else {
thenBlock.invoke(activityVar, "startActivityForResult").arg(intentField).arg(requestCode);
}
if (hasActivityOptionsInFragment()) {
JBlock startInvocationBlock;
if (shouldGuardActivityOptions()) {
startInvocationBlock = createCallWithIfGuard(null, activityCondition._else(), contextField);
} else {
startInvocationBlock = activityCondition._else();
}
startInvocationBlock.invoke(contextField, "startActivity").arg(intentField).arg(optionsField);
} else {
activityCondition._else().invoke(contextField, "startActivity").arg(intentField);
}
body._return(_new(postActivityStarterClass).arg(contextField));
}
Aggregations