-
Notifications
You must be signed in to change notification settings - Fork 4.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support binding array resources (String, int, text and TypedArray). #301
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This can be simplified to simply match the type against
String[]
,int[]
, etc. Not sure which way is better ¯_(ツ)_/¯