-
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 incremental annotation processing. #1230
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,12 +50,14 @@ | |
import java.util.Arrays; | ||
import java.util.BitSet; | ||
import java.util.Deque; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
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. nit: delete line |
||
import javax.annotation.processing.AbstractProcessor; | ||
import javax.annotation.processing.Filer; | ||
import javax.annotation.processing.ProcessingEnvironment; | ||
|
@@ -131,6 +133,7 @@ public final class ButterKnifeProcessor extends AbstractProcessor { | |
private boolean debuggable = true; | ||
|
||
private final Map<QualifiedId, Id> symbols = new LinkedHashMap<>(); | ||
private HashMap<String, List<Element>> mapGeneratedFileToOriginatingElements = new LinkedHashMap<>(); | ||
|
||
@Override public synchronized void init(ProcessingEnvironment env) { | ||
super.init(env); | ||
|
@@ -198,9 +201,10 @@ private Set<Class<? extends Annotation>> getSupportedAnnotations() { | |
TypeElement typeElement = entry.getKey(); | ||
BindingSet binding = entry.getValue(); | ||
|
||
JavaFile javaFile = binding.brewJava(sdk, debuggable); | ||
JavaFile javaFile = binding.brewJava(sdk, debuggable, typeElement); | ||
try { | ||
javaFile.writeTo(filer); | ||
mapGeneratedFileToOriginatingElements.put(javaFile.toJavaFileObject().getName(), javaFile.typeSpec.originatingElements); | ||
} catch (IOException e) { | ||
error(typeElement, "Unable to write binding for type %s: %s", typeElement, e.getMessage()); | ||
} | ||
|
@@ -209,6 +213,10 @@ private Set<Class<? extends Annotation>> getSupportedAnnotations() { | |
return false; | ||
} | ||
|
||
public HashMap<String, List<Element>> getMapGeneratedFileToOriginatingElements() { | ||
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.
|
||
return mapGeneratedFileToOriginatingElements; | ||
} | ||
|
||
private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) { | ||
Map<TypeElement, BindingSet.Builder> builderMap = new LinkedHashMap<>(); | ||
Set<TypeElement> erasedTargetNames = new LinkedHashSet<>(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
butterknife.compiler.ButterKnifeProcessor,isolating |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,21 @@ | |
import butterknife.compiler.ButterKnifeProcessor; | ||
import com.google.common.collect.ImmutableList; | ||
import com.google.testing.compile.JavaFileObjects; | ||
|
||
import javax.lang.model.element.Element; | ||
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. nit: delete empty lines |
||
import javax.tools.JavaFileObject; | ||
import javax.tools.StandardLocation; | ||
|
||
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. nit: delete empty lines |
||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
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. nit: delete empty lines |
||
import java.util.Map; | ||
|
||
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; | ||
import static junit.framework.Assert.assertEquals; | ||
|
||
public class BindViewTest { | ||
@Test public void bindingView() { | ||
|
@@ -50,12 +57,21 @@ public class BindViewTest { | |
+ "}" | ||
); | ||
|
||
ButterKnifeProcessor butterKnifeProcessor = new ButterKnifeProcessor(); | ||
assertAbout(javaSource()).that(source) | ||
.withCompilerOptions("-Xlint:-processing") | ||
.processedWith(new ButterKnifeProcessor()) | ||
.processedWith(butterKnifeProcessor) | ||
.compilesWithoutWarnings() | ||
.and() | ||
.generatesSources(bindingSource); | ||
|
||
Map<String, List<Element>> map = butterKnifeProcessor.getMapGeneratedFileToOriginatingElements(); | ||
assertEquals(1, map.size()); | ||
Map.Entry<String, List<Element>> entry = map.entrySet().iterator().next(); | ||
assertEquals("test/Test_ViewBinding.java", entry.getKey()); | ||
List<Element> elements = entry.getValue(); | ||
assertEquals(1, elements.size()); | ||
assertEquals("test.Test", elements.get(0).asType().toString()); | ||
} | ||
|
||
@Test public void bindingViewNonDebuggable() { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions-snapshots/gradle-4.7-20180323002511+0000-bin.zip | ||
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. is this required? |
||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-4.5.1-all.zip |
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.
nit: delete line