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 块只支持一种类型。即使 Java 注解处理器也无法观察到 UnionType
WildcardTypeKSType / KSTypeArgument

其他

JavaKSP 中对应的功能备注
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns部分可在内置类型中找到,否则请检查 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

NOTE

函数的 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
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不需要