Skip to content

Commit 3d2b49c

Browse files
committed
Use javac from provided JDK. Add a bunch of unit tests
1 parent 93b5065 commit 3d2b49c

File tree

9 files changed

+734
-185
lines changed

9 files changed

+734
-185
lines changed

.idea/misc.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main/kotlin/StreamUtils.kt renamed to src/main/kotlin/com/tschuchort/compiletesting/StreamUtils.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import java.io.IOException
2-
import java.io.OutputStream
1+
package com.tschuchort.compiletesting
2+
3+
import java.io.*
4+
35

46
/** An output stream that does nothing, like /dev/null */
57
internal object NullStream : OutputStream() {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.tschuchort.compiletesting
2+
3+
import java.io.File
4+
import javax.lang.model.SourceVersion
5+
6+
internal fun <E> MutableCollection<E>.addAll(vararg elems: E) = addAll(elems)
7+
8+
internal fun getJavaHome(): File {
9+
val path = System.getProperty("java.home")
10+
?: throw IllegalStateException("no java home found")
11+
12+
return File(path).also { check(it.isDirectory) }
13+
}
14+
15+
internal fun getJdkHome()
16+
= if(isJdk9OrLater())
17+
getJavaHome()
18+
else
19+
getJavaHome().parentFile
20+
21+
/** Checks if the JDK of the host process is version 9 or later */
22+
internal fun isJdk9OrLater(): Boolean
23+
= SourceVersion.latestSupported().compareTo(SourceVersion.RELEASE_8) > 0
24+
25+
internal fun File.listFilesRecursively(): List<File> {
26+
return listFiles().flatMap { file ->
27+
if(file.isDirectory)
28+
file.listFilesRecursively()
29+
else
30+
listOf(file)
31+
}
32+
}
33+
34+
internal fun File.isKotlinFile()
35+
= listOf("kt", "kts").any{ it.equals(extension, ignoreCase = true) }

src/test/java/JavaTestProcessor.java renamed to src/test/java/com/tschuchort/compiletesting/JavaTestProcessor.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
package com.tschuchort.compiletesting;
2+
13
import com.squareup.javapoet.JavaFile;
24
import com.squareup.kotlinpoet.FileSpec;
35
import com.squareup.kotlinpoet.FunSpec;
@@ -56,14 +58,15 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
5658
).build();
5759
}
5860

59-
FileSpec fileSpec = FileSpec.builder("", "JavaGeneratedKotlinClass.kt")
61+
FileSpec fileSpec = FileSpec.builder("com.tschuchort.compiletesting", "JavaGeneratedKotlinClass.kt")
6062
.addType(typeSpecBuilder.build())
6163
.build();
6264

6365
writeKotlinFile(fileSpec, fileSpec.getName(), fileSpec.getPackageName());
6466

6567
try {
66-
JavaFile.builder("", com.squareup.javapoet.TypeSpec.classBuilder("JavaGeneratedJavaClass").build())
68+
JavaFile.builder("com.tschuchort.compiletesting",
69+
com.squareup.javapoet.TypeSpec.classBuilder("JavaGeneratedJavaClass").build())
6770
.build().writeTo(processingEnv.getFiler());
6871
} catch (Exception e) {
6972
}

src/test/kotlin/JavacUtils.kt renamed to src/test/kotlin/com/tschuchort/compiletesting/JavacUtils.kt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
package com.tschuchort.compiletesting
2+
3+
import okio.Buffer
14
import java.io.*
5+
import java.lang.IllegalArgumentException
26
import java.net.URI
37
import java.nio.charset.Charset
48
import javax.tools.JavaCompiler
@@ -11,7 +15,9 @@ import javax.tools.SimpleJavaFileObject
1115
* Used for interfacing with javac ([JavaCompiler]).
1216
*/
1317
internal class FileJavaFileObject(val sourceFile: File, val charset: Charset = Charset.defaultCharset())
14-
: SimpleJavaFileObject(sourceFile.toURI(), deduceKind(sourceFile.toURI())) {
18+
: SimpleJavaFileObject(sourceFile.toURI(),
19+
deduceKind(sourceFile.toURI())
20+
) {
1521

1622
init {
1723
require(sourceFile.isFile)
@@ -51,4 +57,34 @@ internal class StringJavaFileObject(className: String, private val contents: Str
5157
override fun openReader(ignoreEncodingErrors: Boolean): Reader = StringReader(contents)
5258

5359
override fun getLastModified(): Long = _lastModified
60+
}
61+
62+
/**
63+
* Gets the version string of a javac executable that can be started using the
64+
* given [javacCommand] via [Runtime.exec].
65+
*/
66+
internal fun getJavacVersionString(javacCommand: String): String {
67+
val javacProc = ProcessBuilder(listOf(javacCommand, "-version"))
68+
.redirectErrorStream(true)
69+
.start()
70+
71+
val buffer = Buffer()
72+
73+
javacProc.inputStream.copyTo(buffer.outputStream())
74+
javacProc.waitFor()
75+
76+
val output = buffer.readUtf8()
77+
78+
return Regex("javac (.*)?[\\s\\S]*").matchEntire(output)?.destructured?.component1()
79+
?: throw IllegalStateException("Command '$javacCommand -version' did not print expected output. " +
80+
"Output was: '$output'")
81+
}
82+
83+
internal fun isJavac9OrLater(javacVersionString: String): Boolean {
84+
val (majorv, minorv, patchv, otherv) = Regex("([0-9]*)\\.([0-9]*)\\.([0-9]*)(.*)")
85+
.matchEntire(javacVersionString)?.destructured
86+
?: throw IllegalArgumentException("Could not parse javac version string: '$javacVersionString'")
87+
88+
return (majorv.toInt() == 1 && minorv.toInt() >= 9) // old versioning scheme: 1.8.x
89+
|| (majorv.toInt() >= 9) // new versioning scheme: 10.x.x
5490
}

0 commit comments

Comments
 (0)