-
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 inheritance across modules. Issue #808. #1045
Open
stephanenicolas
wants to merge
5
commits into
JakeWharton:master
Choose a base branch
from
stephanenicolas:sni/support-inheritance-in-modules
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f1df851
Add support for inheritance in modules
stephanenicolas cbc7c47
make BK support inheritance between modules
stephanenicolas f68da6b
Add module to sample to demonstrate that it works
stephanenicolas 3f2f26b
Add a unit test that simulates a base class already compiled in a module
stephanenicolas 664f760
fix build, we need to explicitly pass auto dep with the new model
stephanenicolas 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package butterknife.compiler; | ||
|
||
import android.support.annotation.NonNull; | ||
import butterknife.BindAnim; | ||
import butterknife.BindArray; | ||
import butterknife.BindBitmap; | ||
|
@@ -50,10 +51,10 @@ | |
import java.util.Arrays; | ||
import java.util.BitSet; | ||
import java.util.Deque; | ||
import java.util.HashSet; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
import javax.annotation.processing.AbstractProcessor; | ||
|
@@ -75,6 +76,7 @@ | |
import javax.lang.model.type.TypeKind; | ||
import javax.lang.model.type.TypeMirror; | ||
import javax.lang.model.type.TypeVariable; | ||
import javax.lang.model.util.ElementFilter; | ||
import javax.lang.model.util.Elements; | ||
import javax.lang.model.util.Types; | ||
import javax.tools.Diagnostic.Kind; | ||
|
@@ -367,6 +369,12 @@ private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) { | |
bindingMap.put(type, builder.build()); | ||
} else { | ||
BindingSet parentBinding = bindingMap.get(parentType); | ||
|
||
// parent binding is null, let's try to find a previouly generated binding | ||
if (parentBinding == null && hasViewBinder(parentType)) { | ||
parentBinding = createStubBindingSet(parentType); | ||
} | ||
|
||
if (parentBinding != null) { | ||
builder.setParent(parentBinding); | ||
bindingMap.put(type, builder.build()); | ||
|
@@ -380,6 +388,33 @@ private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) { | |
return bindingMap; | ||
} | ||
|
||
@NonNull private BindingSet createStubBindingSet(TypeElement parentType) { | ||
BindingSet parentBinding; | ||
BindingSet.Builder parentBuilder = BindingSet.newBuilder(parentType); | ||
if (hasViewBindings(parentType)) { | ||
//add a fake field to the parent class so that it will indicate it has a view bindings. | ||
//this is required for the subclass to generate a proper view binder | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
parentBuilder.addField(new Id(-1), new FieldViewBinding("", null, false)); | ||
} | ||
parentBinding = parentBuilder.build(); | ||
return parentBinding; | ||
} | ||
|
||
private boolean hasViewBindings(TypeElement parentType) { | ||
for (VariableElement fieldElement : ElementFilter.fieldsIn(parentType.getEnclosedElements())) { | ||
if (fieldElement.getAnnotation(BindView.class) != null | ||
|| fieldElement.getAnnotation(BindViews.class) != null) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean hasViewBinder(TypeElement typeElement) { | ||
final String viewBindingClassName = typeElement.getQualifiedName().toString() + "_ViewBinding"; | ||
return elementUtils.getTypeElement(viewBindingClassName) != null; | ||
} | ||
|
||
private void logParsingError(Element element, Class<? extends Annotation> annotation, | ||
Exception e) { | ||
StringWriter stackTrace = new StringWriter(); | ||
|
@@ -1284,7 +1319,7 @@ private TypeElement findParentType(TypeElement typeElement, Set<TypeElement> par | |
return null; | ||
} | ||
typeElement = (TypeElement) ((DeclaredType) type).asElement(); | ||
if (parents.contains(typeElement)) { | ||
if (parents.contains(typeElement) || hasViewBinder(typeElement)) { | ||
return typeElement; | ||
} | ||
} | ||
|
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
62 changes: 62 additions & 0 deletions
62
butterknife/src/test/java/butterknife/InheritanceTest.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,62 @@ | ||
package butterknife; | ||
|
||
import butterknife.compiler.ButterKnifeProcessor; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.testing.compile.JavaFileObjects; | ||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardLocation; | ||
import org.junit.Test; | ||
|
||
import static com.google.common.truth.Truth.assertAbout; | ||
import static com.google.testing.compile.JavaSourceSubjectFactory.javaSource; | ||
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources; | ||
import static java.util.Arrays.asList; | ||
|
||
public class InheritanceTest { | ||
|
||
@Test public void bindingViewFinalClassWithBaseClassAlreadyCompiledInDifferentModule() { | ||
JavaFileObject testSource = JavaFileObjects.forSourceString("test.Test", "" | ||
+ "package test;\n" | ||
+ "import android.view.View;\n" | ||
+ "import butterknife.BindView;\n" | ||
+ "import butterknife.precompiled.Base;\n" | ||
+ "public final class Test extends Base {\n" | ||
+ " @BindView(1) View thing;\n" | ||
+ "}" | ||
); | ||
|
||
JavaFileObject bindingTestSource = JavaFileObjects.forSourceString("test/Test_ViewBinding", "" | ||
+ "package test;\n" | ||
+ "import android.support.annotation.UiThread;\n" | ||
+ "import android.view.View;\n" | ||
+ "import butterknife.internal.Utils;\n" | ||
+ "import butterknife.precompiled.Base_ViewBinding;\n" | ||
+ "import java.lang.IllegalStateException;\n" | ||
+ "import java.lang.Override;\n" | ||
+ "public final class Test_ViewBinding extends Base_ViewBinding {\n" | ||
+ " private Test target;\n" | ||
+ " @UiThread\n" | ||
+ " public Test_ViewBinding(Test target, View source) {\n" | ||
+ " super(target, source);\n" | ||
+ " this.target = target;\n" | ||
+ " target.thing = Utils.findRequiredView(source, 1, \"field 'thing'\");\n" | ||
+ " }\n" | ||
+ " @Override\n" | ||
+ " public void unbind() {\n" | ||
+ " Test target = this.target;\n" | ||
+ " if (target == null) throw new IllegalStateException(\"Bindings already cleared.\");\n" | ||
+ " this.target = null\n" | ||
+ " target.thing = null;\n" | ||
+ " super.unbind();\n" | ||
+ " }\n" | ||
+ "}" | ||
); | ||
|
||
assertAbout(javaSources()).that(asList(testSource)) | ||
.withCompilerOptions("-Xlint:-processing") | ||
.processedWith(new ButterKnifeProcessor()) | ||
.compilesWithoutWarnings() | ||
.and() | ||
.generatesSources(bindingTestSource); | ||
} | ||
} |
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,8 @@ | ||
package butterknife.precompiled; | ||
|
||
import android.view.View; | ||
import butterknife.BindView; | ||
|
||
public class Base { | ||
@BindView(1) View thing; | ||
} |
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,27 @@ | ||
buildscript { | ||
repositories { | ||
mavenCentral() | ||
jcenter() | ||
} | ||
|
||
dependencies { | ||
classpath "com.jakewharton:butterknife-gradle-plugin:${versions.release}" | ||
} | ||
} | ||
|
||
apply plugin: 'com.android.library' | ||
apply plugin: 'com.jakewharton.butterknife' | ||
|
||
android { | ||
compileSdkVersion versions.compileSdk | ||
buildToolsVersion versions.buildTools | ||
|
||
defaultConfig { | ||
minSdkVersion versions.minSdk | ||
} | ||
} | ||
|
||
dependencies { | ||
compile deps.release.runtime | ||
annotationProcessor deps.release.compiler | ||
} |
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 @@ | ||
<manifest package="com.example.butterknife.baselibrary"/> |
11 changes: 11 additions & 0 deletions
11
sample/base-library/src/main/java/com/example/butterknife/baselibrary/BaseActivity.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,11 @@ | ||
package com.example.butterknife.baselibrary; | ||
|
||
import android.app.Activity; | ||
import butterknife.BindString; | ||
|
||
public class BaseActivity extends Activity { | ||
@BindString(R2.string.app_name) protected String butterKnife; | ||
@BindString(R2.string.field_method) protected String fieldMethod; | ||
@BindString(R2.string.by_jake_wharton) protected String byJakeWharton; | ||
@BindString(R2.string.say_hello) protected String sayHello; | ||
} |
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,8 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
|
||
<resources> | ||
<string name="app_name">Butter Knife</string> | ||
<string name="field_method">Field and method binding for Android views.</string> | ||
<string name="by_jake_wharton">by Jake Wharton</string> | ||
<string name="say_hello">Say Hello</string> | ||
</resources> |
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
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
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
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.
Typo in comment -> "previouly"