我想知道是否有一种方法可以在注释处理过程中将注释应用于函数并访问这些函数的主体。如果无法通过检查注释处理器中的Element
对象直接获取方法主体,那么是否有其他替代方法可以访问应用这些注释的函数的源代码?
作为我正在进行的项目的一部分,我试图使用kapt来检查使用特定类型的注释进行注释的Kotlin函数,并基于它们生成类。例如,给定这样的带注释功能:
@ElementaryNode
fun addTwoNumbers(x: Int, y: Int) = x + y
我的注释处理器当前生成以下内容:
class AddTwoNumbers : Node {
val x: InputPort<Int> = TODO("implement node port property")
val y: InputPort<Int> = TODO("implement node port property")
val output: OutputPort<Int> = TODO("implement node port property")
}
但是,我需要在此类中包括原始函数本身,基本上就像将其作为私有函数复制/粘贴一样:
class AddTwoNumbers : Node {
val x: InputPort<Int> = TODO("implement node port property")
val y: InputPort<Int> = TODO("implement node port property")
val output: OutputPort<Int> = TODO("implement node port property")
private fun body(x: Int, y: Int) = x + y
}
我尝试过的事情
基于this answer,我尝试使用com.sun.source.util.Trees
来访问ExecutableElement
的方法主体,该方法主体与带注释的函数相对应:
override fun inspectElement(element: Element) {
if (element !is ExecutableElement) {
processingEnv.messager.printMessage(
Diagnostic.Kind.ERROR,
"Cannot generate elementary node from non-executable element"
)
return
}
val docComment = processingEnv.elementUtils.getDocComment(element)
val trees = Trees.instance(processingEnv)
val body = trees.getTree(element).body
processingEnv.messager.printMessage(Diagnostic.Kind.WARNING, "Processing ${element.simpleName}: $body")
}
但是,kapt只生成方法主体的存根,所以我为每个方法主体获得的都是这样的东西:
$ gradle clean build
...
> Task :kaptGenerateStubsKotlin
w: warning: Processing addTwoNumbers: {
return 0;
}
w: warning: Processing subtractTwoNumbers: {
return 0.0;
}
w: warning: Processing transform: {
return null;
}
w: warning: Processing minAndMax: {
return null;
}
w: warning: Processing dummy: {
}
更新
访问表示每个功能的Element.enclosingElement
上的ExecutableElement
会给我定义该功能的程序包/模块的限定名称。例如,addTwoNumbers
在Main.kt
中被声明为顶级函数,在注释处理期间,我得到以下输出:Processing addTwoNumbers: com.mycompany.testmaster.playground.MainKt
。
根据此信息,是否可以访问原始源文件(Main.kt
)?
答案 0 :(得分:1)
这并不容易,但是我最终设法找到了一个解决方案。
我发现在批注处理期间,Kotlin在临时构建输出目录下生成元数据文件。这些元数据文件包含序列化的信息,其中包括原始源文件的路径,该原始文件包含我正在处理的注释:
浏览Kapt插件的源代码,发现this file使我得以弄清楚如何反序列化这些文件中的信息,从而使我能够提取原始源代码的位置。
我创建了一个Kotlin对象SourceCodeLocator
,将其放在一起,以便可以将代表函数的Element
传递给它,并且它将返回包含它的源代码的字符串表示形式:
package com.mycompany.testmaster.nodegen.parsers
import com.mycompany.testmaster.nodegen.KAPT_KOTLIN_GENERATED_OPTION_NAME
import com.mycompany.testmaster.nodegen.KAPT_METADATA_EXTENSION
import java.io.ByteArrayInputStream
import java.io.File
import java.io.ObjectInputStream
import javax.annotation.processing.ProcessingEnvironment
import javax.lang.model.element.Element
import javax.lang.model.element.ElementKind
import javax.lang.model.element.ExecutableElement
internal object SourceCodeLocator {
fun sourceOf(function: Element, environment: ProcessingEnvironment): String {
if (function !is ExecutableElement)
error("Cannot extract source code from non-executable element")
return getSourceCodeContainingFunction(function, environment)
}
private fun getSourceCodeContainingFunction(function: Element, environment: ProcessingEnvironment): String {
val metadataFile = getMetadataForFunction(function, environment)
val path = deserializeMetadata(metadataFile.readBytes()).entries
.single { it.key.contains(function.simpleName) }
.value
val sourceFile = File(path)
assert(sourceFile.isFile) { "Source file does not exist at stated position within metadata" }
return sourceFile.readText()
}
private fun getMetadataForFunction(element: Element, environment: ProcessingEnvironment): File {
val enclosingClass = element.enclosingElement
assert(enclosingClass.kind == ElementKind.CLASS)
val stubDirectory = locateStubDirectory(environment)
val metadataPath = enclosingClass.toString().replace(".", "/")
val metadataFile = File("$stubDirectory/$metadataPath.$KAPT_METADATA_EXTENSION")
if (!metadataFile.isFile) error("Cannot locate kapt metadata for function")
return metadataFile
}
private fun deserializeMetadata(data: ByteArray): Map<String, String> {
val metadata = mutableMapOf<String, String>()
val ois = ObjectInputStream(ByteArrayInputStream(data))
ois.readInt() // Discard version information
val lineInfoCount = ois.readInt()
repeat(lineInfoCount) {
val fqName = ois.readUTF()
val path = ois.readUTF()
val isRelative = ois.readBoolean()
ois.readInt() // Discard position information
assert(!isRelative)
metadata[fqName] = path
}
return metadata
}
private fun locateStubDirectory(environment: ProcessingEnvironment): File {
val kaptKotlinGeneratedDir = environment.options[KAPT_KOTLIN_GENERATED_OPTION_NAME]
val buildDirectory = File(kaptKotlinGeneratedDir).ancestors.firstOrNull { it.name == "build" }
val stubDirectory = buildDirectory?.let { File("${buildDirectory.path}/tmp/kapt3/stubs/main") }
if (stubDirectory == null || !stubDirectory.isDirectory)
error("Could not locate kapt stub directory")
return stubDirectory
}
// TODO: convert into generator for Kotlin 1.3
private val File.ancestors: Iterable<File>
get() {
val ancestors = mutableListOf<File>()
var currentAncestor: File? = this
while (currentAncestor != null) {
ancestors.add(currentAncestor)
currentAncestor = currentAncestor.parentFile
}
return ancestors
}
}
注意事项
该解决方案似乎对我有用,但我不能保证它在一般情况下都能起作用。特别是,我通过Kapt Gradle plugin(当前版本1.3.0-rc-198)在我的项目中配置了Kapt,它确定了存储所有生成的文件(包括元数据文件)的目录。然后,我假设元数据文件存储在项目构建输出文件夹下的/tmp/kapt3/stubs/main
处。
我已经在JetBrain的问题跟踪器中创建了一个功能请求,以使此过程更轻松,更可靠,因此这些黑客是不必要的。
示例
就我而言,我已经可以使用它来转换这样的源代码:
minAndMax.kt
package com.mycompany.testmaster.playground.nodes
import com.mycompany.testmaster.nodegen.annotations.ElementaryNode
@ElementaryNode
private fun <T: Comparable<T>> minAndMax(values: Iterable<T>) =
Output(values.min(), values.max())
private data class Output<T : Comparable<T>>(val min: T?, val max: T?)
并生成这样的源代码,其中包含原始源代码的修改版本:
MinAndMax.gen.kt
// This code was generated by the <Company> Test Master node generation tool at 2018-10-29T08:31:35.847.
//
// Do not modify this file. Any changes may be overwritten at a later time.
package com.mycompany.testmaster.playground.nodes.gen
import com.mycompany.testmaster.domain.ElementaryNode
import com.mycompany.testmaster.domain.InputPort
import com.mycompany.testmaster.domain.OutputPort
import com.mycompany.testmaster.domain.Port
import kotlin.collections.Set
import kotlinx.coroutines.async
import kotlinx.coroutines.coroutineScope
class MinAndMax<T : Comparable<in T>> : ElementaryNode() {
private val _values: Port<Iterable<out T>> = Port<Iterable<out T>>()
val values: InputPort<Iterable<out T>> = _values
private val _min: Port<T?> = Port<T?>()
val min: OutputPort<T?> = _min
private val _max: Port<T?> = Port<T?>()
val max: OutputPort<T?> = _max
override val ports: Set<Port<*>> = setOf(_values, _min, _max)
override suspend fun executeOnce() {
coroutineScope {
val values = async { _values.receive() }
val output = _nodeBody(values.await())
_min.forward(output.min)
_max.forward(output.max)
}
}
}
private fun <T: Comparable<T>> _nodeBody(values: Iterable<T>) =
Output(values.min(), values.max())
private data class Output<T : Comparable<T>>(val min: T?, val max: T?)