@@ -59,7 +59,7 @@ public static Compiler javac() {
5959 /** Returns a {@link Compiler} that uses a given {@link JavaCompiler} instance. */
6060 public static Compiler compiler (JavaCompiler javaCompiler ) {
6161 return new AutoValue_Compiler (
62- javaCompiler , ImmutableList .of (), ImmutableList .of (), Optional .empty ());
62+ javaCompiler , ImmutableList .of (), ImmutableList .of (), Optional .empty (), Optional . empty () );
6363 }
6464
6565 abstract JavaCompiler javaCompiler ();
@@ -73,6 +73,11 @@ public static Compiler compiler(JavaCompiler javaCompiler) {
7373 /** The compilation class path. If not present, the system class path is used. */
7474 public abstract Optional <ImmutableList <File >> classPath ();
7575
76+ /**
77+ * The annotation processor path. If not present, the system annotation processor path is used.
78+ */
79+ public abstract Optional <ImmutableList <File >> annotationProcessorPath ();
80+
7681 /**
7782 * Uses annotation processors during compilation. These replace any previously specified.
7883 *
@@ -92,7 +97,8 @@ public final Compiler withProcessors(Processor... processors) {
9297 * @return a new instance with the same options and the given processors
9398 */
9499 public final Compiler withProcessors (Iterable <? extends Processor > processors ) {
95- return copy (ImmutableList .copyOf (processors ), options (), classPath ());
100+ return copy (
101+ ImmutableList .copyOf (processors ), options (), classPath (), annotationProcessorPath ());
96102 }
97103
98104 /**
@@ -113,7 +119,8 @@ public final Compiler withOptions(Iterable<?> options) {
113119 return copy (
114120 processors (),
115121 FluentIterable .from (options ).transform (toStringFunction ()).toList (),
116- classPath ());
122+ classPath (),
123+ annotationProcessorPath ());
117124 }
118125
119126 /**
@@ -128,12 +135,32 @@ public final Compiler withOptions(Iterable<?> options) {
128135 */
129136 @ Deprecated
130137 public final Compiler withClasspathFrom (ClassLoader classloader ) {
131- return copy (processors (), options (), Optional .of (getClasspathFromClassloader (classloader )));
138+ return copy (
139+ processors (),
140+ options (),
141+ Optional .of (getClasspathFromClassloader (classloader )),
142+ annotationProcessorPath ());
132143 }
133144
134145 /** Uses the given classpath for the compilation instead of the system classpath. */
135146 public final Compiler withClasspath (Iterable <File > classPath ) {
136- return copy (processors (), options (), Optional .of (ImmutableList .copyOf (classPath )));
147+ return copy (
148+ processors (),
149+ options (),
150+ Optional .of (ImmutableList .copyOf (classPath )),
151+ annotationProcessorPath ());
152+ }
153+
154+ /**
155+ * Uses the given annotation processor path for the compilation instead of the system annotation
156+ * processor path.
157+ */
158+ public final Compiler withAnnotationProcessorPath (Iterable <File > annotationProcessorPath ) {
159+ return copy (
160+ processors (),
161+ options (),
162+ classPath (),
163+ Optional .of (ImmutableList .copyOf (annotationProcessorPath )));
137164 }
138165
139166 /**
@@ -155,16 +182,10 @@ public final Compilation compile(Iterable<? extends JavaFileObject> files) {
155182 InMemoryJavaFileManager fileManager =
156183 new InMemoryJavaFileManager (
157184 javaCompiler ().getStandardFileManager (diagnosticCollector , Locale .getDefault (), UTF_8 ));
158- classPath ()
185+ classPath ().ifPresent (path -> setLocation (fileManager , StandardLocation .CLASS_PATH , path ));
186+ annotationProcessorPath ()
159187 .ifPresent (
160- classPath -> {
161- try {
162- fileManager .setLocation (StandardLocation .CLASS_PATH , classPath );
163- } catch (IOException e ) {
164- // impossible by specification
165- throw new UncheckedIOException (e );
166- }
167- });
188+ path -> setLocation (fileManager , StandardLocation .ANNOTATION_PROCESSOR_PATH , path ));
168189 CompilationTask task =
169190 javaCompiler ()
170191 .getTask (
@@ -247,10 +268,22 @@ private static ImmutableList<File> getClasspathFromClassloader(ClassLoader curre
247268 return classpaths .stream ().map (File ::new ).collect (toImmutableList ());
248269 }
249270
271+ private static void setLocation (
272+ InMemoryJavaFileManager fileManager , StandardLocation location , ImmutableList <File > path ) {
273+ try {
274+ fileManager .setLocation (location , path );
275+ } catch (IOException e ) {
276+ // impossible by specification
277+ throw new UncheckedIOException (e );
278+ }
279+ }
280+
250281 private Compiler copy (
251282 ImmutableList <Processor > processors ,
252283 ImmutableList <String > options ,
253- Optional <ImmutableList <File >> classPath ) {
254- return new AutoValue_Compiler (javaCompiler (), processors , options , classPath );
284+ Optional <ImmutableList <File >> classPath ,
285+ Optional <ImmutableList <File >> annotationProcessorPath ) {
286+ return new AutoValue_Compiler (
287+ javaCompiler (), processors , options , classPath , annotationProcessorPath );
255288 }
256289}
0 commit comments