From e61a58e1f32087cde6a085ee38c53c00517a7fa3 Mon Sep 17 00:00:00 2001 From: Su5eD Date: Wed, 24 Jul 2024 15:15:22 +0200 Subject: [PATCH] Make findMethodByUniqueName return value optional --- .../sinytra/adapter/patch/analysis/MethodCallAnalyzer.java | 6 +++--- .../patch/transformer/dynfix/DynFixMethodComparison.java | 4 ++-- .../adapter/patch/transformer/dynfix/DynFixSplitMethod.java | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/definition/src/main/java/org/sinytra/adapter/patch/analysis/MethodCallAnalyzer.java b/definition/src/main/java/org/sinytra/adapter/patch/analysis/MethodCallAnalyzer.java index 02776da..efd05ae 100644 --- a/definition/src/main/java/org/sinytra/adapter/patch/analysis/MethodCallAnalyzer.java +++ b/definition/src/main/java/org/sinytra/adapter/patch/analysis/MethodCallAnalyzer.java @@ -42,12 +42,12 @@ public static List findLambdasInMethod(ClassNode cls, MethodNode method, return list; } - public static MethodNode findMethodByUniqueName(ClassNode cls, String name) { + public static Optional findMethodByUniqueName(ClassNode cls, String name) { List methods = cls.methods.stream().filter(m -> m.name.equals(name)).toList(); if (methods.size() != 1) { - throw new IllegalStateException("Multiple candidates found for method " + name + " in class " + cls.name); + return Optional.empty(); } - return methods.getFirst(); + return Optional.of(methods.getFirst()); } public static Multimap getMethodCalls(MethodNode node, List callOrder) { diff --git a/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixMethodComparison.java b/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixMethodComparison.java index c5299e9..9bed36e 100644 --- a/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixMethodComparison.java +++ b/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixMethodComparison.java @@ -205,8 +205,8 @@ private static Patch.Result handleTargetModification(List for (List insns : hunkLabels) { for (AbstractInsnNode insn : insns) { if (insn instanceof MethodInsnNode minsn && minsn.owner.equals(dirtyTarget.name)) { - MethodNode method = MethodCallAnalyzer.findMethodByUniqueName(dirtyTarget, minsn.name); - if (!methodContext.findInjectionTargetInsns(new MethodContext.TargetPair(dirtyTarget, method)).isEmpty()) { + MethodNode method = MethodCallAnalyzer.findMethodByUniqueName(dirtyTarget, minsn.name).orElse(null); + if (method != null && !methodContext.findInjectionTargetInsns(new MethodContext.TargetPair(dirtyTarget, method)).isEmpty()) { return BundledMethodTransform.builder().modifyTarget(minsn.name + minsn.desc).apply(methodContext); } } diff --git a/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixSplitMethod.java b/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixSplitMethod.java index 1651b98..7ab5383 100644 --- a/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixSplitMethod.java +++ b/definition/src/main/java/org/sinytra/adapter/patch/transformer/dynfix/DynFixSplitMethod.java @@ -69,7 +69,7 @@ public Patch.Result apply(ClassNode classNode, MethodNode methodNode, MethodCont if (candidates.isEmpty()) { List nestedLambdas = invocations.stream() .flatMap(m -> MethodCallAnalyzer.findLambdasInMethod(dirtyTargetClass, m, null).stream()) - .map(s -> MethodCallAnalyzer.findMethodByUniqueName(dirtyTargetClass, s)) + .flatMap(s -> MethodCallAnalyzer.findMethodByUniqueName(dirtyTargetClass, s).stream()) .toList(); candidates = findInsnsCalls(nestedLambdas, methodContext); }