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
UnionType不适用Kotlin 的每个 catch 块只有一个类型。甚至 Java 注解处理器也无法观察到 UnionType
WildcardTypeKSType / KSTypeArgument

杂项

JavaKSP 中最接近的设施备注
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns有些可以在 builtins 中找到,否则请检查 KSClassDeclaration 以获取 DeclaredType
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSP 中不需要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utils某些 utils 也集成到了符号接口中
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
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
// 应该可以在不进行解析的情况下完成
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()
getThrownTypesKotlin 中不需要
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不需要