Skip to content

Commit e3f9763

Browse files
committed
Add inspection for parameter name mismatch to target class in unobf versions
1 parent bde4418 commit e3f9763

File tree

8 files changed

+313
-35
lines changed

8 files changed

+313
-35
lines changed

src/main/kotlin/platform/mixin/handlers/InjectorAnnotationHandler.kt

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,24 +201,24 @@ abstract class InjectorAnnotationHandler : MixinAnnotationHandler {
201201
val localVariables = targetMethod.localVariables?.sortedBy { it.index }
202202
return targetMethod.getGenericParameterTypes(clazz, project).asSequence().withIndex()
203203
.map { (index, type) ->
204-
val name = localVariables
204+
val knownName = localVariables
205205
?.getOrNull(index + numLocalsToDrop)
206206
?.name
207207
?.toJavaIdentifier()
208-
?: "par${index + 1}"
209-
type to name
208+
val name = knownName ?: "par${index + 1}"
209+
sanitizedParameter(type, name, knownName != null)
210210
}
211-
.map { (type, name) -> sanitizedParameter(type, name) }
212211
.toList()
213212
}
214213

215214
@JvmStatic
216-
protected fun sanitizedParameter(type: PsiType, name: String?): Parameter {
215+
@JvmOverloads
216+
protected fun sanitizedParameter(type: PsiType, name: String?, knownName: Boolean = false): Parameter {
217217
// Parameters should not use ellipsis because others like CallbackInfo may follow
218218
return if (type is PsiEllipsisType) {
219-
Parameter(name?.toJavaIdentifier(), type.toArrayType())
219+
Parameter(name?.toJavaIdentifier(), type.toArrayType(), knownName)
220220
} else {
221-
Parameter(name?.toJavaIdentifier(), type)
221+
Parameter(name?.toJavaIdentifier(), type, knownName)
222222
}
223223
}
224224
}

src/main/kotlin/platform/mixin/handlers/ModifyVariableHandler.kt

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import com.demonwav.mcdev.platform.mixin.inspection.injector.MethodSignature
2727
import com.demonwav.mcdev.platform.mixin.inspection.injector.ParameterGroup
2828
import com.demonwav.mcdev.platform.mixin.util.LocalInfo
2929
import com.demonwav.mcdev.platform.mixin.util.toPsiType
30+
import com.demonwav.mcdev.util.Parameter
3031
import com.demonwav.mcdev.util.constantStringValue
3132
import com.demonwav.mcdev.util.findContainingMethod
3233
import com.demonwav.mcdev.util.findModule
@@ -61,31 +62,28 @@ class ModifyVariableHandler : InjectorAnnotationHandler() {
6162
val localType = method.parameterList.getParameter(0)?.type
6263
val info = LocalInfo.fromAnnotation(localType, annotation)
6364

64-
val possibleTypes = mutableSetOf<String>()
65+
val elementFactory = JavaPsiFacade.getElementFactory(annotation.project)
66+
val seenParams = mutableSetOf<String>()
67+
val result = mutableListOf<MethodSignature>()
6568
for (insn in targets) {
6669
val matchedLocals = info.matchLocals(
6770
module, targetClass, targetMethod, insn.insn,
6871
CollectVisitor.Mode.COMPLETION, matchType = false
6972
) ?: continue
7073
for (local in matchedLocals) {
71-
possibleTypes += local.desc!!
74+
if (seenParams.add(local.desc + local.name)) {
75+
val localType = Type.getType(local.desc).toPsiType(elementFactory)
76+
result += MethodSignature(
77+
listOf(
78+
ParameterGroup(listOf(sanitizedParameter(localType, local.name, local.isNamed))),
79+
targetParamsGroup,
80+
),
81+
localType,
82+
)
83+
}
7284
}
7385
}
7486

75-
val result = mutableListOf<MethodSignature>()
76-
77-
val elementFactory = JavaPsiFacade.getElementFactory(annotation.project)
78-
for (type in possibleTypes) {
79-
val psiType = Type.getType(type).toPsiType(elementFactory)
80-
result += MethodSignature(
81-
listOf(
82-
ParameterGroup(listOf(sanitizedParameter(psiType, "value"))),
83-
targetParamsGroup,
84-
),
85-
psiType,
86-
)
87-
}
88-
8987
return result
9088
}
9189

src/main/kotlin/platform/mixin/handlers/RedirectInjectorHandler.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,13 @@ class RedirectInjectorHandler : InjectorAnnotationHandler() {
255255
.mapTo(parameters) { (index, type) ->
256256
val i = if (firstMatch.opcode == Opcodes.INVOKESTATIC) index else index + 1
257257
val name = sourceClassAndMethod.method.localVariables?.getOrNull(i)?.name?.toJavaIdentifier()
258-
sanitizedParameter(type, name)
258+
sanitizedParameter(type, name, name != null)
259259
}
260260
} else {
261-
Type.getArgumentTypes(firstMatch.desc).mapTo(parameters) {
262-
sanitizedParameter(it.toPsiType(elementFactory), null)
261+
Type.getArgumentTypes(firstMatch.desc).withIndex().mapTo(parameters) { (index, type) ->
262+
val i = if (firstMatch.opcode == Opcodes.INVOKESTATIC) index else index + 1
263+
val name = sourceClassAndMethod?.method?.localVariables?.getOrNull(i)?.name?.toJavaIdentifier()
264+
sanitizedParameter(type.toPsiType(elementFactory), name, name != null)
263265
}
264266
}
265267

src/main/kotlin/platform/mixin/handlers/mixinextras/MixinExtrasInjectorAnnotationHandler.kt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,11 +319,13 @@ abstract class MixinExtrasInjectorAnnotationHandler : InjectorAnnotationHandler(
319319
genericParams.withIndex().mapTo(parameters) { (index, type) ->
320320
val i = if (insn.opcode == Opcodes.INVOKESTATIC) index else index + 1
321321
val name = sourceClassAndMethod.method.localVariables?.getOrNull(i)?.name?.toJavaIdentifier()
322-
sanitizedParameter(type, name)
322+
sanitizedParameter(type, name, name != null)
323323
}
324324
} else {
325-
Type.getArgumentTypes(insn.desc).mapTo(parameters) {
326-
sanitizedParameter(it.toPsiType(elementFactory), null)
325+
Type.getArgumentTypes(insn.desc).withIndex().mapTo(parameters) { (index, type) ->
326+
val i = if (insn.opcode == Opcodes.INVOKESTATIC) index else index + 1
327+
val name = sourceClassAndMethod?.method?.localVariables?.getOrNull(i)?.name?.toJavaIdentifier()
328+
sanitizedParameter(type.toPsiType(elementFactory), name, name != null)
327329
}
328330
}
329331
parameters

src/main/kotlin/platform/mixin/inspection/injector/InvalidInjectorMethodSignatureInspection.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,10 @@ class InvalidInjectorMethodSignatureInspection : MixinInspection() {
262262
}
263263
return isAssignable(methodReturnType, expectedReturnType)
264264
}
265+
}
265266

266-
private fun checkParameters(
267+
companion object {
268+
fun checkParameters(
267269
parameterList: PsiParameterList,
268270
expected: List<ParameterGroup>,
269271
allowCoerce: Boolean,
@@ -310,7 +312,7 @@ class InvalidInjectorMethodSignatureInspection : MixinInspection() {
310312
}
311313
}
312314

313-
private enum class CheckResult {
315+
enum class CheckResult {
314316
OK, WARNING, ERROR
315317
}
316318

0 commit comments

Comments
 (0)