Skip to content

Commit e6887e1

Browse files
committed
Improve usability
1 parent 0c46408 commit e6887e1

File tree

7 files changed

+609
-321
lines changed

7 files changed

+609
-321
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.
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2018-present Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may
5+
* not use this file except in compliance with the License. You may obtain
6+
* a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
17+
package com.facebook.buck.jvm.java.javax
18+
19+
import com.tschuchort.compiletesting.isJdk9OrLater
20+
import org.jetbrains.kotlin.com.intellij.util.lang.JavaVersion
21+
22+
import java.lang.reflect.InvocationTargetException
23+
import java.lang.reflect.Method
24+
import javax.tools.JavaCompiler
25+
import javax.tools.ToolProvider
26+
27+
28+
/**
29+
* ToolProvider has no synchronization internally, so if we don't synchronize from the outside we
30+
* could wind up loading the compiler classes multiple times from different class loaders.
31+
*/
32+
object SynchronizedToolProvider {
33+
private var getPlatformClassLoaderMethod: Method? = null
34+
35+
val systemJavaCompiler: JavaCompiler
36+
get() {
37+
val compiler = synchronized(ToolProvider::class.java) {
38+
ToolProvider.getSystemJavaCompiler()
39+
}
40+
41+
check(compiler != null) { "System java compiler is null! Are you running without JDK?" }
42+
return compiler
43+
}
44+
45+
// The compiler classes are loaded using the platform class loader in Java 9+.
46+
val systemToolClassLoader: ClassLoader
47+
get() {
48+
if (isJdk9OrLater()) {
49+
try {
50+
return getPlatformClassLoaderMethod!!.invoke(null) as ClassLoader
51+
} catch (e: IllegalAccessException) {
52+
throw RuntimeException(e)
53+
} catch (e: InvocationTargetException) {
54+
throw RuntimeException(e)
55+
}
56+
57+
}
58+
59+
val classLoader: ClassLoader
60+
synchronized(ToolProvider::class.java) {
61+
classLoader = ToolProvider.getSystemToolClassLoader()
62+
}
63+
return classLoader
64+
}
65+
66+
init {
67+
if (isJdk9OrLater()) {
68+
try {
69+
getPlatformClassLoaderMethod = ClassLoader::class.java.getMethod("getPlatformClassLoader")
70+
} catch (e: NoSuchMethodException) {
71+
throw RuntimeException(e)
72+
}
73+
74+
}
75+
}
76+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.tschuchort.compiletesting
2+
3+
import kotlin.properties.ReadWriteProperty
4+
import kotlin.reflect.KProperty
5+
6+
@Suppress("MemberVisibilityCanBePrivate")
7+
internal class DefaultPropertyDelegate<R,T>(private val createDefault: () -> T) : ReadWriteProperty<R, T> {
8+
val hasDefaultValue
9+
@Synchronized get() = (value == DEFAULT)
10+
11+
private var value: Any? = DEFAULT
12+
val defaultValue by lazy { createDefault() }
13+
14+
@Synchronized
15+
override operator fun getValue(thisRef: R, property: KProperty<*>): T {
16+
@Suppress("UNCHECKED_CAST")
17+
return if(hasDefaultValue)
18+
defaultValue
19+
else
20+
value as T
21+
}
22+
23+
@Synchronized
24+
override operator fun setValue(thisRef: R, property: KProperty<*>, value: T) {
25+
this.value = value
26+
}
27+
28+
companion object {
29+
private object DEFAULT
30+
}
31+
}
32+
33+
internal fun <R,T> default(createDefault: () -> T) = DefaultPropertyDelegate<R,T>(createDefault)

src/main/kotlin/com/tschuchort/compiletesting/JavacUtils.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,8 @@ internal fun isJavac9OrLater(javacVersionString: String): Boolean {
8787

8888
return (majorv.toInt() == 1 && minorv.toInt() >= 9) // old versioning scheme: 1.8.x
8989
|| (majorv.toInt() >= 9) // new versioning scheme: 10.x.x
90-
}
90+
}
91+
92+
/** Finds the tools.jar given a path to a JDK 8 or earlier */
93+
internal fun findToolsJarFromJdk(jdkHome: File): File
94+
= File(jdkHome.absolutePath + "/../lib/tools.jar").also { check(it.isFile) }

src/main/kotlin/com/tschuchort/compiletesting/KaptComponentRegistrar.kt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,9 @@ import org.jetbrains.kotlin.resolve.jvm.extensions.AnalysisHandlerExtension
4848
import java.io.File
4949
import javax.annotation.processing.Processor
5050

51-
@Suppress("RemoveEmptyPrimaryConstructor")
52-
internal class KaptComponentRegistrar() : ComponentRegistrar {
51+
internal class KaptComponentRegistrar : ComponentRegistrar {
5352

5453
override fun registerProjectComponents(project: MockProject, configuration: CompilerConfiguration) {
55-
println("registerProjectComponents called")
5654
if (threadLocalParameters.get().processors.isEmpty())
5755
return
5856

0 commit comments

Comments
 (0)