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

Modify configs earlier; Support sourceCompatibility #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -35,22 +35,23 @@ final class CheckerPlugin implements Plugin<Project> {

@Override void apply(Project project) {
CheckerExtension userConfig = project.extensions.create("checkerFramework", CheckerExtension)

project.gradle.projectsEvaluated {
AppliedPlugin prereq = (ANDROID_IDS + "java").findResult(project.pluginManager.&findPlugin)
if (!prereq) LOG.warn('No android or java plugins found, checker compiler options will not be applied.')
else {
LOG.info('Found plugin {}, applying checker compiler options.', prereq.id)
boolean applied = false
(ANDROID_IDS + "java").each { id ->
project.pluginManager.withPlugin(id) {
LOG.info('Found plugin {}, applying checker compiler options.', id)
configureProject(project, userConfig)
if (!applied) applied = true
}
}
project.gradle.projectsEvaluated {
if (!applied) LOG.warn('No android or java plugins found, checker compiler options will not be applied.')
}
}

private static configureProject(Project project, CheckerExtension userConfig) {
JavaVersion javaVersion =
project.extensions.findByName('android')?.compileOptions?.sourceCompatibility ?:
project.convention.findByName('jdk')?.sourceCompatibility ?:
JavaVersion.current()
project.property('sourceCompatibility')

// Check for Java 7 or Java 8 to make sure to get correct annotations dependency
def jdkVersion
Expand Down Expand Up @@ -89,23 +90,25 @@ final class CheckerPlugin implements Plugin<Project> {
}

// Apply checker to project
project.tasks.withType(AbstractCompile).all { compile ->
compile.options.annotationProcessorPath = project.configurations.checkerFramework
compile.options.compilerArgs = [
"-Xbootclasspath/p:${project.configurations.checkerFrameworkAnnotatedJDK.asPath}".toString()
]
if (!userConfig.checkers.empty) {
compile.options.compilerArgs << "-processor" << userConfig.checkers.join(",")
}
project.gradle.projectsEvaluated {
Copy link
Contributor Author

@mwhipple mwhipple Jan 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like using this hook shouldn't be needed given the behavior of the TaskContainer/DomainObjectCollection#withType below, but tests start failing without it (maybe an issue with other defaults clobbering these values or something).

project.tasks.withType(AbstractCompile).all { compile ->
compile.options.annotationProcessorPath = project.configurations.checkerFramework
compile.options.compilerArgs = [
"-Xbootclasspath/p:${project.configurations.checkerFrameworkAnnotatedJDK.asPath}".toString()
]
if (!userConfig.checkers.empty) {
compile.options.compilerArgs << "-processor" << userConfig.checkers.join(",")
}

ANDROID_IDS.each { id ->
project.plugins.withId(id) {
options.bootClasspath = System.getProperty("sun.boot.class.path") + ":" + options.bootClasspath
options.bootClasspath = "${project.configurations.checkerFrameworkJavac.asPath}:".toString() + ":" + options.bootClasspath
ANDROID_IDS.each { id ->
project.plugins.withId(id) {
options.bootClasspath = System.getProperty("sun.boot.class.path") + ":" + options.bootClasspath
options.bootClasspath = "${project.configurations.checkerFrameworkJavac.asPath}:".toString() + ":" + options.bootClasspath
}
}
options.fork = true
// options.forkOptions.jvmArgs += ["-Xbootclasspath/p:${project.configurations.checkerFrameworkJavac.asPath}"]
}
options.fork = true
// options.forkOptions.jvmArgs += ["-Xbootclasspath/p:${project.configurations.checkerFrameworkJavac.asPath}"]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,68 @@ final class CheckerPluginSpec extends BaseSpecification {
gradleVersion << TESTED_GRADLE_VERSIONS
}

def 'with relevant plugin loaded subsequently, compiler settings are applied'() {
given:
buildFile <<
"""
plugins {
id 'com.jaredsburrows.checkerframework'
id 'java'
}

repositories {
maven {
url "${getClass().getResource("/maven/").toURI()}"
}
}
""".stripIndent().trim()

when:
BuildResult result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(testProjectDir.root)
.withPluginClasspath()
.withArguments('--info')
.build()

then:
result.output.contains('applying checker compiler options')

where:
gradleVersion << TESTED_GRADLE_VERSIONS
}

def 'with resolved configuration dependencies, compilation settings are still applied'() {
given:
buildFile <<
"""
plugins {
id 'com.jaredsburrows.checkerframework'
id 'java'
id 'application'
}

repositories {
maven {
url "${getClass().getResource("/maven/").toURI()}"
}
}
// Trigger resolution of compile classpath
println project.sourceSets.main.compileClasspath as List
""".stripIndent().trim()

when:
BuildResult result = GradleRunner.create()
.withGradleVersion(gradleVersion)
.withProjectDir(testProjectDir.root)
.withPluginClasspath()
.withArguments('--info')
.build()

then:
result.output.contains('applying checker compiler options')

where:
gradleVersion << TESTED_GRADLE_VERSIONS
}
}