in src/main/kotlin/com/netflix/dgs/plugin/hints/DgsDataSimplifyingInspector.kt [90:130]
override fun applyFix(project: Project, descriptor: ProblemDescriptor) {
val factory: PsiElementFactory = JavaPsiFacade.getInstance(project).elementFactory
val file = descriptor.psiElement.parentOfType<PsiFile>()
val fieldValue =
(descriptor.psiElement.toUElement() as UAnnotation).findAttributeValue("field")?.evaluateString()
val method = descriptor.psiElement.toUElement()?.getParentOfType<UMethod>()
val annotationFQN = "com.netflix.graphql.dgs.${newAnnotation.substringAfter("@")}"
val annotationFQNKotlin = ClassId.fromString(annotationFQN.replace(".", "/"))
if (file is PsiJavaFile) {
val importStatement =
factory.createImportStatement(factory.createTypeByFQClassName(annotationFQN).resolve()!!)
val importList = file.importList
if (importList?.importStatements?.any { it.qualifiedName == annotationFQN } == false) {
importList.add(importStatement)
}
val newAnnotation = if (fieldValue != null && fieldValue != method?.name) {
factory.createAnnotationFromText("${newAnnotation}(field = \"$fieldValue\")", null)
} else {
factory.createAnnotationFromText(newAnnotation, null)
}
method?.sourcePsi?.addBefore(newAnnotation, method.modifierList)
project.getService(DgsService::class.java).clearCache()
} else if (file is KtFile) {
if (fieldValue != null && fieldValue != method?.name) {
(method?.sourcePsi as KtFunction).addAnnotation(
annotationFQNKotlin,
"field = \"${fieldValue}\""
)
} else {
(method?.sourcePsi as KtFunction).addAnnotation(annotationFQNKotlin)
}
project.getService(DgsService::class.java).clearCache()
}
descriptor.psiElement.delete()
}