The following tables map the Java annotation processing API to its equivalents in Kotlin Symbol Processing (KSP).
Program elements
Java
Closest facility in KSP
Notes
AnnotationMirror
KSAnnotation
AnnotationValue
KSValueArgument
Element
KSDeclaration/KSDeclarationContainer
ExecutableElement
KSFunctionDeclaration
PackageElement
KSFile
KSP doesn't model packages as program elements
Parameterizable
KSDeclaration
QualifiedNameable
KSDeclaration
TypeElement
KSClassDeclaration
TypeParameterElement
KSTypeParameter
VariableElement
KSValueParameter/KSPropertyDeclaration
Types
KSP represents unresolved types as KSTypeReference and requires explicit type resolution. Call KSTypeReference.resolve() when you need the underlying KSType, for example, to inspect its declaration or nullability. For more information, see When to resolve types.
Java
Closest facility in KSP
Notes
ArrayType
KSBuiltIns.arrayType
Covers only Array<T>. Primitive arrays map to dedicated classes, for example, int[] maps to IntArray.
DeclaredType
KSType/KSClassifierReference
ErrorType
KSType.isError
ExecutableType
KSType/KSCallableReference
IntersectionType
KSType/KSTypeParameter
NoType
KSBuiltIns.unitType
Kotlin uses Unit instead of void. Other NoType kinds have no equivalent in KSP.
NullType
N/A in KSP
PrimitiveType
KSBuiltIns
Kotlin built-in types can be boxed on the JVM
ReferenceType
KSTypeReference
TypeMirror
KSType
TypeVariable
KSTypeParameter
UnionType
N/A
Kotlin has only one type per catch block. UnionType isn't observable even by Java annotation processors.
WildcardType
KSType/KSTypeArgument
Miscellaneous
Java
Closest facility in KSP
Notes
Name
KSName
ElementKind
ClassKind/FunctionKind
Modifier
Modifier
NestingKind
ClassKind/FunctionKind
AnnotationValueVisitor
ElementVisitor
KSVisitor
AnnotatedConstruct
KSAnnotated
TypeVisitor
TypeKind
KSBuiltIns
Use KSBuiltIns for built-in types. For declared types, inspect the KSClassDeclaration.
ElementFilter
Collection.filterIsInstance()
ElementKindVisitor
KSVisitor
ElementScanner
KSTopDownVisitor
SimpleAnnotationValueVisitor
Not needed in KSP
SimpleElementVisitor
KSVisitor
SimpleTypeVisitor
TypeKindVisitor
Types
Resolver/utils
Some of the utils are also integrated into symbol interfaces
Elements
Resolver/utils
Details
The following tables show how to carry out individual operations of the Java annotation processing API with KSP.
AnnotationMirror
Java
KSP equivalent
getAnnotationType
ksAnnotation.annotationType
getElementValues
ksAnnotation.arguments
AnnotationValue
Java
KSP equivalent
getValue
ksValueArgument.value
Element
Java
KSP equivalent
asType
ksClassDeclaration.asType(...) is available for KSClassDeclaration only. Type arguments need to be supplied.
getAnnotation
ksAnnotated.getAnnotationsByType()
getAnnotationMirrors
ksDeclaration.annotations
getEnclosedElements
ksDeclarationContainer.declarations
getEnclosingElement
ksDeclaration.parentDeclaration
getKind
Type check and cast following ClassKind or FunctionKind
getModifiers
ksDeclaration.modifiers
getSimpleName
ksDeclaration.simpleName
ExecutableElement
Java
KSP equivalent
getDefaultValue
ksAnnotation.defaultArguments for annotation members
getParameters
ksFunctionDeclaration.parameters
getReceiverType
ksFunctionDeclaration.parentDeclaration for member functions, ksFunctionDeclaration.extensionReceiver for extension functions
Compare with types in KSBuiltIns for primitive types, Unit type, otherwise declared types
TypeVariable
Java
KSP equivalent
asElement
ksType.declaration
getLowerBound
Not supported. Kotlin type parameters can't declare lower bounds, and KSP doesn't support capture conversion.
getUpperBound
ksTypeParameter.bounds
WildcardType
Java
KSP equivalent
getExtendsBound
if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null
getSuperBound
if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null
Elements
Java
KSP equivalent
getAllAnnotationMirrors
KSDeclaration.annotations
getAllMembers
ksClassDeclaration.getAllFunctions() and ksClassDeclaration.getAllProperties()
getBinaryName
No direct equivalent. resolver.mapToJvmSignature() returns the JVM descriptor of a class, such as Lcom/example/Outer$Inner;, which contains its binary name
getConstantExpression
KSP provides constant values, not expressions
getDocComment
ksDeclaration.docString
getElementValuesWithDefaults
ksAnnotation.arguments, which includes the default values of omitted arguments
getName
resolver.getKSNameFromString()
getPackageElement
Not supported. To get the declarations in a package, use resolver.getDeclarationsFromPackage().