Skip to content

Commit db900da

Browse files
committed
feat: getProjectEndpoints
1 parent ec5f9b2 commit db900da

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/detect/JVMEndpointDetector.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import spp.jetbrains.marker.source.mark.guide.GuideMark
4545
*/
4646
class JVMEndpointDetector(project: Project) : EndpointDetector<JVMEndpointNameDetector>(project) {
4747

48-
override val detectorSet: Set<JVMEndpointNameDetector> = setOf(
48+
override val detectorSet = mutableSetOf(
4949
SkywalkingTraceEndpoint(),
5050
SpringMVCEndpoint(),
5151
MicronautEndpoint(),

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/detect/endpoint/MicronautEndpoint.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.psi.psiUtil.containingClassOrObject
3131
import org.jetbrains.kotlin.resolve.BindingContext
3232
import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode
3333
import spp.jetbrains.marker.jvm.detect.JVMEndpointDetector.JVMEndpointNameDetector
34+
import spp.jetbrains.marker.service.ArtifactNamingService
3435
import spp.jetbrains.marker.source.info.EndpointDetector.DetectedEndpoint
3536

3637
/**
@@ -48,7 +49,7 @@ class MicronautEndpoint : JVMEndpointNameDetector {
4849
val promise = Promise.promise<List<DetectedEndpoint>>()
4950
ApplicationManager.getApplication().runReadAction {
5051
val annotation = element.annotations.find {
51-
it.qualifiedName?.startsWith(endpointAnnotationPrefix) == true
52+
ArtifactNamingService.getFullyQualifiedName(it).identifier.startsWith(endpointAnnotationPrefix)
5253
}
5354
if (annotation != null) {
5455
val endpointType = annotation.qualifiedName!!.substringAfterLast(".").uppercase()
@@ -61,7 +62,7 @@ class MicronautEndpoint : JVMEndpointNameDetector {
6162

6263
//get controller
6364
val controllerAnnotation = element.containingClass?.annotations?.find {
64-
it.qualifiedName?.startsWith(endpointAnnotationPrefix) == true
65+
ArtifactNamingService.getFullyQualifiedName(it).identifier.startsWith(endpointAnnotationPrefix)
6566
}
6667
val controllerValue = controllerAnnotation?.let { getAttributeValue(it, "value") }
6768
val controller = if (controllerValue is PsiLiteral) {

marker/jvm-marker/src/main/kotlin/spp/jetbrains/marker/jvm/service/utils/JVMMarkerUtils.kt

+30
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ object JVMMarkerUtils {
5252
when {
5353
element.isKotlin() && element is KtClass -> return getFullyQualifiedName(element)
5454
element.isKotlin() && element is KtNamedFunction -> return getFullyQualifiedName(element)
55+
element is PsiAnnotation -> return getFullyQualifiedName(element)
5556
element is PsiClass -> return getFullyQualifiedName(element)
5657
element is PsiMethod -> return getFullyQualifiedName(element)
5758
else -> Unit
@@ -79,6 +80,35 @@ object JVMMarkerUtils {
7980
)
8081
}
8182

83+
private fun getFullyQualifiedName(annotation: PsiAnnotation): ArtifactQualifiedName {
84+
val qualifiedName = getFullyQualifiedName(annotation, annotation.qualifiedName.toString())
85+
return ArtifactQualifiedName(
86+
qualifiedName,
87+
type = ArtifactType.ANNOTATION,
88+
lineNumber = annotation.nameReferenceElement?.let { SourceMarkerUtils.getLineNumber(it) }
89+
)
90+
}
91+
92+
private fun getFullyQualifiedName(psiElement: PsiElement, simpleName: String): String {
93+
var parent = psiElement
94+
while (parent !is PsiJavaFile && parent.parent != null) {
95+
parent = parent.parent
96+
}
97+
val javaFile = parent as? PsiJavaFile ?: TODO() //todo: others
98+
99+
// Loop through imports to find fully qualified name
100+
for (importStatement in javaFile.importList?.importStatements ?: emptyArray()) {
101+
val qName = importStatement.qualifiedName
102+
if (qName?.endsWith(".$simpleName") == true || qName == simpleName) {
103+
return qName
104+
}
105+
}
106+
107+
// If the simple name wasn't found in the imports, it might be in the same package.
108+
val packageName = javaFile.packageStatement?.packageName
109+
return if (packageName != null) "$packageName.$simpleName" else simpleName
110+
}
111+
82112
private fun getFullyQualifiedName(clazz: PsiClass): ArtifactQualifiedName {
83113
return ArtifactQualifiedName(
84114
"${JvmClassUtil.getJvmClassName(clazz)}",

0 commit comments

Comments
 (0)