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

Remove warning in Gradle 4 #124

Merged
merged 2 commits into from
Jul 26, 2017
Merged
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 @@ -27,6 +27,7 @@
import java.net.URL;
import java.net.URLClassLoader;
import java.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
Expand All @@ -43,7 +44,7 @@
import org.gradle.api.tasks.Input;
import org.gradle.api.tasks.InputFiles;
import org.gradle.api.tasks.Optional;
import org.gradle.api.tasks.OutputDirectory;
import org.gradle.api.tasks.OutputDirectories;
import org.gradle.api.tasks.ParallelizableTask;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
Expand Down Expand Up @@ -109,23 +110,49 @@ public class CheckForbiddenApis extends DefaultTask implements PatternFilterable

private final CheckForbiddenApisExtension data = new CheckForbiddenApisExtension();
private final PatternSet patternSet = new PatternSet().include("**/*.class");
private File classesDir;
private FileCollection classesDirs;
private FileCollection classpath;
private String targetCompatibility;

/**
* Directories with the class files to check.
* Defaults to current sourseSet's output directory (Gradle 2/3) or output directories (Gradle 4.0+).
*/
@OutputDirectories
// no @InputDirectories, we use separate getter for a list of all input files
public FileCollection getClassesDirs() {
return classesDirs;
}

/** @see #getClassesDirs */
public void setClassesDirs(FileCollection classesDirs) {
if (classesDirs == null) throw new NullPointerException("classesDirs");
this.classesDirs = classesDirs;
}

/**
* Directory with the class files to check.
* Defaults to current sourseSet's output directory.
* Defaults to current sourseSet's output directory (Gradle 2/3 only).
* @deprecated use {@link #getClassesDirs()} instead. If there are more than one
* {@code classesDir} set by {@link #setClassesDirs(FileCollection)}, this getter may
* throw an exception!
*/
@OutputDirectory
// no @InputDirectory, we use separate getter for a list of all input files
@Deprecated
public File getClassesDir() {
return classesDir;
final FileCollection col = getClassesDirs();
return (col == null) ? null : col.getSingleFile();
}

/** @see #getClassesDir */
/** Sets the directory where to look for classes. Overwrites any value set by {@link #setClassesDirs(FileCollection)}!
* @deprecated use {@link #setClassesDirs(FileCollection)} instead.
* @see #getClassesDir
*/
@Deprecated
public void setClassesDir(File classesDir) {
this.classesDir = classesDir;
if (classesDir == null) throw new NullPointerException("classesDir");
getLogger().warn("The 'classesDir' property on the '{}' task is deprecated. Use 'classesDirs' of type FileCollection instead!",
getName());
setClassesDirs(getProject().files(classesDir));
}

/** Returns the pattern set to match against class files in {@link #getClassesDir()}. */
Expand All @@ -149,6 +176,7 @@ public FileCollection getClasspath() {

/** @see #getClasspath */
public void setClasspath(FileCollection classpath) {
if (classpath == null) throw new NullPointerException("classpath");
this.classpath = classpath;
}

Expand Down Expand Up @@ -453,16 +481,16 @@ public CheckForbiddenApis include(@SuppressWarnings("rawtypes") Closure arg0) {
@InputFiles
@SkipWhenEmpty
public FileTree getClassFiles() {
return getProject().files(getClassesDir()).getAsFileTree().matching(getPatternSet());
return getClassesDirs().getAsFileTree().matching(getPatternSet());
}

/** Executes the forbidden apis task. */
@TaskAction
public void checkForbidden() throws ForbiddenApiException {
final File classesDir = getClassesDir();
final FileCollection classesDirs = getClassesDirs();
final FileCollection classpath = getClasspath();
if (classesDir == null || classpath == null) {
throw new InvalidUserDataException("Missing 'classesDir' or 'classpath' property.");
if (classesDirs == null || classpath == null) {
throw new InvalidUserDataException("Missing 'classesDirs' or 'classpath' property.");
}

final Logger log = new Logger() {
Expand All @@ -482,14 +510,15 @@ public void info(String msg) {
}
};

final Set<File> cpElements = classpath.getFiles();
final URL[] urls = new URL[cpElements.size() + 1];
final Set<File> cpElements = new LinkedHashSet<File>();
cpElements.addAll(classpath.getFiles());
cpElements.addAll(classesDirs.getFiles());
final URL[] urls = new URL[cpElements.size()];
try {
int i = 0;
for (final File cpElement : cpElements) {
urls[i++] = cpElement.toURI().toURL();
}
urls[i++] = classesDir.toURI().toURL();
assert i == urls.length;
} catch (MalformedURLException mfue) {
throw new InvalidUserDataException("Failed to build classpath URLs.", mfue);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,20 @@ def extensionProps = CheckForbiddenApisExtension.class.declaredFields.findAll{ f

// Define our tasks (one for each SourceSet):
def forbiddenTasks = project.sourceSets.collect{ sourceSet ->
def getClassesDirs = { sourceSet.output.hasProperty('classesDirs') ? sourceSet.output.classesDirs : project.files(sourceSet.output.classesDir) }
project.tasks.create(sourceSet.getTaskName(FORBIDDEN_APIS_TASK_NAME, null), CheckForbiddenApis.class) {
description = "Runs forbidden-apis checks on '${sourceSet.name}' classes.";
conventionMapping.with{
extensionProps.each{ key ->
map(key, { extension[key] });
}
classesDir = { sourceSet.output.classesDir }
classesDirs = { getClassesDirs() }
classpath = { sourceSet.compileClasspath }
targetCompatibility = { project.targetCompatibility?.toString() }
}
// add dependency to compile task after evaluation, if the classesDir is from our SourceSet:
project.afterEvaluate{
if (classesDir == sourceSet.output.classesDir) {
// add dependency to compile task after evaluation, if the classesDirs collection has overlaps with our SourceSet:
project.afterEvaluate{
if (!classesDirs.minus(getClassesDirs()).isEmpty()) {
dependsOn(sourceSet.output);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/test/gradle/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,13 @@ forbiddenApis {
}

forbiddenApisMain {
classesDir = new File(forbiddenRootDir, 'build/main')
classesDirs = files(new File(forbiddenRootDir, 'build/main'))
classpath = files(forbiddenClasspath.tokenize(File.pathSeparator))
bundledSignatures += 'jdk-system-out'
}

forbiddenApisTest {
// use classesDir here to check backwards compatibility!:
classesDir = new File(forbiddenRootDir, 'build/test')
classpath = files(forbiddenTestClasspath.tokenize(File.pathSeparator))
}