27
27
import java .net .URL ;
28
28
import java .net .URLClassLoader ;
29
29
import java .util .EnumSet ;
30
+ import java .util .LinkedHashSet ;
30
31
import java .util .List ;
31
32
import java .util .Locale ;
32
33
import java .util .Set ;
43
44
import org .gradle .api .tasks .Input ;
44
45
import org .gradle .api .tasks .InputFiles ;
45
46
import org .gradle .api .tasks .Optional ;
46
- import org .gradle .api .tasks .OutputDirectory ;
47
+ import org .gradle .api .tasks .OutputDirectories ;
47
48
import org .gradle .api .tasks .ParallelizableTask ;
48
49
import org .gradle .api .tasks .SkipWhenEmpty ;
49
50
import org .gradle .api .tasks .TaskAction ;
@@ -109,23 +110,49 @@ public class CheckForbiddenApis extends DefaultTask implements PatternFilterable
109
110
110
111
private final CheckForbiddenApisExtension data = new CheckForbiddenApisExtension ();
111
112
private final PatternSet patternSet = new PatternSet ().include ("**/*.class" );
112
- private File classesDir ;
113
+ private FileCollection classesDirs ;
113
114
private FileCollection classpath ;
114
115
private String targetCompatibility ;
115
116
117
+ /**
118
+ * Directories with the class files to check.
119
+ * Defaults to current sourseSet's output directory (Gradle 2/3) or output directories (Gradle 4.0+).
120
+ */
121
+ @ OutputDirectories
122
+ // no @InputDirectories, we use separate getter for a list of all input files
123
+ public FileCollection getClassesDirs () {
124
+ return classesDirs ;
125
+ }
126
+
127
+ /** @see #getClassesDirs */
128
+ public void setClassesDirs (FileCollection classesDirs ) {
129
+ if (classesDirs == null ) throw new NullPointerException ("classesDirs" );
130
+ this .classesDirs = classesDirs ;
131
+ }
132
+
116
133
/**
117
134
* Directory with the class files to check.
118
- * Defaults to current sourseSet's output directory.
135
+ * Defaults to current sourseSet's output directory (Gradle 2/3 only).
136
+ * @deprecated use {@link #getClassesDirs()} instead. If there are more than one
137
+ * {@code classesDir} set by {@link #setClassesDirs(FileCollection)}, this getter may
138
+ * throw an exception!
119
139
*/
120
- @ OutputDirectory
121
- // no @InputDirectory, we use separate getter for a list of all input files
140
+ @ Deprecated
122
141
public File getClassesDir () {
123
- return classesDir ;
142
+ final FileCollection col = getClassesDirs ();
143
+ return (col == null ) ? null : col .getSingleFile ();
124
144
}
125
145
126
- /** @see #getClassesDir */
146
+ /** Sets the directory where to look for classes. Overwrites any value set by {@link #setClassesDirs(FileCollection)}!
147
+ * @deprecated use {@link #setClassesDirs(FileCollection)} instead.
148
+ * @see #getClassesDir
149
+ */
150
+ @ Deprecated
127
151
public void setClassesDir (File classesDir ) {
128
- this .classesDir = classesDir ;
152
+ if (classesDir == null ) throw new NullPointerException ("classesDir" );
153
+ getLogger ().warn ("The 'classesDir' property on the '{}' task is deprecated. Use 'classesDirs' of type FileCollection instead!" ,
154
+ getName ());
155
+ setClassesDirs (getProject ().files (classesDir ));
129
156
}
130
157
131
158
/** Returns the pattern set to match against class files in {@link #getClassesDir()}. */
@@ -149,6 +176,7 @@ public FileCollection getClasspath() {
149
176
150
177
/** @see #getClasspath */
151
178
public void setClasspath (FileCollection classpath ) {
179
+ if (classpath == null ) throw new NullPointerException ("classpath" );
152
180
this .classpath = classpath ;
153
181
}
154
182
@@ -453,16 +481,16 @@ public CheckForbiddenApis include(@SuppressWarnings("rawtypes") Closure arg0) {
453
481
@ InputFiles
454
482
@ SkipWhenEmpty
455
483
public FileTree getClassFiles () {
456
- return getProject (). files ( getClassesDir () ).getAsFileTree ().matching (getPatternSet ());
484
+ return getClassesDirs ( ).getAsFileTree ().matching (getPatternSet ());
457
485
}
458
486
459
487
/** Executes the forbidden apis task. */
460
488
@ TaskAction
461
489
public void checkForbidden () throws ForbiddenApiException {
462
- final File classesDir = getClassesDir ();
490
+ final FileCollection classesDirs = getClassesDirs ();
463
491
final FileCollection classpath = getClasspath ();
464
- if (classesDir == null || classpath == null ) {
465
- throw new InvalidUserDataException ("Missing 'classesDir ' or 'classpath' property." );
492
+ if (classesDirs == null || classpath == null ) {
493
+ throw new InvalidUserDataException ("Missing 'classesDirs ' or 'classpath' property." );
466
494
}
467
495
468
496
final Logger log = new Logger () {
@@ -482,14 +510,15 @@ public void info(String msg) {
482
510
}
483
511
};
484
512
485
- final Set <File > cpElements = classpath .getFiles ();
486
- final URL [] urls = new URL [cpElements .size () + 1 ];
513
+ final Set <File > cpElements = new LinkedHashSet <File >();
514
+ cpElements .addAll (classpath .getFiles ());
515
+ cpElements .addAll (classesDirs .getFiles ());
516
+ final URL [] urls = new URL [cpElements .size ()];
487
517
try {
488
518
int i = 0 ;
489
519
for (final File cpElement : cpElements ) {
490
520
urls [i ++] = cpElement .toURI ().toURL ();
491
521
}
492
- urls [i ++] = classesDir .toURI ().toURL ();
493
522
assert i == urls .length ;
494
523
} catch (MalformedURLException mfue ) {
495
524
throw new InvalidUserDataException ("Failed to build classpath URLs." , mfue );
0 commit comments