跳至主要内容

Java 註解處理至 KSP 參考指南

程式元素

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

類型 (Types)

KSP 需要顯式的類型解析 (type resolution),因此 Java 中的某些功能只能在解析之前由 KSType 和相應的元素執行。

JavaKSP 中最接近的設施備註
ArrayTypeKSBuiltIns.arrayType
DeclaredTypeKSType / KSClassifierReference
ErrorTypeKSType.isError
ExecutableTypeKSType / KSCallableReference
IntersectionTypeKSType / KSTypeParameter
NoTypeKSType.isError在 KSP 中不適用 (N/A)
NullType在 KSP 中不適用 (N/A)
PrimitiveTypeKSBuiltIns與 Java 中的 primitive type 不完全相同
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlin 每個 catch 塊只有一個類型。即使 Java annotation processor 也無法觀察到 UnionType
WildcardTypeKSType / KSTypeArgument

其他 (Misc)

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 介面中
ElementsResolver / utils

詳情 (Details)

請參閱如何通過 KSP 執行 Java annotation processing 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
getThrownTypesKotlin 中不需要
getTypeParametersksFunctionDeclaration.typeParameters
isDefault檢查 parent declaration 是否為 interface
isVarArgsksFunctionDeclaration.parameters.any { it.isVarArg }

Parameterizable

JavaKSP 等效項
getTypeParametersksFunctionDeclaration.typeParameters

QualifiedNameable

JavaKSP 等效項
getQualifiedNameksDeclaration.qualifiedName

TypeElement

JavaKSP 等效項

getEnclosedElements

ksClassDeclaration.declarations

getEnclosingElement

ksClassDeclaration.parentDeclaration

getInterfaces

// Should be able to do without resolution
ksClassDeclaration.superTypes
.map { it.resolve() }
.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }

getNestingKind

檢查 KSClassDeclaration.parentDeclarationinner modifier

getQualifiedName

ksClassDeclaration.qualifiedName

getSimpleName

ksClassDeclaration.simpleName

getSuperclass

// Should be able to do without resolution
ksClassDeclaration.superTypes
.map { it.resolve() }
.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS }

getTypeParameters

ksClassDeclaration.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> 系列表示的簽名 (signature)。

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 等效項
getKind比較 KSBuiltIns 中的類型以取得 primitive types,Unit 類型,否則為 declared types

TypeVariable

JavaKSP 等效項
asElementksType.declaration
getLowerBound待決定。僅當提供捕獲 (capture) 且需要顯式邊界檢查時才需要。
getUpperBoundksTypeParameter.bounds

WildcardType

JavaKSP 等效項

getExtendsBound

if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null

getSuperBound

if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null

Elements

JavaKSP 等效項

getAllAnnotationMirrors

KSDeclarations.annotations

getAllMembers

getAllFunctions, getAllProperties 待實現

getBinaryName

待決定,請參閱 Java Specification

getConstantExpression

存在 constant value,而非 expression

getDocComment

待實現

getElementValuesWithDefaults

待實現

getName

resolver.getKSNameFromString

getPackageElement

Package 不受支援,但可以檢索 package 資訊。 KSP 無法對 package 進行操作

getPackageOf

Package 不受支援

getTypeElement

Resolver.getClassDeclarationByName

hides

待實現

isDeprecated

KsDeclaration.annotations.any { 
it.annotationType.resolve()!!.declaration.qualifiedName!!.asString() == Deprecated::class.qualifiedName
}

overrides

KSFunctionDeclaration.overrides / KSPropertyDeclaration.overrides (各自類別的成員函數)

printElements

KSP 在大多數類別上都有基本的 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不需要