Build: add instrumentation to all JavaCompile tasks

This commit is contained in:
Vyacheslav Gerasimov
2018-12-13 22:19:24 +03:00
parent 9ad899fb01
commit 95a3b337c6
5 changed files with 121 additions and 5 deletions
+1
View File
@@ -3,6 +3,7 @@
<words>
<w>cidr</w>
<w>foldable</w>
<w>instrumentator</w>
<w>redirector</w>
</words>
</dictionary>
+3 -5
View File
@@ -1,13 +1,9 @@
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import org.gradle.api.Project
import java.util.*
import java.io.File
import org.gradle.api.tasks.bundling.Jar
import org.gradle.plugins.ide.idea.model.IdeaModel
import org.gradle.api.file.FileCollection
import org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.Kotlin2JsCompile
import proguard.gradle.ProGuardTask
buildscript {
@@ -148,7 +144,9 @@ fun checkJDK() {
rootProject.apply {
from(rootProject.file("versions.gradle.kts"))
from(rootProject.file("report.gradle.kts"))
from(rootProject.file("javaInstrumentation.gradle.kts"))
}
IdeVersionConfigurator.setCurrentIde(this)
extra["versions.protobuf-java"] = "2.6.1"
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.test.util;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
public class NotNullInstrumentationTest extends TestCase {
public void testArgument() {
try {
//noinspection ConstantConditions
instrumented(null);
} catch (IllegalArgumentException e) {
return;
}
Assert.fail("IllegalArgumentException is expected");
}
private void instrumented(@NotNull Object o) {
// This method should be instrumented with not null check
}
}
@@ -0,0 +1,28 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.test;
import junit.framework.TestCase;
import org.jetbrains.annotations.NotNull;
import org.junit.Assert;
public class NotNullInstrumentationTest extends TestCase {
public void testArgument() {
try {
//noinspection ConstantConditions
instrumented(null);
} catch (IllegalArgumentException e) {
return;
}
Assert.fail("IllegalArgumentException is expected");
}
private void instrumented(@NotNull Object o) {
// This method should be instrumented with not null check
}
}
+61
View File
@@ -0,0 +1,61 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
allprojects {
afterEvaluate {
configureJavaInstrumentation()
}
}
// Hide window of instrumentation tasks
val headlessOldValue: String? = System.setProperty("java.awt.headless", "true")
logger.info("Setting java.awt.headless=true, old value was $headlessOldValue")
/**
* Configures instrumentation for all JavaCompile tasks in project
*/
fun Project.configureJavaInstrumentation() {
if (plugins.hasPlugin("org.gradle.java")) {
val javaInstrumentator by configurations.creating
dependencies {
javaInstrumentator(intellijDep()) {
includeJars("javac2", "jdom", "asm-all", rootProject = rootProject)
}
}
tasks.withType<JavaCompile> {
doLast {
instrumentClasses(javaInstrumentator.asPath)
}
}
}
}
fun JavaCompile.instrumentClasses(instrumentatorClasspath: String) {
ant.withGroovyBuilder {
"taskdef"(
"name" to "instrumentIdeaExtensions",
"classpath" to instrumentatorClasspath,
"loaderref" to "java2.loader",
"classname" to "com.intellij.ant.InstrumentIdeaExtensions"
)
}
val sourceSet = project.sourceSets.single { it.compileJavaTaskName == name }
val javaSourceDirectories = sourceSet.allJava.sourceDirectories.filter { it.exists() }
ant.withGroovyBuilder {
javaSourceDirectories.forEach { directory ->
"instrumentIdeaExtensions"(
"srcdir" to directory,
"destdir" to destinationDir,
"classpath" to classpath.asPath,
"includeantruntime" to false,
"instrumentNotNull" to true
)
}
}
}