Skip to content

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.isErrorKSPでは該当なし
NullTypeKSPでは該当なし
PrimitiveTypeKSBuiltInsJavaのプリミティブ型と全く同じではありません
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlinではcatchブロックごとに1つの型しかありません。UnionTypeはJavaのアノテーションプロセッサーでも観測できません。
WildcardTypeKSType / KSTypeArgument

その他

JavaKSPで最も近い機能備考
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns一部は組み込み型で見つかりますが、それ以外はDeclaredTypeについてKSClassDeclarationを確認してください
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSPでは不要です
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utils一部のutilsはシンボルインターフェースにも統合されています
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
getKindClassKind または FunctionKind に従った型チェックとキャスト
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同等
getEnclosedElementsksClassDeclaration.declarations
getEnclosingElementksClassDeclaration.parentDeclaration
getInterfaces
kotlin
// 解決なしで実行できるはずです
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
getNestingKindKSClassDeclaration.parentDeclarationinner 修飾子を確認
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclass
kotlin
// 解決なしで実行できるはずです
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS }
getTypeParametersksClassDeclaration.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同等
getKindプリミティブ型、Unit型については KSBuiltIns の型と比較し、それ以外の場合は宣言された型と比較します

TypeVariable

JavaKSP同等
asElementksType.declaration
getLowerBound検討中。キャプチャが提供され、明示的な境界チェックが必要な場合にのみ必要です。
getUpperBoundksTypeParameter.bounds

WildcardType

JavaKSP同等
getExtendsBound
kotlin
if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null
getSuperBound
kotlin
if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null

Elements

JavaKSP同等
getAllAnnotationMirrorsKSDeclarations.annotations
getAllMembersgetAllFunctionsgetAllProperties は実装予定です
getBinaryName検討中、Java Specificationを参照
getConstantExpression定数値はありますが、式ではありません
getDocComment実装予定
getElementValuesWithDefaults実装予定
getNameresolver.getKSNameFromString
getPackageElementパッケージはサポートされていませんが、パッケージ情報は取得可能です。KSPではパッケージに対する操作はできません
getPackageOfパッケージはサポートされていません
getTypeElementResolver.getClassDeclarationByName
hides実装予定
isDeprecated
kotlin
KsDeclaration.annotations.any { 
    it.annotationType.resolve()!!.declaration.qualifiedName!!.asString() == Deprecated::class.qualifiedName
}
overridesKSFunctionDeclaration.overrides / KSPropertyDeclaration.overrides (それぞれのクラスのメンバー関数)
printElementsKSPはほとんどのクラスで基本的な 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 を確認してください
getWildcardTypeKSTypeArgument を期待する箇所で Variance を使用
isAssignableksType.isAssignableFrom
isSameTypeksType.equals
isSubsignaturefunctionTypeA == functionTypeB / functionTypeA == functionTypeB.starProjection()
isSubtypeksType.isAssignableFrom
unboxedType不要です