|
| 1 | +/* |
| 2 | + * codebook is a remapper utility for the PaperMC project. |
| 3 | + * |
| 4 | + * Copyright (c) 2023 Kyle Wood (DenWav) |
| 5 | + * Contributors |
| 6 | + * |
| 7 | + * This library is free software; you can redistribute it and/or |
| 8 | + * modify it under the terms of the GNU Lesser General Public |
| 9 | + * License as published by the Free Software Foundation; |
| 10 | + * version 3 only, no later versions. |
| 11 | + * |
| 12 | + * This library is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | + * Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public |
| 18 | + * License along with this library; if not, write to the Free Software |
| 19 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 20 | + * USA |
| 21 | + */ |
| 22 | + |
| 23 | +package io.papermc.codebook.report.type; |
| 24 | + |
| 25 | +import dev.denwav.hypo.hydrate.generic.HypoHydration; |
| 26 | +import dev.denwav.hypo.hydrate.generic.LambdaClosure; |
| 27 | +import dev.denwav.hypo.hydrate.generic.LocalClassClosure; |
| 28 | +import dev.denwav.hypo.model.data.ClassData; |
| 29 | +import dev.denwav.hypo.model.data.ClassKind; |
| 30 | +import dev.denwav.hypo.model.data.MethodData; |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.ArrayList; |
| 33 | +import java.util.Comparator; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Optional; |
| 37 | +import java.util.concurrent.ConcurrentHashMap; |
| 38 | +import java.util.function.IntUnaryOperator; |
| 39 | +import java.util.regex.Pattern; |
| 40 | +import org.cadixdev.lorenz.model.Mapping; |
| 41 | +import org.cadixdev.lorenz.model.MethodMapping; |
| 42 | +import org.checkerframework.checker.nullness.qual.Nullable; |
| 43 | + |
| 44 | +public class MissingMethodParam implements Report { |
| 45 | + |
| 46 | + private final Map<ClassData, List<String>> data = new ConcurrentHashMap<>(); |
| 47 | + |
| 48 | + private void checkMappings( |
| 49 | + final MethodData method, |
| 50 | + final @Nullable MethodMapping methodMapping, |
| 51 | + final int descriptorParamOffset, |
| 52 | + final IntUnaryOperator descriptorToMappingOffset) { |
| 53 | + this.checkMappings(method, methodMapping, descriptorParamOffset, descriptorToMappingOffset, null); |
| 54 | + } |
| 55 | + |
| 56 | + private void checkMappings( |
| 57 | + final MethodData method, |
| 58 | + final @Nullable MethodMapping methodMapping, |
| 59 | + final int descriptorParamOffset, |
| 60 | + final IntUnaryOperator descriptorToMappingOffset, |
| 61 | + final @Nullable LambdaClosure lambdaClosure) { |
| 62 | + if (method.params().size() == descriptorParamOffset) { |
| 63 | + return; |
| 64 | + } |
| 65 | + if (methodMapping == null |
| 66 | + || (method.params().size() - descriptorParamOffset |
| 67 | + > methodMapping.getParameterMappings().size())) { |
| 68 | + // != should have been sufficient here, but hypo's CopyMappingsDown for constructors incorrectly applies |
| 69 | + // mappings to implicit constructor params |
| 70 | + this.reportMissingParam( |
| 71 | + method, methodMapping, descriptorParamOffset, descriptorToMappingOffset, lambdaClosure); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + private static final Pattern ANONYMOUS_CLASS = Pattern.compile(".+\\$\\d+$"); |
| 76 | + |
| 77 | + private static boolean shouldSkipMapping( |
| 78 | + final MethodData method, |
| 79 | + final ClassData parentClass, |
| 80 | + final @Nullable ClassData superClass, |
| 81 | + final @Nullable List<LambdaClosure> lambdaCalls) { |
| 82 | + final String name = method.name(); |
| 83 | + if (name.startsWith("access$") && method.isSynthetic()) { |
| 84 | + // never in source |
| 85 | + return true; |
| 86 | + } else if (name.startsWith("lambda$") |
| 87 | + && method.isSynthetic() |
| 88 | + && (lambdaCalls == null || lambdaCalls.isEmpty())) { |
| 89 | + // lambdas that had their use stripped by mojang |
| 90 | + return true; |
| 91 | + } else { |
| 92 | + final String descriptorText = method.descriptorText(); |
| 93 | + if (superClass != null |
| 94 | + && superClass.name().equals("java/lang/Enum") |
| 95 | + && name.equals("valueOf") |
| 96 | + && descriptorText.startsWith("(Ljava/lang/String;)")) { |
| 97 | + // created by the compiler |
| 98 | + return true; |
| 99 | + } else if (parentClass.is(ClassKind.RECORD) |
| 100 | + && name.equals("equals") |
| 101 | + && descriptorText.equals("(Ljava/lang/Object;)Z")) { |
| 102 | + // created by the compiler |
| 103 | + return true; |
| 104 | + } else if (method.isSynthetic() && method.get(HypoHydration.SYNTHETIC_TARGET) != null) { |
| 105 | + // don't trust isBridge, apparently it's not always accurate |
| 106 | + return true; |
| 107 | + } else { |
| 108 | + return false; |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + private void handleConstructorMappings( |
| 114 | + final MethodData method, |
| 115 | + final ClassData parentClass, |
| 116 | + final @Nullable MethodMapping methodMapping, |
| 117 | + final @Nullable LocalClassClosure localClassClosure) |
| 118 | + throws IOException { |
| 119 | + if (parentClass.is(ClassKind.ENUM)) { |
| 120 | + // enum constructors include name and ordinal |
| 121 | + this.checkMappings(method, methodMapping, 2, i -> i + 1); |
| 122 | + } else { |
| 123 | + if (!ANONYMOUS_CLASS.matcher(parentClass.name()).matches()) { |
| 124 | + // anonymous classes cannot have constructors in source |
| 125 | + if (parentClass.outerClass() != null) { |
| 126 | + final int descriptorParamOffset = parentClass.isStaticInnerClass() ? 0 : 1; |
| 127 | + if (localClassClosure == null) { |
| 128 | + this.checkMappings(method, methodMapping, descriptorParamOffset, i -> i + 1); |
| 129 | + } else { |
| 130 | + this.checkMappings( |
| 131 | + method, |
| 132 | + methodMapping, |
| 133 | + descriptorParamOffset + localClassClosure.getParamLvtIndices().length, |
| 134 | + i -> i + 1); |
| 135 | + } |
| 136 | + } else { |
| 137 | + this.checkMappings(method, methodMapping, 0, i -> i + 1); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + public void handleCheckingMappings( |
| 144 | + final MethodData method, |
| 145 | + final ClassData parentClass, |
| 146 | + final @Nullable ClassData superClass, |
| 147 | + final @Nullable List<LambdaClosure> lambdaCalls, |
| 148 | + final @Nullable MethodMapping methodMapping, |
| 149 | + final int @Nullable [] outerMethodParamLvtIndices, |
| 150 | + final @Nullable LambdaClosure lambdaClosure, |
| 151 | + final @Nullable LocalClassClosure localClassClosure) |
| 152 | + throws IOException { |
| 153 | + if (shouldSkipMapping(method, parentClass, superClass, lambdaCalls)) { |
| 154 | + return; |
| 155 | + } |
| 156 | + if (method.isConstructor()) { |
| 157 | + this.handleConstructorMappings(method, parentClass, methodMapping, localClassClosure); |
| 158 | + } else { |
| 159 | + if (outerMethodParamLvtIndices == null) { |
| 160 | + this.checkMappings(method, methodMapping, 0, i -> i + (method.isStatic() ? 0 : 1)); |
| 161 | + } else { |
| 162 | + final int descriptorOffset; |
| 163 | + if (!method.isStatic() && outerMethodParamLvtIndices.length > 0 && outerMethodParamLvtIndices[0] == 0) { |
| 164 | + descriptorOffset = outerMethodParamLvtIndices.length - 1; |
| 165 | + } else { |
| 166 | + descriptorOffset = outerMethodParamLvtIndices.length; |
| 167 | + } |
| 168 | + this.checkMappings( |
| 169 | + method, methodMapping, descriptorOffset, i -> i + (method.isStatic() ? 0 : 1), lambdaClosure); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private void reportMissingParam( |
| 175 | + final MethodData method, |
| 176 | + final @Nullable MethodMapping methodMapping, |
| 177 | + final int descriptorParamOffset, |
| 178 | + final IntUnaryOperator descriptorToMappingOffset, |
| 179 | + final @Nullable LambdaClosure lambdaClosure) { |
| 180 | + final ClassData parentClass = method.parentClass(); |
| 181 | + final StringBuilder msg = new StringBuilder("\t#%s %s".formatted(method.name(), method.descriptorText())); |
| 182 | + if (lambdaClosure != null) { |
| 183 | + final MethodData containingMethod = lambdaClosure.getContainingMethod(); |
| 184 | + msg.append("%n\t\tLambda Source: %s#%s %s" |
| 185 | + .formatted( |
| 186 | + containingMethod.parentClass().equals(parentClass) |
| 187 | + ? "" |
| 188 | + : containingMethod.parentClass().name(), |
| 189 | + containingMethod.name(), |
| 190 | + containingMethod.descriptorText())); |
| 191 | + } |
| 192 | + for (int i = descriptorParamOffset; i < method.params().size(); i++) { |
| 193 | + final int paramIdx = i; |
| 194 | + final int lastIdxOfDot = method.param(i).toString().lastIndexOf('.'); |
| 195 | + msg.append("%n\t\t%s\t%-50s\t%s" |
| 196 | + .formatted( |
| 197 | + i, |
| 198 | + method.param(i).toString().substring(lastIdxOfDot + 1), |
| 199 | + Optional.ofNullable(methodMapping) |
| 200 | + .flatMap(m -> m.getParameterMapping(descriptorToMappingOffset.applyAsInt(paramIdx))) |
| 201 | + .map(Mapping::getDeobfuscatedName) |
| 202 | + .orElse("<<MISSING>>"))); |
| 203 | + } |
| 204 | + this.data.computeIfAbsent(parentClass, ignored -> new ArrayList<>()).add(msg.toString()); |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public String generate() { |
| 209 | + final StringBuilder output = new StringBuilder(); |
| 210 | + this.data.entrySet().stream() |
| 211 | + .sorted(Comparator.comparing(entry -> entry.getKey().name())) |
| 212 | + .forEach(entry -> { |
| 213 | + output.append("Missing param mappings in %s, Method Count: %s, Param Count: TODO\n" |
| 214 | + .formatted(entry.getKey().name(), entry.getValue().size())); |
| 215 | + entry.getValue().forEach(msg -> output.append(msg).append("\n")); |
| 216 | + }); |
| 217 | + return output.toString(); |
| 218 | + } |
| 219 | +} |
0 commit comments