Skip to content

Commit a911762

Browse files
add report system (#21)
Co-authored-by: MiniDigger | Martin <admin@benndorf.dev>
1 parent 133f0b9 commit a911762

16 files changed

Lines changed: 318 additions & 48 deletions

File tree

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies {
1010
api(libs.checker)
1111

1212
implementation(projects.codebookLvt)
13+
api(projects.codebookReports)
1314

1415
implementation(libs.guice)
1516
implementation(libs.inject)

codebook-cli/src/main/java/io/papermc/codebook/cli/Main.java

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@
3232
import io.papermc.codebook.config.CodeBookUriResource;
3333
import io.papermc.codebook.config.CodeBookVersionInput;
3434
import io.papermc.codebook.exceptions.UserErrorException;
35+
import io.papermc.codebook.report.ReportType;
36+
import io.papermc.codebook.report.Reports;
3537
import io.papermc.codebook.util.Downloader;
3638
import java.io.IOException;
3739
import java.net.URI;
3840
import java.nio.file.Files;
3941
import java.nio.file.Path;
4042
import java.util.List;
43+
import java.util.Set;
4144
import java.util.concurrent.Callable;
4245
import java.util.function.Function;
4346
import java.util.zip.ZipFile;
@@ -59,6 +62,39 @@
5962
usageHelpAutoWidth = true)
6063
public final class Main implements Callable<Integer> {
6164

65+
@CommandLine.ArgGroup(multiplicity = "1", exclusive = false)
66+
private @Nullable ReportOptions reports;
67+
68+
static final class ReportOptions {
69+
70+
@CommandLine.Option(
71+
names = "--reports-dir",
72+
paramLabel = "<reports-dir>",
73+
description = "Parent directory to output any generated reports",
74+
hidden = true)
75+
private @Nullable Path reportsDir;
76+
77+
@CommandLine.ArgGroup(multiplicity = "1")
78+
private SelectedReports selectedReports;
79+
80+
static final class SelectedReports {
81+
82+
@CommandLine.Option(
83+
names = "--report",
84+
paramLabel = "<report>",
85+
description = "Set of report types to generate",
86+
hidden = true)
87+
private Set<ReportType> reports;
88+
89+
@CommandLine.Option(
90+
names = "--all-reports",
91+
paramLabel = "<all-reports>",
92+
description = "Generate all reports",
93+
hidden = true)
94+
private boolean allReports;
95+
}
96+
}
97+
6298
@CommandLine.ArgGroup(
6399
multiplicity = "1",
64100
heading = "%n%nThe remapper must be an executable tiny-remapper jar. "
@@ -86,13 +122,6 @@ static final class RemapperOptions {
86122
description =
87123
"A download URL for the executable AutoRenamingTool jar to use for the remapping process.")
88124
private @Nullable URI remapperUri;
89-
90-
@CommandLine.Option(
91-
names = "--log-missing-lvt-suggestions",
92-
paramLabel = "<log-missing-lvt-suggestions>",
93-
description = "Include a report of missing lvt name suggestions in the remapping log",
94-
hidden = true)
95-
private boolean logMissingLvtSuggestions;
96125
}
97126

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

453+
@Nullable Reports reports = null;
454+
if (this.reports != null && this.reports.reportsDir != null) {
455+
final Set<ReportType> reportsToGenerate;
456+
if (this.reports.selectedReports.allReports) {
457+
reportsToGenerate = Set.of(ReportType.values());
458+
} else {
459+
reportsToGenerate = this.reports.selectedReports.reports;
460+
}
461+
reports = new Reports(this.reports.reportsDir, reportsToGenerate);
462+
}
463+
424464
return CodeBookContext.builder()
425465
.remapperJar(remapper)
426466
.mappings(mappings)
@@ -430,7 +470,7 @@ private CodeBookContext createContext() {
430470
.outputJar(this.outputJar)
431471
.overwrite(this.forceWrite)
432472
.input(input)
433-
.logMissingLvtSuggestions(this.remapper.logMissingLvtSuggestions)
473+
.reports(reports)
434474
.build();
435475
}
436476

codebook-lvt/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55

66
dependencies {
77
implementation(platform(libs.hypo.platform))
8+
implementation(projects.codebookReports)
89

910
api(libs.checker)
1011
api(libs.bundles.hypo.base)

codebook-lvt/src/main/java/io/papermc/codebook/lvt/LvtNamer.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,15 @@
3434
import dev.denwav.hypo.model.data.MethodData;
3535
import dev.denwav.hypo.model.data.types.JvmType;
3636
import dev.denwav.hypo.model.data.types.PrimitiveType;
37+
import io.papermc.codebook.report.Reports;
3738
import java.io.IOException;
3839
import java.util.ArrayList;
3940
import java.util.Arrays;
4041
import java.util.HashSet;
4142
import java.util.LinkedHashSet;
4243
import java.util.List;
43-
import java.util.Map;
4444
import java.util.Optional;
4545
import java.util.Set;
46-
import java.util.concurrent.ConcurrentHashMap;
47-
import java.util.concurrent.atomic.AtomicInteger;
4846
import org.cadixdev.lorenz.MappingSet;
4947
import org.cadixdev.lorenz.model.Mapping;
5048
import org.cadixdev.lorenz.model.MethodMapping;
@@ -61,12 +59,10 @@ public class LvtNamer {
6159
private final LvtTypeSuggester lvtTypeSuggester;
6260
private final RootLvtSuggester lvtAssignSuggester;
6361

64-
public final Map<String, AtomicInteger> missedNameSuggestions = new ConcurrentHashMap<>();
65-
66-
public LvtNamer(final HypoContext context, final MappingSet mappings) throws IOException {
62+
public LvtNamer(final HypoContext context, final MappingSet mappings, final Reports reports) throws IOException {
6763
this.mappings = mappings;
6864
this.lvtTypeSuggester = new LvtTypeSuggester(context);
69-
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, this.missedNameSuggestions);
65+
this.lvtAssignSuggester = new RootLvtSuggester(context, this.lvtTypeSuggester, reports);
7066
}
7167

7268
public void processClass(final AsmClassData classData) throws IOException {

codebook-lvt/src/main/java/io/papermc/codebook/lvt/RootLvtSuggester.java

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@
4949
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext;
5050
import io.papermc.codebook.lvt.suggestion.numbers.MthRandomSuggester;
5151
import io.papermc.codebook.lvt.suggestion.numbers.RandomSourceSuggester;
52+
import io.papermc.codebook.report.Reports;
53+
import io.papermc.codebook.report.type.MissingMethodLvtSuggestion;
5254
import java.io.IOException;
5355
import java.util.List;
54-
import java.util.Map;
5556
import java.util.Set;
56-
import java.util.concurrent.atomic.AtomicInteger;
5757
import org.checkerframework.checker.nullness.qual.Nullable;
5858
import org.objectweb.asm.Opcodes;
5959
import org.objectweb.asm.tree.AbstractInsnNode;
@@ -81,18 +81,15 @@ public final class RootLvtSuggester extends AbstractModule implements LvtSuggest
8181

8282
private final HypoContext hypoContext;
8383
private final LvtTypeSuggester lvtTypeSuggester;
84-
public final Map<String, AtomicInteger> missedNameSuggestions;
84+
private final Injector injector;
8585
private final List<? extends LvtSuggester> suggesters;
8686

8787
public RootLvtSuggester(
88-
final HypoContext hypoContext,
89-
final LvtTypeSuggester lvtTypeSuggester,
90-
final Map<String, AtomicInteger> missedNameSuggestions) {
88+
final HypoContext hypoContext, final LvtTypeSuggester lvtTypeSuggester, final Reports reports) {
9189
this.hypoContext = hypoContext;
9290
this.lvtTypeSuggester = lvtTypeSuggester;
93-
this.missedNameSuggestions = missedNameSuggestions;
94-
final Injector injector = Guice.createInjector(this);
95-
this.suggesters = SUGGESTERS.stream().map(injector::getInstance).toList();
91+
this.injector = Guice.createInjector(this, reports);
92+
this.suggesters = SUGGESTERS.stream().map(this.injector::getInstance).toList();
9693
}
9794

9895
@Override
@@ -207,11 +204,9 @@ public static String determineFinalName(final String suggestedName, final Set<St
207204
return suggestion;
208205
}
209206
}
210-
this.missedNameSuggestions
211-
.computeIfAbsent(
212-
call.data().name() + "," + insn.owner().name() + "," + insn.node().desc,
213-
(k) -> new AtomicInteger(0))
214-
.incrementAndGet();
207+
this.injector
208+
.getInstance(MissingMethodLvtSuggestion.class)
209+
.reportMissingMethodLvtSuggestion(call.data(), insn.node());
215210
return null;
216211
}
217212

codebook-reports/build.gradle.kts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
plugins {
2+
`java-library`
3+
id("codebook")
4+
}
5+
6+
dependencies {
7+
implementation(platform(libs.hypo.platform))
8+
9+
api(libs.checker)
10+
11+
implementation(libs.bundles.hypo.impl)
12+
implementation(libs.bundles.asm)
13+
14+
implementation(libs.guice)
15+
implementation(libs.inject)
16+
implementation(libs.guava)
17+
}
18+
19+
publishing {
20+
publications {
21+
codebook {
22+
pom {
23+
name.set("codebook-reports")
24+
description.set("Codebook reports for PaperMC")
25+
}
26+
}
27+
}
28+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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;
24+
25+
public enum ReportType {
26+
MISSING_METHOD_LVT_SUGGESTION,
27+
;
28+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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;
24+
25+
import com.google.inject.AbstractModule;
26+
import io.papermc.codebook.report.type.MissingMethodLvtSuggestion;
27+
import io.papermc.codebook.report.type.Report;
28+
import java.io.IOException;
29+
import java.nio.file.Files;
30+
import java.nio.file.Path;
31+
import java.util.Locale;
32+
import java.util.Map;
33+
import java.util.Map.Entry;
34+
import java.util.Set;
35+
36+
public class Reports extends AbstractModule {
37+
38+
@SuppressWarnings({"DataFlowIssue"})
39+
public static final Reports NOOP = new Reports(null, Set.of()) {
40+
@Override
41+
public void generateReports() {
42+
// NO-OP
43+
}
44+
45+
@Override
46+
protected void configure() {
47+
// NO-OP
48+
}
49+
};
50+
51+
private final Path reportsDir;
52+
private final Set<ReportType> typesToGenerate;
53+
private final Map<ReportType, Report> reports;
54+
55+
public Reports(final Path reportsDir, final Set<ReportType> typesToGenerate) {
56+
this.reportsDir = reportsDir;
57+
this.typesToGenerate = typesToGenerate;
58+
this.reports = Map.of(ReportType.MISSING_METHOD_LVT_SUGGESTION, new MissingMethodLvtSuggestion());
59+
}
60+
61+
public void generateReports() throws IOException {
62+
Files.createDirectories(this.reportsDir);
63+
for (final Entry<ReportType, Report> entry : this.reports.entrySet()) {
64+
if (this.typesToGenerate.contains(entry.getKey())) {
65+
final Path reportPath =
66+
this.reportsDir.resolve(entry.getKey().name().toLowerCase(Locale.ENGLISH) + ".txt");
67+
Files.writeString(reportPath, entry.getValue().generate());
68+
}
69+
}
70+
}
71+
72+
@Override
73+
protected void configure() {
74+
this.reports.values().forEach(this::bindReport);
75+
}
76+
77+
@SuppressWarnings("unchecked")
78+
private <R extends Report> void bindReport(final R report) {
79+
this.bind((Class<R>) report.getClass()).toInstance(report);
80+
}
81+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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.model.data.MethodData;
26+
import java.util.Comparator;
27+
import java.util.Map;
28+
import java.util.concurrent.ConcurrentHashMap;
29+
import java.util.concurrent.atomic.AtomicInteger;
30+
import org.objectweb.asm.tree.MethodInsnNode;
31+
32+
public class MissingMethodLvtSuggestion implements Report {
33+
34+
private static final Comparator<Map.Entry<String, AtomicInteger>> COMPARATOR =
35+
Comparator.comparing(e -> e.getValue().get());
36+
37+
private final Map<String, AtomicInteger> data = new ConcurrentHashMap<>();
38+
39+
public void reportMissingMethodLvtSuggestion(final MethodData method, final MethodInsnNode insn) {
40+
this.data
41+
.computeIfAbsent(method.name() + "," + insn.owner + "," + insn.desc, (k) -> new AtomicInteger(0))
42+
.incrementAndGet();
43+
}
44+
45+
@Override
46+
public String generate() {
47+
final StringBuilder output = new StringBuilder();
48+
this.data.entrySet().stream()
49+
.sorted(COMPARATOR.reversed())
50+
.forEach(s -> output.append("missed: %s -- %s times%n".formatted(s.getKey(), s.getValue())));
51+
return output.toString();
52+
}
53+
}

0 commit comments

Comments
 (0)