public class CheckForbiddenApis
extends org.gradle.api.DefaultTask
implements org.gradle.api.tasks.util.PatternFilterable, org.gradle.api.tasks.VerificationTask, de.thetaphi.forbiddenapis.Constants
The plugin registers a separate task for each defined sourceSet
using
the default task naming convention. For default Java projects, two tasks are created:
forbiddenApisMain
and forbiddenApisTest
. Additional source sets
will produce a task with similar names ('forbiddenApis' + nameOfSourceSet
).
All tasks are added as dependencies to the check
default Gradle task.
For convenience, the plugin also defines an additional task forbiddenApis
that runs checks on all source sets.
Installation can be done from your build.gradle
file:
buildscript { repositories { mavenCentral() } dependencies { classpath 'de.thetaphi:forbiddenapis:' + FORBIDDEN_APIS_VERSION } } apply plugin: 'java' apply plugin: 'de.thetaphi.forbiddenapis'After that you can add the following task configuration closures:
forbiddenApisMain { bundledSignatures += 'jdk-system-out' }(using the
'+='
notation, you can add additional bundled signatures to the defaults).
To define those defaults, which are used by all source sets, you can use the
extension / convention mapping provided by CheckForbiddenApisExtension
:
forbiddenApis { bundledSignatures = [ 'jdk-unsafe', 'jdk-deprecated' ] signaturesFiles = files('path/to/my/signatures.txt') ignoreFailures = false }
BS_JDK_NONPORTABLE, CLASS_CONSTRUCTOR_METHOD_NAME, CONSTRUCTOR_METHOD_NAME, DEPRECATED_DESCRIPTOR, DEPRECATED_TYPE, DEPRECATED_WARN_INTERNALRUNTIME, JDK_SIG_PATTERN, LAMBDA_META_FACTORY_INTERNALNAME, LAMBDA_METHOD_NAME_PREFIX, SIGNATURE_POLYMORPHIC_DESCRIPTOR, SIGNATURE_POLYMORPHIC_PKG_INTERNALNAME
Constructor and Description |
---|
CheckForbiddenApis() |
Modifier and Type | Method and Description |
---|---|
void |
checkForbidden()
Executes the forbidden apis task.
|
CheckForbiddenApis |
exclude(groovy.lang.Closure arg0) |
CheckForbiddenApis |
exclude(java.lang.Iterable<java.lang.String> arg0) |
CheckForbiddenApis |
exclude(org.gradle.api.specs.Spec<org.gradle.api.file.FileTreeElement> arg0) |
CheckForbiddenApis |
exclude(java.lang.String... arg0) |
java.util.Set<java.lang.String> |
getBundledSignatures()
Specifies built-in signatures files (e.g., deprecated APIs for specific Java versions,
unsafe method calls using default locale, default charset,...)
|
java.io.File |
getClassesDir()
Deprecated.
use
getClassesDirs() instead. If there are more than one
classesDir set by setClassesDirs(FileCollection) , this getter may
throw an exception! |
org.gradle.api.file.FileCollection |
getClassesDirs()
Directories with the class files to check.
|
org.gradle.api.file.FileTree |
getClassFiles()
Returns the classes to check.
|
org.gradle.api.file.FileCollection |
getClasspath()
A
FileCollection used to configure the classpath. |
boolean |
getDisableClassloadingCache()
Disable the internal JVM classloading cache when getting bytecode from
the classpath.
|
java.util.Set<java.lang.String> |
getExcludes() |
boolean |
getFailOnMissingClasses()
Fail the build, if a class referenced in the scanned code is missing.
|
boolean |
getFailOnUnresolvableSignatures()
Fail the build if a signature is not resolving.
|
boolean |
getFailOnUnsupportedJava()
Fail the build, if the bundled ASM library cannot read the class file format
of the runtime library or the runtime library cannot be discovered.
|
boolean |
getIgnoreFailures() |
java.util.Set<java.lang.String> |
getIncludes() |
boolean |
getInternalRuntimeForbidden()
Deprecated.
Use bundled signatures
"jdk-non-portable" or "jdk-internal" instead. |
org.gradle.api.tasks.util.PatternSet |
getPatternSet()
Returns the pattern set to match against class files in
getClassesDir() . |
java.util.List<java.lang.String> |
getSignatures()
Gives multiple API signatures that are joined with newlines and
parsed like a single
getSignaturesFiles() . |
org.gradle.api.file.FileCollection |
getSignaturesFiles()
A
FileCollection containing all files, which contain signatures and comments for forbidden API calls. |
java.util.Set<java.net.URL> |
getSignaturesURLs()
A list of references to URLs, which contain signatures and comments for forbidden API calls.
|
java.util.Set<java.lang.String> |
getSuppressAnnotations()
List of a custom Java annotations (full class names) that are used in the checked
code to suppress errors.
|
java.lang.String |
getTargetCompatibility()
The default compiler target version used to expand references to bundled JDK signatures.
|
CheckForbiddenApis |
include(groovy.lang.Closure arg0) |
CheckForbiddenApis |
include(java.lang.Iterable<java.lang.String> arg0) |
CheckForbiddenApis |
include(org.gradle.api.specs.Spec<org.gradle.api.file.FileTreeElement> arg0) |
CheckForbiddenApis |
include(java.lang.String... arg0) |
void |
setBundledSignatures(java.util.Set<java.lang.String> bundledSignatures) |
void |
setClassesDir(java.io.File classesDir)
Deprecated.
use
setClassesDirs(FileCollection) instead. |
void |
setClassesDirs(org.gradle.api.file.FileCollection classesDirs) |
void |
setClasspath(org.gradle.api.file.FileCollection classpath) |
void |
setDisableClassloadingCache(boolean disableClassloadingCache) |
CheckForbiddenApis |
setExcludes(java.lang.Iterable<java.lang.String> excludes) |
void |
setFailOnMissingClasses(boolean failOnMissingClasses) |
void |
setFailOnUnresolvableSignatures(boolean failOnUnresolvableSignatures) |
void |
setFailOnUnsupportedJava(boolean failOnUnsupportedJava) |
void |
setIgnoreFailures(boolean ignoreFailures) |
CheckForbiddenApis |
setIncludes(java.lang.Iterable<java.lang.String> includes) |
void |
setInternalRuntimeForbidden(boolean internalRuntimeForbidden)
Deprecated.
Use bundled signatures
"jdk-non-portable" or "jdk-internal" instead. |
void |
setPatternSet(org.gradle.api.tasks.util.PatternSet patternSet) |
void |
setSignatures(java.util.List<java.lang.String> signatures) |
void |
setSignaturesFiles(org.gradle.api.file.FileCollection signaturesFiles) |
void |
setSignaturesURLs(java.util.Set<java.net.URL> signaturesURLs) |
void |
setSuppressAnnotations(java.util.Set<java.lang.String> suppressAnnotations) |
void |
setTargetCompatibility(java.lang.String targetCompatibility) |
addValidator, appendParallelSafeAction, compareTo, configure, deleteAllActions, dependsOn, dependsOnTaskDidWork, doFirst, doFirst, doFirst, doLast, doLast, doLast, execute, finalizedBy, getActions, getAnt, getAsDynamicObject, getConvention, getDependsOn, getDescription, getDestroyables, getDidWork, getEnabled, getExecuter, getExtensions, getFinalizedBy, getGroup, getIdentityPath, getImpliesSubProjects, getInputs, getLocalState, getLogger, getLogging, getMustRunAfter, getName, getOnlyIf, getOutputs, getPath, getProject, getShouldRunAfter, getStandardOutputCapture, getState, getTaskActions, getTaskDependencies, getTemporaryDir, getTemporaryDirFactory, getValidators, hasProperty, injectIntoNewInstance, isEnabled, isHasCustomActions, leftShift, mustRunAfter, onlyIf, onlyIf, prependParallelSafeAction, property, setActions, setDependsOn, setDescription, setDidWork, setEnabled, setExecuter, setFinalizedBy, setGroup, setImpliesSubProjects, setMustRunAfter, setOnlyIf, setOnlyIf, setProperty, setShouldRunAfter, shouldRunAfter, toString
@OutputDirectories public org.gradle.api.file.FileCollection getClassesDirs()
public void setClassesDirs(org.gradle.api.file.FileCollection classesDirs)
getClassesDirs()
@Deprecated public java.io.File getClassesDir()
getClassesDirs()
instead. If there are more than one
classesDir
set by setClassesDirs(FileCollection)
, this getter may
throw an exception!@Deprecated public void setClassesDir(java.io.File classesDir)
setClassesDirs(FileCollection)
instead.setClassesDirs(FileCollection)
!getClassesDir()
public org.gradle.api.tasks.util.PatternSet getPatternSet()
getClassesDir()
.public void setPatternSet(org.gradle.api.tasks.util.PatternSet patternSet)
getPatternSet()
@InputFiles public org.gradle.api.file.FileCollection getClasspath()
FileCollection
used to configure the classpath.
Defaults to current sourseSet's compile classpath.public void setClasspath(org.gradle.api.file.FileCollection classpath)
getClasspath()
@InputFiles @Optional public org.gradle.api.file.FileCollection getSignaturesFiles()
FileCollection
containing all files, which contain signatures and comments for forbidden API calls.
The signatures are resolved against getClasspath()
.public void setSignaturesFiles(org.gradle.api.file.FileCollection signaturesFiles)
getSignaturesFiles()
@Input @Optional @Incubating public java.util.Set<java.net.URL> getSignaturesURLs()
getClasspath()
.
This property is useful to refer to resources in plugin classpath, e.g., using
Class.getResource(String)
. It is not useful for general gradle builds. Especially,
don't use it to refer to resources on foreign servers!
public void setSignaturesURLs(java.util.Set<java.net.URL> signaturesURLs)
getSignaturesURLs()
@Input @Optional public java.util.List<java.lang.String> getSignatures()
getSignaturesFiles()
.
The signatures are resolved against getClasspath()
.public void setSignatures(java.util.List<java.lang.String> signatures)
getSignatures()
@Input @Optional public java.util.Set<java.lang.String> getBundledSignatures()
public void setBundledSignatures(java.util.Set<java.lang.String> bundledSignatures)
getBundledSignatures()
@Deprecated @Input public boolean getInternalRuntimeForbidden()
"jdk-non-portable"
or "jdk-internal"
instead.sun.misc.Unsafe
).
Please note: This enables "jdk-non-portable"
bundled signatures for backwards compatibility.
Defaults to false
.@Deprecated public void setInternalRuntimeForbidden(boolean internalRuntimeForbidden)
"jdk-non-portable"
or "jdk-internal"
instead.getInternalRuntimeForbidden()
@Input public boolean getFailOnUnsupportedJava()
false
.public void setFailOnUnsupportedJava(boolean failOnUnsupportedJava)
getFailOnUnsupportedJava()
@Input public boolean getFailOnMissingClasses()
true
.public void setFailOnMissingClasses(boolean failOnMissingClasses)
getFailOnMissingClasses()
@Input public boolean getFailOnUnresolvableSignatures()
true
.public void setFailOnUnresolvableSignatures(boolean failOnUnresolvableSignatures)
getFailOnUnresolvableSignatures()
@Input public boolean getIgnoreFailures()
This setting is to conform with VerificationTask
interface.
Other ForbiddenApis implementations use another name: failOnViolation
Default is false
.
getIgnoreFailures
in interface org.gradle.api.tasks.VerificationTask
public void setIgnoreFailures(boolean ignoreFailures)
setIgnoreFailures
in interface org.gradle.api.tasks.VerificationTask
@Input public boolean getDisableClassloadingCache()
FileNotFoundException
s related to non-existent JAR entries
you can try to work around using this setting.
The default is false
, unless the plugin detects that your build is
running in the Gradle Daemon (which has this problem), setting the
default to true
as a consequence.
public void setDisableClassloadingCache(boolean disableClassloadingCache)
getDisableClassloadingCache()
@Input @Optional public java.util.Set<java.lang.String> getSuppressAnnotations()
RetentionPolicy.CLASS
. They can be applied to classes, their methods,
or fields. By default, @de.thetaphi.forbiddenapis.SuppressForbidden
can always be used, but needs the forbidden-apis.jar
file in classpath
of compiled project, which may not be wanted.
Instead of a full class name, a glob pattern may be used (e.g.,
**.SuppressForbidden
).public void setSuppressAnnotations(java.util.Set<java.lang.String> suppressAnnotations)
getSuppressAnnotations()
@Input @Optional public java.lang.String getTargetCompatibility()
project.targetCompatibility
.public void setTargetCompatibility(java.lang.String targetCompatibility)
getTargetCompatibility()
@Input public java.util.Set<java.lang.String> getIncludes()
Set of patterns matching all class files to be parsed from the classesDirectory. Can be changed to e.g. exclude several files (using excludes). The default is a single include with pattern '**/*.class'
getIncludes
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis setIncludes(java.lang.Iterable<java.lang.String> includes)
setIncludes
in interface org.gradle.api.tasks.util.PatternFilterable
@Input public java.util.Set<java.lang.String> getExcludes()
Set of patterns matching class files to be excluded from checking.
getExcludes
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis setExcludes(java.lang.Iterable<java.lang.String> excludes)
setExcludes
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis exclude(java.lang.String... arg0)
exclude
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis exclude(java.lang.Iterable<java.lang.String> arg0)
exclude
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis exclude(org.gradle.api.specs.Spec<org.gradle.api.file.FileTreeElement> arg0)
exclude
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis exclude(groovy.lang.Closure arg0)
exclude
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis include(java.lang.String... arg0)
include
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis include(java.lang.Iterable<java.lang.String> arg0)
include
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis include(org.gradle.api.specs.Spec<org.gradle.api.file.FileTreeElement> arg0)
include
in interface org.gradle.api.tasks.util.PatternFilterable
public CheckForbiddenApis include(groovy.lang.Closure arg0)
include
in interface org.gradle.api.tasks.util.PatternFilterable
@InputFiles @SkipWhenEmpty public org.gradle.api.file.FileTree getClassFiles()
public void checkForbidden() throws de.thetaphi.forbiddenapis.ForbiddenApiException
de.thetaphi.forbiddenapis.ForbiddenApiException