@Override public CodeBlock render(int sdk) { return CodeBlock.of("target.$L = $T.loadAnimation(context, $L)", name, ANIMATION_UTILS, id.code); } }
@Override public CodeBlock render(int sdk) { if (tintAttributeId.value != NO_RES_ID) { return CodeBlock.of("target.$L = $T.getTintedDrawable(context, $L, $L)", name, UTILS, id.code, tintAttributeId.code); } if (sdk >= 21) { return CodeBlock.of("target.$L = context.getDrawable($L)", name, id.code); } return CodeBlock.of("target.$L = $T.getDrawable(context, $L)", name, CONTEXT_COMPAT, id.code); } }
@Override public CodeBlock render(int sdk) { CodeBlock typeface = sdk >= 26 ? CodeBlock.of("res.getFont($L)", id.code) : CodeBlock.of("$T.getFont(context, $L)", RESOURCES_COMPAT, id.code); if (style != TypefaceStyles.NORMAL) { typeface = CodeBlock.of("$1T.create($2L, $1T.$3L)", TYPEFACE, typeface, style); } return CodeBlock.of("target.$L = $L", name, typeface); } }
@Override public CodeBlock render(int sdk) { ResourceMethod method = type.methodForSdk(sdk); if (method.typeName == null) { if (method.requiresResources) { return CodeBlock.of("target.$L = res.$L($L)", name, method.name, id.code); } return CodeBlock.of("target.$L = context.$L($L)", name, method.name, id.code); } if (method.requiresResources) { return CodeBlock.of("target.$L = $T.$L(res, $L)", name, method.typeName, method.name, id.code); } return CodeBlock.of("target.$L = $T.$L(context, $L)", name, method.typeName, method.name, id.code); } }
@NonNull @Override public CodeBlock visitType(TypeMirror t, Void v) { return CodeBlock.of("$T.class", t); }
@NonNull @Override protected CodeBlock defaultAction(Object o, Void v) { return CodeBlock.of("$L", o); }
@NonNull @Override public CodeBlock visitString(String s, Void v) { return CodeBlock.of("$S", s); }
@NonNull @Override public CodeBlock visitEnumConstant(@NonNull VariableElement c, Void v) { return CodeBlock.of("$T.$L", c.asType(), c.getSimpleName()); }
@Override public void exitWhen(Rule rule) { final CodeBlock result = codeSnippet.getOrDefault(rule.when(), CodeBlock.of("$$when")); when.addStatement("return $L", result); classFile.addMethod(when.build()); // sanity to catch errors earlier currentMethod = null; }
Id(int value, @Nullable Symbol rSymbol) { this.value = value; if (rSymbol != null) { ClassName className = ClassName.get(rSymbol.packge().getQualifiedName().toString(), R, rSymbol.enclClass().name.toString()); String resourceName = rSymbol.name.toString(); this.code = className.topLevelClassName().equals(ANDROID_R) ? CodeBlock.of("$L.$N", className, resourceName) : CodeBlock.of("$T.$N", className, resourceName); this.qualifed = true; } else { this.code = CodeBlock.of("$L", value); this.qualifed = false; } }
@Override public CodeBlock generateFactoryMethodsComponentInstance(SpecModel specModel) { return CodeBlock.of( "$L instance = new $L(context.getAndroidContext());\n", specModel.getComponentName(), specModel.getComponentName()); }
@Override public void exitNot(NotExpression expr) { final CodeBlock right = codeSnippet.get(expr.right()); codeSnippet.put(expr, CodeBlock.of("!$L", blockOrMissing(right, expr.right()))); }
@Override public void exitDouble(DoubleExpression expr) { // double should have a suffix codeSnippet.putIfAbsent(expr, CodeBlock.of("$Ld", expr.evaluateUnsafe())); }
@Override public void exitString(StringExpression expr) { // this overrides what exitConstant would do for stringsā¦ codeSnippet.putIfAbsent(expr, CodeBlock.of("$S", expr.evaluateUnsafe())); }
@Override public void exitMessageRef(MessageRefExpression expr) { final Object field = blockOrMissing(codeSnippet.get(expr.getFieldExpr()), expr.getFieldExpr()); codeSnippet.putIfAbsent(expr, CodeBlock.of("context.currentMessage().getField($S)", field)); }
@NonNull @Override public CodeBlock visitArray(@NonNull List<? extends AnnotationValue> values, Void v) { ArrayTypeName arrayTypeName = (ArrayTypeName) type; if (arrayTypeName.componentType instanceof ParameterizedTypeName) { arrayTypeName = ArrayTypeName.of(((ParameterizedTypeName) arrayTypeName.componentType).rawType); } return CodeBlock.of("new $T{$L}", arrayTypeName, values.stream().map(value -> value.accept(this, null)) .reduce((c1, c2) -> CodeBlock.builder().add(c1).add(", ").add(c2).build()).orElseGet(() -> CodeBlock.builder().build())); } }
@Override public void exitAnd(AndExpression expr) { final CodeBlock left = codeSnippet.get(expr.left()); final CodeBlock right = codeSnippet.get(expr.right()); codeSnippet.put(expr, CodeBlock.of("($L && $L)", blockOrMissing(left, expr.left()), blockOrMissing(right, expr.right()))); }
CodeBlock codeToSetDefaultValue() { if (defaultAttribute == null || defaultAttribute.getCodeToSetDefault().isEmpty()) { throw new IllegalStateException("No default value exists"); } return CodeBlock .of(defaultAttribute.setterCode(), defaultAttribute.getCodeToSetDefault().value()); } }
@Override public void exitSigned(SignedExpression expr) { final Object rightBlock = blockOrMissing(codeSnippet.get(expr.right()), expr.right()); codeSnippet.putIfAbsent(expr, CodeBlock.of((expr.isPlus() ? "+" : "-") + "$L", rightBlock)); }
@Override public void addToBuildMethod(@NonNull BuildMethodCreator method) { if (getDefaultValue() == null) { method.addNotUnset(getName(), getType()); } if (hasMarker(Types.ANY_NON_DEFAULT)) { method.addAnyNonDefault(getName(), CodeBlock.of("null")); } } }