Skip to content

Java 註解處理到 KSP 參考

程式元素

JavaKSP 中的近似功能備註
AnnotationMirrorKSAnnotation
AnnotationValueKSValueArguments
ElementKSDeclaration / KSDeclarationContainer
ExecutableElementKSFunctionDeclaration
PackageElementKSFileKSP 不將套件 (package) 建模為程式元素
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 中的基本類型 (primitive type) 不完全相同
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionType不適用Kotlin 每個 catch 區塊只支援一種類型。UnionType 即使在 Java 註解處理器中也無法觀察到。
WildcardTypeKSType / KSTypeArgument

其他

JavaKSP 中的近似功能備註
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns部分可在內建 (builtins) 中找到,否則檢查 KSClassDeclaration 以獲取 DeclaredType
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitor在 KSP 中不需要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utils部分 utils 也整合到符號介面 (symbol interface) 中
ElementsResolver / utils

細節

了解如何透過 KSP 實現 Java 註解處理 API 的功能。

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檢查父宣告 (parent declaration) 是否為介面
isVarArgsksFunctionDeclaration.parameters.any { it.isVarArg }

Parameterizable

JavaKSP 等效
getTypeParametersksFunctionDeclaration.typeParameters

QualifiedNameable

JavaKSP 等效
getQualifiedNameksDeclaration.qualifiedName

TypeElement

JavaKSP 等效
getEnclosedElementsksClassDeclaration.declarations
getEnclosingElementksClassDeclaration.parentDeclaration
getInterfaces
kotlin
// Should be able to do without resolution
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
getNestingKind檢查 KSClassDeclaration.parentDeclarationinner 修飾符
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclass
kotlin
// Should be able to do without resolution
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

NOTE

函數的 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 等效
getKindKSBuiltIns 中的基本類型 (primitive type)、Unit 類型進行比較,否則為宣告類型

TypeVariable

JavaKSP 等效
asElementksType.declaration
getLowerBound待定。僅在提供捕獲 (capture) 且需要明確的邊界檢查時才需要。
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存在常數值,而非表達式 (expression)
getDocComment待實作
getElementValuesWithDefaults待實作
getNameresolver.getKSNameFromString
getPackageElement不支援套件 (package),但可以檢索套件資訊。KSP 無法對套件執行操作。
getPackageOf不支援套件 (package)
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不需要