|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.platform.mixin.handlers.mixinextras |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.mixin.handlers.InjectorAnnotationHandler |
| 24 | +import com.demonwav.mcdev.platform.mixin.handlers.MixinAnnotationHandler |
| 25 | +import com.demonwav.mcdev.platform.mixin.util.MethodTargetMember |
| 26 | +import com.demonwav.mcdev.platform.mixin.util.mixinTargets |
| 27 | +import com.demonwav.mcdev.util.constantStringValue |
| 28 | +import com.demonwav.mcdev.util.findContainingClass |
| 29 | +import com.demonwav.mcdev.util.findContainingMethod |
| 30 | +import com.demonwav.mcdev.util.internalName |
| 31 | +import com.demonwav.mcdev.util.mapFirstNotNull |
| 32 | +import com.intellij.openapi.components.Service |
| 33 | +import com.intellij.openapi.project.Project |
| 34 | +import com.intellij.psi.PsiAnnotation |
| 35 | +import com.intellij.psi.PsiClass |
| 36 | +import com.intellij.psi.impl.java.stubs.index.JavaAnnotationIndex |
| 37 | +import com.intellij.psi.search.GlobalSearchScope |
| 38 | +import com.intellij.psi.search.LocalSearchScope |
| 39 | +import com.intellij.psi.util.CachedValueProvider |
| 40 | +import com.intellij.psi.util.CachedValuesManager |
| 41 | +import com.intellij.psi.util.PsiModificationTracker |
| 42 | +import com.intellij.psi.util.PsiTreeUtil |
| 43 | + |
| 44 | +@Service(Service.Level.PROJECT) |
| 45 | +class ShareUtil(private val project: Project) { |
| 46 | + fun getShares(shareAnnotation: PsiAnnotation): List<PsiAnnotation> { |
| 47 | + val shareClass = shareAnnotation.resolveAnnotationType() ?: return listOf(shareAnnotation) |
| 48 | + val allShares = getAllShares(shareClass) |
| 49 | + return shareAnnotation.getShareKeys(project).flatMap { allShares[it] ?: listOf(shareAnnotation) }.distinct() |
| 50 | + } |
| 51 | + |
| 52 | + private fun getAllShares(shareClass: PsiClass): Map<ShareKey, List<PsiAnnotation>> { |
| 53 | + return CachedValuesManager.getManager(project).getCachedValue(shareClass) { |
| 54 | + CachedValueProvider.Result.create(computeAllShares(shareClass), PsiModificationTracker.MODIFICATION_COUNT) |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private fun computeAllShares(shareClass: PsiClass): Map<ShareKey, List<PsiAnnotation>> { |
| 59 | + val result = hashMapOf<ShareKey, MutableList<PsiAnnotation>>() |
| 60 | + for (annotation in getUsages(shareClass)) { |
| 61 | + for (key in annotation.getShareKeys(project)) { |
| 62 | + result.getOrPut(key) { mutableListOf() } += annotation |
| 63 | + } |
| 64 | + } |
| 65 | + return result |
| 66 | + } |
| 67 | + |
| 68 | + private fun getUsages(shareClass: PsiClass): List<PsiAnnotation> { |
| 69 | + return when (val useScope = shareClass.useScope) { |
| 70 | + is GlobalSearchScope -> JavaAnnotationIndex.getInstance().getAnnotations("Share", project, useScope).filter { annotation -> |
| 71 | + annotation.nameReferenceElement?.isReferenceTo(shareClass) == true |
| 72 | + } |
| 73 | + is LocalSearchScope -> { |
| 74 | + useScope.scope.flatMap { element -> |
| 75 | + PsiTreeUtil.collectElementsOfType(element, PsiAnnotation::class.java).filter { annotation -> |
| 76 | + annotation.nameReferenceElement?.isReferenceTo(shareClass) == true |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + else -> throw IllegalStateException("Unknown scope class ${useScope.javaClass.name}") |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private data class ShareKey( |
| 85 | + private val namespace: String, |
| 86 | + private val id: String, |
| 87 | + private val targetOwner: String, |
| 88 | + private val targetName: String, |
| 89 | + private val targetDesc: String, |
| 90 | + ) |
| 91 | + |
| 92 | + companion object { |
| 93 | + fun getInstance(project: Project): ShareUtil = project.getService(ShareUtil::class.java) |
| 94 | + |
| 95 | + private val PsiAnnotation.namespace: String? |
| 96 | + get() = findDeclaredAttributeValue("namespace")?.constantStringValue |
| 97 | + ?: findContainingClass()?.internalName |
| 98 | + private val PsiAnnotation.value: String? |
| 99 | + get() = findDeclaredAttributeValue("value")?.constantStringValue |
| 100 | + |
| 101 | + private fun PsiAnnotation.getShareKeys(project: Project): List<ShareKey> { |
| 102 | + val method = findContainingMethod() ?: return emptyList() |
| 103 | + val (injectorAnnotation, injector) = method.annotations.mapFirstNotNull { annotation -> |
| 104 | + (MixinAnnotationHandler.forMixinAnnotation(annotation, project) as? InjectorAnnotationHandler)?.let { annotation to it } |
| 105 | + } ?: return emptyList() |
| 106 | + |
| 107 | + val mixinTargets = method.findContainingClass()?.mixinTargets ?: return emptyList() |
| 108 | + val namespace = this.namespace ?: return emptyList() |
| 109 | + val id = this.value ?: return emptyList() |
| 110 | + |
| 111 | + return mixinTargets.flatMap { targetClass -> |
| 112 | + injector.resolveTarget(injectorAnnotation, targetClass).mapNotNull { target -> |
| 113 | + if (target !is MethodTargetMember) { |
| 114 | + return@mapNotNull null |
| 115 | + } |
| 116 | + |
| 117 | + ShareKey( |
| 118 | + namespace, |
| 119 | + id, |
| 120 | + target.classAndMethod.clazz.name, |
| 121 | + target.classAndMethod.method.name, |
| 122 | + target.classAndMethod.method.desc, |
| 123 | + ) |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | +} |
0 commit comments