Skip to content

Commit 81b5140

Browse files
authored
Ktln 863 (#960)
* KTLN-507 source code * KTLN-507 Core review fixes * KTLN-863 * Polishing KTLN-863
1 parent 381f1b5 commit 81b5140

File tree

9 files changed

+174
-2
lines changed

9 files changed

+174
-2
lines changed

k2-compiler/pom.xml

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
44

5-
<modelVersion>4.0.0</modelVersion>
5+
<modelVersion>4.0.0</modelVersion>
66
<artifactId>k2-compiler</artifactId>
77
<name>k2-compiler</name>
88

@@ -14,6 +14,68 @@
1414

1515
<properties>
1616
<kotlin.version>2.0.0</kotlin.version>
17+
<kotlin-stdlib-jdk8.version>2.0.0</kotlin-stdlib-jdk8.version>
1718
</properties>
1819

20+
<dependencies>
21+
<dependency>
22+
<groupId>org.jetbrains</groupId>
23+
<artifactId>annotations</artifactId>
24+
<version>24.1.0</version>
25+
</dependency>
26+
</dependencies>
27+
28+
<build>
29+
<sourceDirectory>src/main/kotlin</sourceDirectory>
30+
<testSourceDirectory>src/test/kotlin</testSourceDirectory>
31+
<plugins>
32+
<plugin>
33+
<groupId>org.codehaus.mojo</groupId>
34+
<artifactId>build-helper-maven-plugin</artifactId>
35+
<version>3.2.0</version>
36+
<executions>
37+
<execution>
38+
<phase>generate-sources</phase>
39+
<goals>
40+
<goal>add-source</goal>
41+
</goals>
42+
<configuration>
43+
<sources>
44+
<source>src/main/java</source>
45+
</sources>
46+
</configuration>
47+
</execution>
48+
</executions>
49+
</plugin>
50+
<plugin>
51+
<groupId>org.jetbrains.kotlin</groupId>
52+
<artifactId>kotlin-maven-plugin</artifactId>
53+
<version>${kotlin-stdlib-jdk8.version}</version>
54+
<executions>
55+
<execution>
56+
<id>compile</id>
57+
<phase>compile</phase>
58+
<goals>
59+
<goal>compile</goal>
60+
</goals>
61+
</execution>
62+
<execution>
63+
<id>test-compile</id>
64+
<phase>test-compile</phase>
65+
<goals>
66+
<goal>test-compile</goal>
67+
</goals>
68+
</execution>
69+
</executions>
70+
</plugin>
71+
<plugin>
72+
<groupId>org.apache.maven.plugins</groupId>
73+
<artifactId>maven-compiler-plugin</artifactId>
74+
<configuration>
75+
<source>11</source>
76+
<target>11</target>
77+
</configuration>
78+
</plugin>
79+
</plugins>
80+
</build>
1981
</project>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package arrays_nullability;
2+
3+
import org.jetbrains.annotations.Nullable;
4+
5+
public class StringUtils {
6+
7+
public static @Nullable String fromCharArray(char[] s) {
8+
if (s == null || s.length == 0) return null;
9+
10+
return new String(s);
11+
}
12+
13+
public static char @Nullable [] toCharArray(String s) {
14+
if (s == null) return null;
15+
16+
return s.toCharArray();
17+
}
18+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package generics;
2+
3+
public class Box<E> {
4+
5+
private E value;
6+
7+
public E getValue() {
8+
return value;
9+
}
10+
11+
public void setValue(E value) {
12+
this.value = value;
13+
}
14+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package resolution;
2+
3+
public class AbstractEntity {
4+
public String type = "ABSTRACT_TYPE";
5+
public String status = "ABSTRACT_STATUS";
6+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package arrays_nullability
2+
3+
fun main() {
4+
val resultString : String? = StringUtils.fromCharArray(null) // Corret type should be String?
5+
val array : CharArray = StringUtils.toCharArray(null) // That compiles fine in K1
6+
println(array[0]) // NPE
7+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package generics
2+
3+
import generics.Box
4+
5+
fun execute(list: MutableList<*>, element: Any) {
6+
list.add(element)
7+
}
8+
9+
fun explicitSetter() {
10+
val box = Box<String>()
11+
val tmpBox: Box<*> = box
12+
tmpBox.setValue (12) // Compile Error! Thats unsafe!
13+
val myValue : String? = box.value
14+
}
15+
16+
fun syntheticSetter() {
17+
val box = Box<String>()
18+
val tmpBox : Box<*> = box
19+
tmpBox.value = 12 // That compiles!
20+
val foo : String? = box.value // And here we fail with ClassCastException
21+
}
22+
23+
fun syntheticSetter_inVariance() {
24+
val box = Box<String>()
25+
val tmpBox : Box<in String> = box
26+
tmpBox.value = 12 // Wow, thats a trap again!
27+
val foo : String? = box.value // Blast! ClassCastException
28+
}
29+
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.time.Instant
2+
3+
// Does not compile in Both old and K2 Kotlin compilers
4+
//open class BaseEntity {
5+
// open val points: Int
6+
// open var pages: Long?
7+
//
8+
// init {
9+
// points = 1
10+
// pages = 12
11+
// }
12+
//}
13+
14+
open class BaseEntity {
15+
open val points: Int = 1
16+
open var pages: Long? = 12
17+
}
18+
19+
open class WithLateinit {
20+
open lateinit var point: Instant
21+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package resolution
2+
3+
class AbstractEntitySubclass(val type: String) : AbstractEntity() {
4+
5+
val status: String
6+
get() = "CONCRETE_STATUS"
7+
}
8+
9+
fun main() {
10+
val sublcass = AbstractEntitySubclass("CONCRETE_TYPE")
11+
println(sublcass.type)
12+
println(sublcass.status)
13+
}

pom.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,8 +490,9 @@
490490
<module>core-kotlin-companion</module>
491491
<module>core-kotlin-modules</module>
492492
<!-- <module>gradle-kotlin-dsl</module> --> <!-- not a Maven module -->
493+
<!-- <module>k2-kotlin</module> --> <!-- Contains a lot of examples where code intentionally does not compile, by design -->
493494
<!-- <module>kotlin-jee</module> --> <!-- Can't upgrade this project. Arquillian not maintained anymore.-->
494-
<module>k2-compiler</module>
495+
<!-- <module>k2-compiler</module>--> <!-- Contains a lot of examples where code intentionally does not compile, by design -->
495496
<module>koin-guide</module>
496497
<module>kotlin-kotest</module>
497498
<module>kotlin-apache-kafka</module>
@@ -571,6 +572,7 @@
571572
<module>core-kotlin-companion</module>
572573
<module>core-kotlin-modules</module>
573574
<!-- <module>gradle-kotlin-dsl</module> --> <!-- not a Maven module -->
575+
<!-- <module>k2-kotlin</module> --> <!-- Contains a lot of examples where code intentionally does not compile, by design -->
574576
<!-- <module>kotlin-jee</module> --> <!-- Can't upgrade this project. Arquillian not maintained anymore.-->
575577
<module>k2-compiler</module>
576578
<module>koin-guide</module>

0 commit comments

Comments
 (0)