/
FunctionSinceRepository.kt
143 lines (122 loc) · 5.27 KB
/
FunctionSinceRepository.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/*
* Copyright 2023 the original author or authors.
*
* 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 gradlebuild.kotlindsl.generator.codegen
import com.thoughtworks.qdox.JavaProjectBuilder
import com.thoughtworks.qdox.library.OrderedClassLibraryBuilder
import com.thoughtworks.qdox.model.JavaAnnotatedElement
import com.thoughtworks.qdox.model.JavaClass
import org.gradle.internal.classloader.DefaultClassLoaderFactory
import org.gradle.internal.classpath.DefaultClassPath
import java.io.File
import java.io.StringReader
import java.net.URLClassLoader
/**
* Provides `@since` javadoc tag value from sources.
*/
class FunctionSinceRepository(classPath: Set<File>, sourcePath: Set<File>) : AutoCloseable {
// https://github.com/paul-hammant/qdox/issues/182
private
val filesWithUnsupportedAnnotations = listOf(
"Transformer.java",
"Provider.java",
"ListProperty.java",
"MapProperty.java",
"Property.java",
"SetProperty.java",
)
private
val loader: URLClassLoader =
isolatedClassLoaderFor(classPath)
private
val builder: JavaProjectBuilder =
javaProjectBuilderFor(loader, sourcePath)
fun since(functionSignature: String): String? {
val javaFunction = parsedJavaFunctionOf(functionSignature)
val matchingType = builder.sources
.flatMap { it.classes }
.singleOrNull { javaFunction.typeName == it.binaryName }
?: throw IllegalArgumentException("Class for function '$functionSignature' not found in since repository! See FunctionSinceRepository.kt")
val matchingFunction = javaFunction
.run { matchingType.getMethodsBySignature(name, parameterTypes, false, isVararg) }
.singleOrNull()
?: throw IllegalArgumentException("Function '$functionSignature' not found in @since repository! See FunctionSinceRepository.kt")
return matchingFunction.since ?: matchingType.since
}
private
fun parsedJavaFunctionOf(functionSignature: String): JavaFunction<JavaClass> =
javaFunctionOf(functionSignature).map {
builder.getClassByName(it)
}
private
val JavaAnnotatedElement.since: String?
get() = getTagByName("since")?.value
private
fun isolatedClassLoaderFor(classPath: Set<File>): URLClassLoader =
DefaultClassLoaderFactory().createIsolatedClassLoader("FunctionsSinceRepository", DefaultClassPath.of(classPath)) as URLClassLoader
private
fun javaProjectBuilderFor(loader: ClassLoader, sourcePath: Set<File>): JavaProjectBuilder =
JavaProjectBuilder(OrderedClassLibraryBuilder().apply {
appendClassLoader(loader)
}).apply {
sourcePath.filter { it.extension == "java" }
.forEach { sourceFile ->
if (sourceFile.name in filesWithUnsupportedAnnotations) {
// This is a hack.
//
// qdox doesn't understand annotations placed in generic type parameters
// The only place we use this is with Nullable, so this hackily removes the annotation when
// the source file is processed by qdox.
//
// https://github.com/paul-hammant/qdox/issues/182
val filteredSourceCode =
sourceFile.bufferedReader().readText().replace("@org.jetbrains.annotations.Nullable", "")
addSource(StringReader(filteredSourceCode))
} else {
addSource(sourceFile)
}
}
}
override fun close() =
loader.close()
}
/**
* Extract a [JavaFunction] helper object from a function signature string.
*/
internal
fun javaFunctionOf(functionSignature: String): JavaFunction<String> =
JavaFunction(
typeName = functionSignature.split('(')[0].dropLastWhile { it != '.' }.dropLast(1),
name = functionSignature.split('(')[0].takeLastWhile { it != '.' },
parameterTypes = functionSignature.split('(')[1].dropLast(1).split(",").map { it.trim() }.let { paramStrings ->
paramStrings.mapIndexed { idx: Int, paramString: String ->
if (idx != paramStrings.size - 1) paramString
else paramString.replace("[]", "")
}
},
isVararg = functionSignature.dropLast(1).endsWith("[]")
)
internal
data class JavaFunction<out ParameterType>(
val typeName: String,
val name: String,
val parameterTypes: List<ParameterType>,
val isVararg: Boolean,
) {
fun <T> map(transform: (ParameterType) -> T): JavaFunction<T> =
JavaFunction(
typeName, name, parameterTypes.map(transform), isVararg
)
}