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

Add module-info.java #1486

Open
wants to merge 21 commits 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
3 changes: 2 additions & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[versions]
jmh = "1.37"
ktlint = "0.48.2"
junit = "4.13.2"

[libraries]
android-gradle-plugin = { module = "com.android.tools.build:gradle", version = "7.4.2" }
Expand All @@ -18,7 +19,7 @@ dokka = { module = "org.jetbrains.dokka:dokka-gradle-plugin", version = "1.9.20"
spotless = { module = "com.diffplug.spotless:spotless-plugin-gradle", version = "6.25.0" }
bnd = { module = "biz.aQute.bnd:biz.aQute.bnd.gradle", version = "7.0.0" }
vanniktech-publish-plugin = { module = "com.vanniktech:gradle-maven-publish-plugin", version = "0.27.0" }
test-junit = { module = "junit:junit", version = "4.13.2" }
test-junit = { module = "junit:junit", version.ref = "junit" }
test-assertj = { module = "org.assertj:assertj-core", version = "3.25.3" }
test-assertk = "com.willowtreeapps.assertk:assertk:0.28.1"
test-jimfs = "com.google.jimfs:jimfs:1.3.0"
68 changes: 64 additions & 4 deletions okio/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import kotlinx.validation.ApiValidationExtension
import org.jetbrains.kotlin.gradle.plugin.mpp.KotlinNativeTargetWithTests
import org.jetbrains.kotlin.gradle.plugin.mpp.NativeBuildType
import org.jetbrains.kotlin.gradle.plugin.mpp.TestExecutable
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("multiplatform")
id("org.jetbrains.dokka")
id("com.vanniktech.maven.publish.base")
id("build-support")
id("binary-compatibility-validator")
`jvm-test-suite`
}

/*
Expand Down Expand Up @@ -186,17 +188,61 @@ kotlin {
}
}
}

jvm {
withJava()
}
}

val java9 by sourceSets.creating

configurations.named("java9CompileClasspath") {
extendsFrom(configurations["jvmCompileClasspath"])
}

testing {
suites {
register<JvmTestSuite>("integrationTest") {
useJUnit(libs.versions.junit)
dependencies {
implementation(project())
}
targets.configureEach {
testTask.configure {
onlyIf {
!javaLauncher.get().metadata.javaRuntimeVersion.startsWith("1.8")
}
}
}
}
}
}

tasks {
val jvmJar by getting(Jar::class) {
// BundleTaskExtension() crashes unless there's a 'main' source set.
sourceSets.create(SourceSet.MAIN_SOURCE_SET_NAME)
val compileJava9Java = named<JavaCompile>("compileJava9Java") {
dependsOn("compileKotlinJvm")
// https://kotlinlang.org/docs/gradle-configure-project.html#configure-with-java-modules-jpms-enabled
options.compilerArgumentProviders.add(CommandLineArgumentProvider {
listOf("--patch-module", "okio=${sourceSets["main"].output.asPath}")
})
options.release = 9
}

val compileJava9KotlinJvm = named<KotlinCompile>("compileJava9KotlinJvm")

named<Jar>("jvmJar") {
from(compileJava9Java.flatMap { it.destinationDirectory }) {
into("META-INF/versions/9")
}
from(compileJava9KotlinJvm.flatMap { it.destinationDirectory }) {
into("META-INF/versions/9")
exclude("META-INF")
}
val bndExtension = BundleTaskExtension(this)
bndExtension.setBnd(
"""
Export-Package: okio
Automatic-Module-Name: okio
Multi-Release: true
Bundle-SymbolicName: com.squareup.okio
""",
)
Expand All @@ -206,6 +252,20 @@ tasks {
.execute(this)
}
}

named<JavaCompile>("compileIntegrationTestJava") {
options.release = 9
}

val integrationTest = named<Test>("integrationTest") {
jvmArgumentProviders.add(CommandLineArgumentProvider {
listOf("--patch-module", "okio.test.integration=${sourceSets["integrationTest"].output.asPath}")
})
}

check {
dependsOn(integrationTest)
}
}

configure<MavenPublishBaseExtension> {
Expand Down
5 changes: 5 additions & 0 deletions okio/src/jvmIntegrationTest/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
open module okio.test.integration {
requires okio;
requires junit;
requires kotlin.stdlib;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package okio.test.integration

import org.junit.Assert.assertFalse
import org.junit.Assert.assertNotNull
import org.junit.Assert.assertTrue
import org.junit.Test

class ModuleTest {
@Test
fun testModule() {
// assert okio is modular
val okioModule = ModuleLayer.boot().modules().single { it.name == "okio" }
assertFalse(okioModule.descriptor.isAutomatic)
assertTrue(okioModule.isExported("okio"))
assertFalse(okioModule.isExported("okio.internal"))
}

@Test
fun testLoggerClassIsPresent() {
val logger = Class.forName("okio.internal.Logger")
assertNotNull(logger.getResource("/META-INF/versions/9/okio/internal/Logger.class"))
}
}
5 changes: 5 additions & 0 deletions okio/src/jvmJava9/java/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module okio {
requires kotlin.stdlib;

exports okio;
}
6 changes: 6 additions & 0 deletions okio/src/jvmJava9/kotlin/okio/internal/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package okio.internal

internal class Logger internal constructor(name: String) {
private val logger: System.Logger = System.getLogger(name)
fun warn(msg: String, e: Throwable): Unit = logger.log(System.Logger.Level.WARNING, msg, e)
}
9 changes: 4 additions & 5 deletions okio/src/jvmMain/kotlin/okio/JvmOkio.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ import java.nio.file.Files
import java.nio.file.OpenOption
import java.nio.file.Path as NioPath
import java.security.MessageDigest
import java.util.logging.Level
import java.util.logging.Logger
import javax.crypto.Cipher
import javax.crypto.Mac
import okio.internal.Logger
import okio.internal.ResourceFileSystem

/** Returns a sink that writes to `out`. */
Expand Down Expand Up @@ -139,7 +138,7 @@ fun Socket.source(): Source {
return timeout.source(source)
}

private val logger = Logger.getLogger("okio.Okio")
private val logger = Logger("okio.Okio")

private class SocketAsyncTimeout(private val socket: Socket) : AsyncTimeout() {
override fun newTimeoutException(cause: IOException?): IOException {
Expand All @@ -154,12 +153,12 @@ private class SocketAsyncTimeout(private val socket: Socket) : AsyncTimeout() {
try {
socket.close()
} catch (e: Exception) {
logger.log(Level.WARNING, "Failed to close timed out socket $socket", e)
logger.warn("Failed to close timed out socket $socket", e)
} catch (e: AssertionError) {
if (e.isAndroidGetsocknameError) {
// Catch this exception due to a Firmware issue up to android 4.2.2
// https://code.google.com/p/android/issues/detail?id=54072
logger.log(Level.WARNING, "Failed to close timed out socket $socket", e)
logger.warn("Failed to close timed out socket $socket", e)
} else {
throw e
}
Expand Down
6 changes: 6 additions & 0 deletions okio/src/jvmMain/kotlin/okio/internal/Logger.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package okio.internal

internal class Logger internal constructor(name: String) {
private val logger: java.util.logging.Logger = java.util.logging.Logger.getLogger(name)
fun warn(msg: String, e: Throwable): Unit = logger.log(java.util.logging.Level.WARNING, msg, e)
}