Skip to content

Java 註解處理到 KSP 參考指南

程式元素

JavaKSP 中最接近的設施備註
AnnotationMirrorKSAnnotation
AnnotationValueKSValueArguments
ElementKSDeclaration / KSDeclarationContainer
ExecutableElementKSFunctionDeclaration
PackageElementKSFileKSP 不會將套件建模為程式元素
ParameterizableKSDeclaration
QualifiedNameableKSDeclaration
TypeElementKSClassDeclaration
TypeParameterElementKSTypeParameter
VariableElementKSValueParameter / KSPropertyDeclaration

型別

KSP 需要明確的型別解析,因此 Java 中的某些功能只能由 KSType 和解析前的對應元素來執行。

JavaKSP 中最接近的設施備註
ArrayTypeKSBuiltIns.arrayType
DeclaredTypeKSType / KSClassifierReference
ErrorTypeKSType.isError
ExecutableTypeKSType / KSCallableReference
IntersectionTypeKSType / KSTypeParameter
NoTypeKSType.isError在 KSP 中不適用
NullType在 KSP 中不適用
PrimitiveTypeKSBuiltIns與 Java 中的基本型別不完全相同
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionType不適用Kotlin 每個 catch 區塊只有一種型別。UnionType 甚至對於 Java 註解處理器也是不可見的
WildcardTypeKSType / KSTypeArgument

其他

JavaKSP 中最接近的設施備註
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns有些可以在內建功能中找到,否則請檢查 KSClassDeclaration 以獲取 DeclaredType
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitor在 KSP 中不需要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utils某些 utils 也整合到了符號介面中
ElementsResolver / utils

詳細資訊

查看 Java 註解處理 API 的功能如何由 KSP 執行。

AnnotationMirror

JavaKSP 對應項目
getAnnotationTypeksAnnotation.annotationType
getElementValuesksAnnotation.arguments

AnnotationValue

JavaKSP 對應項目
getValueksValueArgument.value

Element

JavaKSP 對應項目
asTypeksClassDeclaration.asType(...) 僅適用於 KSClassDeclaration。需要提供型別引數。
getAnnotation待實作
getAnnotationMirrorsksDeclaration.annotations
getEnclosedElementsksDeclarationContainer.declarations
getEnclosingElementsksDeclaration.parentDeclaration
getKind根據 ClassKindFunctionKind 進行型別檢查與轉型
getModifiersksDeclaration.modifiers
getSimpleNameksDeclaration.simpleName

ExecutableElement

JavaKSP 對應項目
getDefaultValue待實作
getParametersksFunctionDeclaration.parameters
getReceiverTypeksFunctionDeclaration.parentDeclaration
getReturnTypeksFunctionDeclaration.returnType
getSimpleNameksFunctionDeclaration.simpleName
getThrownTypes在 Kotlin 中不需要
getTypeParametersksFunctionDeclaration.typeParameters
isDefault檢查父級宣告是否為介面
isVarArgsksFunctionDeclaration.parameters.any { it.isVarArg }

Parameterizable

JavaKSP 對應項目
getTypeParametersksFunctionDeclaration.typeParameters

QualifiedNameable

JavaKSP 對應項目
getQualifiedNameksDeclaration.qualifiedName

TypeElement

JavaKSP 對應項目
getEnclosedElementsksClassDeclaration.declarations
getEnclosingElementksClassDeclaration.parentDeclaration
getInterfaces
kotlin
// 應該可以在不進行解析的情況下完成
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
getNestingKind檢查 KSClassDeclaration.parentDeclarationinner 修飾符
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclass
kotlin
// 應該可以在不進行解析的情況下完成
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS }
getTypeParametersksClassDeclaration.typeParameters

TypeParameterElement

JavaKSP 對應項目
getBoundsksTypeParameter.bounds
getEnclosingElementksTypeParameter.parentDeclaration
getGenericElementksTypeParameter.parentDeclaration

VariableElement

JavaKSP 對應項目
getConstantValue待實作
getEnclosingElementksValueParameter.parentDeclaration
getSimpleNameksValueParameter.simpleName

ArrayType

JavaKSP 對應項目
getComponentTypeksType.arguments.first()

DeclaredType

JavaKSP 對應項目
asElementksType.declaration
getEnclosingTypeksType.declaration.parentDeclaration
getTypeArgumentsksType.arguments

ExecutableType

函式的 KSType 僅是由 FunctionN<R, T1, T2, ..., TN> 系列所表示的簽章。

JavaKSP 對應項目
getParameterTypesksType.declaration.typeParameters, ksFunctionDeclaration.parameters.map { it.type }
getReceiverTypeksFunctionDeclaration.parentDeclaration.asType(...)
getReturnTypeksType.declaration.typeParameters.last()
getThrownTypes在 Kotlin 中不需要
getTypeVariablesksFunctionDeclaration.typeParameters

IntersectionType

JavaKSP 對應項目
getBoundsksTypeParameter.bounds

TypeMirror

JavaKSP 對應項目
getKind對於基本型別、Unit 型別,與 KSBuiltIns 中的型別進行比較,否則為宣告型別

TypeVariable

JavaKSP 對應項目
asElementksType.declaration
getLowerBound待定。僅在提供擷取並需要進行明確邊界檢查時才需要。
getUpperBoundksTypeParameter.bounds

WildcardType

JavaKSP 對應項目
getExtendsBound
kotlin
if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null
getSuperBound
kotlin
if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null

Elements

JavaKSP 對應項目
getAllAnnotationMirrorsKSDeclarations.annotations
getAllMembersgetAllFunctionsgetAllProperties 待實作
getBinaryName待定,參見 Java 規範
getConstantExpression只有常數值,沒有運算式
getDocComment待實作
getElementValuesWithDefaults待實作
getNameresolver.getKSNameFromString
getPackageElement不支援套件,但可以檢索套件資訊。在 KSP 中無法對套件進行操作
getPackageOf不支援套件
getTypeElementResolver.getClassDeclarationByName
hides待實作
isDeprecated
kotlin
KsDeclaration.annotations.any { 
    it.annotationType.resolve()!!.declaration.qualifiedName!!.asString() == Deprecated::class.qualifiedName
}
overridesKSFunctionDeclaration.overrides / KSPropertyDeclaration.overrides (各種類別的成員函式)
printElementsKSP 在大多數類別上都有基本的 toString() 實作

Types

JavaKSP 對應項目
asElementksType.declaration
asMemberOfresolver.asMemberOf
boxedClass不需要
capture待定
containsKSType.isAssignableFrom
directSuperTypes(ksType.declaration as KSClassDeclaration).superTypes
erasureksType.starProjection()
getArrayTypeksBuiltIns.arrayType.replace(...)
getDeclaredTypeksClassDeclaration.asType
getNoTypeksBuiltIns.nothingType / null
getNullType根據上下文,KSType.markNullable 可能很有用
getPrimitiveType不需要,請檢查 KSBuiltins
getWildcardType在預期 KSTypeArgument 的地方使用 Variance
isAssignableksType.isAssignableFrom
isSameTypeksType.equals
isSubsignaturefunctionTypeA == functionTypeB / functionTypeA == functionTypeB.starProjection()
isSubtypeksType.isAssignableFrom
unboxedType不需要