跳到主要内容

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

其他

JavaKSP 中最接近的工具备注
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns一些可以在内置函数中找到,否则检查 KSClassDeclarationDeclaredType
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSP 中不需要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utilsutils 的某些部分也集成到符号接口中
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 等效项

getEnclosedElements

ksClassDeclaration.declarations

getEnclosingElement

ksClassDeclaration.parentDeclaration

getInterfaces

// 应该能够在不进行解析的情况下执行
ksClassDeclaration.superTypes
.map { it.resolve() }
.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }

getNestingKind

检查 KSClassDeclaration.parentDeclarationinner 修饰符

getQualifiedName

ksClassDeclaration.qualifiedName

getSimpleName

ksClassDeclaration.simpleName

getSuperclass

// 应该能够在不进行解析的情况下执行
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> 系列表示的签名。

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

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

存在常量值,而不是表达式

getDocComment

待实现

getElementValuesWithDefaults

待实现

getName

resolver.getKSNameFromString

getPackageElement

不支持包,但可以检索包信息。KSP 无法对包进行操作

getPackageOf

不支持包

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