Search in sources :

Example 21 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class RClassTest method issue779.

@Test
public void issue779() {
    JavaFileObject r2Bar = JavaFileObjects.forSourceString("test.bar.R2", "" + "package test.bar;\n" + "public final class R2 {\n" + "  public static final class array {\n" + "    public static final int res = 0x7f040001;\n" + "  }\n" + "  public static final class bool {\n" + "    public static final int res = 0x7f040002;\n" + "  }\n" + "  public static final class color {\n" + "    public static final int res = 0x7f040003;\n" + "  }\n" + "  public static final class id {\n" + "    public static final int res = 0x7f040004;\n" + "  }\n" + "  public static final class string {\n" + "    public static final int res = 0x7f040005;\n" + "  }\n" + "}");
    JavaFileObject nonFinalRBar = JavaFileObjects.forSourceString("test.bar.R", "" + "package test.bar;\n" + "public final class R {\n" + "  public static final class array {\n" + "    public static int res = 0x7f040001;\n" + "  }\n" + "  public static final class bool {\n" + "    public static int res = 0x7f040002;\n" + "  }\n" + "  public static final class color {\n" + "    public static int res = 0x7f040003;\n" + "  }\n" + "  public static final class id {\n" + "    public static int res = 0x7f040004;\n" + "  }\n" + "  public static final class styleable {\n" + "    public static int[] ActionBar = { 0x7f010001, 0x7f010003 };\n" + "  }\n" + "}");
    JavaFileObject nonFinalRFoo = JavaFileObjects.forSourceString("test.foo.R", "" + "package test.foo;\n" + "public final class R {\n" + "  public static final class array {\n" + "    public static int res = 0x7f040001;\n" + "  }\n" + "  public static final class bool {\n" + "    public static int res = 0x7f040002;\n" + "  }\n" + "  public static final class color {\n" + "    public static int res = 0x7f040003;\n" + "  }\n" + "  public static final class id {\n" + "    public static int bogus = 0x7f040004;\n" + "  }\n" + "  public static final class styleable {\n" + "    public static int[] ActionBar = { 0x7f010001, 0x7f010003 };\n" + "  }\n" + "}");
    JavaFileObject r2Foo = JavaFileObjects.forSourceString("test.foo.R2", "" + "package test.foo;\n" + "public final class R2 {\n" + "  public static final class array {\n" + "    public static final int res = 0x7f040001;\n" + "  }\n" + "  public static final class bool {\n" + "    public static final int res = 0x7f040002;\n" + "  }\n" + "  public static final class color {\n" + "    public static final int res = 0x7f040003;\n" + "  }\n" + "  public static final class id {\n" + "    public static final int bogus = 0x7f040004;\n" + "  }\n" + "  public static final class string {\n" + "    public static final int res = 0x7f040005;\n" + "  }\n" + "}");
    JavaFileObject fooSource = JavaFileObjects.forSourceString("test.foo.FooTest", "" + "package test.foo;\n" + "import android.app.Activity;\n" + "import android.view.View;\n" + "import butterknife.BindView;\n" + "public class FooTest extends Activity {\n" + "  @BindView(R2.id.bogus) View one;\n" + "}");
    JavaFileObject barSource = JavaFileObjects.forSourceString("test.bar.Test", "" + "package test.bar;\n" + "import android.app.Activity;\n" + "import butterknife.BindView;\n" + "public class Test extends Activity {\n" + "  @BindView(R2.id.res) CustomView one;\n" + "}");
    JavaFileObject customView = JavaFileObjects.forSourceString("test.bar.CustomView", "" + "package test.bar;\n" + "import android.view.View;\n" + "import android.content.Context;\n" + "public class CustomView extends View {\n" + "  public CustomView(Context context) {\n" + "    super(context);" + "  }" + "}");
    JavaFileObject bindingSourceBar = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "// Generated code from Butter Knife. Do not modify!\n" + "package test.bar;\n\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target) {\n" + "    this(target, target.getWindow().getDecorView());\n" + "  }\n\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target, View source) {\n" + "    this.target = target;\n" + "    target.one = Utils.findRequiredViewAsType(source, R.id.res, \"field 'one'\", " + "CustomView.class);\n" + "  }\n\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    Test target = this.target;\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    this.target = null;\n\n" + "    target.one = null;\n\n" + "  }\n" + "}");
    JavaFileObject bindingSourceFoo = JavaFileObjects.forSourceString("test/FooTest_ViewBinding", "" + "// Generated code from Butter Knife. Do not modify!\n" + "package test.foo;\n\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class FooTest_ViewBinding implements Unbinder {\n" + "  private FooTest target;\n\n" + "  @UiThread\n" + "  public FooTest_ViewBinding(FooTest target) {\n" + "    this(target, target.getWindow().getDecorView());\n" + "  }\n\n" + "  @UiThread\n" + "  public Test_ViewBinding(FooTest target, View source) {\n" + "    this.target = target;\n" + "    target.one = Utils.findRequiredView(source, R.id.bogus, \"field 'one'\");\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    FooTest target = this.target;\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n\n" + "    this.target = null;\n\n" + "    target.one = null;\n\n" + "  }\n" + "}");
    assertAbout(javaSources()).that(asList(customView, fooSource, barSource, nonFinalRBar, nonFinalRFoo, r2Bar, r2Foo)).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSourceBar);
    assertAbout(javaSources()).that(asList(customView, fooSource, barSource, nonFinalRBar, nonFinalRFoo, r2Bar, r2Foo)).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSourceFoo);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 22 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class RClassTest method compiledRClass.

@Test
public void compiledRClass() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import butterknife.BindColor;\n" + "public class Test {\n" + "  @BindColor(android.R.color.black) int black;\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "// Generated code from Butter Knife. Do not modify!\n" + "package test;\n" + "import android.content.Context;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.support.v4.content.ContextCompat;\n" + "import android.view.View;\n" + "import butterknife.Unbinder;\n" + "import java.lang.Deprecated;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  /**\n" + "   * @deprecated Use {@link #Test_ViewBinding(Test, Context)} for direct creation.\n" + "   *     Only present for runtime invocation through {@code ButterKnife.bind()}.\n" + "   */\n" + "  @Deprecated\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target, View source) {\n" + "    this(target, source.getContext());\n" + "  }\n" + "  @UiThread\n" + "  public Test_ViewBinding(Test target, Context context) {\n" + "    target.black = ContextCompat.getColor(context, android.R.color.black);\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "  }\n" + "}");
    assertAbout(javaSources()).that(asList(source, NON_FINAL_R)).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 23 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class OnItemClickTest method onItemClickBindingWithParameters.

@Test
public void onItemClickBindingWithParameters() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import android.widget.AdapterView;\n" + "import butterknife.OnItemClick;\n" + "public class Test {\n" + "  @OnItemClick(1) void doStuff(\n" + "    AdapterView<?> parent,\n" + "    View view,\n" + "    int position,\n" + "    long id\n" + "  ) {}\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "package test;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import android.widget.AdapterView;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n" + "  private View view1;\n" + "  @UiThread\n" + "  public Test_ViewBinding(final Test target, View source) {\n" + "    this.target = target;\n" + "    View view;\n" + "    view = Utils.findRequiredView(source, 1, \"method 'doStuff'\");\n" + "    view1 = view;\n" + "    ((AdapterView<?>) view).setOnItemClickListener(new AdapterView.OnItemClickListener() {\n" + "      @Override\n" + "      public void onItemClick(AdapterView<?> p0, View p1, int p2, long p3) {\n" + "        target.doStuff(p0, p1, p2, p3);\n" + "      }\n" + "    });\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    target = null;\n" + "    ((AdapterView<?>) view1).setOnItemClickListener(null);\n" + "    view1 = null;\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutWarnings().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 24 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class OnItemClickTest method failsWithInvalidParameterConfiguration.

@Test
public void failsWithInvalidParameterConfiguration() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import android.widget.AdapterView;\n" + "import butterknife.OnItemClick;\n" + "public class Test {\n" + "  @OnItemClick(1) void doStuff(\n" + "    AdapterView<?> parent,\n" + "    View view,\n" + "    View whatIsThis\n" + "  ) {}\n" + "}");
    assertAbout(javaSource()).that(source).processedWith(new ButterKnifeProcessor()).failsToCompile().withErrorContaining("" + "Unable to match @OnItemClick method arguments. (test.Test.doStuff)\n" + "  \n" + "    Parameter #1: android.widget.AdapterView<?>\n" + "      matched listener parameter #1: android.widget.AdapterView<?>\n" + "  \n" + "    Parameter #2: android.view.View\n" + "      matched listener parameter #2: android.view.View\n" + "  \n" + "    Parameter #3: android.view.View\n" + "      did not match any listener parameters\n" + "  \n" + "  Methods may have up to 4 parameter(s):\n" + "  \n" + "    android.widget.AdapterView<?>\n" + "    android.view.View\n" + "    int\n" + "    long\n" + "  \n" + "  These may be listed in any order but will be searched for from top to bottom.").in(source).onLine(6);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Example 25 with ButterKnifeProcessor

use of butterknife.compiler.ButterKnifeProcessor in project butterknife by JakeWharton.

the class OnItemClickTest method onItemClickBindingWithParameterSubsetAndGenerics.

@Test
public void onItemClickBindingWithParameterSubsetAndGenerics() {
    JavaFileObject source = JavaFileObjects.forSourceString("test.Test", "" + "package test;\n" + "import android.view.View;\n" + "import android.widget.ListView;\n" + "import butterknife.OnItemClick;\n" + "public class Test<T extends ListView> {\n" + "  @OnItemClick(1) void doStuff(\n" + "    T parent,\n" + "    int position\n" + "  ) {}\n" + "}");
    JavaFileObject bindingSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" + "package test;\n" + "import android.support.annotation.CallSuper;\n" + "import android.support.annotation.UiThread;\n" + "import android.view.View;\n" + "import android.widget.AdapterView;\n" + "import android.widget.ListView;\n" + "import butterknife.Unbinder;\n" + "import butterknife.internal.Utils;\n" + "import java.lang.IllegalStateException;\n" + "import java.lang.Override;\n" + "public class Test_ViewBinding implements Unbinder {\n" + "  private Test target;\n" + "  private View view1;\n" + "  @UiThread\n" + "  public Test_ViewBinding(final Test target, View source) {\n" + "    this.target = target;\n" + "    View view;\n" + "    view = Utils.findRequiredView(source, 1, \"method 'doStuff'\");\n" + "    view1 = view;\n" + "    ((AdapterView<?>) view).setOnItemClickListener(new AdapterView.OnItemClickListener() {\n" + "      @Override\n" + "      public void onItemClick(AdapterView<?> p0, View p1, int p2, long p3) {\n" + "        target.doStuff(Utils.<ListView>castParam(p0, \"onItemClick\", 0, \"doStuff\", 0)\n" + "        , p2);\n" + "      }\n" + "    });\n" + "  }\n" + "  @Override\n" + "  @CallSuper\n" + "  public void unbind() {\n" + "    if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" + "    target = null;\n" + "    ((AdapterView<?>) view1).setOnItemClickListener(null);\n" + "    view1 = null;\n" + "  }\n" + "}");
    assertAbout(javaSource()).that(source).withCompilerOptions("-Xlint:-processing").processedWith(new ButterKnifeProcessor()).compilesWithoutError().and().generatesSources(bindingSource);
}
Also used : JavaFileObject(javax.tools.JavaFileObject) ButterKnifeProcessor(butterknife.compiler.ButterKnifeProcessor) Test(org.junit.Test)

Aggregations

ButterKnifeProcessor (butterknife.compiler.ButterKnifeProcessor)121 JavaFileObject (javax.tools.JavaFileObject)121 Test (org.junit.Test)121