Skip to content

Commit

Permalink
renamed Configured to Guarded and mirror for fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Feb 13, 2024
1 parent f547bd9 commit 544f8bf
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import io.papermc.asm.ClassInfo;
import io.papermc.asm.ClassInfoProvider;
import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.MethodRewriteRule;
import io.papermc.asm.rules.RewriteRule;
import io.papermc.asm.rules.method.MethodRewriteRule;
import java.lang.constant.MethodTypeDesc;
import java.util.Set;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package io.papermc.asm.rules.builder;

import io.papermc.asm.rules.FieldRewrites;
import io.papermc.asm.rules.MethodRewrites;
import io.papermc.asm.rules.RewriteRule;
import io.papermc.asm.rules.StaticRewrite;
import io.papermc.asm.rules.StaticRewrites;
import io.papermc.asm.rules.builder.matcher.FieldMatcher;
import io.papermc.asm.rules.builder.matcher.MethodMatcher;
import io.papermc.asm.rules.builder.matcher.TargetedMethodMatcher;
import io.papermc.asm.rules.field.FieldRewrites;
import io.papermc.asm.rules.method.MethodRewrites;
import io.papermc.asm.rules.method.StaticRewrite;
import io.papermc.asm.rules.method.StaticRewrites;
import io.papermc.asm.util.Builder;
import java.lang.constant.ClassDesc;
import java.lang.reflect.Method;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.field;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.builder.matcher.FieldMatcher;
import io.papermc.asm.rules.RewriteRule;
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
import org.checkerframework.checker.nullness.qual.Nullable;
Expand All @@ -10,9 +10,11 @@

import static io.papermc.asm.util.DescriptorUtils.parseType;

public interface FieldRewriteRule extends OwnableRewriteRule {
public interface FieldRewriteRule extends RewriteRule {

FieldMatcher fieldMatcher();
default boolean shouldProcess(final ClassProcessingContext context, final int opcode, final String owner, final String name, final String descriptor) {
return true;
}

@Override
default ClassVisitor createVisitor(final int api, final ClassVisitor parent, final ClassProcessingContext context) {
Expand All @@ -22,7 +24,7 @@ public MethodVisitor visitMethod(final int access, final String name, final Stri
return new MethodVisitor(this.api, super.visitMethod(access, name, descriptor, signature, exceptions)) {
@Override
public void visitFieldInsn(final int opcode, final String owner, final String name, final String descriptor) {
if (FieldRewriteRule.this.matchesOwner(context, owner) && FieldRewriteRule.this.fieldMatcher().matches(name, descriptor)) {
if (FieldRewriteRule.this.shouldProcess(context, opcode, owner, name, descriptor)) {
final @Nullable Rewrite rewrite = FieldRewriteRule.this.rewrite(context, opcode, owner, name, parseType(descriptor));
if (rewrite != null) {
rewrite.apply(this.getDelegate());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.field;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.builder.matcher.FieldMatcher;
Expand All @@ -12,7 +12,7 @@ private FieldRewrites() {
}

// Keep in mind that you have to include all subtype owners as well as the field could be referenced via them as well
public record Rename(Set<Class<?>> owners, FieldMatcher fieldMatcher, String newName) implements FieldRewriteRule {
public record Rename(Set<Class<?>> owners, FieldMatcher fieldMatcher, String newName) implements GuardedMethodRewriteRule {

@Override
public @Nullable Rewrite rewrite(final ClassProcessingContext context, final int opcode, final String owner, final String name, final ClassDesc desc) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.papermc.asm.rules.field;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.OwnableRewriteRule;
import io.papermc.asm.rules.builder.matcher.FieldMatcher;

/**
* A rule that targets specific fields and owners.
*/
public interface GuardedMethodRewriteRule extends FieldRewriteRule, OwnableRewriteRule {

FieldMatcher fieldMatcher();

@Override
default boolean shouldProcess(final ClassProcessingContext context, final int opcode, final String owner, final String name, final String descriptor) {
return this.matchesOwner(context, owner) && this.fieldMatcher().matches(name, descriptor);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.method;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.OwnableRewriteRule;
import io.papermc.asm.rules.builder.matcher.MethodMatcher;
import java.lang.constant.MethodTypeDesc;
import java.lang.reflect.Constructor;
Expand All @@ -12,7 +13,10 @@

import static io.papermc.asm.util.DescriptorUtils.fromExecutable;

public interface ConfiguredMethodRewriteRule extends MethodRewriteRule, OwnableRewriteRule {
/**
* A rule that targets specific methods and owners.
*/
public interface GuardedMethodRewriteRule extends MethodRewriteRule, OwnableRewriteRule {

private static String transformExecutableName(final Executable executable) {
return executable instanceof Constructor<?> ? "<init>" : executable.getName();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.method;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.RewriteRule;
import java.lang.constant.MethodTypeDesc;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.ClassVisitor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.method;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.builder.matcher.MethodMatcher;
Expand All @@ -17,7 +17,7 @@ private MethodRewrites() {

// Changes a parameter type to a super type. This isn't a compile break, but is an ABI break. We just change the
// offending parameter in the descriptor and move on.
public record SuperTypeParam(Set<Class<?>> owners, MethodMatcher methodMatcher, ClassDesc oldParamType, ClassDesc newParamType) implements MethodRewriteRule {
public record SuperTypeParam(Set<Class<?>> owners, MethodMatcher methodMatcher, ClassDesc oldParamType, ClassDesc newParamType) implements GuardedMethodRewriteRule {

@Override
public Rewrite rewrite(final ClassProcessingContext context, final boolean invokeDynamic, final int opcode, final String owner, final String name, final MethodTypeDesc descriptor, final boolean isInterface) {
Expand All @@ -31,7 +31,7 @@ private MethodTypeDesc modifyMethodDescriptor(final MethodTypeDesc methodDescrip

// Changes a return type to a subtype of the old type. This isn't a compile break as subtypes inherit everything, but it is an ABI break.
// We just change the return type in the descriptor and move on.
public record SubTypeReturn(Set<Class<?>> owners, MethodMatcher methodMatcher, ClassDesc oldReturnType, ClassDesc newReturnType) implements MethodRewriteRule {
public record SubTypeReturn(Set<Class<?>> owners, MethodMatcher methodMatcher, ClassDesc oldReturnType, ClassDesc newReturnType) implements GuardedMethodRewriteRule {

@Override
public @Nullable Rewrite rewrite(final ClassProcessingContext context, final boolean invokeDynamic, final int opcode, final String owner, final String name, final MethodTypeDesc descriptor, final boolean isInterface) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.method;

import io.papermc.asm.ClassProcessingContext;
import io.papermc.asm.rules.builder.matcher.MethodMatcher;
Expand All @@ -25,7 +25,7 @@
import static io.papermc.asm.util.OpcodeUtils.staticOp;
import static java.util.function.Predicate.isEqual;

public interface StaticRewrite extends ConfiguredMethodRewriteRule {
public interface StaticRewrite extends GuardedMethodRewriteRule {

ClassDesc staticRedirectOwner();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package io.papermc.asm.rules;
package io.papermc.asm.rules.method;

import io.papermc.asm.rules.builder.matcher.TargetedMethodMatcher;
import java.lang.constant.ClassDesc;
Expand Down

0 comments on commit 544f8bf

Please sign in to comment.