From 2d9f9af8af39880106072f5e5a4911114a74663c Mon Sep 17 00:00:00 2001 From: Jake Potrebic Date: Tue, 27 Aug 2024 22:26:14 -0700 Subject: [PATCH] further improve various builders for clarity --- .../matcher/method/MethodMatcherBuilder.java | 35 +++++----------- .../method/MethodMatcherBuilderImpl.java | 42 +------------------ .../method/MethodParamMatcherBuilder.java | 31 ++++++++++++++ .../method/MethodTypeMatcherBuilder.java | 18 ++++++++ .../TargetedMethodMatcherBuilder.java | 25 ++++++----- .../TargetedMethodMatcherBuilderImpl.java | 39 +++++++++-------- .../StaticMethodRewriteParamsTest.java | 8 ++-- .../StaticMethodRewriteReturnsTest.java | 4 +- 8 files changed, 101 insertions(+), 101 deletions(-) create mode 100644 src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodParamMatcherBuilder.java create mode 100644 src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodTypeMatcherBuilder.java diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilder.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilder.java index edaa332..3ec6b44 100644 --- a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilder.java +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilder.java @@ -1,45 +1,30 @@ package io.papermc.asm.rules.builder.matcher.method; import io.papermc.asm.util.Builder; -import java.lang.constant.ClassDesc; -import java.lang.constant.MethodTypeDesc; import java.util.Collection; import java.util.function.Consumer; -import java.util.function.Predicate; -public interface MethodMatcherBuilder extends Builder { +public interface MethodMatcherBuilder extends MethodParamMatcherBuilder, Builder { MethodMatcherBuilder ctor(final Consumer matchBuilderConsumer); - MethodMatcherBuilder match(final String name, final Consumer matchBuilderConsumer); + default MethodMatcherBuilder match(final String name) { + return this.match(name, b -> {}); + } - MethodMatcherBuilder match(final Collection names, final Consumer matchBuilderConsumer); + MethodMatcherBuilder match(final String name, final Consumer matchBuilderConsumer); - MethodMatcherBuilder hasParam(final ClassDesc paramClassDesc); + default MethodMatcherBuilder match(final Collection names) { + return this.match(names, b -> {}); + } - MethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc); + MethodMatcherBuilder match(final Collection names, final Consumer matchBuilderConsumer); /** * Used to match methods with specific names. * * @see MethodMatcherBuilder#match(String, Consumer) */ - interface MatchBuilder { - - MatchBuilder virtual(); - - MatchBuilder statik(); - - MatchBuilder type(final MethodType... types); - - MatchBuilder hasParam(final ClassDesc paramClassDesc); - - MatchBuilder hasReturn(final ClassDesc returnClassDesc); - - MatchBuilder desc(final String... descriptors); - - MatchBuilder desc(final MethodTypeDesc... descriptors); - - MatchBuilder desc(final Predicate descPredicate); + interface MatchBuilder extends MethodParamMatcherBuilder, MethodTypeMatcherBuilder { } } diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilderImpl.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilderImpl.java index c080d66..f8e7cc0 100644 --- a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilderImpl.java +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilderImpl.java @@ -1,7 +1,6 @@ package io.papermc.asm.rules.builder.matcher.method; import io.papermc.asm.rules.method.StaticRewrite; -import java.lang.constant.ClassDesc; import java.lang.constant.MethodTypeDesc; import java.util.Arrays; import java.util.Collection; @@ -43,16 +42,7 @@ public MethodMatcherBuilder match(final Collection names, final Consumer } @Override - public MethodMatcherBuilder hasParam(final ClassDesc paramClassDesc) { - return this.desc(d -> d.parameterList().contains(paramClassDesc)); - } - - @Override - public MethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc) { - return this.desc(d -> d.returnType().equals(returnClassDesc)); - } - - private MethodMatcherBuilder desc(final Predicate descPredicate) { + public MethodMatcherBuilder desc(final Predicate descPredicate) { this.matcher = this.matcher.and(descPredicate); return this; } @@ -73,42 +63,12 @@ private void apply() { }); } - @Override - public MatchBuilder virtual() { - return this.type(MethodType.VIRTUAL); - } - - @Override - public MatchBuilder statik() { - return this.type(MethodType.STATIC); - } - @Override public MatchBuilder type(final MethodType...types) { this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b)); return this; } - @Override - public MatchBuilder hasParam(final ClassDesc paramClassDesc) { - return this.desc(d -> d.parameterList().contains(paramClassDesc)); - } - - @Override - public MatchBuilder hasReturn(final ClassDesc returnClassDesc) { - return this.desc(d -> d.returnType().equals(returnClassDesc)); - } - - @Override - public MatchBuilder desc(final String...descriptors) { - return this.desc(desc -> Arrays.stream(descriptors).anyMatch(d -> desc.descriptorString().equals(d))); - } - - @Override - public MatchBuilder desc(final MethodTypeDesc...descriptors) { - return this.desc(desc -> Arrays.asList(descriptors).contains(desc)); - } - @Override public MatchBuilder desc(final Predicate descPredicate) { this.bytecodeDescPredicate = descPredicate; diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodParamMatcherBuilder.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodParamMatcherBuilder.java new file mode 100644 index 0000000..406d02e --- /dev/null +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodParamMatcherBuilder.java @@ -0,0 +1,31 @@ +package io.papermc.asm.rules.builder.matcher.method; + +import java.lang.constant.ClassDesc; +import java.lang.constant.MethodTypeDesc; +import java.util.Arrays; +import java.util.function.Predicate; + +public interface MethodParamMatcherBuilder { + + default B hasParam(final ClassDesc paramClassDesc) { + return this.desc(d -> d.parameterList().contains(paramClassDesc)); + } + + default B hasParam(final ClassDesc paramClassDesc, final int paramIdx) { + return this.desc(d -> d.parameterType(paramIdx).equals(paramClassDesc)); + } + + default B hasReturn(final ClassDesc returnClassDesc) { + return this.desc(d -> d.returnType().equals(returnClassDesc)); + } + + default B desc(final String... descriptors) { + return this.desc(desc -> Arrays.stream(descriptors).anyMatch(d -> desc.descriptorString().equals(d))); + } + + default B desc(final MethodTypeDesc... descriptors) { + return this.desc(desc -> Arrays.asList(descriptors).contains(desc)); + } + + B desc(final Predicate descPredicate); +} diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodTypeMatcherBuilder.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodTypeMatcherBuilder.java new file mode 100644 index 0000000..e6d0e55 --- /dev/null +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodTypeMatcherBuilder.java @@ -0,0 +1,18 @@ +package io.papermc.asm.rules.builder.matcher.method; + +public interface MethodTypeMatcherBuilder { + + default B virtual() { + return this.type(MethodType.VIRTUAL); + } + + default B statik() { + return this.type(MethodType.STATIC); + } + + default B itf() { + return this.type(MethodType.INTERFACE); + } + + B type(final MethodType... types); +} diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilder.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilder.java index caba1cc..b4b283a 100644 --- a/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilder.java +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilder.java @@ -1,29 +1,32 @@ package io.papermc.asm.rules.builder.matcher.method.targeted; -import io.papermc.asm.rules.builder.matcher.method.MethodType; +import io.papermc.asm.rules.builder.matcher.method.MethodParamMatcherBuilder; +import io.papermc.asm.rules.builder.matcher.method.MethodTypeMatcherBuilder; import io.papermc.asm.util.Builder; import java.lang.constant.ClassDesc; import java.util.Collection; import java.util.function.Consumer; -public interface TargetedMethodMatcherBuilder extends Builder { +public interface TargetedMethodMatcherBuilder extends MethodParamMatcherBuilder, Builder { TargetedMethodMatcherBuilder ctor(); - TargetedMethodMatcherBuilder match(final String name, final Consumer matchBuilderConsumer); - - TargetedMethodMatcherBuilder match(final Collection names, final Consumer matchBuilderConsumer); + default TargetedMethodMatcherBuilder match(final String name) { + return this.match(name, b -> {}); + } - TargetedMethodMatcherBuilder hasParam(final ClassDesc paramClassDesc); + TargetedMethodMatcherBuilder match(final String name, final Consumer matchBuilderConsumer); - TargetedMethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc); + default TargetedMethodMatcherBuilder match(final Collection names) { + return this.match(names, b -> {}); + } - interface MatchBuilder { + TargetedMethodMatcherBuilder match(final Collection names, final Consumer matchBuilderConsumer); - MatchBuilder virtual(); + TargetedMethodMatcherBuilder targetParam(final ClassDesc paramClassDesc); - MatchBuilder statik(); + TargetedMethodMatcherBuilder targetReturn(final ClassDesc returnClassDesc); - MatchBuilder type(final MethodType... types); + interface MatchBuilder extends MethodParamMatcherBuilder, MethodTypeMatcherBuilder { } } diff --git a/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilderImpl.java b/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilderImpl.java index bf8efdd..18e6a15 100644 --- a/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilderImpl.java +++ b/src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilderImpl.java @@ -17,7 +17,7 @@ class TargetedMethodMatcherBuilderImpl implements TargetedMethodMatcherBuilder { private MethodMatcher matcher = (opcode, isInvokeDynamic, name, descriptor) -> false; - private @MonotonicNonNull Predicate byDesc; + private Predicate byDesc = $ -> true; private @MonotonicNonNull ClassDesc oldType; TargetedMethodMatcherBuilderImpl() { @@ -43,28 +43,32 @@ public TargetedMethodMatcherBuilder match(final Collection names, final } @Override - public TargetedMethodMatcherBuilder hasParam(final ClassDesc classDesc) { + public TargetedMethodMatcherBuilder targetParam(final ClassDesc classDesc) { if (this.oldType != null) { throw new IllegalArgumentException("Targeted type was already set to " + this.oldType); } this.oldType = classDesc; - this.byDesc = d -> d.parameterList().contains(classDesc); - return this; + return this.hasParam(classDesc); } @Override - public TargetedMethodMatcherBuilder hasReturn(final ClassDesc classDesc) { + public TargetedMethodMatcherBuilder targetReturn(final ClassDesc classDesc) { if (this.oldType != null) { throw new IllegalArgumentException("Targeted type was already set to " + this.oldType); } this.oldType = classDesc; - this.byDesc = d -> d.returnType().equals(classDesc); + return this.hasReturn(classDesc); + } + + @Override + public TargetedMethodMatcherBuilder desc(final Predicate descPredicate) { + this.byDesc = this.byDesc.and(descPredicate); return this; } @Override public TargetedMethodMatcher build() { - if (this.oldType == null || this.byDesc == null) { + if (this.oldType == null) { throw new IllegalStateException("Targeted type was not set"); } final MethodMatcher finalMatcher = this.matcher.and(this.byDesc); @@ -74,7 +78,8 @@ public TargetedMethodMatcher build() { final class SpecificMatchBuilder implements TargetedMethodMatcherBuilder.MatchBuilder { private final Predicate namePredicate; - private @MonotonicNonNull OpcodePredicate opcodePredicate; + private Predicate bytecodeDescPredicate = $ -> true; + private OpcodePredicate opcodePredicate = ($, $$) -> true; private SpecificMatchBuilder(final Predicate namePredicate) { this.namePredicate = namePredicate; @@ -82,23 +87,21 @@ private SpecificMatchBuilder(final Predicate namePredicate) { private void apply() { TargetedMethodMatcherBuilderImpl.this.matcher = TargetedMethodMatcherBuilderImpl.this.matcher.or((o, isInvokeDynamic, n, d) -> { - return this.namePredicate.test(n) && this.opcodePredicate.matches(o, isInvokeDynamic); + return this.namePredicate.test(n) + && this.opcodePredicate.matches(o, isInvokeDynamic) + && this.bytecodeDescPredicate.test(MethodTypeDesc.ofDescriptor(d)); }); } @Override - public TargetedMethodMatcherBuilder.MatchBuilder virtual() { - return this.type(MethodType.VIRTUAL); - } - - @Override - public TargetedMethodMatcherBuilder.MatchBuilder statik() { - return this.type(MethodType.STATIC); + public TargetedMethodMatcherBuilder.MatchBuilder type(final MethodType... types) { + this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b)); + return this; } @Override - public TargetedMethodMatcherBuilder.MatchBuilder type(final MethodType... types) { - this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b)); + public MatchBuilder desc(final Predicate descPredicate) { + this.bytecodeDescPredicate = descPredicate; return this; } } diff --git a/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteParamsTest.java b/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteParamsTest.java index 255bb9a..66723f5 100644 --- a/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteParamsTest.java +++ b/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteParamsTest.java @@ -27,7 +27,7 @@ void testParamDirectStaticRewrite(final TransformerCheck check) throws NoSuchMet TargetedMethodMatcher.builder() .match("consumeLoc", b -> b.virtual()) .match("consumeLocStatic", b -> b.statik()) - .hasParam(LOCATION) + .targetParam(LOCATION) .build() ) ); @@ -35,7 +35,7 @@ void testParamDirectStaticRewrite(final TransformerCheck check) throws NoSuchMet builder.changeParamDirect( POSITION, handler, - TargetedMethodMatcher.builder().ctor().hasParam(LOCATION).build() + TargetedMethodMatcher.builder().ctor().targetParam(LOCATION).build() ); }); check.run(RewriteRule.chain(rule, ctorRule)); @@ -52,7 +52,7 @@ void testParamFuzzyStaticRewrite(final TransformerCheck check) throws NoSuchMeth TargetedMethodMatcher.builder() .match("consumePos", b -> b.virtual()) .match("consumePosStatic", b -> b.statik()) - .hasParam(POSITION) + .targetParam(POSITION) .build() ); }); @@ -60,7 +60,7 @@ void testParamFuzzyStaticRewrite(final TransformerCheck check) throws NoSuchMeth builder.changeParamFuzzy( POSITION, handler, - TargetedMethodMatcher.builder().ctor().hasParam(POSITION).build() + TargetedMethodMatcher.builder().ctor().targetParam(POSITION).build() ); }); check.run(RewriteRule.chain(rule, ctorRule)); diff --git a/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteReturnsTest.java b/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteReturnsTest.java index 3197b9b..d156ef9 100644 --- a/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteReturnsTest.java +++ b/src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteReturnsTest.java @@ -27,7 +27,7 @@ void testReturnDirectStaticRewrite(final TransformerCheck check) throws NoSuchMe TargetedMethodMatcher.builder() .match("getLoc", b -> b.virtual()) .match("getLocStatic", b -> b.statik()) - .hasReturn(LOCATION) + .targetReturn(LOCATION) .build() ); }); @@ -45,7 +45,7 @@ void testReturnDirectWithContextStaticRewrite(final TransformerCheck check) thro TargetedMethodMatcher.builder() .match("getLoc", b -> b.virtual()) .match("getLocStatic", b -> b.statik()) - .hasReturn(LOCATION) + .targetReturn(LOCATION) .build() ); });