メインコンテンツまでスキップ

Java annotation processing から KSP へのリファレンス

Program elements

JavaClosest facility in KSPNotes
AnnotationMirrorKSAnnotation
AnnotationValueKSValueArguments
ElementKSDeclaration / KSDeclarationContainer
ExecutableElementKSFunctionDeclaration
PackageElementKSFileKSPはパッケージをプログラム要素としてモデル化しません
ParameterizableKSDeclaration
QualifiedNameableKSDeclaration
TypeElementKSClassDeclaration
TypeParameterElementKSTypeParameter
VariableElementKSValueParameter / KSPropertyDeclaration

Types

KSPでは明示的な型解決が必要なため、Javaの一部の機能は、解決前のKSTypeおよび対応する要素でのみ実行できます。

JavaClosest facility in KSPNotes
ArrayTypeKSBuiltIns.arrayType
DeclaredTypeKSType / KSClassifierReference
ErrorTypeKSType.isError
ExecutableTypeKSType / KSCallableReference
IntersectionTypeKSType / KSTypeParameter
NoTypeKSType.isErrorKSPではN/A
NullTypeKSPではN/A
PrimitiveTypeKSBuiltInsJavaのプリミティブ型と完全に同じではありません
ReferenceTypeKSTypeReference
TypeMirrorKSType
TypeVariableKSTypeParameter
UnionTypeN/AKotlinのcatchブロックごとの型は1つだけです。UnionTypeは、Javaのアノテーションプロセッサでも観察できません
WildcardTypeKSType / KSTypeArgument

Misc

JavaClosest facility in KSPNotes
NameKSName
ElementKindClassKind / FunctionKind
ModifierModifier
NestingKindClassKind / FunctionKind
AnnotationValueVisitor
ElementVisitorKSVisitor
AnnotatedConstructKSAnnotated
TypeVisitor
TypeKindKSBuiltIns一部は組み込みで見つけることができます。それ以外の場合は、DeclaredTypeKSClassDeclarationを確認してください
ElementFilterCollection.filterIsInstance
ElementKindVisitorKSVisitor
ElementScannerKSTopDownVisitor
SimpleAnnotationValueVisitorKSPでは不要
SimpleElementVisitorKSVisitor
SimpleTypeVisitor
TypeKindVisitor
TypesResolver / utilsutilsの一部は、シンボルインターフェースにも統合されています
ElementsResolver / utils

Details

Javaのアノテーション処理APIの機能をKSPでどのように実行できるかをご覧ください。

AnnotationMirror

JavaKSP equivalent
getAnnotationTypeksAnnotation.annotationType
getElementValuesksAnnotation.arguments

AnnotationValue

JavaKSP equivalent
getValueksValueArgument.value

Element

JavaKSP equivalent
asTypeksClassDeclaration.asType(...)KSClassDeclaration に対してのみ使用可能です。型引数を指定する必要があります。
getAnnotation実装予定
getAnnotationMirrorsksDeclaration.annotations
getEnclosedElementsksDeclarationContainer.declarations
getEnclosingElementsksDeclaration.parentDeclaration
getKind型チェックを行い、ClassKind または FunctionKind に従ってキャストします
getModifiersksDeclaration.modifiers
getSimpleNameksDeclaration.simpleName

ExecutableElement

JavaKSP equivalent
getDefaultValue実装予定
getParametersksFunctionDeclaration.parameters
getReceiverTypeksFunctionDeclaration.parentDeclaration
getReturnTypeksFunctionDeclaration.returnType
getSimpleNameksFunctionDeclaration.simpleName
getThrownTypesKotlinでは不要
getTypeParametersksFunctionDeclaration.typeParameters
isDefault親の宣言がインターフェースであるかどうかを確認します
isVarArgsksFunctionDeclaration.parameters.any { it.isVarArg }

Parameterizable

JavaKSP equivalent
getTypeParametersksFunctionDeclaration.typeParameters

QualifiedNameable

JavaKSP equivalent
getQualifiedNameksDeclaration.qualifiedName

TypeElement

JavaKSP equivalent

getEnclosedElements

ksClassDeclaration.declarations

getEnclosingElement

ksClassDeclaration.parentDeclaration

getInterfaces

// Should be able to do without resolution
ksClassDeclaration.superTypes
.map { it.resolve() }
.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.INTERFACE }

getNestingKind

KSClassDeclaration.parentDeclarationinner 修飾子を確認してください

getQualifiedName

ksClassDeclaration.qualifiedName

getSimpleName

ksClassDeclaration.simpleName

getSuperclass

// Should be able to do without resolution
ksClassDeclaration.superTypes
.map { it.resolve() }
.filter { (it?.declaration as? KSClassDeclaration)?.classKind == ClassKind.CLASS }

getTypeParameters

ksClassDeclaration.typeParameters

TypeParameterElement

JavaKSP equivalent
getBoundsksTypeParameter.bounds
getEnclosingElementksTypeParameter.parentDeclaration
getGenericElementksTypeParameter.parentDeclaration

VariableElement

JavaKSP equivalent
getConstantValue実装予定
getEnclosingElementksValueParameter.parentDeclaration
getSimpleNameksValueParameter.simpleName

ArrayType

JavaKSP equivalent
getComponentTypeksType.arguments.first()

DeclaredType

JavaKSP equivalent
asElementksType.declaration
getEnclosingTypeksType.declaration.parentDeclaration
getTypeArgumentsksType.arguments

ExecutableType

注記

関数の KSType は、FunctionN<R, T1, T2, ..., TN> ファミリで表されるシグネチャにすぎません。

JavaKSP equivalent
getParameterTypesksType.declaration.typeParameters, ksFunctionDeclaration.parameters.map { it.type }
getReceiverTypeksFunctionDeclaration.parentDeclaration.asType(...)
getReturnTypeksType.declaration.typeParameters.last()
getThrownTypesKotlinでは不要
getTypeVariablesksFunctionDeclaration.typeParameters

IntersectionType

JavaKSP equivalent
getBoundsksTypeParameter.bounds

TypeMirror

JavaKSP equivalent
getKindプリミティブ型、Unit 型の場合は KSBuiltIns の型と比較し、それ以外の場合は宣言された型と比較します

TypeVariable

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

WildcardType

JavaKSP equivalent

getExtendsBound

if (ksTypeArgument.variance == Variance.COVARIANT) ksTypeArgument.type else null

getSuperBound

if (ksTypeArgument.variance == Variance.CONTRAVARIANT) ksTypeArgument.type else null

Elements

JavaKSP equivalent

getAllAnnotationMirrors

KSDeclarations.annotations

getAllMembers

getAllFunctionsgetAllProperties は実装予定

getBinaryName

決定予定、Java Specificationを参照してください

getConstantExpression

式ではなく定数値があります

getDocComment

実装予定

getElementValuesWithDefaults

実装予定

getName

resolver.getKSNameFromString

getPackageElement

パッケージはサポートされていませんが、パッケージ情報を取得できます。KSPではパッケージの操作はできません

getPackageOf

パッケージはサポートされていません

getTypeElement

Resolver.getClassDeclarationByName

hides

実装予定

isDeprecated

KsDeclaration.annotations.any { 
it.annotationType.resolve()!!.declaration.qualifiedName!!.asString() == Deprecated::class.qualifiedName
}

overrides

KSFunctionDeclaration.overrides / KSPropertyDeclaration.overrides (それぞれのクラスのメンバー関数)

printElements

KSPには、ほとんどのクラスに基本的なtoString()実装があります

Types

JavaKSP equivalent
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不要