Java 注解处理对应到 KSP 参考

Program elements

JavaClosest facility in KSPNotes
AnnotationMirrorKSAnnotation
AnnotationValueKSValueArguments
ElementKSDeclaration / KSDeclarationContainer
ExecutableElementKSFunctionDeclaration
PackageElementKSFileKSP doesn’t model packages as program elements
ParameterizableKSDeclaration
QualifiedNameableKSDeclaration
TypeElementKSClassDeclaration
TypeParameterElementKSTypeParameter
VariableElementKSValueParameter / KSPropertyDeclaration

Types

KSP requires explicit type resolution, so some functionalities in Java can only be carried out by KSType and the corresponding elements before resolution.

JavaClosest facility in KSPNotes
ArrayTypeKSBuiltIns.arrayType
DeclaredTypeKSType / KSClassifierReference
ErrorTypeKSType.isError
ExecutableTypeKSType / KSCallableReference
IntersectionTypeKSType / KSTypeParameter
NoTypeKSType.isErrorN/A in KSP
NullTypeN/A in KSP
PrimitiveTypeKSBuiltInsNot exactly same as primitive type in Java
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlin has only one type per catch block. UnionType is also not observable by even Java annotation processors
WildcardTypeKSType / KSTypeArgument

Misc

JavaClosest facility in KSPNotes
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltInsSome can be found in builtins, otherwise check KSClassDeclaration for DeclaredType
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorNot needed in KSP
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utilsSome of the utils are also integrated into symbol interfaces
ElementsResolver / utils

Details

See how functionalities of Java annotation processing API can be carried out by KSP.

AnnotationMirror

JavaKSP equivalent
getAnnotationTypeksAnnotation.annotationType
getElementValuesksAnnotation.arguments

AnnotationValue

JavaKSP equivalent
getValueksValueArgument.value

Element

JavaKSP equivalent
asTypeksClassDeclaration.asType(…) is available for KSClassDeclaration only. Type arguments need to be supplied.
getAnnotationTo be implemented
getAnnotationMirrorsksDeclaration.annotations
getEnclosedElementsksDeclarationContainer.declarations
getEnclosingElementsksDeclaration.parentDeclaration
getKindType check and cast following ClassKind or FunctionKind
getModifiersksDeclaration.modifiers
getSimpleNameksDeclaration.simpleName

ExecutableElement

JavaKSP equivalent
getDefaultValueTo be implemented
getParametersksFunctionDeclaration.parameters
getReceiverTypeksFunctionDeclaration.parentDeclaration
getReturnTypeksFunctionDeclaration.returnType
getSimpleNameksFunctionDeclaration.simpleName
getThrownTypesNot needed in Kotlin
getTypeParametersksFunctionDeclaration.typeParameters
isDefaultCheck whether parent declaration is an interface or not
isVarArgsksFunctionDeclaration.parameters.any { it.isVarArg }

Parameterizable

JavaKSP equivalent
getTypeParametersksFunctionDeclaration.typeParameters

QualifiedNameable

JavaKSP equivalent
getQualifiedNameksDeclaration.qualifiedName

TypeElement

JavaKSP equivalent
getEnclosedElementsksClassDeclaration.declarations
getEnclosingElementksClassDeclaration.parentDeclaration
getInterfaceskotlin // Should be able to do without resolution ksClassDeclaration.superTypes .map { it.resolve() } .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
getNestingKindCheck KSClassDeclaration.parentDeclaration and inner modifier
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclasskotlin // Should be able to do without resolution ksClassDeclaration.superTypes .map { it.resolve() } .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS }
getTypeParametersksClassDeclaration.typeParameters

TypeParameterElement

JavaKSP equivalent
getBoundsksTypeParameter.bounds
getEnclosingElementksTypeParameter.parentDeclaration
getGenericElementksTypeParameter.parentDeclaration

VariableElement

JavaKSP equivalent
getConstantValueTo be implemented
getEnclosingElementksValueParameter.parentDeclaration
getSimpleNameksValueParameter.simpleName

ArrayType

JavaKSP equivalent
getComponentTypeksType.arguments.first()

DeclaredType

JavaKSP equivalent
asElementksType.declaration
getEnclosingTypeksType.declaration.parentDeclaration
getTypeArgumentsksType.arguments

ExecutableType

A KSType for a function is just a signature represented by the FunctionN<R, T1, T2, ..., TN> family.

Java 注解处理对应到 KSP 参考 - 图1

JavaKSP equivalent
getParameterTypesksType.declaration.typeParameters, ksFunctionDeclaration.parameters.map { it.type }
getReceiverTypeksFunctionDeclaration.parentDeclaration.asType(…)
getReturnTypeksType.declaration.typeParameters.last()
getThrownTypesNot needed in Kotlin
getTypeVariablesksFunctionDeclaration.typeParameters

IntersectionType

JavaKSP equivalent
getBoundsksTypeParameter.bounds

TypeMirror

JavaKSP equivalent
getKindCompare with types in KSBuiltIns for primitive types, Unit type, otherwise declared types

TypeVariable

JavaKSP equivalent
asElementksType.declaration
getLowerBoundTo be decided. Only needed if capture is provided and explicit bound checking is needed.
getUpperBoundksTypeParameter.bounds

WildcardType

JavaKSP equivalent
getExtendsBoundkotlin if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null
getSuperBoundkotlin if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null

Elements

JavaKSP equivalent
getAllAnnotationMirrorsKSDeclarations.annotations
getAllMembersgetAllFunctions, getAllProperties is to be implemented
getBinaryNameTo be decided, see Java Specification
getConstantExpressionThere is constant value, not expression
getDocCommentTo be implemented
getElementValuesWithDefaultsTo be implemented
getNameresolver.getKSNameFromString
getPackageElementPackage not supported, while package information can be retrieved. Operation on package is not possible for KSP
getPackageOfPackage not supported
getTypeElementResolver.getClassDeclarationByName
hidesTo be implemented
isDeprecatedkotlin KsDeclaration.annotations.any { it.annotationType.resolve()!!.declaration.qualifiedName!!.asString() == Deprecated::class.qualifiedName }
overridesKSFunctionDeclaration.overrides / KSPropertyDeclaration.overrides (member function of respective class)
printElementsKSP has basic toString() implementation on most classes

Types

JavaKSP equivalent
asElementksType.declaration
asMemberOfresolver.asMemberOf
boxedClassNot needed
captureTo be decided
containsKSType.isAssignableFrom
directSuperTypes(ksType.declaration as KSClassDeclaration).superTypes
erasureksType.starProjection()
getArrayTypeksBuiltIns.arrayType.replace(…)
getDeclaredTypeksClassDeclaration.asType
getNoTypeksBuiltIns.nothingType / null
getNullTypeDepending on the context, KSType.markNullable could be useful
getPrimitiveTypeNot needed, check for KSBuiltins
getWildcardTypeUse Variance in places expecting KSTypeArgument
isAssignableksType.isAssignableFrom
isSameTypeksType.equals
isSubsignaturefunctionTypeA == functionTypeB / functionTypeA == functionTypeB.starProjection()
isSubtypeksType.isAssignableFrom
unboxedTypeNot needed