Skip to content

Commit 2d9f9af

Browse files
committed
further improve various builders for clarity
1 parent 0acc5a8 commit 2d9f9af

File tree

8 files changed

+101
-101
lines changed

8 files changed

+101
-101
lines changed
Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,30 @@
11
package io.papermc.asm.rules.builder.matcher.method;
22

33
import io.papermc.asm.util.Builder;
4-
import java.lang.constant.ClassDesc;
5-
import java.lang.constant.MethodTypeDesc;
64
import java.util.Collection;
75
import java.util.function.Consumer;
8-
import java.util.function.Predicate;
96

10-
public interface MethodMatcherBuilder extends Builder<MethodMatcher> {
7+
public interface MethodMatcherBuilder extends MethodParamMatcherBuilder<MethodMatcherBuilder>, Builder<MethodMatcher> {
118

129
MethodMatcherBuilder ctor(final Consumer<MatchBuilder> matchBuilderConsumer);
1310

14-
MethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);
11+
default MethodMatcherBuilder match(final String name) {
12+
return this.match(name, b -> {});
13+
}
1514

16-
MethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);
15+
MethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);
1716

18-
MethodMatcherBuilder hasParam(final ClassDesc paramClassDesc);
17+
default MethodMatcherBuilder match(final Collection<String> names) {
18+
return this.match(names, b -> {});
19+
}
1920

20-
MethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc);
21+
MethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);
2122

2223
/**
2324
* Used to match methods with specific names.
2425
*
2526
* @see MethodMatcherBuilder#match(String, Consumer)
2627
*/
27-
interface MatchBuilder {
28-
29-
MatchBuilder virtual();
30-
31-
MatchBuilder statik();
32-
33-
MatchBuilder type(final MethodType... types);
34-
35-
MatchBuilder hasParam(final ClassDesc paramClassDesc);
36-
37-
MatchBuilder hasReturn(final ClassDesc returnClassDesc);
38-
39-
MatchBuilder desc(final String... descriptors);
40-
41-
MatchBuilder desc(final MethodTypeDesc... descriptors);
42-
43-
MatchBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate);
28+
interface MatchBuilder extends MethodParamMatcherBuilder<MatchBuilder>, MethodTypeMatcherBuilder<MatchBuilder> {
4429
}
4530
}

src/main/java/io/papermc/asm/rules/builder/matcher/method/MethodMatcherBuilderImpl.java

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.papermc.asm.rules.builder.matcher.method;
22

33
import io.papermc.asm.rules.method.StaticRewrite;
4-
import java.lang.constant.ClassDesc;
54
import java.lang.constant.MethodTypeDesc;
65
import java.util.Arrays;
76
import java.util.Collection;
@@ -43,16 +42,7 @@ public MethodMatcherBuilder match(final Collection<String> names, final Consumer
4342
}
4443

4544
@Override
46-
public MethodMatcherBuilder hasParam(final ClassDesc paramClassDesc) {
47-
return this.desc(d -> d.parameterList().contains(paramClassDesc));
48-
}
49-
50-
@Override
51-
public MethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc) {
52-
return this.desc(d -> d.returnType().equals(returnClassDesc));
53-
}
54-
55-
private MethodMatcherBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
45+
public MethodMatcherBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
5646
this.matcher = this.matcher.and(descPredicate);
5747
return this;
5848
}
@@ -73,42 +63,12 @@ private void apply() {
7363
});
7464
}
7565

76-
@Override
77-
public MatchBuilder virtual() {
78-
return this.type(MethodType.VIRTUAL);
79-
}
80-
81-
@Override
82-
public MatchBuilder statik() {
83-
return this.type(MethodType.STATIC);
84-
}
85-
8666
@Override
8767
public MatchBuilder type(final MethodType...types) {
8868
this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b));
8969
return this;
9070
}
9171

92-
@Override
93-
public MatchBuilder hasParam(final ClassDesc paramClassDesc) {
94-
return this.desc(d -> d.parameterList().contains(paramClassDesc));
95-
}
96-
97-
@Override
98-
public MatchBuilder hasReturn(final ClassDesc returnClassDesc) {
99-
return this.desc(d -> d.returnType().equals(returnClassDesc));
100-
}
101-
102-
@Override
103-
public MatchBuilder desc(final String...descriptors) {
104-
return this.desc(desc -> Arrays.stream(descriptors).anyMatch(d -> desc.descriptorString().equals(d)));
105-
}
106-
107-
@Override
108-
public MatchBuilder desc(final MethodTypeDesc...descriptors) {
109-
return this.desc(desc -> Arrays.asList(descriptors).contains(desc));
110-
}
111-
11272
@Override
11373
public MatchBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
11474
this.bytecodeDescPredicate = descPredicate;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package io.papermc.asm.rules.builder.matcher.method;
2+
3+
import java.lang.constant.ClassDesc;
4+
import java.lang.constant.MethodTypeDesc;
5+
import java.util.Arrays;
6+
import java.util.function.Predicate;
7+
8+
public interface MethodParamMatcherBuilder<B> {
9+
10+
default B hasParam(final ClassDesc paramClassDesc) {
11+
return this.desc(d -> d.parameterList().contains(paramClassDesc));
12+
}
13+
14+
default B hasParam(final ClassDesc paramClassDesc, final int paramIdx) {
15+
return this.desc(d -> d.parameterType(paramIdx).equals(paramClassDesc));
16+
}
17+
18+
default B hasReturn(final ClassDesc returnClassDesc) {
19+
return this.desc(d -> d.returnType().equals(returnClassDesc));
20+
}
21+
22+
default B desc(final String... descriptors) {
23+
return this.desc(desc -> Arrays.stream(descriptors).anyMatch(d -> desc.descriptorString().equals(d)));
24+
}
25+
26+
default B desc(final MethodTypeDesc... descriptors) {
27+
return this.desc(desc -> Arrays.asList(descriptors).contains(desc));
28+
}
29+
30+
B desc(final Predicate<? super MethodTypeDesc> descPredicate);
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.papermc.asm.rules.builder.matcher.method;
2+
3+
public interface MethodTypeMatcherBuilder<B> {
4+
5+
default B virtual() {
6+
return this.type(MethodType.VIRTUAL);
7+
}
8+
9+
default B statik() {
10+
return this.type(MethodType.STATIC);
11+
}
12+
13+
default B itf() {
14+
return this.type(MethodType.INTERFACE);
15+
}
16+
17+
B type(final MethodType... types);
18+
}
Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
package io.papermc.asm.rules.builder.matcher.method.targeted;
22

3-
import io.papermc.asm.rules.builder.matcher.method.MethodType;
3+
import io.papermc.asm.rules.builder.matcher.method.MethodParamMatcherBuilder;
4+
import io.papermc.asm.rules.builder.matcher.method.MethodTypeMatcherBuilder;
45
import io.papermc.asm.util.Builder;
56
import java.lang.constant.ClassDesc;
67
import java.util.Collection;
78
import java.util.function.Consumer;
89

9-
public interface TargetedMethodMatcherBuilder extends Builder<TargetedMethodMatcher> {
10+
public interface TargetedMethodMatcherBuilder extends MethodParamMatcherBuilder<TargetedMethodMatcherBuilder>, Builder<TargetedMethodMatcher> {
1011

1112
TargetedMethodMatcherBuilder ctor();
1213

13-
TargetedMethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);
14-
15-
TargetedMethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);
14+
default TargetedMethodMatcherBuilder match(final String name) {
15+
return this.match(name, b -> {});
16+
}
1617

17-
TargetedMethodMatcherBuilder hasParam(final ClassDesc paramClassDesc);
18+
TargetedMethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);
1819

19-
TargetedMethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc);
20+
default TargetedMethodMatcherBuilder match(final Collection<String> names) {
21+
return this.match(names, b -> {});
22+
}
2023

21-
interface MatchBuilder {
24+
TargetedMethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);
2225

23-
MatchBuilder virtual();
26+
TargetedMethodMatcherBuilder targetParam(final ClassDesc paramClassDesc);
2427

25-
MatchBuilder statik();
28+
TargetedMethodMatcherBuilder targetReturn(final ClassDesc returnClassDesc);
2629

27-
MatchBuilder type(final MethodType... types);
30+
interface MatchBuilder extends MethodParamMatcherBuilder<MatchBuilder>, MethodTypeMatcherBuilder<MatchBuilder> {
2831
}
2932
}

src/main/java/io/papermc/asm/rules/builder/matcher/method/targeted/TargetedMethodMatcherBuilderImpl.java

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
class TargetedMethodMatcherBuilderImpl implements TargetedMethodMatcherBuilder {
1818

1919
private MethodMatcher matcher = (opcode, isInvokeDynamic, name, descriptor) -> false;
20-
private @MonotonicNonNull Predicate<MethodTypeDesc> byDesc;
20+
private Predicate<MethodTypeDesc> byDesc = $ -> true;
2121
private @MonotonicNonNull ClassDesc oldType;
2222

2323
TargetedMethodMatcherBuilderImpl() {
@@ -43,28 +43,32 @@ public TargetedMethodMatcherBuilder match(final Collection<String> names, final
4343
}
4444

4545
@Override
46-
public TargetedMethodMatcherBuilder hasParam(final ClassDesc classDesc) {
46+
public TargetedMethodMatcherBuilder targetParam(final ClassDesc classDesc) {
4747
if (this.oldType != null) {
4848
throw new IllegalArgumentException("Targeted type was already set to " + this.oldType);
4949
}
5050
this.oldType = classDesc;
51-
this.byDesc = d -> d.parameterList().contains(classDesc);
52-
return this;
51+
return this.hasParam(classDesc);
5352
}
5453

5554
@Override
56-
public TargetedMethodMatcherBuilder hasReturn(final ClassDesc classDesc) {
55+
public TargetedMethodMatcherBuilder targetReturn(final ClassDesc classDesc) {
5756
if (this.oldType != null) {
5857
throw new IllegalArgumentException("Targeted type was already set to " + this.oldType);
5958
}
6059
this.oldType = classDesc;
61-
this.byDesc = d -> d.returnType().equals(classDesc);
60+
return this.hasReturn(classDesc);
61+
}
62+
63+
@Override
64+
public TargetedMethodMatcherBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
65+
this.byDesc = this.byDesc.and(descPredicate);
6266
return this;
6367
}
6468

6569
@Override
6670
public TargetedMethodMatcher build() {
67-
if (this.oldType == null || this.byDesc == null) {
71+
if (this.oldType == null) {
6872
throw new IllegalStateException("Targeted type was not set");
6973
}
7074
final MethodMatcher finalMatcher = this.matcher.and(this.byDesc);
@@ -74,31 +78,30 @@ public TargetedMethodMatcher build() {
7478
final class SpecificMatchBuilder implements TargetedMethodMatcherBuilder.MatchBuilder {
7579

7680
private final Predicate<String> namePredicate;
77-
private @MonotonicNonNull OpcodePredicate opcodePredicate;
81+
private Predicate<? super MethodTypeDesc> bytecodeDescPredicate = $ -> true;
82+
private OpcodePredicate opcodePredicate = ($, $$) -> true;
7883

7984
private SpecificMatchBuilder(final Predicate<String> namePredicate) {
8085
this.namePredicate = namePredicate;
8186
}
8287

8388
private void apply() {
8489
TargetedMethodMatcherBuilderImpl.this.matcher = TargetedMethodMatcherBuilderImpl.this.matcher.or((o, isInvokeDynamic, n, d) -> {
85-
return this.namePredicate.test(n) && this.opcodePredicate.matches(o, isInvokeDynamic);
90+
return this.namePredicate.test(n)
91+
&& this.opcodePredicate.matches(o, isInvokeDynamic)
92+
&& this.bytecodeDescPredicate.test(MethodTypeDesc.ofDescriptor(d));
8693
});
8794
}
8895

8996
@Override
90-
public TargetedMethodMatcherBuilder.MatchBuilder virtual() {
91-
return this.type(MethodType.VIRTUAL);
92-
}
93-
94-
@Override
95-
public TargetedMethodMatcherBuilder.MatchBuilder statik() {
96-
return this.type(MethodType.STATIC);
97+
public TargetedMethodMatcherBuilder.MatchBuilder type(final MethodType... types) {
98+
this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b));
99+
return this;
97100
}
98101

99102
@Override
100-
public TargetedMethodMatcherBuilder.MatchBuilder type(final MethodType... types) {
101-
this.opcodePredicate = (o, b) -> Arrays.stream(types).anyMatch(type -> type.matches(o, b));
103+
public MatchBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
104+
this.bytecodeDescPredicate = descPredicate;
102105
return this;
103106
}
104107
}

src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteParamsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ void testParamDirectStaticRewrite(final TransformerCheck check) throws NoSuchMet
2727
TargetedMethodMatcher.builder()
2828
.match("consumeLoc", b -> b.virtual())
2929
.match("consumeLocStatic", b -> b.statik())
30-
.hasParam(LOCATION)
30+
.targetParam(LOCATION)
3131
.build()
3232
)
3333
);
3434
final RewriteRule ctorRule = RewriteRule.forOwnerClass(Methods.Wrapper.class, builder -> {
3535
builder.changeParamDirect(
3636
POSITION,
3737
handler,
38-
TargetedMethodMatcher.builder().ctor().hasParam(LOCATION).build()
38+
TargetedMethodMatcher.builder().ctor().targetParam(LOCATION).build()
3939
);
4040
});
4141
check.run(RewriteRule.chain(rule, ctorRule));
@@ -52,15 +52,15 @@ void testParamFuzzyStaticRewrite(final TransformerCheck check) throws NoSuchMeth
5252
TargetedMethodMatcher.builder()
5353
.match("consumePos", b -> b.virtual())
5454
.match("consumePosStatic", b -> b.statik())
55-
.hasParam(POSITION)
55+
.targetParam(POSITION)
5656
.build()
5757
);
5858
});
5959
final RewriteRule ctorRule = RewriteRule.forOwnerClass(Methods.PosWrapper.class, builder -> {
6060
builder.changeParamFuzzy(
6161
POSITION,
6262
handler,
63-
TargetedMethodMatcher.builder().ctor().hasParam(POSITION).build()
63+
TargetedMethodMatcher.builder().ctor().targetParam(POSITION).build()
6464
);
6565
});
6666
check.run(RewriteRule.chain(rule, ctorRule));

src/test/java/io/papermc/asm/rules/methods/StaticMethodRewriteReturnsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ void testReturnDirectStaticRewrite(final TransformerCheck check) throws NoSuchMe
2727
TargetedMethodMatcher.builder()
2828
.match("getLoc", b -> b.virtual())
2929
.match("getLocStatic", b -> b.statik())
30-
.hasReturn(LOCATION)
30+
.targetReturn(LOCATION)
3131
.build()
3232
);
3333
});
@@ -45,7 +45,7 @@ void testReturnDirectWithContextStaticRewrite(final TransformerCheck check) thro
4545
TargetedMethodMatcher.builder()
4646
.match("getLoc", b -> b.virtual())
4747
.match("getLocStatic", b -> b.statik())
48-
.hasReturn(LOCATION)
48+
.targetReturn(LOCATION)
4949
.build()
5050
);
5151
});

0 commit comments

Comments
 (0)