Skip to content

Java アノテーションプロセッシングから KSP へのリファレンス

プログラム要素 (Program elements)

JavaKSP における最も近い機能備考
AnnotationMirrorKSAnnotation
AnnotationValueKSValueArguments
ElementKSDeclaration / KSDeclarationContainer
ExecutableElementKSFunctionDeclaration
PackageElementKSFileKSP はパッケージをプログラム要素としてモデル化しません
ParameterizableKSDeclaration
QualifiedNameableKSDeclaration
TypeElementKSClassDeclaration
TypeParameterElementKSTypeParameter
VariableElementKSValueParameter / KSPropertyDeclaration

型 (Types)

KSP では明示的な型解決(type resolution)が必要なため、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

その他 (Misc)

JavaKSP における最も近い機能備考
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns一部は builtins にあります。それ以外は DeclaredType について KSClassDeclaration を確認してください
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSP では不要です
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utilsutils の一部はシンボルインターフェースにも統合されています
ElementsResolver / utils

詳細 (Details)

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
// 解決(resolution)なしで実行できるはずです
ksClassDeclaration.superTypes
    .map { it.resolve() }
    .filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }
getNestingKindKSClassDeclaration.parentDeclarationinner 修飾子を確認します
getQualifiedNameksClassDeclaration.qualifiedName
getSimpleNameksClassDeclaration.simpleName
getSuperclass
kotlin
// 解決(resolution)なしで実行できるはずです
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 仕様を参照してください
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不要です