-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from tomxor/bind-arrays
Support binding array resources (String, int, text and TypedArray).
- Loading branch information
Showing
5 changed files
with
250 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package butterknife; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import static java.lang.annotation.ElementType.FIELD; | ||
import static java.lang.annotation.RetentionPolicy.CLASS; | ||
|
||
/** | ||
* Bind a field to the specified array resource ID. The type of array will be inferred from the | ||
* annotated element. | ||
* | ||
* String array: | ||
* <pre><code> | ||
* {@literal @}BindArray(R.array.countries) String[] countries; | ||
* </code></pre> | ||
* | ||
* Int array: | ||
* <pre><code> | ||
* {@literal @}BindArray(R.array.phones) int[] phones; | ||
* </code></pre> | ||
* | ||
* Text array: | ||
* <pre><code> | ||
* {@literal @}BindArray(R.array.options) CharSequence[] options; | ||
* </code></pre> | ||
* | ||
* {@link android.content.res.TypedArray}: | ||
* <pre><code> | ||
* {@literal @}BindArray(R.array.icons) TypedArray icons; | ||
* </code></pre> | ||
*/ | ||
@Retention(CLASS) @Target(FIELD) | ||
public @interface BindArray { | ||
/** Array resource ID to which the field will be bound. */ | ||
int value(); | ||
} |
19 changes: 0 additions & 19 deletions
19
butterknife/src/main/java/butterknife/BindStringArray.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
butterknife/src/test/java/butterknife/internal/BindArrayTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,166 @@ | ||
package butterknife.internal; | ||
|
||
import com.google.common.base.Joiner; | ||
import com.google.testing.compile.JavaFileObjects; | ||
import javax.tools.JavaFileObject; | ||
import org.junit.Test; | ||
|
||
import static com.google.common.truth.Truth.ASSERT; | ||
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; | ||
|
||
public class BindArrayTest { | ||
@Test public void stringArray() throws Exception { | ||
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( | ||
"package test;", | ||
"import android.app.Activity;", | ||
"import butterknife.BindArray;", | ||
"import android.content.res.TypedArray;", | ||
"public class Test extends Activity {", | ||
" @BindArray(1) String[] one;", | ||
"}" | ||
)); | ||
|
||
JavaFileObject expectedSource = JavaFileObjects.forSourceString("test/Test$$ViewBinder", | ||
Joiner.on('\n').join( | ||
"package test;", | ||
"import android.content.res.Resources;", | ||
"import butterknife.ButterKnife.Finder;", | ||
"import butterknife.ButterKnife.ViewBinder;", | ||
"public class Test$$ViewBinder<T extends test.Test> implements ViewBinder<T> {", | ||
" @Override public void bind(final Finder finder, final T target, Object source) {", | ||
" Resources res = finder.getContext(source).getResources();", | ||
" target.one = res.getStringArray(1);", | ||
" }", | ||
" @Override public void unbind(T target) {", | ||
" }", | ||
"}" | ||
)); | ||
|
||
ASSERT.about(javaSource()).that(source) | ||
.processedWith(new ButterKnifeProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(expectedSource); | ||
} | ||
|
||
@Test public void intArray() throws Exception { | ||
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( | ||
"package test;", | ||
"import android.app.Activity;", | ||
"import butterknife.BindArray;", | ||
"import android.content.res.TypedArray;", | ||
"public class Test extends Activity {", | ||
" @BindArray(1) int[] one;", | ||
"}" | ||
)); | ||
|
||
JavaFileObject expectedSource = JavaFileObjects.forSourceString("test/Test$$ViewBinder", | ||
Joiner.on('\n').join( | ||
"package test;", | ||
"import android.content.res.Resources;", | ||
"import butterknife.ButterKnife.Finder;", | ||
"import butterknife.ButterKnife.ViewBinder;", | ||
"public class Test$$ViewBinder<T extends test.Test> implements ViewBinder<T> {", | ||
" @Override public void bind(final Finder finder, final T target, Object source) {", | ||
" Resources res = finder.getContext(source).getResources();", | ||
" target.one = res.getIntArray(1);", | ||
" }", | ||
" @Override public void unbind(T target) {", | ||
" }", | ||
"}" | ||
)); | ||
|
||
ASSERT.about(javaSource()).that(source) | ||
.processedWith(new ButterKnifeProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(expectedSource); | ||
} | ||
|
||
@Test public void textArray() throws Exception { | ||
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( | ||
"package test;", | ||
"import android.app.Activity;", | ||
"import butterknife.BindArray;", | ||
"import android.content.res.TypedArray;", | ||
"public class Test extends Activity {", | ||
" @BindArray(1) CharSequence[] one;", | ||
"}" | ||
)); | ||
|
||
JavaFileObject expectedSource = JavaFileObjects.forSourceString("test/Test$$ViewBinder", | ||
Joiner.on('\n').join( | ||
"package test;", | ||
"import android.content.res.Resources;", | ||
"import butterknife.ButterKnife.Finder;", | ||
"import butterknife.ButterKnife.ViewBinder;", | ||
"public class Test$$ViewBinder<T extends test.Test> implements ViewBinder<T> {", | ||
" @Override public void bind(final Finder finder, final T target, Object source) {", | ||
" Resources res = finder.getContext(source).getResources();", | ||
" target.one = res.getTextArray(1);", | ||
" }", | ||
" @Override public void unbind(T target) {", | ||
" }", | ||
"}" | ||
)); | ||
|
||
ASSERT.about(javaSource()).that(source) | ||
.processedWith(new ButterKnifeProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(expectedSource); | ||
} | ||
|
||
@Test public void typedArray() throws Exception { | ||
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( | ||
"package test;", | ||
"import android.app.Activity;", | ||
"import butterknife.BindArray;", | ||
"import android.content.res.TypedArray;", | ||
"public class Test extends Activity {", | ||
" @BindArray(1) TypedArray one;", | ||
"}" | ||
)); | ||
|
||
JavaFileObject expectedSource = JavaFileObjects.forSourceString("test/Test$$ViewBinder", | ||
Joiner.on('\n').join( | ||
"package test;", | ||
"import android.content.res.Resources;", | ||
"import butterknife.ButterKnife.Finder;", | ||
"import butterknife.ButterKnife.ViewBinder;", | ||
"public class Test$$ViewBinder<T extends test.Test> implements ViewBinder<T> {", | ||
" @Override public void bind(final Finder finder, final T target, Object source) {", | ||
" Resources res = finder.getContext(source).getResources();", | ||
" target.one = res.obtainTypedArray(1);", | ||
" }", | ||
" @Override public void unbind(T target) {", | ||
" }", | ||
"}" | ||
)); | ||
|
||
ASSERT.about(javaSource()).that(source) | ||
.processedWith(new ButterKnifeProcessor()) | ||
.compilesWithoutError() | ||
.and() | ||
.generatesSources(expectedSource); | ||
} | ||
|
||
@Test public void typeMustBeSupported() { | ||
JavaFileObject source = JavaFileObjects.forSourceString("test.Test", Joiner.on('\n').join( | ||
"package test;", | ||
"import android.app.Activity;", | ||
"import butterknife.BindArray;", | ||
"public class Test extends Activity {", | ||
" @BindArray(1) String one;", | ||
"}" | ||
)); | ||
|
||
ASSERT.about(javaSource()).that(source) | ||
.processedWith(new ButterKnifeProcessor()) | ||
.failsToCompile() | ||
.withErrorContaining( | ||
"@BindArray field type must be one of: String[], int[], CharSequence[], " | ||
+ "android.content.res.TypedArray. (test.Test.one)") | ||
.in(source).onLine(5); | ||
} | ||
} |
Oops, something went wrong.