Skip to content

Commit a175f54

Browse files
committed
Add ReflectionRemapper#withClassNamePreprocessor
1 parent 6adc013 commit a175f54

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* reflection-remapper
3+
*
4+
* Copyright (c) 2021-2023 Jason Penilla
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package xyz.jpenilla.reflectionremapper;
19+
20+
import java.util.function.UnaryOperator;
21+
import org.checkerframework.checker.nullness.qual.NonNull;
22+
import org.checkerframework.framework.qual.DefaultQualifier;
23+
24+
@DefaultQualifier(NonNull.class)
25+
final class ClassNamePreprocessingReflectionRemapper implements ReflectionRemapper {
26+
private final ReflectionRemapper delegate;
27+
private final UnaryOperator<String> processor;
28+
29+
ClassNamePreprocessingReflectionRemapper(
30+
final ReflectionRemapper delegate,
31+
final UnaryOperator<String> processor
32+
) {
33+
this.delegate = delegate;
34+
this.processor = processor;
35+
}
36+
37+
@Override
38+
public String remapClassName(final String className) {
39+
return this.delegate.remapClassName(this.processor.apply(className));
40+
}
41+
42+
@Override
43+
public String remapFieldName(final Class<?> holdingClass, final String fieldName) {
44+
return this.delegate.remapFieldName(holdingClass, fieldName);
45+
}
46+
47+
@Override
48+
public String remapMethodName(final Class<?> holdingClass, final String methodName, final Class<?>... paramTypes) {
49+
return this.delegate.remapMethodName(holdingClass, methodName, paramTypes);
50+
}
51+
}

src/main/java/xyz/jpenilla/reflectionremapper/ReflectionRemapper.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.util.ArrayList;
2828
import java.util.Collections;
2929
import java.util.Objects;
30+
import java.util.function.UnaryOperator;
3031
import net.fabricmc.mappingio.MappingReader;
3132
import net.fabricmc.mappingio.tree.MemoryMappingTree;
3233
import org.checkerframework.checker.nullness.qual.NonNull;
@@ -108,6 +109,19 @@ default String remapClassOrArrayName(final String name) {
108109
return this.remapClassName(name);
109110
}
110111

112+
/**
113+
* Creates a new reflection remapper that processes class names using the provided
114+
* operator before remapping them with this remapper.
115+
*
116+
* <p>This may be useful when class names must be mangled to avoid relocation.</p>
117+
*
118+
* @param preprocessor class name preprocessor
119+
* @return delegating reflection remapper
120+
*/
121+
default ReflectionRemapper withClassNamePreprocessor(final UnaryOperator<String> preprocessor) {
122+
return new ClassNamePreprocessingReflectionRemapper(this, preprocessor);
123+
}
124+
111125
/**
112126
* Returns a noop {@link ReflectionRemapper} instance which simply passes through the given
113127
* names without remapping.

0 commit comments

Comments
 (0)