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.isErrorKSP 中不適用
NullTypeKSP 中不適用
PrimitiveTypeKSBuiltIns與 Java 中的原始類型不完全相同
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlin 每個 catch 區塊只有一種類型。UnionType 即使是 Java 註解處理器也無法觀察到。
WildcardTypeKSType / KSTypeArgument

其他

JavaKSP 中的對應功能備註
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns部分可在內建類型中找到,否則請檢查 DeclaredTypeKSClassDeclaration
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSP 中不需要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / 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
getThrownTypesKotlin 中不需要
getTypeParametersksFunctionDeclaration.typeParameters
isDefault檢查父宣告是否為介面
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

函數的 KSType 只是由 FunctionN<R, T1, T2, ..., TN> 家族表示的簽名。

JavaKSP 對等物
getParameterTypesksType.declaration.typeParameters, ksFunctionDeclaration.parameters.map { it.type }
getReceiverTypeksFunctionDeclaration.parentDeclaration.asType(...)
getReturnTypeksType.declaration.typeParameters.last()
getThrownTypesKotlin 中不需要
getTypeVariablesksFunctionDeclaration.typeParameters

IntersectionType

JavaKSP 對等物
getBoundsksTypeParameter.bounds

TypeMirror

JavaKSP 對等物
getKindKSBuiltIns 中的類型比較,適用於原始類型、Unit 類型,否則為宣告類型

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
getAllMembersgetAllFunctions, getAllProperties 待實作
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不需要