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 / utilsutilsの一部はシンボルインターフェースにも統合されています。
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

NOTE

関数の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不要