aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChristian Williams <christianw@google.com>2017-10-30 18:52:44 -0700
committerChristian Williams <christianw@google.com>2017-10-30 18:58:30 -0700
commit6a4bdde44decbc23efd0bdce0d2512f7e5508c50 (patch)
treefa7009825f6c810d2a60ef56c640cbbe63c4c124
parent3be3311dc3456cc69679cfaeb0cfa28f6f48ca2f (diff)
downloadrobolectric-shadows-6a4bdde44decbc23efd0bdce0d2512f7e5508c50.tar.gz
Use net.ltgt.apt gradle plugin to enable annotation processing.
-rw-r--r--build.gradle24
-rw-r--r--buildSrc/src/main/groovy/ShadowsPlugin.groovy57
-rw-r--r--processor/build.gradle4
-rw-r--r--processor/src/main/java/org/robolectric/annotation/processing/RobolectricProcessor.java8
-rw-r--r--processor/src/test/java/org/robolectric/annotation/processing/RobolectricProcessorTest.java22
-rw-r--r--processor/src/test/java/org/robolectric/annotation/processing/validator/RealObjectValidatorTest.java5
-rw-r--r--processor/src/test/java/org/robolectric/annotation/processing/validator/SingleClassSubject.java3
7 files changed, 67 insertions, 56 deletions
diff --git a/build.gradle b/build.gradle
index d59c9feb3..c0d8cd754 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,3 +1,16 @@
+buildscript {
+ repositories { jcenter() }
+
+ dependencies {
+ classpath 'com.netflix.nebula:gradle-aggregate-javadocs-plugin:2.2.+'
+ classpath 'ch.raffael.pegdown-doclet:pegdown-doclet:1.3'
+ }
+}
+
+plugins {
+ id "net.ltgt.apt" version "0.12" // automatic annotation processing
+}
+
allprojects {
repositories {
mavenLocal()
@@ -8,12 +21,13 @@ allprojects {
version = thisVersion
}
-buildscript {
- repositories { jcenter() }
+apply plugin: 'idea'
+apply plugin: 'net.ltgt.apt-idea'
- dependencies {
- classpath 'com.netflix.nebula:gradle-aggregate-javadocs-plugin:2.2.+'
- classpath 'ch.raffael.pegdown-doclet:pegdown-doclet:1.3'
+idea {
+ project {
+ // experimental: whether annotation processing will be configured in the IDE; only actually used with the 'idea' task.
+ configureAnnotationProcessing = true
}
}
diff --git a/buildSrc/src/main/groovy/ShadowsPlugin.groovy b/buildSrc/src/main/groovy/ShadowsPlugin.groovy
index 7b61e64f0..46c6c7cc8 100644
--- a/buildSrc/src/main/groovy/ShadowsPlugin.groovy
+++ b/buildSrc/src/main/groovy/ShadowsPlugin.groovy
@@ -7,50 +7,37 @@ import org.gradle.util.GFileUtils
class ShadowsPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
- project.extensions.create("shadows", ShadowsPluginExtension)
+ project.apply plugin: "idea"
+ project.apply plugin: "net.ltgt.apt-idea"
- project.configurations {
- robolectricProcessor
- }
+ project.extensions.create("shadows", ShadowsPluginExtension)
project.dependencies {
- robolectricProcessor project.project(":processor")
+ apt project.project(":processor")
}
- def generatedSourcesDir = "${project.buildDir}/generated-shadows"
-
- project.sourceSets.main.java.srcDirs += project.file(generatedSourcesDir)
-
- project.task("generateShadowProvider", type: JavaCompile, description: "Generate Shadows.shadowOf()s class") { task ->
- classpath = project.configurations.robolectricProcessor
- source = project.sourceSets.main.java
- destinationDir = project.file(generatedSourcesDir)
-
- doFirst {
- logger.info "Generating Shadows.java for ${project.name}…"
-
- // reset our classpath at the last minute, since other plugins might mutate
- // compileJava's classpath and we want to pick up any changes…
- classpath = project.tasks['compileJava'].classpath + project.configurations.robolectricProcessor
+ def compileJavaTask = project.tasks["compileJava"]
+ compileJavaTask.doFirst {
+ options.compilerArgs.add("-Aorg.robolectric.annotation.processing.shadowPackage=${project.shadows.packageName}")
+ }
- options.compilerArgs.addAll(
- "-proc:only",
- "-processor", "org.robolectric.annotation.processing.RobolectricProcessor",
- "-Aorg.robolectric.annotation.processing.shadowPackage=${project.shadows.packageName}"
- )
+ project.idea {
+ module {
+ apt {
+ // whether generated sources dirs are added as generated sources root
+ addGeneratedSourcesDirs = true
+ // whether the apt and testApt dependencies are added as module dependencies
+ addAptDependencies = true
+
+ // The following are mostly internal details; you shouldn't ever need to configure them.
+ // whether the compileOnly and testCompileOnly dependencies are added as module dependencies
+ addCompileOnlyDependencies = false // defaults to true in Gradle < 2.12
+ // the dependency scope used for apt and/or compileOnly dependencies (when enabled above)
+ mainDependenciesScope = "PROVIDED" // defaults to "COMPILE" in Gradle < 3.4, or when using the Gradle integration in IntelliJ IDEA
+ }
}
- doLast {
- def src = project.file("$generatedSourcesDir/META-INF/services/org.robolectric.internal.ShadowProvider")
- def dest = project.file("${project.buildDir}/resources/main/META-INF/services/org.robolectric.internal.ShadowProvider")
-
- GFileUtils.mkdirs(dest.getParentFile());
- GFileUtils.copyFile(src, dest);
- }
}
-
- def compileJavaTask = project.tasks["compileJava"]
- compileJavaTask.dependsOn("generateShadowProvider")
}
static class ShadowsPluginExtension {
diff --git a/processor/build.gradle b/processor/build.gradle
index 03de51a05..5ee74548d 100644
--- a/processor/build.gradle
+++ b/processor/build.gradle
@@ -6,6 +6,10 @@ new RoboJavaModulePlugin(
deploy: true
).apply(project)
+// Disable annotation processor for tests
+compileTestJava {
+ options.compilerArgs.add("-proc:none")
+}
dependencies {
// Project dependencies
diff --git a/processor/src/main/java/org/robolectric/annotation/processing/RobolectricProcessor.java b/processor/src/main/java/org/robolectric/annotation/processing/RobolectricProcessor.java
index 0b0fd4808..95882bdbb 100644
--- a/processor/src/main/java/org/robolectric/annotation/processing/RobolectricProcessor.java
+++ b/processor/src/main/java/org/robolectric/annotation/processing/RobolectricProcessor.java
@@ -1,5 +1,6 @@
package org.robolectric.annotation.processing;
+import com.google.common.annotations.VisibleForTesting;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -57,7 +58,8 @@ public class RobolectricProcessor extends AbstractProcessor {
* @param options simulated options that would ordinarily
* be passed in the {@link ProcessingEnvironment}.
*/
- RobolectricProcessor(Map<String, String> options) {
+ @VisibleForTesting
+ public RobolectricProcessor(Map<String, String> options) {
processOptions(options);
}
@@ -108,6 +110,10 @@ public class RobolectricProcessor extends AbstractProcessor {
this.shadowPackage = options.get(PACKAGE_OPT);
this.shouldInstrumentPackages =
!"false".equalsIgnoreCase(options.get(SHOULD_INSTRUMENT_PKG_OPT));
+
+ if (this.shadowPackage == null) {
+ throw new IllegalArgumentException("no package specified for " + PACKAGE_OPT);
+ }
}
}
diff --git a/processor/src/test/java/org/robolectric/annotation/processing/RobolectricProcessorTest.java b/processor/src/test/java/org/robolectric/annotation/processing/RobolectricProcessorTest.java
index 33501a287..6b7a7725b 100644
--- a/processor/src/test/java/org/robolectric/annotation/processing/RobolectricProcessorTest.java
+++ b/processor/src/test/java/org/robolectric/annotation/processing/RobolectricProcessorTest.java
@@ -32,21 +32,19 @@ import org.junit.runners.JUnit4;
@RunWith(JUnit4.class)
public class RobolectricProcessorTest {
- private static final Map<String,String> DEFAULT_OPTS = new HashMap<>();
-
- static {
- DEFAULT_OPTS.put(PACKAGE_OPT, "org.robolectric");
- }
+ public static final Map<String,String> DEFAULT_OPTS = new HashMap<String, String>() {{
+ put(PACKAGE_OPT, "org.robolectric");
+ }};
@Test
public void robolectricProcessor_supportsPackageOption() {
- assertThat(new RobolectricProcessor().getSupportedOptions()).contains(PACKAGE_OPT);
+ assertThat(new RobolectricProcessor(DEFAULT_OPTS).getSupportedOptions()).contains(PACKAGE_OPT);
}
@Test
public void robolectricProcessor_supportsShouldInstrumentPackageOption() {
assertThat(
- new RobolectricProcessor().getSupportedOptions()).contains(SHOULD_INSTRUMENT_PKG_OPT);
+ new RobolectricProcessor(DEFAULT_OPTS).getSupportedOptions()).contains(SHOULD_INSTRUMENT_PKG_OPT);
}
@Test
@@ -57,7 +55,7 @@ public class RobolectricProcessorTest {
SHADOW_PROVIDER_SOURCE,
SHADOW_EXTRACTOR_SOURCE,
forSourceString("HelloWorld", "final class HelloWorld {}")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.compilesWithoutError();
//.and().generatesNoSources(); Should add this assertion onces
// it becomes available in compile-testing
@@ -190,7 +188,7 @@ public class RobolectricProcessorTest {
SHADOW_PROVIDER_SOURCE,
SHADOW_EXTRACTOR_SOURCE,
forResource("org/robolectric/annotation/TestWithUnrecognizedAnnotation.java")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.compilesWithoutError();
}
@@ -198,7 +196,7 @@ public class RobolectricProcessorTest {
public void shouldGracefullyHandleNoAnythingClass_withNoRealObject() {
assertAbout(javaSource())
.that(forResource("org/robolectric/annotation/processing/shadows/ShadowAnything.java"))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.failsToCompile();
}
@@ -209,7 +207,7 @@ public class RobolectricProcessorTest {
SHADOW_PROVIDER_SOURCE,
SHADOW_EXTRACTOR_SOURCE,
forResource("org/robolectric/annotation/processing/shadows/ShadowRealObjectWithCorrectAnything.java")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.failsToCompile();
}
@@ -253,7 +251,7 @@ public class RobolectricProcessorTest {
.that(ImmutableList.of(
ROBO_SOURCE,
forResource("org/robolectric/annotation/processing/shadows/DocumentedObjectShadow.java")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.compilesWithoutError();
JsonParser jsonParser = new JsonParser();
String jsonFile = "build/docs/json/org.robolectric.Robolectric.DocumentedObject.json";
diff --git a/processor/src/test/java/org/robolectric/annotation/processing/validator/RealObjectValidatorTest.java b/processor/src/test/java/org/robolectric/annotation/processing/validator/RealObjectValidatorTest.java
index a37796b13..38dd39f5b 100644
--- a/processor/src/test/java/org/robolectric/annotation/processing/validator/RealObjectValidatorTest.java
+++ b/processor/src/test/java/org/robolectric/annotation/processing/validator/RealObjectValidatorTest.java
@@ -3,6 +3,7 @@ package org.robolectric.annotation.processing.validator;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaFileObjects.forResource;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
+import static org.robolectric.annotation.processing.RobolectricProcessorTest.DEFAULT_OPTS;
import static org.robolectric.annotation.processing.validator.SingleClassSubject.singleClass;
import static org.robolectric.annotation.processing.validator.Utils.SHADOW_EXTRACTOR_SOURCE;
@@ -102,7 +103,7 @@ public class RealObjectValidatorTest {
.that(ImmutableList.of(
SHADOW_EXTRACTOR_SOURCE,
forResource("org/robolectric/annotation/processing/shadows/ShadowRealObjectWithCorrectType.java")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.compilesWithoutError();
}
@@ -120,7 +121,7 @@ public class RealObjectValidatorTest {
.that(ImmutableList.of(
SHADOW_EXTRACTOR_SOURCE,
forResource("org/robolectric/annotation/processing/shadows/ShadowRealObjectWithCorrectClassName.java")))
- .processedWith(new RobolectricProcessor())
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS))
.compilesWithoutError();
}
diff --git a/processor/src/test/java/org/robolectric/annotation/processing/validator/SingleClassSubject.java b/processor/src/test/java/org/robolectric/annotation/processing/validator/SingleClassSubject.java
index 92a024bd8..e17061293 100644
--- a/processor/src/test/java/org/robolectric/annotation/processing/validator/SingleClassSubject.java
+++ b/processor/src/test/java/org/robolectric/annotation/processing/validator/SingleClassSubject.java
@@ -2,6 +2,7 @@ package org.robolectric.annotation.processing.validator;
import static com.google.common.truth.Truth.assertAbout;
import static com.google.testing.compile.JavaSourcesSubjectFactory.javaSources;
+import static org.robolectric.annotation.processing.RobolectricProcessorTest.DEFAULT_OPTS;
import com.google.common.collect.ImmutableList;
import com.google.common.truth.FailureStrategy;
@@ -37,7 +38,7 @@ public final class SingleClassSubject extends Subject<SingleClassSubject, String
source = JavaFileObjects.forResource(Utils.toResourcePath(subject));
tester = assertAbout(javaSources())
.that(ImmutableList.of(source, Utils.ROBO_SOURCE, Utils.SHADOW_EXTRACTOR_SOURCE))
- .processedWith(new RobolectricProcessor());
+ .processedWith(new RobolectricProcessor(DEFAULT_OPTS));
}
public SuccessfulCompilationClause compilesWithoutError() {