From 54e999efca62ae60cf4915cbf80b84fe4d02fcf8 Mon Sep 17 00:00:00 2001 From: zavakid Date: Fri, 3 May 2024 00:45:14 +0800 Subject: [PATCH] feat: detect annotation --- .../processor/AutoPipelineDescriptor.kt | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/auto-pipeline-processor/src/main/java/com/foldright/auto/pipeline/processor/AutoPipelineDescriptor.kt b/auto-pipeline-processor/src/main/java/com/foldright/auto/pipeline/processor/AutoPipelineDescriptor.kt index b7299fd..f547f77 100644 --- a/auto-pipeline-processor/src/main/java/com/foldright/auto/pipeline/processor/AutoPipelineDescriptor.kt +++ b/auto-pipeline-processor/src/main/java/com/foldright/auto/pipeline/processor/AutoPipelineDescriptor.kt @@ -1,10 +1,8 @@ package com.foldright.auto.pipeline.processor +import com.foldright.auto.pipeline.AutoPipeline import com.foldright.auto.pipeline.PipelineDirection -import com.squareup.javapoet.ClassName -import com.squareup.javapoet.ParameterizedTypeName -import com.squareup.javapoet.TypeName -import com.squareup.javapoet.TypeVariableName +import com.squareup.javapoet.* import javax.lang.model.element.ElementKind import javax.lang.model.element.ExecutableElement import javax.lang.model.element.Modifier.ABSTRACT @@ -24,6 +22,7 @@ class AutoPipelineClassDescriptor( private val entityPackage = elements.getPackageOf(entityElement).toString() private val entitySimpleName = entityElement.simpleName.toString() val entityType: TypeName = TypeName.get(entityElement.asType()) + val entityDeclaredTypeVariables: List by lazy { if (entityType is ParameterizedTypeName) { entityElement.typeParameters.map { TypeVariableName.get(it) } @@ -32,6 +31,18 @@ class AutoPipelineClassDescriptor( } } + private val entityAnnotations: List by lazy { + entityElement.annotationMirrors + .map { AnnotationSpec.get(it) } + .filterNot { + val tp = it.type + if (tp is ClassName) { + tp.canonicalName() == AutoPipeline::class.java.canonicalName + } else { + false + } + } + } // new package for all pipeline source code private val newPackageName = "${entityPackage}.pipeline" @@ -108,25 +119,28 @@ class AutoPipelineClassDescriptor( .map { AutoPipelineOperatorsDescriptor(it, entityElement) } } -class AutoPipelineOperatorsDescriptor(val executableElement: ExecutableElement, private val entityElement: TypeElement) { +class AutoPipelineOperatorsDescriptor( + val executableElement: ExecutableElement, + private val entityElement: TypeElement +) { val methodName = executableElement.simpleName.toString() val returnType: TypeMirror = executableElement.returnType val params: List = executableElement.parameters - private val defaultDirection : PipelineDirection.Direction by lazy { + private val defaultDirection: PipelineDirection.Direction by lazy { entityElement.getAnnotation(PipelineDirection::class.java)?.value ?: PipelineDirection.Direction.FORWARD } - val direction : PipelineDirection.Direction by lazy { + val direction: PipelineDirection.Direction by lazy { executableElement.getAnnotation(PipelineDirection::class.java)?.value ?: defaultDirection } companion object { - fun List.expand() : CharSequence = this.joinToString(",") { + fun List.expand(): CharSequence = this.joinToString(",") { it.simpleName } - fun List.expandAndAdd(additional: CharSequence) : CharSequence = when { + fun List.expandAndAdd(additional: CharSequence): CharSequence = when { this.isEmpty() -> additional else -> "${this.expand()} , $additional" }