Java annotation processing to KSP reference

Program elements

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

Types

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

JavaClosest facility in KSPNotes
ArrayTypeKSBuiltIns
DeclaredTypeKSType / KSClassifierReference
ErrorTypenull
ExecutableTypeKSType / KSCallableReference
IntersectionTypeKSType / KSTypeParameter
NoTypeKSBuiltIns / null
NullTypeKSBuiltIns
PrimitiveTypeKSBuiltIns
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
SimpleAnnotationValueVisitorNo needed in KSP
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utilsSome of the utils are also integrated into symbol interfaces
ElementsResolver / utils

Details

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(...) // Only available for KSClassDeclaration. Type arguments need to be supplied.
getAnnotation// To be implemented.
getAnnotationMirrorsksDeclaration.annotations
getEnclosedElementsksDeclarationContainer.declarations
getEnclosingElementsksDeclaration.parentDeclaration
getKindis operator + ClassKind or FunctionKind
getModifiersksDeclaration.modifiers
getSimpleNameksDeclaration.simpleName

ExecutableElement

JavaKSP equivalent
getDefaultValue// To be implemented.
getParametersksFunctionDeclaration.parameters
getReceiverTypeksFunctionDeclaration.parentDeclaration
getReturnTypeksFunctionDeclaration.returnType
getSimpleNameksFunctionDeclaration.simpleName
getThrownTypes// Not needed in Kotlin.
getTypeParametersksFunctionDeclaration.typeParameters
isDefault// Check 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
getInterfacesksClassDeclaration.superTypes.map { it.resolve() }.filter {(it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE} // Should be able to do without resolution.
getNestingKind// check KSClassDeclaration.parentDeclaration and inner modifier.
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclassksClassDeclaration.superTypes.map { it.resolve() }.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS } // Should be able to do without resolution.
getTypeParametersksClassDeclaration.typeParameters

TypeParameterElement

JavaKSP equivalent
getBoundsksTypeParameter.bounds
getEnclosingElementksTypeParameter.parentDeclaration
getGenericElementksTypeParameter.parentDeclaration

VariableElement

JavaKSP equivalent
getConstantValue// To be implemented.
getEnclosingElementksVariableParameter.parentDeclaration
getSimpleNameksVariableParameter.simpleName

ArrayType

JavaKSP equivalent
getComponentTypeksType.arguments.first()

DeclaredType

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

ExecutableType

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

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

IntersectionType

JavaKSP equivalent
getBoundsksTypeParameter.bounds

TypeMirror

JavaKSP equivalent
getKind// Compare with types in KSBuiltIns.

TypeVariable

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

WildcardType

JavaKSP equivalent
getExtendsBoundif (ksTypeArgument.variance == Variance.COVARIANT) { ksTypeArgument.type } else { null }
getSuperBoundif (ksTypeArgument.variance == Variance.CONTRAVARIANT) { ksTypeArgument.type } else { null }

Elements

JavaKSP equivalent
getAllAnnotationMirrorsKSDeclarations.annotations
getAllMembersgetAllFunctions and getAllProperties, the latter is not there yet
getBinaryName// To be decided, see Java Spec
getConstantExpressionwe have constant value, not expression
getDocComment// To be implemented
getElementValuesWithDefaults// To 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
hides// To be implemented
isDeprecatedKsDeclaration.annotations.any { it.annotationType.resolve()!!.declaration.quailifiedName!!.asString() == Deprecated::class.quailifiedName }
overridesKSFunctionDeclaration.overrides // extension function defined in util.kt.
printElements// Users can implement them freely.

Types

JavaKSP equivalent
asElementksType.declaration
asMemberOfksClassDeclaration.typeArguments + ksClassDeclaration.asType
boxedClass// Not needed
capture// To be decided.
containsKSType.isAssignableFrom
directSuperTypes(ksType.declaration as KSClassDeclaration).superTypes
erasureksType.starProjection()
getArrayTypeksBuiltIns.arrayType.replace(...)
getDeclaredTypeksClassDeclaration.asType
getNoTypeksBuiltIns.nothingType / null
getNullType// depends on the context, KSType.markNullable maybe useful.
getPrimitiveType// Not needed
getWildcardType// Use Variance in places expecting KSTypeArgument
isAssignableksType.isAssignableFrom
isSameTypeksType.equals
isSubsignaturefunctionTypeA == functionTypeB
isSubtypeksType.isAssignableFrom
unboxedType// Not needed.