Skip to content

Commit

Permalink
further improve various builders for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Aug 28, 2024
1 parent 0acc5a8 commit 2d9f9af
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -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<MethodMatcher> {
public interface MethodMatcherBuilder extends MethodParamMatcherBuilder<MethodMatcherBuilder>, Builder<MethodMatcher> {

MethodMatcherBuilder ctor(final Consumer<MatchBuilder> matchBuilderConsumer);

MethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);
default MethodMatcherBuilder match(final String name) {
return this.match(name, b -> {});
}

MethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);
MethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);

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

MethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc);
MethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> 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<? super MethodTypeDesc> descPredicate);
interface MatchBuilder extends MethodParamMatcherBuilder<MatchBuilder>, MethodTypeMatcherBuilder<MatchBuilder> {
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -43,16 +42,7 @@ public MethodMatcherBuilder match(final Collection<String> 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<? super MethodTypeDesc> descPredicate) {
public MethodMatcherBuilder desc(final Predicate<? super MethodTypeDesc> descPredicate) {
this.matcher = this.matcher.and(descPredicate);
return this;
}
Expand All @@ -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<? super MethodTypeDesc> descPredicate) {
this.bytecodeDescPredicate = descPredicate;
Expand Down
Original file line number Diff line number Diff line change
@@ -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<B> {

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<? super MethodTypeDesc> descPredicate);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.papermc.asm.rules.builder.matcher.method;

public interface MethodTypeMatcherBuilder<B> {

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);
}
Original file line number Diff line number Diff line change
@@ -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<TargetedMethodMatcher> {
public interface TargetedMethodMatcherBuilder extends MethodParamMatcherBuilder<TargetedMethodMatcherBuilder>, Builder<TargetedMethodMatcher> {

TargetedMethodMatcherBuilder ctor();

TargetedMethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);

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

TargetedMethodMatcherBuilder hasParam(final ClassDesc paramClassDesc);
TargetedMethodMatcherBuilder match(final String name, final Consumer<MatchBuilder> matchBuilderConsumer);

TargetedMethodMatcherBuilder hasReturn(final ClassDesc returnClassDesc);
default TargetedMethodMatcherBuilder match(final Collection<String> names) {
return this.match(names, b -> {});
}

interface MatchBuilder {
TargetedMethodMatcherBuilder match(final Collection<String> names, final Consumer<MatchBuilder> matchBuilderConsumer);

MatchBuilder virtual();
TargetedMethodMatcherBuilder targetParam(final ClassDesc paramClassDesc);

MatchBuilder statik();
TargetedMethodMatcherBuilder targetReturn(final ClassDesc returnClassDesc);

MatchBuilder type(final MethodType... types);
interface MatchBuilder extends MethodParamMatcherBuilder<MatchBuilder>, MethodTypeMatcherBuilder<MatchBuilder> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
class TargetedMethodMatcherBuilderImpl implements TargetedMethodMatcherBuilder {

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

TargetedMethodMatcherBuilderImpl() {
Expand All @@ -43,28 +43,32 @@ public TargetedMethodMatcherBuilder match(final Collection<String> 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<? super MethodTypeDesc> 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);
Expand All @@ -74,31 +78,30 @@ public TargetedMethodMatcher build() {
final class SpecificMatchBuilder implements TargetedMethodMatcherBuilder.MatchBuilder {

private final Predicate<String> namePredicate;
private @MonotonicNonNull OpcodePredicate opcodePredicate;
private Predicate<? super MethodTypeDesc> bytecodeDescPredicate = $ -> true;
private OpcodePredicate opcodePredicate = ($, $$) -> true;

private SpecificMatchBuilder(final Predicate<String> namePredicate) {
this.namePredicate = 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<? super MethodTypeDesc> descPredicate) {
this.bytecodeDescPredicate = descPredicate;
return this;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ void testParamDirectStaticRewrite(final TransformerCheck check) throws NoSuchMet
TargetedMethodMatcher.builder()
.match("consumeLoc", b -> b.virtual())
.match("consumeLocStatic", b -> b.statik())
.hasParam(LOCATION)
.targetParam(LOCATION)
.build()
)
);
final RewriteRule ctorRule = RewriteRule.forOwnerClass(Methods.Wrapper.class, builder -> {
builder.changeParamDirect(
POSITION,
handler,
TargetedMethodMatcher.builder().ctor().hasParam(LOCATION).build()
TargetedMethodMatcher.builder().ctor().targetParam(LOCATION).build()
);
});
check.run(RewriteRule.chain(rule, ctorRule));
Expand All @@ -52,15 +52,15 @@ void testParamFuzzyStaticRewrite(final TransformerCheck check) throws NoSuchMeth
TargetedMethodMatcher.builder()
.match("consumePos", b -> b.virtual())
.match("consumePosStatic", b -> b.statik())
.hasParam(POSITION)
.targetParam(POSITION)
.build()
);
});
final RewriteRule ctorRule = RewriteRule.forOwnerClass(Methods.PosWrapper.class, builder -> {
builder.changeParamFuzzy(
POSITION,
handler,
TargetedMethodMatcher.builder().ctor().hasParam(POSITION).build()
TargetedMethodMatcher.builder().ctor().targetParam(POSITION).build()
);
});
check.run(RewriteRule.chain(rule, ctorRule));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
});
Expand All @@ -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()
);
});
Expand Down

0 comments on commit 2d9f9af

Please sign in to comment.