Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add report system #21

Merged
merged 2 commits into from
Jan 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
api(libs.checker)

implementation(projects.codebookLvt)
api(projects.codebookReports)

implementation(libs.guice)
implementation(libs.inject)
Expand Down
56 changes: 48 additions & 8 deletions codebook-cli/src/main/java/io/papermc/codebook/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,15 @@
import io.papermc.codebook.config.CodeBookUriResource;
import io.papermc.codebook.config.CodeBookVersionInput;
import io.papermc.codebook.exceptions.UserErrorException;
import io.papermc.codebook.report.ReportType;
import io.papermc.codebook.report.Reports;
import io.papermc.codebook.util.Downloader;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.function.Function;
import java.util.zip.ZipFile;
Expand All @@ -59,6 +62,39 @@
usageHelpAutoWidth = true)
public final class Main implements Callable<Integer> {

@CommandLine.ArgGroup(multiplicity = "1", exclusive = false)
private @Nullable ReportOptions reports;

static final class ReportOptions {

@CommandLine.Option(
names = "--reports-dir",
paramLabel = "<reports-dir>",
description = "Parent directory to output any generated reports",
hidden = true)
private @Nullable Path reportsDir;

@CommandLine.ArgGroup(multiplicity = "1")
private SelectedReports selectedReports;

static final class SelectedReports {

@CommandLine.Option(
names = "--report",
paramLabel = "<report>",
description = "Set of report types to generate",
hidden = true)
private Set<ReportType> reports;

@CommandLine.Option(
names = "--all-reports",
paramLabel = "<all-reports>",
description = "Generate all reports",
hidden = true)
private boolean allReports;
}
}

@CommandLine.ArgGroup(
multiplicity = "1",
heading = "%n%nThe remapper must be an executable tiny-remapper jar. "
Expand Down Expand Up @@ -86,13 +122,6 @@ static final class RemapperOptions {
description =
"A download URL for the executable AutoRenamingTool jar to use for the remapping process.")
private @Nullable URI remapperUri;

@CommandLine.Option(
names = "--log-missing-lvt-suggestions",
paramLabel = "<log-missing-lvt-suggestions>",
description = "Include a report of missing lvt name suggestions in the remapping log",
hidden = true)
private boolean logMissingLvtSuggestions;
}

@CommandLine.ArgGroup(
Expand Down Expand Up @@ -421,6 +450,17 @@ private CodeBookContext createContext() {
return new Coords(c.constantsCoords, "constants", null, this.unpickMavenBaseUrl);
});

@Nullable Reports reports = null;
if (this.reports != null && this.reports.reportsDir != null) {
final Set<ReportType> reportsToGenerate;
if (this.reports.selectedReports.allReports) {
reportsToGenerate = Set.of(ReportType.values());
} else {
reportsToGenerate = this.reports.selectedReports.reports;
}
reports = new Reports(this.reports.reportsDir, reportsToGenerate);
}

return CodeBookContext.builder()
.remapperJar(remapper)
.mappings(mappings)
Expand All @@ -430,7 +470,7 @@ private CodeBookContext createContext() {
.outputJar(this.outputJar)
.overwrite(this.forceWrite)
.input(input)
.logMissingLvtSuggestions(this.remapper.logMissingLvtSuggestions)
.reports(reports)
.build();
}

Expand Down
1 change: 1 addition & 0 deletions codebook-lvt/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ plugins {

dependencies {
implementation(platform(libs.hypo.platform))
implementation(projects.codebookReports)

api(libs.checker)
api(libs.bundles.hypo.base)
Expand Down
10 changes: 3 additions & 7 deletions codebook-lvt/src/main/java/io/papermc/codebook/lvt/LvtNamer.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,15 @@
import dev.denwav.hypo.model.data.MethodData;
import dev.denwav.hypo.model.data.types.JvmType;
import dev.denwav.hypo.model.data.types.PrimitiveType;
import io.papermc.codebook.report.Reports;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.cadixdev.lorenz.MappingSet;
import org.cadixdev.lorenz.model.Mapping;
import org.cadixdev.lorenz.model.MethodMapping;
Expand All @@ -61,12 +59,10 @@ public class LvtNamer {
private final LvtTypeSuggester lvtTypeSuggester;
private final RootLvtSuggester lvtAssignSuggester;

public final Map<String, AtomicInteger> missedNameSuggestions = new ConcurrentHashMap<>();

public LvtNamer(final HypoContext context, final MappingSet mappings) throws IOException {
public LvtNamer(final HypoContext context, final MappingSet mappings, final Reports reports) throws IOException {
this.mappings = mappings;
this.lvtTypeSuggester = new LvtTypeSuggester(context);
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, this.missedNameSuggestions);
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, reports);
}

public void processClass(final AsmClassData classData) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@
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;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.tree.AbstractInsnNode;
Expand Down Expand Up @@ -81,18 +81,15 @@ public final class RootLvtSuggester extends AbstractModule implements LvtSuggest

private final HypoContext hypoContext;
private final LvtTypeSuggester lvtTypeSuggester;
public final Map<String, AtomicInteger> missedNameSuggestions;
private final Injector injector;
private final List<? extends LvtSuggester> suggesters;

public RootLvtSuggester(
final HypoContext hypoContext,
final LvtTypeSuggester lvtTypeSuggester,
final Map<String, AtomicInteger> missedNameSuggestions) {
final HypoContext hypoContext, final LvtTypeSuggester lvtTypeSuggester, final Reports reports) {
this.hypoContext = hypoContext;
this.lvtTypeSuggester = lvtTypeSuggester;
this.missedNameSuggestions = missedNameSuggestions;
final Injector injector = Guice.createInjector(this);
this.suggesters = SUGGESTERS.stream().map(injector::getInstance).toList();
this.injector = Guice.createInjector(this, reports);
this.suggesters = SUGGESTERS.stream().map(this.injector::getInstance).toList();
}

@Override
Expand Down Expand Up @@ -207,11 +204,9 @@ public static String determineFinalName(final String suggestedName, final Set<St
return suggestion;
}
}
this.missedNameSuggestions
.computeIfAbsent(
call.data().name() + "," + insn.owner().name() + "," + insn.node().desc,
(k) -> new AtomicInteger(0))
.incrementAndGet();
this.injector
.getInstance(MissingMethodLvtSuggestion.class)
.reportMissingMethodLvtSuggestion(call.data(), insn.node());
return null;
}

Expand Down
28 changes: 28 additions & 0 deletions codebook-reports/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
plugins {
`java-library`
id("codebook")
}

dependencies {
implementation(platform(libs.hypo.platform))

api(libs.checker)

implementation(libs.bundles.hypo.impl)
implementation(libs.bundles.asm)

implementation(libs.guice)
implementation(libs.inject)
implementation(libs.guava)
}

publishing {
publications {
codebook {
pom {
name.set("codebook-reports")
description.set("Codebook reports for PaperMC")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

public enum ReportType {
MISSING_METHOD_LVT_SUGGESTION,
;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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;

import com.google.inject.AbstractModule;
import io.papermc.codebook.report.type.MissingMethodLvtSuggestion;
import io.papermc.codebook.report.type.Report;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class Reports extends AbstractModule {

@SuppressWarnings({"DataFlowIssue"})
public static final Reports NOOP = new Reports(null, Set.of()) {
@Override
public void generateReports() {
// NO-OP
}

@Override
protected void configure() {
// NO-OP
}
};

private final Path reportsDir;
private final Set<ReportType> typesToGenerate;
private final Map<ReportType, Report> reports;

public Reports(final Path reportsDir, final Set<ReportType> typesToGenerate) {
this.reportsDir = reportsDir;
this.typesToGenerate = typesToGenerate;
this.reports = Map.of(ReportType.MISSING_METHOD_LVT_SUGGESTION, new MissingMethodLvtSuggestion());
}

public void generateReports() throws IOException {
Files.createDirectories(this.reportsDir);
for (final Entry<ReportType, Report> entry : this.reports.entrySet()) {
if (this.typesToGenerate.contains(entry.getKey())) {
final Path reportPath =
this.reportsDir.resolve(entry.getKey().name().toLowerCase(Locale.ENGLISH) + ".txt");
Files.writeString(reportPath, entry.getValue().generate());
}
}
}

@Override
protected void configure() {
this.reports.values().forEach(this::bindReport);
}

@SuppressWarnings("unchecked")
private <R extends Report> void bindReport(final R report) {
this.bind((Class<R>) report.getClass()).toInstance(report);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.model.data.MethodData;
import java.util.Comparator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import org.objectweb.asm.tree.MethodInsnNode;

public class MissingMethodLvtSuggestion implements Report {

private static final Comparator<Map.Entry<String, AtomicInteger>> COMPARATOR =
Comparator.comparing(e -> e.getValue().get());

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

public void reportMissingMethodLvtSuggestion(final MethodData method, final MethodInsnNode insn) {
this.data
.computeIfAbsent(method.name() + "," + insn.owner + "," + insn.desc, (k) -> new AtomicInteger(0))
.incrementAndGet();
}

@Override
public String generate() {
final StringBuilder output = new StringBuilder();
this.data.entrySet().stream()
.sorted(COMPARATOR.reversed())
.forEach(s -> output.append("missed: %s -- %s times%n".formatted(s.getKey(), s.getValue())));
return output.toString();
}
}
Loading