Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
Machine-Maker committed Jan 16, 2024
1 parent 738baa6 commit 53c1b7b
Show file tree
Hide file tree
Showing 23 changed files with 247 additions and 64 deletions.
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.lvt;

import com.google.inject.Injector;
Expand Down Expand Up @@ -28,10 +50,18 @@ public class InstructionUnwrapper {
new Method("java/lang/Character", "charValue", "()C")));

private static final MethodMatcher UNWRAP_AFTER_CAST = new MethodMatcher(Set.of(
new Method("net/minecraft/world/level/block/state/BlockState", "getValue", "(Lnet/minecraft/world/level/block/state/properties/Property;)Ljava/lang/Comparable;"),
new Method("net/minecraft/world/level/storage/loot/LootContext", "getParamOrNull", "(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;"),
new Method("net/minecraft/world/level/storage/loot/LootParams$Builder", "getOptionalParameter", "(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;")
));
new Method(
"net/minecraft/world/level/block/state/BlockState",
"getValue",
"(Lnet/minecraft/world/level/block/state/properties/Property;)Ljava/lang/Comparable;"),
new Method(
"net/minecraft/world/level/storage/loot/LootContext",
"getParamOrNull",
"(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;"),
new Method(
"net/minecraft/world/level/storage/loot/LootParams$Builder",
"getOptionalParameter",
"(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;")));

public InstructionUnwrapper(final Reports reports, final Injector reportsInjector) {
this.reports = reports;
Expand All @@ -55,10 +85,11 @@ public InstructionUnwrapper(final Reports reports, final Injector reportsInjecto
return null;
}


if (prev.getOpcode() == Opcodes.CHECKCAST) {
final AbstractInsnNode tempPrev = prev.getPrevious();
if (tempPrev.getOpcode() == Opcodes.INVOKEVIRTUAL || tempPrev.getOpcode() == Opcodes.INVOKEINTERFACE || tempPrev.getOpcode() == Opcodes.INVOKESTATIC) {
if (tempPrev.getOpcode() == Opcodes.INVOKEVIRTUAL
|| tempPrev.getOpcode() == Opcodes.INVOKEINTERFACE
|| tempPrev.getOpcode() == Opcodes.INVOKESTATIC) {
final MethodInsnNode methodInsn = (MethodInsnNode) tempPrev;
if (UNWRAP_AFTER_CAST.matches(methodInsn)) {
prev = methodInsn;
Expand All @@ -81,9 +112,10 @@ private MethodMatcher(final Set<Method> methods) {
}

boolean matches(final AbstractInsnNode insn) {
return insn instanceof final MethodInsnNode methodInsnNode && this.methodNames.contains(methodInsnNode.name) && this.methods.stream().anyMatch(m -> m.matches(methodInsnNode));
return insn instanceof final MethodInsnNode methodInsnNode
&& this.methodNames.contains(methodInsnNode.name)
&& this.methods.stream().anyMatch(m -> m.matches(methodInsnNode));
}

}

private record Method(String owner, String name, String desc, boolean itf) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ public LvtNamer(final HypoContext context, final MappingSet mappings, final Repo
this.lvtTypeSuggester = new LvtTypeSuggester(context);
this.reports = reports;
this.reportsInjector = Guice.createInjector(reports);
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, this.reports, this.reportsInjector);
this.lvtAssignSuggester =
new RootLvtSuggester(context, this.lvtTypeSuggester, this.reports, this.reportsInjector);
}

public void processClass(final AsmClassData classData) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public final class RootLvtSuggester extends AbstractModule implements LvtSuggest
private final InstructionUnwrapper unwrapper;

public RootLvtSuggester(
final HypoContext hypoContext, final LvtTypeSuggester lvtTypeSuggester, final Reports reports, final Injector reportsInjector) {
final HypoContext hypoContext,
final LvtTypeSuggester lvtTypeSuggester,
final Reports reports,
final Injector reportsInjector) {
this.hypoContext = hypoContext;
this.lvtTypeSuggester = lvtTypeSuggester;
this.injector = reportsInjector.createChildInjector(this);
Expand Down Expand Up @@ -143,7 +146,8 @@ public String suggestName(
}

if (assignmentNode != null) {
final @Nullable String suggestedName = this.suggestNameFromFirstAssignment(ContainerContext.from(parent), new AssignmentContext(assignmentNode, lvt));
final @Nullable String suggestedName = this.suggestNameFromFirstAssignment(
ContainerContext.from(parent), new AssignmentContext(assignmentNode, lvt));
if (suggestedName != null) {
return determineFinalName(suggestedName, scopedNames);
}
Expand Down Expand Up @@ -176,8 +180,8 @@ public static String determineFinalName(final String suggestedName, final Set<St
}
}

private @Nullable String suggestNameFromFirstAssignment(final ContainerContext container, final AssignmentContext assignment)
throws IOException {
private @Nullable String suggestNameFromFirstAssignment(
final ContainerContext container, final AssignmentContext assignment) throws IOException {
final @Nullable AbstractInsnNode prev = this.unwrapper.unwrapFromAssignment(assignment.assignmentNode());
if (prev == null) {
return null;
Expand All @@ -202,7 +206,9 @@ public static String determineFinalName(final String suggestedName, final Set<St
final @Nullable String suggestion = this.suggestFromMethod(
MethodCallContext.create(method),
MethodInsnContext.create(owner, methodInsnNode),
container, assignment, new SuggesterContext(this.hypoContext, this.lvtTypeSuggester));
container,
assignment,
new SuggesterContext(this.hypoContext, this.lvtTypeSuggester));
if (suggestion == null) {
this.injector
.getInstance(MissingMethodLvtSuggestion.class)
Expand All @@ -213,7 +219,11 @@ public static String determineFinalName(final String suggestedName, final Set<St

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
@Nullable String suggestion;
for (final LvtSuggester delegate : this.suggesters) {
Expand All @@ -227,7 +237,11 @@ public static String determineFinalName(final String suggestedName, final Set<St

@Override
public @Nullable String suggestFromField(
final FieldCallContext call, final FieldInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final FieldCallContext call,
final FieldInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
@Nullable String suggestion;
for (final LvtSuggester delegate : this.suggesters) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,24 @@ public class ComplexGetSuggester implements LvtSuggester {
private static final StaticFieldEntry LOOT_CONTEXT_PARAM = new StaticFieldEntry(
Set.of(
"net/minecraft/world/level/storage/loot/LootContext",
"net/minecraft/world/level/storage/loot/LootParams$Builder"
),
"net/minecraft/world/level/storage/loot/LootParams$Builder"),
Set.of(
Map.entry("getParamOrNull", "(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;"),
Map.entry("getOptionalParameter", "(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;")
),
Set.of(
"Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;"
),
"Param"
);
Map.entry(
"getParamOrNull",
"(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;"),
Map.entry(
"getOptionalParameter",
"(Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;)Ljava/lang/Object;")),
Set.of("Lnet/minecraft/world/level/storage/loot/parameters/LootContextParam;"),
"Param");

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
final MethodInsnNode node = insn.node();
if (BLOCK_STATE_PROPERTY.test(node)) {
Expand All @@ -93,19 +96,23 @@ boolean test(final MethodInsnNode node) {
return matches(this.owners, this.methods, node);
}

String transform(final MethodInsnNode node, final AssignmentContext assignment, final SuggesterContext suggester) throws IOException {
String transform(
final MethodInsnNode node, final AssignmentContext assignment, final SuggesterContext suggester)
throws IOException {
final AbstractInsnNode prev = node.getPrevious();
if (prev instanceof final FieldInsnNode fieldInsnNode
&& fieldInsnNode.getOpcode() == Opcodes.GETSTATIC
&& this.fieldTypes.contains(fieldInsnNode.desc)) {
return staticFinalFieldNameToLocalName(fieldInsnNode.name) + (this.suffix == null ? "" : this.suffix);
}
// always use the type instead of any other suggesters
return suggester.typeSuggester().suggestNameFromType(toJvmType(assignment.lvt().desc)) + (this.suffix == null ? "" : this.suffix);
return suggester.typeSuggester().suggestNameFromType(toJvmType(assignment.lvt().desc))
+ (this.suffix == null ? "" : this.suffix);
}
}

private static boolean matches(final Set<String> owners, final Set<Entry<String, String>> methods, final MethodInsnNode node) {
private static boolean matches(
final Set<String> owners, final Set<Entry<String, String>> methods, final MethodInsnNode node) {
return owners.contains(node.owner)
&& methods.stream()
.anyMatch(e ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ public class FluentGetterSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
// I think it's best to only work with primitive types here, as other types should already have names
// and this dramatically cuts down on the number of methods analyzed because we aren't filtering by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ public class GenericSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester) {
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester) {
return switch (call.data().name()) {
case "hashCode" -> "hashCode";
case "size" -> "size";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,21 @@
public interface LvtSuggester {

default @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
return null;
}

default @Nullable String suggestFromField(
final FieldCallContext call, final FieldInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final FieldCallContext call,
final FieldInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public class MathSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
final String methodName = call.data().name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public class NewPrefixSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
final String methodName = call.data().name();
if (!hasPrefix(methodName, "new")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public class PositionsSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
if ("net/minecraft/core/SectionPos".equals(insn.owner().name())) {
return suggestNameForSectionPos(container.node(), call.data(), insn.node());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public class RecordComponentSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester) {
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester) {
if (insn.owner().is(ClassKind.RECORD)) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ public class SingleVerbBooleanSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
if (call.data().returnType() != PrimitiveType.BOOLEAN) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,11 @@ public class SingleVerbSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester) {
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester) {
final String methodName = call.data().name();

final @Nullable String prefix = tryMatchPrefix(methodName, SINGLE_VERB_PREFIXES);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@ public class StringSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester)
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester)
throws IOException {
final String methodName = call.data().name();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public class VerbPrefixBooleanSuggester implements LvtSuggester {

@Override
public @Nullable String suggestFromMethod(
final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container, final AssignmentContext assignment, final SuggesterContext suggester) {
final MethodCallContext call,
final MethodInsnContext insn,
final ContainerContext container,
final AssignmentContext assignment,
final SuggesterContext suggester) {
if (call.data().returnType() != PrimitiveType.BOOLEAN) {
return null;
}
Expand Down
Loading

0 comments on commit 53c1b7b

Please sign in to comment.