Skip to content

Commit

Permalink
Add ReflectionRemapper#withClassNamePreprocessor
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Mar 12, 2024
1 parent 6adc013 commit a175f54
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* reflection-remapper
*
* Copyright (c) 2021-2023 Jason Penilla
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package xyz.jpenilla.reflectionremapper;

import java.util.function.UnaryOperator;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.framework.qual.DefaultQualifier;

@DefaultQualifier(NonNull.class)
final class ClassNamePreprocessingReflectionRemapper implements ReflectionRemapper {
private final ReflectionRemapper delegate;
private final UnaryOperator<String> processor;

ClassNamePreprocessingReflectionRemapper(
final ReflectionRemapper delegate,
final UnaryOperator<String> processor
) {
this.delegate = delegate;
this.processor = processor;
}

@Override
public String remapClassName(final String className) {
return this.delegate.remapClassName(this.processor.apply(className));
}

@Override
public String remapFieldName(final Class<?> holdingClass, final String fieldName) {
return this.delegate.remapFieldName(holdingClass, fieldName);
}

@Override
public String remapMethodName(final Class<?> holdingClass, final String methodName, final Class<?>... paramTypes) {
return this.delegate.remapMethodName(holdingClass, methodName, paramTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.Objects;
import java.util.function.UnaryOperator;
import net.fabricmc.mappingio.MappingReader;
import net.fabricmc.mappingio.tree.MemoryMappingTree;
import org.checkerframework.checker.nullness.qual.NonNull;
Expand Down Expand Up @@ -108,6 +109,19 @@ default String remapClassOrArrayName(final String name) {
return this.remapClassName(name);
}

/**
* Creates a new reflection remapper that processes class names using the provided
* operator before remapping them with this remapper.
*
* <p>This may be useful when class names must be mangled to avoid relocation.</p>
*
* @param preprocessor class name preprocessor
* @return delegating reflection remapper
*/
default ReflectionRemapper withClassNamePreprocessor(final UnaryOperator<String> preprocessor) {
return new ClassNamePreprocessingReflectionRemapper(this, preprocessor);
}

/**
* Returns a noop {@link ReflectionRemapper} instance which simply passes through the given
* names without remapping.
Expand Down

0 comments on commit a175f54

Please sign in to comment.