# Java annotation processing to KSP reference 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](ksp-kotlin-model.html#when-to-resolve-types). | Java |Closest facility in KSP |Notes | ---------------------------------------- | `ArrayType` |`KSBuiltIns.arrayType` |Covers only `Array`. 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](functions.html#unit-returning-functions) 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. Warning: Some of the listed KSP functions, such as `getAnnotationsByType()`, `getJvmCheckedException()`, `mapToJvmSignature()`, and `getDeclarationsFromPackage()`, are experimental. To use them, opt in with `@OptIn(KspExperimental::class)`. ### `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 | | `getReturnType` |`ksFunctionDeclaration.returnType` | | `getSimpleName` |`ksFunctionDeclaration.simpleName` | | `getThrownTypes` |`resolver.getJvmCheckedException(ksFunctionDeclaration)` | | `getTypeParameters` |`ksFunctionDeclaration.typeParameters` | | `isDefault` |Check that the parent declaration is an interface and that the function isn't abstract (`isAbstract`), static (`functionKind`), or private (`isPrivate()`) | | `isVarArgs` |`ksFunctionDeclaration.parameters.any { it.isVararg }` | ### `Parameterizable` | Java |KSP equivalent | ------------------------ | `getTypeParameters` |`ksDeclaration.typeParameters` | ### `QualifiedNameable` | Java |KSP equivalent | ------------------------ | `getQualifiedName` |`ksDeclaration.qualifiedName` | ### `TypeElement` | Java |KSP equivalent | ------------------------ | `getEnclosedElements` |`ksClassDeclaration.declarations` | | `getEnclosingElement` |`ksClassDeclaration.parentDeclaration` | | `getInterfaces` | ```KOTLIN // Resolves each supertype to check whether it's an interface ksClassDeclaration.superTypes .map { it.resolve() } .filter { (it.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE } ``` | | `getNestingKind` |Check `KSClassDeclaration.parentDeclaration` and `inner` modifier | | `getQualifiedName` |`ksClassDeclaration.qualifiedName` | | `getSimpleName` |`ksClassDeclaration.simpleName` | | `getSuperclass` | ```KOTLIN // Resolves each supertype to find the superclass ksClassDeclaration.superTypes .map { it.resolve() } .firstOrNull { (it.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS } ``` | | `getTypeParameters` |`ksClassDeclaration.typeParameters` | ### `TypeParameterElement` | Java |KSP equivalent | ------------------------ | `getBounds` |`ksTypeParameter.bounds` | | `getEnclosingElement` |`ksTypeParameter.parentDeclaration` | | `getGenericElement` |`ksTypeParameter.parentDeclaration` | ### `VariableElement` | Java |KSP equivalent | ------------------------ | `getConstantValue` |Not supported. KSP doesn't provide access to property initializers. | | `getEnclosingElement` |`ksValueParameter.parentDeclaration` | | `getSimpleName` |`ksValueParameter.simpleName` | ### `ArrayType` | Java |KSP equivalent | ------------------------ | `getComponentType` |`ksType.arguments.first().type?.resolve()` for `Array`. For primitive arrays, `ksType.arguments` is empty, so use the matching type from `KSBuiltIns`. | ### `DeclaredType` | Java |KSP equivalent | ------------------------ | `asElement` |`ksType.declaration` | | `getEnclosingType` |`ksType.declaration.parentDeclaration` | | `getTypeArguments` |`ksType.arguments` | ### `ExecutableType` Note: A `KSType` for a function is just a signature represented by the `FunctionN` family. | Java |KSP equivalent | ------------------------ | `getParameterTypes` |`ksType.declaration.typeParameters`, `ksFunctionDeclaration.parameters.map { it.type }` | | `getReceiverType` |`ksFunctionDeclaration.parentDeclaration.asType(...)` | | `getReturnType` |`ksType.declaration.typeParameters.last()` | | `getThrownTypes` |`resolver.getJvmCheckedException(ksFunctionDeclaration)` | | `getTypeVariables` |`ksFunctionDeclaration.typeParameters` | ### `IntersectionType` | Java |KSP equivalent | ------------------------ | `getBounds` |`ksTypeParameter.bounds` | ### `TypeMirror` | Java |KSP equivalent | ------------------------ | `getKind` |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` | ```KOTLIN if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null ``` | | `getSuperBound` | ```KOTLIN 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](https://docs.oracle.com/javase/specs/jls/se13/html/jls-13.html#jls-13.1) | | `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()`. | | `getPackageOf` |`ksDeclaration.packageName` | | `getTypeElement` |`Resolver.getClassDeclarationByName()` | | `hides` |Not supported | | `isDeprecated` | ```KOTLIN ksDeclaration.annotations.any { it.annotationType.resolve().declaration.qualifiedName?.asString() == Deprecated::class.qualifiedName } ``` | | `overrides` |`resolver.overrides()` | | `printElements` |KSP has a basic `toString()` implementation on most classes | ### `Types` | Java |KSP equivalent | ------------------------ | `asElement` |`ksType.declaration` | | `asMemberOf` |`ksPropertyDeclaration.asMemberOf()` / `ksFunctionDeclaration.asMemberOf()` | | `boxedClass` |Not needed | | `capture` |Not supported | | `contains` |`KSType.isAssignableFrom()` | | `directSupertypes` |`(ksType.declaration as KSClassDeclaration).superTypes` | | `erasure` |`ksType.starProjection()` | | `getArrayType` |`ksBuiltIns.arrayType.replace(...)` | | `getDeclaredType` |`ksClassDeclaration.asType(...)` | | `getNoType` |`ksBuiltIns.unitType` / `null` | | `getNullType` |Depending on the context, `KSType.makeNullable()` could be useful | | `getPrimitiveType` |Not needed, check for `KSBuiltIns` | | `getWildcardType` |Use `Variance` in places expecting `KSTypeArgument` | | `isAssignable` |`ksType.isAssignableFrom()` | | `isSameType` |`ksType.equals()` | | `isSubsignature` |`functionTypeA == functionTypeB` / `functionTypeA == functionTypeB.starProjection()` | | `isSubtype` |`ksType.isAssignableFrom()` | | `unboxedType` |Not needed | ## See also * [How KSP models Kotlin code](ksp-kotlin-model.html) * [Migrate from kapt to KSP](ksp-kapt-migration.html) * [An example project with a Java annotation processor written alongside its KSP counterpart](https://github.com/google/ksp/tree/main/examples/playground/processors/src/main/kotlin)