Skip to content
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

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import java.util.List;
import java.util.Map;
import java.util.Set;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: delete line

import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.TypeElement;
import javax.lang.model.type.TypeMirror;
Expand Down Expand Up @@ -82,13 +84,13 @@ private BindingSet(TypeName targetTypeName, ClassName bindingClassName, boolean
this.parentBinding = parentBinding;
}

JavaFile brewJava(int sdk, boolean debuggable) {
return JavaFile.builder(bindingClassName.packageName(), createType(sdk, debuggable))
JavaFile brewJava(int sdk, boolean debuggable, Element originatingElement) {
return JavaFile.builder(bindingClassName.packageName(), createType(sdk, debuggable, originatingElement))
.addFileComment("Generated code from Butter Knife. Do not modify!")
.build();
}

private TypeSpec createType(int sdk, boolean debuggable) {
private TypeSpec createType(int sdk, boolean debuggable, Element originatingElement) {
TypeSpec.Builder result = TypeSpec.classBuilder(bindingClassName.simpleName())
.addModifiers(PUBLIC);
if (isFinal) {
Expand Down Expand Up @@ -122,6 +124,8 @@ private TypeSpec createType(int sdk, boolean debuggable) {
result.addMethod(createBindingUnbindMethod(result));
}

result.addOriginatingElement(originatingElement);

return result.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Copy link

Choose a reason for hiding this comment

The 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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());
}
Expand All @@ -209,6 +213,10 @@ private Set<Class<? extends Annotation>> getSupportedAnnotations() {
return false;
}

public HashMap<String, List<Element>> getMapGeneratedFileToOriginatingElements() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/**package-private*/ @VisibleForTesting would make more sense.

return mapGeneratedFileToOriginatingElements;
}

private Map<TypeElement, BindingSet> findAndParseTargets(RoundEnvironment env) {
Map<TypeElement, BindingSet.Builder> builderMap = new LinkedHashMap<>();
Set<TypeElement> erasedTargetNames = new LinkedHashSet<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
butterknife.compiler.ButterKnifeProcessor,isolating
18 changes: 17 additions & 1 deletion butterknife/src/test/java/butterknife/BindViewTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: delete empty lines

import javax.tools.JavaFileObject;
import javax.tools.StandardLocation;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: delete empty lines

import org.junit.Test;

import java.util.List;
Copy link

Choose a reason for hiding this comment

The 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() {
Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
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
Copy link

Choose a reason for hiding this comment

The 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