Introduce kotlin-stdlib-jdk7/8 libraries, deprecate kotlin-stdlib-jre7/8

The idea is to keep all declarations in the same packages from Kotlin's
point of view, but use JvmPackageName annotation to move them to another
JVM package, to avoid the split package problem which is otherwise
unsolvable when using module path on Java 9 (KT-19258).

In this commit, kotlin-stdlib-jre7/8 are moved to kotlin-stdlib-jdk7/8
and in the subsequent commit, -jre7/8 are restored. This is done in
order to make Git recognize this as a file move to preserve history.

Include new stdlib-jdkN artifacts in manifest version tests.
This commit is contained in:
Alexander Udalov
2017-09-07 16:31:19 +03:00
committed by Ilya Gorbunov
parent 2fc3f4d07b
commit e253acd5fd
28 changed files with 69 additions and 36 deletions
+85
View File
@@ -0,0 +1,85 @@
description = 'Kotlin Standard Library JDK 8 extension'
apply plugin: 'kotlin'
configureJvm6Project(project)
configurePublishing(project)
ext.javaHome = JDK_18
ext.jvmTarget = "1.8"
dependencies {
compile project(':kotlin-stdlib')
compile project(':kotlin-stdlib-jdk7')
testCompile project(':kotlin-test:kotlin-test-junit')
}
sourceSets {
main {
kotlin {
srcDir 'src'
}
}
test {
kotlin {
srcDir 'test'
if(!System.properties.'idea.active') {
srcDir '../test'
srcDir '../jdk7/test'
}
}
}
}
jar {
manifestAttributes(manifest, project, 'Main')
}
artifacts {
archives sourcesJar
archives javadocJar
}
dist {
from (jar, sourcesJar)
}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile) {
kotlinOptions.jdkHome = JDK_18
kotlinOptions.jvmTarget = 1.8
}
compileKotlin {
kotlinOptions.freeCompilerArgs = [
"-Xallow-kotlin-package",
"-Xmultifile-parts-inherit",
"-Xdump-declarations-to", "${buildDir}/stdlib-jdk8-declarations.json",
"-module-name", project.name
]
}
compileTestKotlin {
kotlinOptions.freeCompilerArgs = ["-Xallow-kotlin-package"]
}
kotlin.experimental.coroutines 'enable'
test {
executable = "$JDK_18/bin/java"
}
task testJdk6Tests(type: Test) {
dependsOn(':kotlin-stdlib:testClasses')
group = "verification"
executable = "$JDK_18/bin/java"
doFirst {
testClassesDirs = project(':kotlin-stdlib').sourceSets.test.output
classpath = files(
testClassesDirs,
configurations.testCompile
)
}
}
check.dependsOn testJdk6Tests
@@ -0,0 +1,39 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("UNCHECKED_CAST", "NOTHING_TO_INLINE", "INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@file:JvmName("CollectionsJDK8Kt")
@file:kotlin.jvm.JvmPackageName("kotlin.collections.jdk8")
package kotlin.collections
/**
* Returns the value to which the specified key is mapped, or
* [defaultValue] if this map contains no mapping for the key.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.getOrDefault(key: K, defaultValue: V): V
= (this as Map<K, V>).getOrDefault(key, defaultValue)
/**
* Removes the entry for the specified key only if it is currently
* mapped to the specified value.
*/
@SinceKotlin("1.2")
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K, @kotlin.internal.OnlyInputTypes V> MutableMap<out K, out V>.remove(key: K, value: V): Boolean
= (this as MutableMap<K, V>).remove(key, value)
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.internal
import java.util.regex.MatchResult
import java.util.regex.Matcher
@Suppress("INVISIBLE_REFERENCE", "INVISIBLE_MEMBER", "CANNOT_OVERRIDE_INVISIBLE_MEMBER")
internal open class JRE8PlatformImplementations : JRE7PlatformImplementations() {
override fun getMatchResultNamedGroup(matchResult: MatchResult, name: String): MatchGroup? {
val matcher = matchResult as? Matcher ?: throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.")
val range = matcher.start(name)..matcher.end(name)-1
return if (range.start >= 0)
MatchGroup(matcher.group(name), range)
else
null
}
}
@@ -0,0 +1,77 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@file:JvmName("StreamsKt")
@file:kotlin.jvm.JvmPackageName("kotlin.streams.jdk8")
package kotlin.streams
import java.util.*
import java.util.stream.*
/**
* Creates a [Sequence] instance that wraps the original stream iterating through its elements.
*/
@SinceKotlin("1.2")
public fun <T> Stream<T>.asSequence(): Sequence<T> = Sequence { iterator() }
/**
* Creates a [Sequence] instance that wraps the original stream iterating through its elements.
*/
@SinceKotlin("1.2")
public fun IntStream.asSequence(): Sequence<Int> = Sequence { iterator() }
/**
* Creates a [Sequence] instance that wraps the original stream iterating through its elements.
*/
@SinceKotlin("1.2")
public fun LongStream.asSequence(): Sequence<Long> = Sequence { iterator() }
/**
* Creates a [Sequence] instance that wraps the original stream iterating through its elements.
*/
@SinceKotlin("1.2")
public fun DoubleStream.asSequence(): Sequence<Double> = Sequence { iterator() }
/**
* Creates a sequential [Stream] instance that produces elements from the original sequence.
*/
@SinceKotlin("1.2")
public fun <T> Sequence<T>.asStream(): Stream<T> = StreamSupport.stream({ Spliterators.spliteratorUnknownSize(iterator(), Spliterator.ORDERED) }, Spliterator.ORDERED, false)
/**
* Returns a [List] containing all elements produced by this stream.
*/
@SinceKotlin("1.2")
public fun <T> Stream<T>.toList(): List<T> = collect(Collectors.toList<T>())
/**
* Returns a [List] containing all elements produced by this stream.
*/
@SinceKotlin("1.2")
public fun IntStream.toList(): List<Int> = toArray().asList()
/**
* Returns a [List] containing all elements produced by this stream.
*/
@SinceKotlin("1.2")
public fun LongStream.toList(): List<Long> = toArray().asList()
/**
* Returns a [List] containing all elements produced by this stream.
*/
@SinceKotlin("1.2")
public fun DoubleStream.toList(): List<Double> = toArray().asList()
@@ -0,0 +1,34 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
@file:Suppress("INVISIBLE_MEMBER", "INVISIBLE_REFERENCE")
@file:JvmName("RegexExtensionsJDK8Kt")
@file:kotlin.jvm.JvmPackageName("kotlin.text.jdk8")
package kotlin.text
/**
* Returns a named group with the specified [name].
*
* @return An instance of [MatchGroup] if the group with the specified [name] was matched or `null` otherwise.
* @throws [UnsupportedOperationException] if getting named groups isn't supported on the current platform.
*/
@SinceKotlin("1.2")
public operator fun MatchGroupCollection.get(name: String): MatchGroup? {
val namedGroups = this as? MatchNamedGroupCollection ?:
throw UnsupportedOperationException("Retrieving groups by name is not supported on this platform.")
return namedGroups[name]
}
@@ -0,0 +1,48 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jdk8.collections.test
import org.junit.Test
import kotlin.test.*
import java.util.function.Predicate
import java.util.stream.Collectors
import kotlin.streams.*
class CollectionTest {
val data = listOf("abc", "fo", "baar")
@Test fun stream() {
assertEquals(
data.flatMap { it.asIterable() },
data.stream()
.flatMap { it.chars().boxed().map { it.toChar() } }
.collect(Collectors.toList()))
assertEquals(data, data.parallelStream().toList())
}
@Test fun removeIf() {
val coll: MutableCollection<String> = data.toMutableList()
assertTrue(coll.removeIf { it.length < 3 })
assertEquals(listOf("abc", "baar"), coll as Collection<String>)
assertFalse(coll.removeIf(Predicate { it.length > 4 }))
}
}
@@ -0,0 +1,53 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jdk8.collections.test
import org.junit.Test
import kotlin.test.*
import java.util.function.*
class IterableTest {
val data = listOf("foo", "bar")
val iterable = Iterable { data.iterator() }
@Test fun spliterator() {
val spliterator = iterable.spliterator()
assertEquals(-1, spliterator.exactSizeIfKnown)
val expected = data.toMutableList()
spliterator.forEachRemaining {
assertEquals(expected.removeAt(0), it)
}
}
@Test fun forEach() {
val expected = data.toMutableList()
iterable.forEach(Consumer {
assertEquals(expected.removeAt(0), it)
})
}
@Test fun forEachRemaining() {
val expected = data.toMutableList()
val iterator = iterable.iterator()
iterator.forEachRemaining(Consumer {
assertEquals(expected.removeAt(0), it)
})
}
}
@@ -0,0 +1,35 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jdk8.collections.test
import org.junit.Test
import kotlin.test.*
import java.util.function.UnaryOperator
class ListTest {
@Test fun replaceAll() {
val list = mutableListOf("ab", "cde", "x")
list.replaceAll { it.length.toString() }
val expected = listOf("2", "3", "1")
assertEquals(expected, list)
list.replaceAll(UnaryOperator.identity())
assertEquals(expected, list)
}
}
@@ -0,0 +1,144 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jdk8.collections.test
import org.junit.Test
import java.util.function.BiFunction
import kotlin.test.*
import kotlin.jdk8.collections.*
class MapTest {
@Test fun getOrDefault() {
val map = mapOf("x" to 1, "z" to null)
assertEquals(1, map.getOrDefault("x", 0))
assertEquals(0, map.getOrDefault("y", 0))
assertEquals(null, map.getOrDefault("z", 0))
assertEquals(null, map.getOrDefault("y" as CharSequence, null))
val nonNullMap = mapOf("x" to 1)
assertEquals(null, nonNullMap.getOrDefault("y" as CharSequence, null)) // should pass
assertEquals("none", nonNullMap.getOrDefault("y" as CharSequence, "none")) // should not compile
}
@Test fun forEach() {
val map = mapOf("k" to "v")
map.forEach { k, v ->
assertEquals("k", k)
assertEquals("v", v)
}
map.forEach {
assertEquals("k", it.key)
assertEquals("v", it.value)
}
}
@Test fun replaceAll() {
val map: MutableMap<String, CharSequence> = mutableMapOf("a" to "b", "c" to "d")
map.replaceAll { k, v -> k + v }
assertEquals(mapOf<String, CharSequence>("a" to "ab", "c" to "cd"), map)
val operator = BiFunction<Any, Any, String> { k, v -> k.toString() + v.toString() }
map.replaceAll(operator)
assertEquals(mapOf<String, CharSequence>("a" to "aab", "c" to "ccd"), map)
}
@Test fun putIfAbsent() {
val map = mutableMapOf(1 to "a")
assertEquals("a", map.putIfAbsent(1, "b"))
assertEquals("a", map[1])
assertEquals(null, map.putIfAbsent(2, "b"))
assertEquals("b", map[2])
}
@Test fun removeKeyValue() {
val map = mutableMapOf(1 to "a")
assertEquals(false, map.remove(1 as Number, null as Any?)) // requires import
assertEquals(true, map.remove(1, "a"))
}
@Test fun replace() {
val map = mutableMapOf(1 to "a", 2 to null)
assertTrue(map.replace(2, null, "x"))
assertEquals("x", map[2])
assertFalse(map.replace(2, null, "x"))
assertEquals("a", map.replace(1, "b"))
assertEquals(null, map.replace(3, "c"))
}
@Test fun computeIfAbsent() {
val map = mutableMapOf(2 to "x")
assertEquals("x", map.computeIfAbsent(2) { it.toString() })
assertEquals("3", map.computeIfAbsent(3) { it.toString() })
// prohibited: map.computeIfAbsent(0) { null }
val map2 = mutableMapOf(2 to "x", 3 to null)
assertEquals("x", map2.computeIfAbsent(2) { it.toString() })
assertEquals("3", map2.computeIfAbsent(3) { it.toString() })
assertEquals(null, map2.computeIfAbsent(0) { null })
assertFalse(0 in map2)
}
@Test fun computeIfPresent() {
val map = mutableMapOf(2 to "x")
assertEquals("2x", map.computeIfPresent(2) { k, v -> k.toString() + v })
assertEquals(null, map.computeIfPresent(3) { k, v -> k.toString() + v })
assertEquals(null, map.computeIfPresent(2) { _, _ -> null })
assertFalse(2 in map)
val map2 = mutableMapOf<Int, String?>(2 to "x")
assertEquals("2x", map2.computeIfPresent(2) { k, v -> k.toString() + v })
assertEquals(null, map2.computeIfPresent(3) { k, v -> k.toString() + v })
assertEquals(null, map2.computeIfPresent(2) { _, _ -> null })
assertFalse(2 in map2)
}
@Test fun compute() {
val map = mutableMapOf(2 to "x")
assertEquals("2x", map.compute(2) { k, v -> k.toString() + v })
assertEquals(null, map.compute(2) { _, _ -> null })
assertFalse { 2 in map }
assertEquals("1null", map.compute(1) { k, v -> k.toString() + v })
}
@Test fun merge() {
val map = mutableMapOf(2 to "x")
assertEquals("y", map.merge(3, "y") { _, _ -> null })
assertEquals(null, map.merge(3, "z") { old, new ->
assertEquals("y", old)
assertEquals("z", new)
null
})
assertFalse(3 in map)
// fails due to KT-12144
val map2 = mutableMapOf<Int, String?>(1 to null)
// new value must be V&Any
assertEquals("e", map2.merge(1, "e") { old, new -> (old.length + new.length).toString() ?: null })
assertEquals("3", map2.merge(1, "fg") { old, new -> (old.length + new.length).toString() ?: null })
assertEquals(null, map2.merge(1, "3") { _, _ -> null })
assertFalse(1 in map)
}
}
@@ -0,0 +1,70 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.jdk8.streams.test
import kotlin.streams.*
import org.junit.Test
import java.util.stream.*
import kotlin.test.*
class StreamsTest {
@Test fun toList() {
val data = arrayOf(1, 2L, 1.23, null)
val streamBuilder = { Stream.of(*data) }
assertEquals(data.asList(), streamBuilder().toList())
assertEquals(listOf(1), streamBuilder().filter { it is Int }.mapToInt { it as Int }.toList())
assertEquals(listOf(2L), streamBuilder().filter { it is Long }.mapToLong { it as Long }.toList())
assertEquals(listOf(1.23), streamBuilder().filter { it is Double }.mapToDouble { it as Double }.toList())
}
@Test fun asSequence() {
val data = arrayOf(1, 2L, 1.23, null)
fun<T> assertSequenceContent(expected: List<T>, actual: Sequence<T>) {
assertEquals(expected, actual.toList())
assertFailsWith<IllegalStateException> ("Second iteration fails") { actual.toList() }
}
assertSequenceContent(data.asList(), Stream.of(*data).asSequence())
assertSequenceContent(listOf(1, 2), IntStream.of(1, 2).asSequence())
assertSequenceContent(listOf(1L, 2L), LongStream.of(1L, 2L).asSequence())
assertSequenceContent(listOf(1.0, 2.0), DoubleStream.of(1.0, 2.0).asSequence())
}
@Test fun asStream() {
val sequence = generateSequence(0) { it -> it * it + 1 }
val stream = sequence.asStream()
val expected = Stream.iterate(0) { it -> it * it + 1 }
assertEquals(expected.limit(7).toList(), stream.limit(7).toList())
}
@Test fun asParallelStream() {
val sequence = generateSequence(0) { it + 2 } // even numbers
val stream = sequence.asStream().parallel().map { it / 2 }
val n = 100000
val expected = (0 until n).toList()
assertEquals(expected, stream.limit(n.toLong()).toList())
}
}
@@ -0,0 +1,42 @@
/*
* Copyright 2010-2017 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kotlin.text.test
import org.junit.Test
import kotlin.test.*
class RegexTest {
@Test fun namedGroups() {
val input = "1a 2b 3c"
val regex = "(?<num>\\d)(?<liter>\\w)".toRegex()
val matches = regex.findAll(input).toList()
assertTrue(matches.all { it.groups.size == 3 })
val m1 = matches[0]
assertEquals("1", m1.groups["num"]?.value)
assertEquals(0..0, m1.groups["num"]?.range)
assertEquals("a", m1.groups["liter"]?.value)
assertEquals(1..1, m1.groups["liter"]?.range)
val m2 = matches[1]
assertEquals("2", m2.groups["num"]?.value)
assertEquals(3..3, m2.groups["num"]?.range)
assertEquals("b", m2.groups["liter"]?.value)
assertEquals(4..4, m2.groups["liter"]?.range)
}
}