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.isError在 KSP 中不适用
NullType在 KSP 中不适用
PrimitiveTypeKSBuiltIns与 Java 中的原生类型不完全相同
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlin 每个 catch 代码块只允许一种类型。UnionType 即使对于 Java 注解处理器也无法观察到。
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

详情

了解 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
// 应该可以在不解析的情况下完成
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 等效项
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() 实现

类型

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不需要