|
16 | 16 |
|
17 | 17 | package net.fabricmc.mappingio.tree;
|
18 | 18 |
|
| 19 | +import java.util.Arrays; |
19 | 20 | import java.util.Collection;
|
20 | 21 | import java.util.List;
|
| 22 | +import java.util.regex.Pattern; |
21 | 23 |
|
22 | 24 | import org.jetbrains.annotations.Nullable;
|
23 | 25 |
|
@@ -110,6 +112,86 @@ default ClassMapping getClass(String name, int namespace) {
|
110 | 112 | @Nullable
|
111 | 113 | ClassMapping removeClass(String srcName);
|
112 | 114 |
|
| 115 | + /** |
| 116 | + * Searches for inner classes whose effective destination name contains outer classes referenced via their source name, |
| 117 | + * scans the tree for potential mappings for these enclosing classes, and applies the latters' destination names |
| 118 | + * to the formers' fully qualified name. |
| 119 | + * |
| 120 | + * <p>For example, it takes a class {@code class_1$class_2} that doesn't have a mapping, |
| 121 | + * tries to find {@code class_1}, which let's say has the mapping {@code SomeClass}, |
| 122 | + * and changes the former's destination name to {@code SomeClass$class_2}. |
| 123 | + * |
| 124 | + * <p>Equivalent of {@link OuterClassNameInheritingVisitor}, but more efficient |
| 125 | + * since the tree's existing class map can be reused. |
| 126 | + * |
| 127 | + * @param processRemappedDstNames Whether already remapped destination names should also get their unmapped outer classes replaced. |
| 128 | + */ |
| 129 | + default void propagateOuterClassNames(boolean processRemappedDstNames) { |
| 130 | + propagateOuterClassNames(getSrcNamespace(), getDstNamespaces(), processRemappedDstNames); |
| 131 | + } |
| 132 | + |
| 133 | + /** |
| 134 | + * Searches for inner classes whose effective destination name contains outer classes referenced via their source name, |
| 135 | + * scans the tree for potential mappings for these enclosing classes, and applies the latters' destination names |
| 136 | + * to the formers' fully qualified name. |
| 137 | + * |
| 138 | + * <p>For example, it takes a class {@code class_1$class_2} that doesn't have a mapping, |
| 139 | + * tries to find {@code class_1}, which let's say has the mapping {@code SomeClass}, |
| 140 | + * and changes the former's destination name to {@code SomeClass$class_2}. |
| 141 | + * |
| 142 | + * <p>Equivalent of {@link OuterClassNameInheritingVisitor}, but more efficient |
| 143 | + * since the tree's existing class map can be reused. |
| 144 | + * |
| 145 | + * @param srcNamespace The namespace where the original/unmapped outer class names originate from. |
| 146 | + * @param dstNamespaces The namespaces where outer class names shall be propagated. |
| 147 | + * @param processRemappedDstNames Whether already remapped destination names should also get their unmapped outer classes replaced. |
| 148 | + */ |
| 149 | + default void propagateOuterClassNames(String srcNamespace, Collection<String> dstNamespaces, boolean processRemappedDstNames) { |
| 150 | + int srcNsId = getNamespaceId(srcNamespace); |
| 151 | + if (srcNsId == NULL_NAMESPACE_ID) throw new IllegalArgumentException("Namespace " + srcNsId + " does not exist"); |
| 152 | + |
| 153 | + for (ClassMapping cls : getClasses()) { |
| 154 | + for (String dstNs : dstNamespaces) { |
| 155 | + int dstNsId = getNamespaceId(dstNs); |
| 156 | + if (dstNsId == SRC_NAMESPACE_ID) throw new UnsupportedOperationException("Cannot change source names"); |
| 157 | + if (dstNsId == NULL_NAMESPACE_ID) throw new IllegalArgumentException("Destination namespace " + dstNs + " does not exist"); |
| 158 | + if (dstNsId == srcNsId) continue; |
| 159 | + |
| 160 | + String srcName = cls.getName(srcNsId); |
| 161 | + if (srcName == null) continue; |
| 162 | + String dstName = cls.getName(dstNsId); |
| 163 | + |
| 164 | + int idx = srcName.lastIndexOf('$'); |
| 165 | + if (idx == -1) continue; |
| 166 | + |
| 167 | + if (!processRemappedDstNames && dstName != null && !dstName.equals(srcName)) { |
| 168 | + continue; |
| 169 | + } |
| 170 | + |
| 171 | + String[] srcParts = srcName.split(Pattern.quote("$")); |
| 172 | + String[] dstParts = dstName == null ? srcParts : dstName.split(Pattern.quote("$")); |
| 173 | + assert dstParts.length == srcParts.length; |
| 174 | + |
| 175 | + for (int pos = srcParts.length - 2; pos >= 0; pos--) { |
| 176 | + String outerSrcName = String.join("$", Arrays.copyOfRange(srcParts, 0, pos + 1)); |
| 177 | + |
| 178 | + if (dstName != null && !dstParts[pos].equals(srcParts[pos])) { |
| 179 | + // That part already has a different mapping |
| 180 | + continue; |
| 181 | + } |
| 182 | + |
| 183 | + ClassMapping outerCls = getClass(outerSrcName, srcNsId); |
| 184 | + String outerDstName; |
| 185 | + |
| 186 | + if (outerCls != null && (outerDstName = outerCls.getName(dstNsId)) != null && !outerDstName.equals(outerSrcName)) { |
| 187 | + cls.setDstName(outerDstName + "$" + String.join("$", Arrays.copyOfRange(dstParts, pos + 1, dstParts.length)), dstNsId); |
| 188 | + break; |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + |
113 | 195 | @Override
|
114 | 196 | @Nullable
|
115 | 197 | default FieldMapping getField(String srcClsName, String srcName, @Nullable String srcDesc) {
|
|
0 commit comments