use of com.helger.jcodemodel.IJExpression in project androidannotations by androidannotations.
the class ViewsByIdHandler method assignValue.
@Override
public void assignValue(JBlock targetBlock, IJAssignmentTarget fieldRef, EComponentWithViewSupportHolder holder, Element element, Element param) {
TypeMirror viewType = extractViewClass(param);
AbstractJClass viewClass = codeModelHelper.typeMirrorToJClass(viewType);
String listName = getListName(element, param);
IJExpression arrayList = instantiateArrayList(viewType, holder, "list_" + listName);
List<JFieldRef> idsRefs = annotationHelper.extractAnnotationFieldRefs(element, IRClass.Res.ID, true);
for (JFieldRef idRef : idsRefs) {
addViewToListIfNotNull(arrayList, viewClass, idRef, holder);
}
targetBlock.add(fieldRef.assign(arrayList));
}
use of com.helger.jcodemodel.IJExpression 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 = targetBlock._if(contextRef._instanceof(extendingContextClass));
cond._then().add(fieldRef.assign(cast(extendingContextClass, contextRef)));
JInvocation warningInvoke = getClasses().LOG.staticInvoke("w");
warningInvoke.arg(logTagForClassHolder(holder));
warningInvoke.arg(lit("Due to Context class ").plus(contextRef.invoke("getClass").invoke("getSimpleName")).plus(lit(", the @RootContext " + extendingContextClass.name() + " won't be populated")));
cond._else().add(warningInvoke);
}
}
use of com.helger.jcodemodel.IJExpression in project androidannotations by androidannotations.
the class SharedPrefHandler method getLocalClassName.
private JMethod getLocalClassName(SharedPrefHolder holder) {
AbstractJClass stringClass = getClasses().STRING;
JMethod getLocalClassName = holder.getGeneratedClass().method(PRIVATE | STATIC, stringClass, "getLocalClassName");
AbstractJClass contextClass = getClasses().CONTEXT;
JVar contextParam = getLocalClassName.param(contextClass, "context");
JBlock body = getLocalClassName.body();
JVar packageName = body.decl(stringClass, "packageName", contextParam.invoke("getPackageName"));
JVar className = body.decl(stringClass, "className", contextParam.invoke("getClass").invoke("getName"));
JVar packageLen = body.decl(getCodeModel().INT, "packageLen", packageName.invoke("length"));
IJExpression condition = //
className.invoke("startsWith").arg(packageName).not().cor(//
className.invoke("length").lte(packageLen)).cor(className.invoke("charAt").arg(packageLen).ne(lit('.')));
body._if(condition)._then()._return(className);
body._return(className.invoke("substring").arg(packageLen.plus(lit(1))));
return getLocalClassName;
}
use of com.helger.jcodemodel.IJExpression 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;
}
use of com.helger.jcodemodel.IJExpression in project androidannotations by androidannotations.
the class PageScrollStateChangedHandler method process.
@Override
public void process(Element element, EComponentWithViewSupportHolder holder) {
String methodName = element.getSimpleName().toString();
ExecutableElement executableElement = (ExecutableElement) element;
List<? extends VariableElement> parameters = executableElement.getParameters();
int stateParameterPosition = -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())) {
stateParameterPosition = i;
} else {
if (isViewPagerParameter(parameterType)) {
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.getPageScrollStateChangedBody();
IJExpression thisRef = holder.getGeneratedClass().staticRef("this");
JInvocation methodCall = methodBody.invoke(thisRef, methodName);
for (int i = 0; i < parameters.size(); i++) {
if (i == stateParameterPosition) {
JVar stateParam = pageChangeHolder.getPageScrollStateChangedStateParam();
methodCall.arg(stateParam);
} else if (i == viewPagerParameterPosition) {
JVar viewParameter = pageChangeHolder.getViewPagerVariable();
methodCall.arg(viewParameter);
}
}
}
}
Aggregations