Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jan 14, 2024
1 parent 72b73ee commit cb0dc3c
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 26 deletions.
67 changes: 54 additions & 13 deletions codebook-lvt/src/main/java/io/papermc/codebook/lvt/LvtNamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,24 @@ public void fillNames(final MethodData method) throws IOException {
}
}

private void checkMappings(final MethodData method, final @Nullable MethodMapping methodMapping, final int descriptorParamOffset, final IntUnaryOperator descriptorToMappingOffset) throws IOException {
private void checkMappings(
final MethodData method,
final @Nullable MethodMapping methodMapping,
final int descriptorParamOffset,
final IntUnaryOperator descriptorToMappingOffset) {
this.checkMappings(method, methodMapping, descriptorParamOffset, descriptorToMappingOffset, null);
}
private void checkMappings(final MethodData method, final @Nullable MethodMapping methodMapping, final int descriptorParamOffset, final IntUnaryOperator descriptorToMappingOffset, final @Nullable LambdaClosure lambdaClosure) throws IOException {

private void checkMappings(
final MethodData method,
final @Nullable MethodMapping methodMapping,
final int descriptorParamOffset,
final IntUnaryOperator descriptorToMappingOffset,
final @Nullable LambdaClosure lambdaClosure) {
if (method.params().size() == descriptorParamOffset) {
return;
}
if (methodMapping == null || (method.params().size() - descriptorParamOffset > methodMapping.getParameterMappings().size())) { // != should be sufficient here, but hypo's CopyMappingsDown for constructors incorrectly applies if (methodMapping == null || (method.params().size() - descriptorParamOffset > methodMapping.getParameterMappings().size())) { // != should be sufficient here, but hypo's CopyMappingsDown for constructors incorrectly applies mappings to implicit constructor params
if (methodMapping == null || (method.params().size() - descriptorParamOffset > methodMapping.getParameterMappings().size())) { // != should have be sufficient here, but hypo's CopyMappingsDown for constructors incorrectly applies mappings to implicit constructor params
this.reports.getInstance(MissingMethodParam.class).reportMissingParam(method, methodMapping, descriptorParamOffset, descriptorToMappingOffset, lambdaClosure);
}
}
Expand Down Expand Up @@ -207,13 +217,22 @@ private void fillNames0(final MethodData method) throws IOException {
final boolean skipMapping;
if (method.name().startsWith("access$") && method.isSynthetic()) { // never in source
skipMapping = true;
} else if (method.name().startsWith("lambda$") && method.isSynthetic() && (lambdaCalls == null || lambdaCalls.isEmpty())) { // lambdas that had their use stripped by mojang
} else if (method.name().startsWith("lambda$")
&& method.isSynthetic()
&& (lambdaCalls == null || lambdaCalls.isEmpty())) { // lambdas that had their use stripped by mojang
skipMapping = true;
} else if (superClass != null && superClass.name().equals("java/lang/Enum") && method.name().equals("valueOf") && method.descriptorText().startsWith("(Ljava/lang/String;)")) { // created by the compiler
} else if (superClass != null
&& superClass.name().equals("java/lang/Enum")
&& method.name().equals("valueOf")
&& method.descriptorText().startsWith("(Ljava/lang/String;)")) { // created by the compiler
skipMapping = true;
} else if (parentClass.is(ClassKind.RECORD) && method.name().equals("equals") && method.descriptorText().equals("(Ljava/lang/Object;)Z")) { // created by the compiler
} else if (parentClass.is(ClassKind.RECORD)
&& method.name().equals("equals")
&& method.descriptorText().equals("(Ljava/lang/Object;)Z")) { // created by the compiler
skipMapping = true;
} else if (method.isSynthetic() && method.get(HypoHydration.SYNTHETIC_TARGET) != null) { // don't trust isBridge, apparently it's not always accurate
} else if (method.isSynthetic()
&& method.get(HypoHydration.SYNTHETIC_TARGET)
!= null) { // don't trust isBridge, apparently it's not always accurate
skipMapping = true;
} else {
skipMapping = false;
Expand All @@ -222,14 +241,29 @@ private void fillNames0(final MethodData method) throws IOException {
if (!skipMapping) {
if (method.isConstructor()) {
if (parentClass.is(ClassKind.ENUM)) {
this.checkMappings(method, methodMapping.orElse(null), 2, i -> i + 1); // enum constructors include name and ordinal
this.checkMappings(
method,
methodMapping.orElse(null),
2,
i -> i + 1); // enum constructors include name and ordinal
} else {
if (!ANONYMOUS_CLASS.matcher(parentClass.name()).matches()) { // anonymous classes cannot have constructors in source
if (!ANONYMOUS_CLASS
.matcher(parentClass.name())
.matches()) { // anonymous classes cannot have constructors in source
if (parentClass.outerClass() != null) {
if (localClassClosure == null) {
this.checkMappings(method, methodMapping.orElse(null), parentClass.isStaticInnerClass() ? 0 : 1, i -> i + 1);
this.checkMappings(
method,
methodMapping.orElse(null),
parentClass.isStaticInnerClass() ? 0 : 1,
i -> i + 1);
} else {
this.checkMappings(method, methodMapping.orElse(null), (parentClass.isStaticInnerClass() ? 0 : 1) + localClassClosure.getParamLvtIndices().length, i -> i + 1);
this.checkMappings(
method,
methodMapping.orElse(null),
(parentClass.isStaticInnerClass() ? 0 : 1)
+ localClassClosure.getParamLvtIndices().length,
i -> i + 1);
}
} else {
this.checkMappings(method, methodMapping.orElse(null), 0, i -> i + 1);
Expand All @@ -241,12 +275,19 @@ private void fillNames0(final MethodData method) throws IOException {
this.checkMappings(method, methodMapping.orElse(null), 0, i -> i + (method.isStatic() ? 0 : 1));
} else {
final int descriptorOffset;
if (!method.isStatic() && outerMethodParamLvtIndices.length > 0 && outerMethodParamLvtIndices[0] == 0) {
if (!method.isStatic()
&& outerMethodParamLvtIndices.length > 0
&& outerMethodParamLvtIndices[0] == 0) {
descriptorOffset = outerMethodParamLvtIndices.length - 1;
} else {
descriptorOffset = outerMethodParamLvtIndices.length;
}
this.checkMappings(method, methodMapping.orElse(null), descriptorOffset, i -> i + (method.isStatic() ? 0 : 1), lambdaClosure);
this.checkMappings(
method,
methodMapping.orElse(null),
descriptorOffset,
i -> i + (method.isStatic() ? 0 : 1),
lambdaClosure);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import static io.papermc.codebook.lvt.LvtUtil.toJvmType;

import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import dev.denwav.hypo.core.HypoContext;
import dev.denwav.hypo.model.data.ClassData;
Expand All @@ -51,7 +50,6 @@
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
import io.papermc.codebook.lvt.suggestion.numbers.MthRandomSuggester;
import io.papermc.codebook.lvt.suggestion.numbers.RandomSourceSuggester;
import io.papermc.codebook.report.Reports;
import io.papermc.codebook.report.type.MissingMethodLvtSuggestion;
import java.io.IOException;
import java.util.List;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@

public enum ReportType {
MISSING_METHOD_LVT_SUGGESTION,
MISSING_METHOD_PARAM
;
MISSING_METHOD_PARAM;
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ public Reports(final Path reportsDir, final Set<ReportType> typesToGenerate) {
this.typesToGenerate = typesToGenerate;
this.reports = Map.of(
ReportType.MISSING_METHOD_LVT_SUGGESTION, new MissingMethodLvtSuggestion(),
ReportType.MISSING_METHOD_PARAM, new MissingMethodParam()
);
ReportType.MISSING_METHOD_PARAM, new MissingMethodParam());
}

public void generateReports() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
/*
* codebook is a remapper utility for the PaperMC project.
*
* Copyright (c) 2023 Kyle Wood (DenWav)
* Contributors
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 3 only, no later versions.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package io.papermc.codebook.report.type;

import dev.denwav.hypo.hydrate.generic.LambdaClosure;
Expand All @@ -18,17 +40,35 @@ public class MissingMethodParam implements Report {

private final Map<ClassData, List<String>> data = new ConcurrentHashMap<>();

public void reportMissingParam(final MethodData method, final @Nullable MethodMapping methodMapping, final int descriptorParamOffset, final IntUnaryOperator descriptorToMappingOffset, final @Nullable LambdaClosure lambdaClosure) {
public void reportMissingParam(
final MethodData method,
final @Nullable MethodMapping methodMapping,
final int descriptorParamOffset,
final IntUnaryOperator descriptorToMappingOffset,
final @Nullable LambdaClosure lambdaClosure) {
final ClassData parentClass = method.parentClass();
final StringBuilder msg = new StringBuilder("\t#%s %s".formatted( method.name(), method.descriptorText()));
final StringBuilder msg = new StringBuilder("\t#%s %s".formatted(method.name(), method.descriptorText()));
if (lambdaClosure != null) {
final MethodData containingMethod = lambdaClosure.getContainingMethod();
msg.append("%n\t\tLambda Source: %s#%s %s".formatted(containingMethod.parentClass().equals(parentClass) ? "" : containingMethod.parentClass().name(), containingMethod.name(), containingMethod.descriptorText()));
msg.append("%n\t\tLambda Source: %s#%s %s"
.formatted(
containingMethod.parentClass().equals(parentClass)
? ""
: containingMethod.parentClass().name(),
containingMethod.name(),
containingMethod.descriptorText()));
}
for (int i = descriptorParamOffset; i < method.params().size(); i++) {
final int paramIdx = i;
final int lastIdxOfDot = method.param(i ).toString().lastIndexOf('.');
msg.append("%n\t\t%s\t%-50s\t%s".formatted(i, method.param(i).toString().substring(lastIdxOfDot + 1), Optional.ofNullable(methodMapping).flatMap(m -> m.getParameterMapping(descriptorToMappingOffset.applyAsInt(paramIdx))).map(Mapping::getDeobfuscatedName).orElse("<<MISSING>>")));
final int lastIdxOfDot = method.param(i).toString().lastIndexOf('.');
msg.append("%n\t\t%s\t%-50s\t%s"
.formatted(
i,
method.param(i).toString().substring(lastIdxOfDot + 1),
Optional.ofNullable(methodMapping)
.flatMap(m -> m.getParameterMapping(descriptorToMappingOffset.applyAsInt(paramIdx)))
.map(Mapping::getDeobfuscatedName)
.orElse("<<MISSING>>")));
}
this.data.computeIfAbsent(parentClass, ignored -> new ArrayList<>()).add(msg.toString());
}
Expand All @@ -39,7 +79,8 @@ public String generate() {
this.data.entrySet().stream()
.sorted(Comparator.comparing(entry -> entry.getKey().name()))
.forEach(entry -> {
output.append("Missing param mappings in %s, Method Count: %s, Param Count: TODO\n".formatted(entry.getKey().name(), entry.getValue().size()));
output.append("Missing param mappings in %s, Method Count: %s, Param Count: TODO\n"
.formatted(entry.getKey().name(), entry.getValue().size()));
entry.getValue().forEach(msg -> output.append(msg).append("\n"));
});
return output.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;

import com.google.inject.Guice;
import dev.denwav.hypo.asm.AsmClassData;
import dev.denwav.hypo.asm.AsmMethodData;
import dev.denwav.hypo.core.HypoContext;
Expand Down Expand Up @@ -100,7 +101,8 @@ void setup() throws Exception {

when(this.randomSourceClass.name()).thenReturn(RANDOM_SOURCE_TYPE.asInternalName());

this.suggester = new RootLvtSuggester(context, new LvtTypeSuggester(context), this.reports);
this.suggester =
new RootLvtSuggester(context, new LvtTypeSuggester(context), Guice.createInjector(this.reports));
}

@ParameterizedTest
Expand Down

0 comments on commit cb0dc3c

Please sign in to comment.