diff --git a/paris-processor/src/main/java/com/airbnb/paris/processor/StyleAppliersWriter.kt b/paris-processor/src/main/java/com/airbnb/paris/processor/StyleAppliersWriter.kt index 33b04f01..141b482c 100644 --- a/paris-processor/src/main/java/com/airbnb/paris/processor/StyleAppliersWriter.kt +++ b/paris-processor/src/main/java/com/airbnb/paris/processor/StyleAppliersWriter.kt @@ -10,13 +10,8 @@ import javax.annotation.processing.* import javax.lang.model.element.* import javax.lang.model.type.* import javax.lang.model.util.* -import kotlin.Boolean -import kotlin.String -import kotlin.apply import kotlin.check -import kotlin.let -// TODO Add @UiThread annotation to StyleApplier classes internal object StyleAppliersWriter { val styleablesTree = StyleablesTree() @@ -40,6 +35,7 @@ internal object StyleAppliersWriter { val styleApplierClassName = styleableInfo.styleApplierClassName() val styleTypeBuilder = TypeSpec.classBuilder(styleApplierClassName) + .addAnnotation(ClassNames.ANDROID_UI_THREAD) .addModifiers(Modifier.PUBLIC, Modifier.FINAL) .superclass(ParameterizedTypeName.get(ParisProcessor.STYLE_APPLIER_CLASS_NAME, TypeName.get(styleableInfo.elementType), TypeName.get(styleableInfo.viewElementType))) .addMethod(buildConstructorMethod(styleableInfo)) @@ -297,6 +293,7 @@ internal object StyleAppliersWriter { // StyleBuilder inner class val styleBuilderClassName = styleApplierClassName.nestedClass("StyleBuilder") val styleBuilderTypeBuilder = TypeSpec.classBuilder(styleBuilderClassName) + .addAnnotation(ClassNames.ANDROID_UI_THREAD) .addModifiers(Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) .superclass(ParameterizedTypeName.get(baseClassName, styleBuilderClassName, styleApplierClassName)) .addMethod(buildStyleBuilderApplierConstructorMethod(styleApplierClassName)) @@ -338,10 +335,10 @@ internal object StyleAppliersWriter { StyleInfo.Kind.FIELD -> builder.addStatement("add(\$T.\$L)", styleInfo.enclosingElement, styleInfo.elementName) StyleInfo.Kind.METHOD -> { builder - .addStatement("consumeSimpleStyleBuilder()") + .addStatement("consumeProgrammaticStyleBuilder()") .addStatement("debugName(\$S)", styleInfo.formattedName) .addStatement("\$T.\$L(this)", styleInfo.enclosingElement, styleInfo.elementName) - .addStatement("consumeSimpleStyleBuilder()") + .addStatement("consumeProgrammaticStyleBuilder()") } StyleInfo.Kind.STYLE_RES -> { builder.addStatement("add(\$L)", styleInfo.styleResourceCode) diff --git a/paris-processor/src/main/java/com/airbnb/paris/processor/utils/ClassNames.kt b/paris-processor/src/main/java/com/airbnb/paris/processor/utils/ClassNames.kt index c488bb3c..3071e66c 100644 --- a/paris-processor/src/main/java/com/airbnb/paris/processor/utils/ClassNames.kt +++ b/paris-processor/src/main/java/com/airbnb/paris/processor/utils/ClassNames.kt @@ -17,4 +17,5 @@ internal object ClassNames { val ANDROID_INTEGER_RES = "android.support.annotation.IntegerRes".className() val ANDROID_STRING_RES = "android.support.annotation.StringRes".className() val ANDROID_STYLE_RES = "android.support.annotation.StyleRes".className() + val ANDROID_UI_THREAD = "android.support.annotation.UiThread".className() } diff --git a/paris-test/src/test/resources/attrs/MyViewStyleApplier.java b/paris-test/src/test/resources/attrs/MyViewStyleApplier.java index 282be89c..65877b07 100644 --- a/paris-test/src/test/resources/attrs/MyViewStyleApplier.java +++ b/paris-test/src/test/resources/attrs/MyViewStyleApplier.java @@ -12,6 +12,7 @@ import android.support.annotation.FractionRes; import android.support.annotation.IntegerRes; import android.support.annotation.StringRes; +import android.support.annotation.UiThread; import com.airbnb.paris.Style; import com.airbnb.paris.StyleApplier; import com.airbnb.paris.TypedArrayWrapper; @@ -20,256 +21,258 @@ import java.lang.Override; import java.lang.String; +@UiThread public final class MyViewStyleApplier extends StyleApplier { - public MyViewStyleApplier(MyView view) { - super(view); - } - - @Override - protected void applyParent(Style style) { - new ViewProxyStyleApplier(getView()).apply(style); - } - - @Override - protected int[] attributes() { - return R.styleable.Formats; - } - - @Override - protected void processStyleableFields(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - } - - @Override - protected void processAttributes(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - if (a.hasValue(R.styleable.Formats_formatBoolean)) { - getProxy().formatBoolean(a.getBoolean(R.styleable.Formats_formatBoolean, false)); - } - if (a.hasValue(R.styleable.Formats_formatBoolean)) { - getProxy().formatBoolean(a.getResourceId(R.styleable.Formats_formatBoolean, -1)); - } - if (a.hasValue(R.styleable.Formats_formatBoolean2)) { - getProxy().formatBoolean2(a.getResourceId(R.styleable.Formats_formatBoolean2, -1)); - } - if (a.hasValue(R.styleable.Formats_formatColor)) { - getProxy().formatColor(a.getColor(R.styleable.Formats_formatColor, -1)); - } - if (a.hasValue(R.styleable.Formats_formatDimension)) { - getProxy().formatDimension_res(a.getResourceId(R.styleable.Formats_formatDimension, -1)); - } - if (a.hasValue(R.styleable.Formats_formatDimension)) { - getProxy().formatDimension_px(a.getDimensionPixelSize(R.styleable.Formats_formatDimension, -1)); - } - if (a.hasValue(R.styleable.Formats_formatDimension)) { - getProxy().formatDimension_LayoutDimension(a.getLayoutDimension(R.styleable.Formats_formatDimension, -1)); - } - if (a.hasValue(R.styleable.Formats_formatEnum)) { - getProxy().formatEnum(a.getInt(R.styleable.Formats_formatEnum, -1)); - } - if (a.hasValue(R.styleable.Formats_formatFlag)) { - getProxy().formatFlag(a.getInt(R.styleable.Formats_formatFlag, -1)); - } - if (a.hasValue(R.styleable.Formats_formatFloat)) { - getProxy().formatFloat(a.getFloat(R.styleable.Formats_formatFloat, -1f)); - } - if (a.hasValue(R.styleable.Formats_formatFraction)) { - getProxy().formatFraction(a.getFraction(R.styleable.Formats_formatFraction, 2, 3, -1f)); - } - if (a.hasValue(R.styleable.Formats_formatInteger)) { - getProxy().formatInteger(a.getInt(R.styleable.Formats_formatInteger, -1)); - } - if (a.hasValue(R.styleable.Formats_formatReference)) { - getProxy().formatReference_CharSequenceArray(a.getTextArray(R.styleable.Formats_formatReference)); - } - if (a.hasValue(R.styleable.Formats_formatReference)) { - getProxy().formatReference_res(a.getResourceId(R.styleable.Formats_formatReference, -1)); - } - if (a.hasValue(R.styleable.Formats_formatReference2)) { - getProxy().formatReference2_ColorStateList(a.getColorStateList(R.styleable.Formats_formatReference2)); - } - if (a.hasValue(R.styleable.Formats_formatReference3)) { - getProxy().formatReference3_Drawable(a.getDrawable(R.styleable.Formats_formatReference3)); - } - if (a.hasValue(R.styleable.Formats_formatString)) { - getProxy().formatString_CharSequence(a.getText(R.styleable.Formats_formatString)); - } - if (a.hasValue(R.styleable.Formats_formatString2)) { - getProxy().formatString2_String(a.getString(R.styleable.Formats_formatString2)); - } - } - - public StyleBuilder builder() { - return new StyleBuilder(this); - } - - public void applyDefault() { - } - - public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { - public BaseStyleBuilder(A applier) { - super(applier); - } - - public BaseStyleBuilder() { - } - - public B formatBoolean(boolean value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatBoolean], value); - return (B) this; - } - - public B formatBoolean(@BoolRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean], resId); - return (B) this; - } - - public B formatBoolean2(@AnyRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean2], resId); - return (B) this; - } - - public B formatColor(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatColor], value); - return (B) this; - } - - public B formatColorRes(@ColorRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatColor], resId); - return (B) this; - } - - public B formatDimension(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatDimension], value); - return (B) this; - } - - public B formatDimensionRes(@DimenRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatDimension], resId); - return (B) this; - } - - public B formatDimensionDp(int value) { - getBuilder().putDp(R.styleable.Formats[R.styleable.Formats_formatDimension], value); - return (B) this; - } - - public B formatEnum(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatEnum], value); - return (B) this; - } - - public B formatEnumRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatEnum], resId); - return (B) this; - } - - public B formatFlag(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFlag], value); - return (B) this; - } - - public B formatFlagRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFlag], resId); - return (B) this; - } - - public B formatFloat(float value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFloat], value); - return (B) this; - } - - public B formatFloat(@AnyRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFloat], resId); - return (B) this; - } - - public B formatFraction(float value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFraction], value); - return (B) this; - } - - public B formatFraction(@FractionRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFraction], resId); - return (B) this; - } - - public B formatInteger(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatInteger], value); - return (B) this; - } - - public B formatIntegerRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatInteger], resId); - return (B) this; - } - - public B formatReference(CharSequence[] value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference], value); - return (B) this; - } - - public B formatReference(@ArrayRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference], resId); - return (B) this; - } - - public B formatReference2(ColorStateList value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference2], value); - return (B) this; - } - - public B formatReference2(@ColorRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference2], resId); - return (B) this; - } - - public B formatReference3(Drawable value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference3], value); - return (B) this; - } - - public B formatReference3(@DrawableRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference3], resId); - return (B) this; - } - - public B formatString(CharSequence value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString], value); - return (B) this; - } - - public B formatString(@StringRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString], resId); - return (B) this; - } - - public B formatString2(String value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString2], value); - return (B) this; - } - - public B formatString2(@StringRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString2], resId); - return (B) this; - } - - public B applyTo(MyView view) { - new MyViewStyleApplier(view).apply(build()); - return (B) this; - } - } - - public static final class StyleBuilder extends BaseStyleBuilder { - public StyleBuilder(MyViewStyleApplier applier) { - super(applier); - } - - public StyleBuilder() { - } - - public StyleBuilder addDefault() { - return this; - } + public MyViewStyleApplier(MyView view) { + super(view); + } + + @Override + protected void applyParent(Style style) { + new ViewProxyStyleApplier(getView()).apply(style); + } + + @Override + protected int[] attributes() { + return R.styleable.Formats; + } + + @Override + protected void processStyleableFields(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + } + + @Override + protected void processAttributes(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + if (a.hasValue(R.styleable.Formats_formatBoolean)) { + getProxy().formatBoolean(a.getBoolean(R.styleable.Formats_formatBoolean, false)); } + if (a.hasValue(R.styleable.Formats_formatBoolean)) { + getProxy().formatBoolean(a.getResourceId(R.styleable.Formats_formatBoolean, -1)); + } + if (a.hasValue(R.styleable.Formats_formatBoolean2)) { + getProxy().formatBoolean2(a.getResourceId(R.styleable.Formats_formatBoolean2, -1)); + } + if (a.hasValue(R.styleable.Formats_formatColor)) { + getProxy().formatColor(a.getColor(R.styleable.Formats_formatColor, -1)); + } + if (a.hasValue(R.styleable.Formats_formatDimension)) { + getProxy().formatDimension_res(a.getResourceId(R.styleable.Formats_formatDimension, -1)); + } + if (a.hasValue(R.styleable.Formats_formatDimension)) { + getProxy().formatDimension_px(a.getDimensionPixelSize(R.styleable.Formats_formatDimension, -1)); + } + if (a.hasValue(R.styleable.Formats_formatDimension)) { + getProxy().formatDimension_LayoutDimension(a.getLayoutDimension(R.styleable.Formats_formatDimension, -1)); + } + if (a.hasValue(R.styleable.Formats_formatEnum)) { + getProxy().formatEnum(a.getInt(R.styleable.Formats_formatEnum, -1)); + } + if (a.hasValue(R.styleable.Formats_formatFlag)) { + getProxy().formatFlag(a.getInt(R.styleable.Formats_formatFlag, -1)); + } + if (a.hasValue(R.styleable.Formats_formatFloat)) { + getProxy().formatFloat(a.getFloat(R.styleable.Formats_formatFloat, -1f)); + } + if (a.hasValue(R.styleable.Formats_formatFraction)) { + getProxy().formatFraction(a.getFraction(R.styleable.Formats_formatFraction, 2, 3, -1f)); + } + if (a.hasValue(R.styleable.Formats_formatInteger)) { + getProxy().formatInteger(a.getInt(R.styleable.Formats_formatInteger, -1)); + } + if (a.hasValue(R.styleable.Formats_formatReference)) { + getProxy().formatReference_CharSequenceArray(a.getTextArray(R.styleable.Formats_formatReference)); + } + if (a.hasValue(R.styleable.Formats_formatReference)) { + getProxy().formatReference_res(a.getResourceId(R.styleable.Formats_formatReference, -1)); + } + if (a.hasValue(R.styleable.Formats_formatReference2)) { + getProxy().formatReference2_ColorStateList(a.getColorStateList(R.styleable.Formats_formatReference2)); + } + if (a.hasValue(R.styleable.Formats_formatReference3)) { + getProxy().formatReference3_Drawable(a.getDrawable(R.styleable.Formats_formatReference3)); + } + if (a.hasValue(R.styleable.Formats_formatString)) { + getProxy().formatString_CharSequence(a.getText(R.styleable.Formats_formatString)); + } + if (a.hasValue(R.styleable.Formats_formatString2)) { + getProxy().formatString2_String(a.getString(R.styleable.Formats_formatString2)); + } + } + + public StyleBuilder builder() { + return new StyleBuilder(this); + } + + public void applyDefault() { + } + + public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { + public BaseStyleBuilder(A applier) { + super(applier); + } + + public BaseStyleBuilder() { + } + + public B formatBoolean(boolean value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatBoolean], value); + return (B) this; + } + + public B formatBoolean(@BoolRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean], resId); + return (B) this; + } + + public B formatBoolean2(@AnyRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean2], resId); + return (B) this; + } + + public B formatColor(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatColor], value); + return (B) this; + } + + public B formatColorRes(@ColorRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatColor], resId); + return (B) this; + } + + public B formatDimension(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatDimension], value); + return (B) this; + } + + public B formatDimensionRes(@DimenRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatDimension], resId); + return (B) this; + } + + public B formatDimensionDp(int value) { + getBuilder().putDp(R.styleable.Formats[R.styleable.Formats_formatDimension], value); + return (B) this; + } + + public B formatEnum(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatEnum], value); + return (B) this; + } + + public B formatEnumRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatEnum], resId); + return (B) this; + } + + public B formatFlag(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFlag], value); + return (B) this; + } + + public B formatFlagRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFlag], resId); + return (B) this; + } + + public B formatFloat(float value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFloat], value); + return (B) this; + } + + public B formatFloat(@AnyRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFloat], resId); + return (B) this; + } + + public B formatFraction(float value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFraction], value); + return (B) this; + } + + public B formatFraction(@FractionRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFraction], resId); + return (B) this; + } + + public B formatInteger(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatInteger], value); + return (B) this; + } + + public B formatIntegerRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatInteger], resId); + return (B) this; + } + + public B formatReference(CharSequence[] value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference], value); + return (B) this; + } + + public B formatReference(@ArrayRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference], resId); + return (B) this; + } + + public B formatReference2(ColorStateList value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference2], value); + return (B) this; + } + + public B formatReference2(@ColorRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference2], resId); + return (B) this; + } + + public B formatReference3(Drawable value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference3], value); + return (B) this; + } + + public B formatReference3(@DrawableRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference3], resId); + return (B) this; + } + + public B formatString(CharSequence value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString], value); + return (B) this; + } + + public B formatString(@StringRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString], resId); + return (B) this; + } + + public B formatString2(String value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString2], value); + return (B) this; + } + + public B formatString2(@StringRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString2], resId); + return (B) this; + } + + public B applyTo(MyView view) { + new MyViewStyleApplier(view).apply(build()); + return (B) this; + } + } + + @UiThread + public static final class StyleBuilder extends BaseStyleBuilder { + public StyleBuilder(MyViewStyleApplier applier) { + super(applier); + } + + public StyleBuilder() { + } + + public StyleBuilder addDefault() { + return this; + } + } } \ No newline at end of file diff --git a/paris-test/src/test/resources/default_values/MyViewStyleApplier.java b/paris-test/src/test/resources/default_values/MyViewStyleApplier.java index 99e8e502..ff6a1f9a 100644 --- a/paris-test/src/test/resources/default_values/MyViewStyleApplier.java +++ b/paris-test/src/test/resources/default_values/MyViewStyleApplier.java @@ -12,6 +12,7 @@ import android.support.annotation.FractionRes; import android.support.annotation.IntegerRes; import android.support.annotation.StringRes; +import android.support.annotation.UiThread; import com.airbnb.paris.Style; import com.airbnb.paris.StyleApplier; import com.airbnb.paris.TypedArrayWrapper; @@ -21,292 +22,294 @@ import java.lang.Override; import java.lang.String; +@UiThread public final class MyViewStyleApplier extends StyleApplier { - public MyViewStyleApplier(MyView view) { - super(view); - } - - @Override - protected void applyParent(Style style) { - new ViewProxyStyleApplier(getView()).apply(style); - } - - @Override - protected int[] attributes() { - return R.styleable.Formats; - } - - @Override - public int[] attributesWithDefaultValue() { - return new int[] {R.styleable.Formats_formatBoolean,R.styleable.Formats_formatColor,R.styleable.Formats_formatDimension,R.styleable.Formats_formatEnum,R.styleable.Formats_formatFlag,R.styleable.Formats_formatFloat,R.styleable.Formats_formatFraction,R.styleable.Formats_formatInteger,R.styleable.Formats_formatReference,R.styleable.Formats_formatReference2,R.styleable.Formats_formatReference3,R.styleable.Formats_formatString,R.styleable.Formats_formatString2,}; - } - - @Override - protected void processStyleableFields(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - } - - @Override - protected void processAttributes(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - if (a.hasValue(R.styleable.Formats_formatBoolean)) { - getProxy().formatBoolean(a.getBoolean(R.styleable.Formats_formatBoolean, false)); - } - else { - getProxy().formatBoolean(res.getBoolean(R.bool.format_boolean)); - } - if (a.hasValue(R.styleable.Formats_formatColor)) { - getProxy().formatColor(a.getColor(R.styleable.Formats_formatColor, -1)); - } - else { - getProxy().formatColor(res.getColor(R.color.format_color)); - } - if (a.hasValue(R.styleable.Formats_formatDimension)) { - getProxy().formatDimension_px(a.getDimensionPixelSize(R.styleable.Formats_formatDimension, -1)); - } - else { - getProxy().formatDimension_px(res.getDimensionPixelSize(R.dimen.format_dimension)); - } - if (a.hasValue(R.styleable.Formats_formatDimension)) { - getProxy().formatDimension_LayoutDimension(a.getLayoutDimension(R.styleable.Formats_formatDimension, -1)); - } - else { - getProxy().formatDimension_LayoutDimension(ResourcesExtensionsKt.getLayoutDimension(res, R.dimen.format_dimension)); - } - if (a.hasValue(R.styleable.Formats_formatEnum)) { - getProxy().formatEnum(a.getInt(R.styleable.Formats_formatEnum, -1)); - } - else { - getProxy().formatEnum(res.getInteger(R.integer.format_enum)); - } - if (a.hasValue(R.styleable.Formats_formatFlag)) { - getProxy().formatFlag(a.getInt(R.styleable.Formats_formatFlag, -1)); - } - else { - getProxy().formatFlag(res.getInteger(R.integer.format_flag)); - } - if (a.hasValue(R.styleable.Formats_formatFloat)) { - getProxy().formatFloat(a.getFloat(R.styleable.Formats_formatFloat, -1f)); - } - else { - getProxy().formatFloat(ResourcesExtensionsKt.getFloat(res, R.dimen.format_float)); - } - if (a.hasValue(R.styleable.Formats_formatFraction)) { - getProxy().formatFraction(a.getFraction(R.styleable.Formats_formatFraction, 2, 3, -1f)); - } - else { - getProxy().formatFraction(res.getFraction(R.fraction.format_fraction, 2, 3)); - } - if (a.hasValue(R.styleable.Formats_formatInteger)) { - getProxy().formatInteger(a.getInt(R.styleable.Formats_formatInteger, -1)); - } - else { - getProxy().formatInteger(res.getInteger(R.integer.format_integer)); - } - if (a.hasValue(R.styleable.Formats_formatReference)) { - getProxy().formatReference_CharSequenceArray(a.getTextArray(R.styleable.Formats_formatReference)); - } - else { - getProxy().formatReference_CharSequenceArray(res.getTextArray(R.array.format_string_array)); - } - if (a.hasValue(R.styleable.Formats_formatReference)) { - getProxy().formatReference_res(a.getResourceId(R.styleable.Formats_formatReference, -1)); - } - else { - getProxy().formatReference_res(R.bool.active); - } - if (a.hasValue(R.styleable.Formats_formatReference2)) { - getProxy().formatReference_ColorStateList(a.getColorStateList(R.styleable.Formats_formatReference2)); - } - else { - getProxy().formatReference_ColorStateList(res.getColorStateList(R.color.format_color_state_list)); - } - if (a.hasValue(R.styleable.Formats_formatReference3)) { - getProxy().formatReference_Drawable(a.getDrawable(R.styleable.Formats_formatReference3)); - } - else { - getProxy().formatReference_Drawable(res.getDrawable(R.drawable.format_drawable)); - } - if (a.hasValue(R.styleable.Formats_formatString)) { - getProxy().formatString_CharSequence(a.getText(R.styleable.Formats_formatString)); - } - else { - getProxy().formatString_CharSequence(res.getText(R.string.format_char_sequence)); - } - if (a.hasValue(R.styleable.Formats_formatString2)) { - getProxy().formatString_String(a.getString(R.styleable.Formats_formatString2)); - } - else { - getProxy().formatString_String(res.getString(R.string.format_string)); - } - } - - public StyleBuilder builder() { - return new StyleBuilder(this); - } - - public void applyDefault() { - } - - public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { - public BaseStyleBuilder(A applier) { - super(applier); - } - - public BaseStyleBuilder() { - } - - public B formatBoolean(boolean value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatBoolean], value); - return (B) this; - } - - public B formatBoolean(@BoolRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean], resId); - return (B) this; - } - - public B formatColor(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatColor], value); - return (B) this; - } - - public B formatColorRes(@ColorRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatColor], resId); - return (B) this; - } - - public B formatDimension(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatDimension], value); - return (B) this; - } - - public B formatDimensionRes(@DimenRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatDimension], resId); - return (B) this; - } - - public B formatDimensionDp(int value) { - getBuilder().putDp(R.styleable.Formats[R.styleable.Formats_formatDimension], value); - return (B) this; - } - - public B formatEnum(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatEnum], value); - return (B) this; - } - - public B formatEnumRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatEnum], resId); - return (B) this; - } - - public B formatFlag(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFlag], value); - return (B) this; - } - - public B formatFlagRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFlag], resId); - return (B) this; - } - - public B formatFloat(float value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFloat], value); - return (B) this; - } - - public B formatFloat(@AnyRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFloat], resId); - return (B) this; - } - - public B formatFraction(float value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFraction], value); - return (B) this; - } - - public B formatFraction(@FractionRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFraction], resId); - return (B) this; - } - - public B formatInteger(int value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatInteger], value); - return (B) this; - } - - public B formatIntegerRes(@IntegerRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatInteger], resId); - return (B) this; - } - - public B formatReference(CharSequence[] value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference], value); - return (B) this; - } - - public B formatReference(@ArrayRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference], resId); - return (B) this; - } - - public B formatReference2(ColorStateList value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference2], value); - return (B) this; - } - - public B formatReference2(@ColorRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference2], resId); - return (B) this; - } - - public B formatReference3(Drawable value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference3], value); - return (B) this; - } - - public B formatReference3(@DrawableRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference3], resId); - return (B) this; - } - - public B formatString(CharSequence value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString], value); - return (B) this; - } - - public B formatString(@StringRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString], resId); - return (B) this; - } - - public B formatString2(String value) { - getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString2], value); - return (B) this; - } - - public B formatString2(@StringRes int resId) { - getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString2], resId); - return (B) this; - } - - public B applyTo(MyView view) { - new MyViewStyleApplier(view).apply(build()); - return (B) this; - } - } - - public static final class StyleBuilder extends BaseStyleBuilder { - public StyleBuilder(MyViewStyleApplier applier) { - super(applier); - } - - public StyleBuilder() { - } - - public StyleBuilder addDefault() { - return this; - } + public MyViewStyleApplier(MyView view) { + super(view); + } + + @Override + protected void applyParent(Style style) { + new ViewProxyStyleApplier(getView()).apply(style); + } + + @Override + protected int[] attributes() { + return R.styleable.Formats; + } + + @Override + public int[] attributesWithDefaultValue() { + return new int[] {R.styleable.Formats_formatBoolean,R.styleable.Formats_formatColor,R.styleable.Formats_formatDimension,R.styleable.Formats_formatEnum,R.styleable.Formats_formatFlag,R.styleable.Formats_formatFloat,R.styleable.Formats_formatFraction,R.styleable.Formats_formatInteger,R.styleable.Formats_formatReference,R.styleable.Formats_formatReference2,R.styleable.Formats_formatReference3,R.styleable.Formats_formatString,R.styleable.Formats_formatString2,}; + } + + @Override + protected void processStyleableFields(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + } + + @Override + protected void processAttributes(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + if (a.hasValue(R.styleable.Formats_formatBoolean)) { + getProxy().formatBoolean(a.getBoolean(R.styleable.Formats_formatBoolean, false)); } + else { + getProxy().formatBoolean(res.getBoolean(R.bool.format_boolean)); + } + if (a.hasValue(R.styleable.Formats_formatColor)) { + getProxy().formatColor(a.getColor(R.styleable.Formats_formatColor, -1)); + } + else { + getProxy().formatColor(res.getColor(R.color.format_color)); + } + if (a.hasValue(R.styleable.Formats_formatDimension)) { + getProxy().formatDimension_px(a.getDimensionPixelSize(R.styleable.Formats_formatDimension, -1)); + } + else { + getProxy().formatDimension_px(res.getDimensionPixelSize(R.dimen.format_dimension)); + } + if (a.hasValue(R.styleable.Formats_formatDimension)) { + getProxy().formatDimension_LayoutDimension(a.getLayoutDimension(R.styleable.Formats_formatDimension, -1)); + } + else { + getProxy().formatDimension_LayoutDimension(ResourcesExtensionsKt.getLayoutDimension(res, R.dimen.format_dimension)); + } + if (a.hasValue(R.styleable.Formats_formatEnum)) { + getProxy().formatEnum(a.getInt(R.styleable.Formats_formatEnum, -1)); + } + else { + getProxy().formatEnum(res.getInteger(R.integer.format_enum)); + } + if (a.hasValue(R.styleable.Formats_formatFlag)) { + getProxy().formatFlag(a.getInt(R.styleable.Formats_formatFlag, -1)); + } + else { + getProxy().formatFlag(res.getInteger(R.integer.format_flag)); + } + if (a.hasValue(R.styleable.Formats_formatFloat)) { + getProxy().formatFloat(a.getFloat(R.styleable.Formats_formatFloat, -1f)); + } + else { + getProxy().formatFloat(ResourcesExtensionsKt.getFloat(res, R.dimen.format_float)); + } + if (a.hasValue(R.styleable.Formats_formatFraction)) { + getProxy().formatFraction(a.getFraction(R.styleable.Formats_formatFraction, 2, 3, -1f)); + } + else { + getProxy().formatFraction(res.getFraction(R.fraction.format_fraction, 2, 3)); + } + if (a.hasValue(R.styleable.Formats_formatInteger)) { + getProxy().formatInteger(a.getInt(R.styleable.Formats_formatInteger, -1)); + } + else { + getProxy().formatInteger(res.getInteger(R.integer.format_integer)); + } + if (a.hasValue(R.styleable.Formats_formatReference)) { + getProxy().formatReference_CharSequenceArray(a.getTextArray(R.styleable.Formats_formatReference)); + } + else { + getProxy().formatReference_CharSequenceArray(res.getTextArray(R.array.format_string_array)); + } + if (a.hasValue(R.styleable.Formats_formatReference)) { + getProxy().formatReference_res(a.getResourceId(R.styleable.Formats_formatReference, -1)); + } + else { + getProxy().formatReference_res(R.bool.active); + } + if (a.hasValue(R.styleable.Formats_formatReference2)) { + getProxy().formatReference_ColorStateList(a.getColorStateList(R.styleable.Formats_formatReference2)); + } + else { + getProxy().formatReference_ColorStateList(res.getColorStateList(R.color.format_color_state_list)); + } + if (a.hasValue(R.styleable.Formats_formatReference3)) { + getProxy().formatReference_Drawable(a.getDrawable(R.styleable.Formats_formatReference3)); + } + else { + getProxy().formatReference_Drawable(res.getDrawable(R.drawable.format_drawable)); + } + if (a.hasValue(R.styleable.Formats_formatString)) { + getProxy().formatString_CharSequence(a.getText(R.styleable.Formats_formatString)); + } + else { + getProxy().formatString_CharSequence(res.getText(R.string.format_char_sequence)); + } + if (a.hasValue(R.styleable.Formats_formatString2)) { + getProxy().formatString_String(a.getString(R.styleable.Formats_formatString2)); + } + else { + getProxy().formatString_String(res.getString(R.string.format_string)); + } + } + + public StyleBuilder builder() { + return new StyleBuilder(this); + } + + public void applyDefault() { + } + + public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { + public BaseStyleBuilder(A applier) { + super(applier); + } + + public BaseStyleBuilder() { + } + + public B formatBoolean(boolean value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatBoolean], value); + return (B) this; + } + + public B formatBoolean(@BoolRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatBoolean], resId); + return (B) this; + } + + public B formatColor(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatColor], value); + return (B) this; + } + + public B formatColorRes(@ColorRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatColor], resId); + return (B) this; + } + + public B formatDimension(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatDimension], value); + return (B) this; + } + + public B formatDimensionRes(@DimenRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatDimension], resId); + return (B) this; + } + + public B formatDimensionDp(int value) { + getBuilder().putDp(R.styleable.Formats[R.styleable.Formats_formatDimension], value); + return (B) this; + } + + public B formatEnum(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatEnum], value); + return (B) this; + } + + public B formatEnumRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatEnum], resId); + return (B) this; + } + + public B formatFlag(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFlag], value); + return (B) this; + } + + public B formatFlagRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFlag], resId); + return (B) this; + } + + public B formatFloat(float value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFloat], value); + return (B) this; + } + + public B formatFloat(@AnyRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFloat], resId); + return (B) this; + } + + public B formatFraction(float value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatFraction], value); + return (B) this; + } + + public B formatFraction(@FractionRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatFraction], resId); + return (B) this; + } + + public B formatInteger(int value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatInteger], value); + return (B) this; + } + + public B formatIntegerRes(@IntegerRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatInteger], resId); + return (B) this; + } + + public B formatReference(CharSequence[] value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference], value); + return (B) this; + } + + public B formatReference(@ArrayRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference], resId); + return (B) this; + } + + public B formatReference2(ColorStateList value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference2], value); + return (B) this; + } + + public B formatReference2(@ColorRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference2], resId); + return (B) this; + } + + public B formatReference3(Drawable value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatReference3], value); + return (B) this; + } + + public B formatReference3(@DrawableRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatReference3], resId); + return (B) this; + } + + public B formatString(CharSequence value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString], value); + return (B) this; + } + + public B formatString(@StringRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString], resId); + return (B) this; + } + + public B formatString2(String value) { + getBuilder().put(R.styleable.Formats[R.styleable.Formats_formatString2], value); + return (B) this; + } + + public B formatString2(@StringRes int resId) { + getBuilder().putRes(R.styleable.Formats[R.styleable.Formats_formatString2], resId); + return (B) this; + } + + public B applyTo(MyView view) { + new MyViewStyleApplier(view).apply(build()); + return (B) this; + } + } + + @UiThread + public static final class StyleBuilder extends BaseStyleBuilder { + public StyleBuilder(MyViewStyleApplier applier) { + super(applier); + } + + public StyleBuilder() { + } + + public StyleBuilder addDefault() { + return this; + } + } } \ No newline at end of file diff --git a/paris-test/src/test/resources/styleable_fields/MyViewStyleApplier.java b/paris-test/src/test/resources/styleable_fields/MyViewStyleApplier.java index 4cbbdc75..58f42605 100644 --- a/paris-test/src/test/resources/styleable_fields/MyViewStyleApplier.java +++ b/paris-test/src/test/resources/styleable_fields/MyViewStyleApplier.java @@ -2,6 +2,7 @@ import android.content.res.Resources; import android.support.annotation.StyleRes; +import android.support.annotation.UiThread; import com.airbnb.paris.Style; import com.airbnb.paris.StyleApplier; import com.airbnb.paris.TypedArrayWrapper; @@ -10,141 +11,143 @@ import com.airbnb.paris.utils.StyleBuilderFunction; import java.lang.Override; +@UiThread public final class MyViewStyleApplier extends StyleApplier { - public MyViewStyleApplier(MyView view) { - super(view); + public MyViewStyleApplier(MyView view) { + super(view); + } + + @Override + protected void applyParent(Style style) { + new ViewProxyStyleApplier(getView()).apply(style); + } + + @Override + protected int[] attributes() { + return R.styleable.MyView; + } + + @Override + protected void processStyleableFields(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + Style subStyle; + if (a.hasValue(R.styleable.MyView_titleStyle)) { + subStyle = a.getStyle(R.styleable.MyView_titleStyle); + subStyle.setDebugListener(style.getDebugListener()); + title().apply(subStyle); } - - @Override - protected void applyParent(Style style) { - new ViewProxyStyleApplier(getView()).apply(style); + if (a.hasValue(R.styleable.MyView_subtitleStyle)) { + subStyle = a.getStyle(R.styleable.MyView_subtitleStyle); + subStyle.setDebugListener(style.getDebugListener()); + subtitle().apply(subStyle); } - - @Override - protected int[] attributes() { - return R.styleable.MyView; + if (a.hasValue(R.styleable.MyView_dividerStyle)) { + subStyle = a.getStyle(R.styleable.MyView_dividerStyle); + subStyle.setDebugListener(style.getDebugListener()); + divider().apply(subStyle); } + } - @Override - protected void processStyleableFields(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - Style subStyle; - if (a.hasValue(R.styleable.MyView_titleStyle)) { - subStyle = a.getStyle(R.styleable.MyView_titleStyle); - subStyle.setDebugListener(style.getDebugListener()); - title().apply(subStyle); - } - if (a.hasValue(R.styleable.MyView_subtitleStyle)) { - subStyle = a.getStyle(R.styleable.MyView_subtitleStyle); - subStyle.setDebugListener(style.getDebugListener()); - subtitle().apply(subStyle); - } - if (a.hasValue(R.styleable.MyView_dividerStyle)) { - subStyle = a.getStyle(R.styleable.MyView_dividerStyle); - subStyle.setDebugListener(style.getDebugListener()); - divider().apply(subStyle); - } - } + @Override + protected void processAttributes(Style style, TypedArrayWrapper a) { + Resources res = getView().getContext().getResources(); + } - @Override - protected void processAttributes(Style style, TypedArrayWrapper a) { - Resources res = getView().getContext().getResources(); - } + public StyleBuilder builder() { + return new StyleBuilder(this); + } - public StyleBuilder builder() { - return new StyleBuilder(this); - } + public TextViewProxyStyleApplier title() { + return new TextViewProxyStyleApplier(getProxy().title); + } - public TextViewProxyStyleApplier title() { - return new TextViewProxyStyleApplier(getProxy().title); - } + public TextViewProxyStyleApplier subtitle() { + return new TextViewProxyStyleApplier(getProxy().subtitle); + } - public TextViewProxyStyleApplier subtitle() { - return new TextViewProxyStyleApplier(getProxy().subtitle); - } + public ViewProxyStyleApplier divider() { + return new ViewProxyStyleApplier(getProxy().divider); + } - public ViewProxyStyleApplier divider() { - return new ViewProxyStyleApplier(getProxy().divider); - } + public void applyDefault() { + } - public void applyDefault() { + public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { + public BaseStyleBuilder(A applier) { + super(applier); } - public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { - public BaseStyleBuilder(A applier) { - super(applier); - } - - public BaseStyleBuilder() { - } + public BaseStyleBuilder() { + } - public B titleStyle(@StyleRes int resId) { - getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_titleStyle], resId); - return (B) this; - } + public B titleStyle(@StyleRes int resId) { + getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_titleStyle], resId); + return (B) this; + } - public B titleStyle(Style style) { - getBuilder().put(R.styleable.MyView[R.styleable.MyView_titleStyle], style); - return (B) this; - } + public B titleStyle(Style style) { + getBuilder().put(R.styleable.MyView[R.styleable.MyView_titleStyle], style); + return (B) this; + } - public B titleStyle(StyleBuilderFunction function) { - TextViewProxyStyleApplier.StyleBuilder subBuilder = new TextViewProxyStyleApplier.StyleBuilder(); - function.invoke(subBuilder); - getBuilder().put(R.styleable.MyView[R.styleable.MyView_titleStyle], subBuilder.build()); - return (B) this; - } + public B titleStyle(StyleBuilderFunction function) { + TextViewProxyStyleApplier.StyleBuilder subBuilder = new TextViewProxyStyleApplier.StyleBuilder(); + function.invoke(subBuilder); + getBuilder().put(R.styleable.MyView[R.styleable.MyView_titleStyle], subBuilder.build()); + return (B) this; + } - public B subtitleStyle(@StyleRes int resId) { - getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_subtitleStyle], resId); - return (B) this; - } + public B subtitleStyle(@StyleRes int resId) { + getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_subtitleStyle], resId); + return (B) this; + } - public B subtitleStyle(Style style) { - getBuilder().put(R.styleable.MyView[R.styleable.MyView_subtitleStyle], style); - return (B) this; - } + public B subtitleStyle(Style style) { + getBuilder().put(R.styleable.MyView[R.styleable.MyView_subtitleStyle], style); + return (B) this; + } - public B subtitleStyle(StyleBuilderFunction function) { - TextViewProxyStyleApplier.StyleBuilder subBuilder = new TextViewProxyStyleApplier.StyleBuilder(); - function.invoke(subBuilder); - getBuilder().put(R.styleable.MyView[R.styleable.MyView_subtitleStyle], subBuilder.build()); - return (B) this; - } + public B subtitleStyle(StyleBuilderFunction function) { + TextViewProxyStyleApplier.StyleBuilder subBuilder = new TextViewProxyStyleApplier.StyleBuilder(); + function.invoke(subBuilder); + getBuilder().put(R.styleable.MyView[R.styleable.MyView_subtitleStyle], subBuilder.build()); + return (B) this; + } - public B dividerStyle(@StyleRes int resId) { - getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_dividerStyle], resId); - return (B) this; - } + public B dividerStyle(@StyleRes int resId) { + getBuilder().putRes(R.styleable.MyView[R.styleable.MyView_dividerStyle], resId); + return (B) this; + } - public B dividerStyle(Style style) { - getBuilder().put(R.styleable.MyView[R.styleable.MyView_dividerStyle], style); - return (B) this; - } + public B dividerStyle(Style style) { + getBuilder().put(R.styleable.MyView[R.styleable.MyView_dividerStyle], style); + return (B) this; + } - public B dividerStyle(StyleBuilderFunction function) { - ViewProxyStyleApplier.StyleBuilder subBuilder = new ViewProxyStyleApplier.StyleBuilder(); - function.invoke(subBuilder); - getBuilder().put(R.styleable.MyView[R.styleable.MyView_dividerStyle], subBuilder.build()); - return (B) this; - } + public B dividerStyle(StyleBuilderFunction function) { + ViewProxyStyleApplier.StyleBuilder subBuilder = new ViewProxyStyleApplier.StyleBuilder(); + function.invoke(subBuilder); + getBuilder().put(R.styleable.MyView[R.styleable.MyView_dividerStyle], subBuilder.build()); + return (B) this; + } - public B applyTo(MyView view) { - new MyViewStyleApplier(view).apply(build()); - return (B) this; - } + public B applyTo(MyView view) { + new MyViewStyleApplier(view).apply(build()); + return (B) this; } + } - public static final class StyleBuilder extends BaseStyleBuilder { - public StyleBuilder(MyViewStyleApplier applier) { - super(applier); - } + @UiThread + public static final class StyleBuilder extends BaseStyleBuilder { + public StyleBuilder(MyViewStyleApplier applier) { + super(applier); + } - public StyleBuilder() { - } + public StyleBuilder() { + } - public StyleBuilder addDefault() { - return this; - } + public StyleBuilder addDefault() { + return this; } + } } \ No newline at end of file diff --git a/paris-test/src/test/resources/styles/MyViewStyleApplier.java b/paris-test/src/test/resources/styles/MyViewStyleApplier.java index 61452ef3..6c9604be 100644 --- a/paris-test/src/test/resources/styles/MyViewStyleApplier.java +++ b/paris-test/src/test/resources/styles/MyViewStyleApplier.java @@ -1,85 +1,88 @@ package com.airbnb.paris.test; +import android.support.annotation.UiThread; import com.airbnb.paris.Style; import com.airbnb.paris.StyleApplier; import com.airbnb.paris.proxies.ViewProxyStyleApplier; import java.lang.Override; +@UiThread public final class MyViewStyleApplier extends StyleApplier { - public MyViewStyleApplier(MyView view) { - super(view); + public MyViewStyleApplier(MyView view) { + super(view); + } + + @Override + protected void applyParent(Style style) { + new ViewProxyStyleApplier(getView()).apply(style); + } + + public StyleBuilder builder() { + return new StyleBuilder(this); + } + + public void applyRedStyle() { + apply(MyView.RED_STYLE); + } + + public void applyGreenStyle() { + apply(MyView.greenStyle); + } + + public void applyBlue() { + StyleBuilder builder = new StyleBuilder(); + MyView.blue(builder); + apply(builder.build()); + } + + public void applyDefault() { + apply(MyView.RED_STYLE); + } + + public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { + public BaseStyleBuilder(A applier) { + super(applier); } - @Override - protected void applyParent(Style style) { - new ViewProxyStyleApplier(getView()).apply(style); + public BaseStyleBuilder() { } - public StyleBuilder builder() { - return new StyleBuilder(this); + public B applyTo(MyView view) { + new MyViewStyleApplier(view).apply(build()); + return (B) this; } + } - public void applyRedStyle() { - apply(MyView.RED_STYLE); + @UiThread + public static final class StyleBuilder extends BaseStyleBuilder { + public StyleBuilder(MyViewStyleApplier applier) { + super(applier); } - public void applyGreenStyle() { - apply(MyView.greenStyle); + public StyleBuilder() { } - public void applyBlue() { - StyleBuilder builder = new StyleBuilder(); - MyView.blue(builder); - apply(builder.build()); + public StyleBuilder addRedStyle() { + add(MyView.RED_STYLE); + return this; } - public void applyDefault() { - apply(MyView.RED_STYLE); + public StyleBuilder addGreenStyle() { + add(MyView.greenStyle); + return this; } - public abstract static class BaseStyleBuilder, A extends StyleApplier> extends ViewProxyStyleApplier.BaseStyleBuilder { - public BaseStyleBuilder(A applier) { - super(applier); - } - - public BaseStyleBuilder() { - } - - public B applyTo(MyView view) { - new MyViewStyleApplier(view).apply(build()); - return (B) this; - } + public StyleBuilder addBlue() { + consumeProgrammaticStyleBuilder(); + debugName("Blue"); + MyView.blue(this); + consumeProgrammaticStyleBuilder(); + return this; } - public static final class StyleBuilder extends BaseStyleBuilder { - public StyleBuilder(MyViewStyleApplier applier) { - super(applier); - } - - public StyleBuilder() { - } - - public StyleBuilder addRedStyle() { - add(MyView.RED_STYLE); - return this; - } - - public StyleBuilder addGreenStyle() { - add(MyView.greenStyle); - return this; - } - - public StyleBuilder addBlue() { - consumeProgrammaticStyleBuilder(); - debugName("Blue"); - MyView.blue(this); - consumeProgrammaticStyleBuilder(); - return this; - } - - public StyleBuilder addDefault() { - add(MyView.RED_STYLE); - return this; - } + public StyleBuilder addDefault() { + add(MyView.RED_STYLE); + return this; } + } } \ No newline at end of file diff --git a/update_processor_tests.rb b/update_processor_tests.rb new file mode 100644 index 00000000..27d4b3f8 --- /dev/null +++ b/update_processor_tests.rb @@ -0,0 +1,61 @@ +# A script to update processor test files in the "src/test/resources" package with the latest test output. +# This is useful when changes are made to the annotation processor and all the tests need to have their expected values updated. +# +# You may have to run this multiple times for the tests that output several generated source files. +# In those cases the test report only details the first failure, so you will have to run `./gradlew test` +# again to generate a new test report before running this script again. + +require 'rubygems' +require 'nokogiri' + +def updateTestClass(test_class_result) + page = Nokogiri::HTML(open(test_class_result)) + + # Failing processor tests have their output in a
 block
+  page.css('pre').each do |preBlock|
+    # Just a sanity check to make sure the pre block we're looking at is a processor source output
+    if preBlock.include? "Source declared the same top-level types of an expected source"
+      puts "Pre block did not contain source. (#{test_class_result}"
+      next
+    end
+
+    # We expect to see a line like:
+    # Expected file: ;
+    # Which tells us where the original processor test file lives
+    expected_file_match = /Expected file: <([^>]*)>/m.match(preBlock)
+    if expected_file_match.nil? || expected_file_match.captures.empty?
+      puts "Could not find expected file name in pre block (#{test_class_result})"
+      next
+    end
+
+    # The test copies the source file to the build folder. We need to modify the original file to update its expected source
+    expected_source_file_path = expected_file_match.captures[0].sub "build/intermediates/classes/test/debug", "src/test/resources"
+
+    # The error message includes the source code that was generated. We use a regex to extract the source from the following expected pattern
+    #
+    # Actual Source:
+    # =================
+    # ... Source code here
+    #     at com.google.testing.compile.JavaSourcesSubject$CompilationClause.failWithCandidate(JavaSourcesSubject.java:224)
+    # at com.google.testing.compile.JavaSourcesSubject$CompilationClause.parsesAs(JavaSourcesSubject.java:186)
+    # at com.google.testing.compile.JavaSourcesSubject.parsesAs(JavaSourcesSubject.java:95)
+    actual_source_match = /Actual Source:[\s]*=*[\s]*(package.*?})[\s]*at com\.google/m.match(preBlock)
+    if actual_source_match.nil? || actual_source_match.captures.empty?
+      puts "Could not find actual source in pre block (#{test_class_result})"
+      next
+    end
+
+    puts "Updating class: #{expected_source_file_path.split('/')[-1]}"
+
+    # Finally we simply overwrite the original expected test source with the actual test output in order to update it
+    actual_source = actual_source_match.captures[0]
+    File.open(expected_source_file_path, "w") do |f|
+      f.write actual_source
+    end
+  end
+end
+
+# Looks through each module's build folder for debug test results
+Dir.glob("*/build/reports/tests/testDebugUnitTest/classes/*.html") do |test_class_result|
+  updateTestClass(test_class_result)
+end