From 0a5901cde466d181aa5db550d6a145fab5e1c17b Mon Sep 17 00:00:00 2001 From: Graham Date: Sat, 19 Oct 2019 17:14:12 +0100 Subject: [PATCH] Add shorthand MemberRef constructors --- .../main/java/dev/openrs2/asm/MemberRef.java | 22 +++++++++++++++++++ .../transform/BufferSizeTransformer.java | 2 +- .../deob/transform/BitwiseOpTransformer.java | 6 ++--- .../deob/transform/CounterTransformer.java | 10 ++++----- .../deob/transform/DummyArgTransformer.java | 6 ++--- .../transform/OpaquePredicateTransformer.java | 4 ++-- .../deob/transform/ResetTransformer.java | 6 ++--- .../deob/transform/UnusedArgTransformer.java | 8 +++---- 8 files changed, 43 insertions(+), 21 deletions(-) diff --git a/asm/src/main/java/dev/openrs2/asm/MemberRef.java b/asm/src/main/java/dev/openrs2/asm/MemberRef.java index d5005f5bd0..611e6d0fdb 100644 --- a/asm/src/main/java/dev/openrs2/asm/MemberRef.java +++ b/asm/src/main/java/dev/openrs2/asm/MemberRef.java @@ -2,9 +2,31 @@ package dev.openrs2.asm; import java.util.Objects; +import org.objectweb.asm.tree.ClassNode; +import org.objectweb.asm.tree.FieldInsnNode; +import org.objectweb.asm.tree.FieldNode; +import org.objectweb.asm.tree.MethodInsnNode; +import org.objectweb.asm.tree.MethodNode; + public final class MemberRef { private final String owner, name, desc; + public MemberRef(ClassNode clazz, FieldNode field) { + this(clazz.name, field.name, field.desc); + } + + public MemberRef(ClassNode clazz, MethodNode method) { + this(clazz.name, method.name, method.desc); + } + + public MemberRef(FieldInsnNode fieldInsn) { + this(fieldInsn.owner, fieldInsn.name, fieldInsn.desc); + } + + public MemberRef(MethodInsnNode methodInsn) { + this(methodInsn.owner, methodInsn.name, methodInsn.desc); + } + public MemberRef(String owner, MemberDesc desc) { this(owner, desc.getName(), desc.getDesc()); } diff --git a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java index 65b415000e..f4f6ac97bc 100644 --- a/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java +++ b/bundler/src/main/java/dev/openrs2/bundler/transform/BufferSizeTransformer.java @@ -30,7 +30,7 @@ public final class BufferSizeTransformer extends Transformer { }) .map(match -> { var getstatic = (FieldInsnNode) match.get(2); - return new MemberRef(getstatic.owner, getstatic.name, getstatic.desc); + return new MemberRef(getstatic); }) .findAny(); } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/BitwiseOpTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/BitwiseOpTransformer.java index 3e76b1c1fa..0bb707820d 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/BitwiseOpTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/BitwiseOpTransformer.java @@ -57,7 +57,7 @@ public final class BitwiseOpTransformer extends Transformer { return; } - var methodRef = new MemberRef(clazz.name, method.name, method.desc); + var methodRef = new MemberRef(clazz, method); methodOps.put(methodRef, match.get(2).getOpcode()); }); } @@ -67,7 +67,7 @@ public final class BitwiseOpTransformer extends Transformer { @Override protected boolean transformClass(ClassPath classPath, Library library, ClassNode clazz) { - clazz.methods.removeIf(m -> methodOps.containsKey(new MemberRef(clazz.name, m.name, m.desc))); + clazz.methods.removeIf(m -> methodOps.containsKey(new MemberRef(clazz, m))); return false; } @@ -80,7 +80,7 @@ public final class BitwiseOpTransformer extends Transformer { } var invokestatic = (MethodInsnNode) insn; - var methodRef = new MemberRef(invokestatic.owner, invokestatic.name, invokestatic.desc); + var methodRef = new MemberRef(invokestatic); var opcode = methodOps.get(methodRef); if (opcode != null) { it.set(new InsnNode(opcode)); diff --git a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java index e6905b96ca..c5fe08663b 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/CounterTransformer.java @@ -55,19 +55,19 @@ public final class CounterTransformer extends Transformer { } var fieldInsn = (FieldInsnNode) insn; - references.merge(new MemberRef(fieldInsn.owner, fieldInsn.name, fieldInsn.desc), 1, Integer::sum); + references.merge(new MemberRef(fieldInsn), 1, Integer::sum); } RESET_PATTERN.match(method).forEach(match -> { var putstatic = (FieldInsnNode) match.get(1); - resets.merge(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc), 1, Integer::sum); + resets.merge(new MemberRef(putstatic), 1, Integer::sum); }); INCREMENT_PATTERN.match(method).forEach(match -> { var getstatic = (FieldInsnNode) match.get(0); var putstatic = (FieldInsnNode) match.get(3); if (getstatic.owner.equals(putstatic.owner) && getstatic.name.equals(putstatic.name) && getstatic.desc.equals(putstatic.desc)) { - increments.merge(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc), 1, Integer::sum); + increments.merge(new MemberRef(putstatic), 1, Integer::sum); } }); } @@ -99,7 +99,7 @@ public final class CounterTransformer extends Transformer { public boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { RESET_PATTERN.match(method).forEach(match -> { var putstatic = (FieldInsnNode) match.get(1); - if (counters.contains(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc))) { + if (counters.contains(new MemberRef(putstatic))) { match.forEach(method.instructions::remove); } }); @@ -109,7 +109,7 @@ public final class CounterTransformer extends Transformer { var putstatic = (FieldInsnNode) match.get(3); if (getstatic.owner.equals(putstatic.owner) && getstatic.name.equals(putstatic.name) && getstatic.desc.equals(putstatic.desc) && - counters.contains(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc))) { + counters.contains(new MemberRef(putstatic))) { match.forEach(method.instructions::remove); } }); diff --git a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java index 7f6b095fb6..a5f10677c3 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/DummyArgTransformer.java @@ -241,7 +241,7 @@ public final class DummyArgTransformer extends Transformer { @Override protected boolean transformCode(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) throws AnalyzerException { - var parentMethod = inheritedMethodSets.get(new MemberRef(clazz.name, method.name, method.desc)); + var parentMethod = inheritedMethodSets.get(new MemberRef(clazz, method)); var stores = new boolean[method.maxLocals]; @@ -301,7 +301,7 @@ public final class DummyArgTransformer extends Transformer { } } - var callee = inheritedMethodSets.get(new MemberRef(invoke.owner, invoke.name, invoke.desc)); + var callee = inheritedMethodSets.get(new MemberRef(invoke)); if (callee == null) { return; } @@ -334,7 +334,7 @@ public final class DummyArgTransformer extends Transformer { case Opcodes.INVOKESTATIC: case Opcodes.INVOKEINTERFACE: var invoke = (MethodInsnNode) insn; - var invokedMethod = inheritedMethodSets.get(new MemberRef(invoke.owner, invoke.name, invoke.desc)); + var invokedMethod = inheritedMethodSets.get(new MemberRef(invoke)); if (invokedMethod == null) { continue; } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java index 0ad2fe9eb9..3d2a740752 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/OpaquePredicateTransformer.java @@ -52,7 +52,7 @@ public final class OpaquePredicateTransformer extends Transformer { FLOW_OBSTRUCTOR_INITIALIZER_MATCHER.match(method).forEach(match -> { /* add flow obstructor to set */ var putstatic = (FieldInsnNode) match.get(match.size() - 1); - flowObstructors.add(new MemberRef(putstatic.owner, putstatic.name, putstatic.desc)); + flowObstructors.add(new MemberRef(putstatic)); /* remove initializer */ match.forEach(method.instructions::remove); @@ -64,7 +64,7 @@ public final class OpaquePredicateTransformer extends Transformer { } private boolean isFlowObstructor(FieldInsnNode insn) { - return flowObstructors.contains(new MemberRef(insn.owner, insn.name, insn.desc)); + return flowObstructors.contains(new MemberRef(insn)); } private boolean isOpaquePredicate(MethodNode method, List match) { diff --git a/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java index 0fbe0c8f1f..bd93807e0e 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/ResetTransformer.java @@ -59,7 +59,7 @@ public final class ResetTransformer extends Transformer { continue; } - return new MemberRef(invoke.owner, invoke.name, invoke.desc); + return new MemberRef(invoke); } return null; @@ -74,7 +74,7 @@ public final class ResetTransformer extends Transformer { } var invoke = (MethodInsnNode) insn; - resetMethods.add(new MemberRef(invoke.owner, invoke.name, invoke.desc)); + resetMethods.add(new MemberRef(invoke)); } } @@ -117,7 +117,7 @@ public final class ResetTransformer extends Transformer { @Override protected boolean transformClass(ClassPath classPath, Library library, ClassNode clazz) { - clazz.methods.removeIf(m -> resetMethods.contains(new MemberRef(clazz.name, m.name, m.desc))); + clazz.methods.removeIf(m -> resetMethods.contains(new MemberRef(clazz, m))); return false; } diff --git a/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.java b/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.java index b7d37b4ed0..b374bfc64e 100644 --- a/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.java +++ b/deob/src/main/java/dev/openrs2/deob/transform/UnusedArgTransformer.java @@ -75,7 +75,7 @@ public final class UnusedArgTransformer extends Transformer { } private void populateRetainedArgs(ClassPath classPath, ClassNode clazz, MethodNode method) throws AnalyzerException { - var partition = inheritedMethodSets.get(new MemberRef(clazz.name, method.name, method.desc)); + var partition = inheritedMethodSets.get(new MemberRef(clazz, method)); var localToArgMap = createLocalToArgMap(method); var analyzer = new Analyzer<>(new ConstSourceInterpreter()); @@ -103,7 +103,7 @@ public final class UnusedArgTransformer extends Transformer { case Opcodes.INVOKESTATIC: case Opcodes.INVOKEINTERFACE: var invoke = (MethodInsnNode) insn; - var invokePartition = inheritedMethodSets.get(new MemberRef(invoke.owner, invoke.name, invoke.desc)); + var invokePartition = inheritedMethodSets.get(new MemberRef(invoke)); if (invokePartition == null || TypedRemapper.isMethodImmutable(classPath, invokePartition)) { continue; } @@ -142,7 +142,7 @@ public final class UnusedArgTransformer extends Transformer { } var methodInsn = (MethodInsnNode) insn; - var partition = inheritedMethodSets.get(new MemberRef(methodInsn.owner, methodInsn.name, methodInsn.desc)); + var partition = inheritedMethodSets.get(new MemberRef(methodInsn)); if (partition == null || TypedRemapper.isMethodImmutable(classPath, partition)) { continue; } @@ -174,7 +174,7 @@ public final class UnusedArgTransformer extends Transformer { @Override protected boolean postTransformMethod(ClassPath classPath, Library library, ClassNode clazz, MethodNode method) { /* delete unused int args from the method itself */ - var partition = inheritedMethodSets.get(new MemberRef(clazz.name, method.name, method.desc)); + var partition = inheritedMethodSets.get(new MemberRef(clazz, method)); if (TypedRemapper.isMethodImmutable(classPath, partition)) { return false; }