Skip to content

Commit c6da79f

Browse files
Make get*Component* accept Class<C> instead of Class<? extends C>
Add some magic to eliminate the need of writing `XXX.class` in some situations Some notes: Since a component should exactly match its class, Class<C> is used instead of Class<? extends C>
1 parent a8066e0 commit c6da79f

11 files changed

Lines changed: 79 additions & 33 deletions

File tree

extensions/parchment/src/main/java/cn/maxpixel/mcdecompiler/mapping/parchment/ParchmentMappingGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private static void writeClasses(ClassifiedMapping<PairedMapping> mappings, Json
101101
}
102102

103103
private static void writeParams(JsonWriter writer, @NotNull PairedMapping method) throws IOException {
104-
var params = method.getComponent(LocalVariableTable.Paired.class);
104+
LocalVariableTable.Paired params = method.getComponent();
105105
if (params != null && !params.isEmpty()) {
106106
writer.name(KEY_PARAMETERS).beginArray();
107107
for (var indexes = params.getLocalVariableIndexes().iterator(); indexes.hasNext(); ) {
@@ -112,7 +112,7 @@ private static void writeParams(JsonWriter writer, @NotNull PairedMapping method
112112
.jsonValue(Integer.toString(i))
113113
.name(KEY_NAME)
114114
.value(param.unmappedName);
115-
Documented doc = param.getComponent(Documented.class);
115+
Documented doc = param.getComponent();
116116
if (doc != null && !doc.contents.isEmpty()) {
117117
writer.name(KEY_JAVADOC).value(doc.getContentString());
118118
}
@@ -123,7 +123,7 @@ private static void writeParams(JsonWriter writer, @NotNull PairedMapping method
123123
}
124124

125125
private static void writeDoc(PairedMapping m, JsonWriter writer) throws IOException {
126-
Documented doc = m.getComponent(Documented.class);
126+
Documented doc = m.getComponent();
127127
if (doc != null && !doc.contents.isEmpty()) {
128128
writer.name(KEY_JAVADOC)
129129
.beginArray();

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/Mapping.java

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,84 @@ protected Mapping(@NotNull Component @NotNull ... components) {
5151
*/
5252
protected Mapping() {}
5353

54+
/**
55+
* Gets the component of the given type if it is present.<br>
56+
* For the {@link Owned} component, it is recommended to use {@link #getOwned()} instead of this method
57+
*
58+
* @param component The given component type. Cannot be null
59+
* @return The component if exists, or {@code null}
60+
*/
61+
@SuppressWarnings("unchecked")
62+
public <C extends Component> C getComponent(@NotNull Class<C> component) {
63+
return (C) components.get(Objects.requireNonNull(component));
64+
}
65+
5466
/**
5567
* Gets the component of given type if it is present.<br>
5668
* For the {@link Owned} component, it is recommended to use {@link #getOwned()} instead of this method
5769
*
58-
* @param component Given component type. Cannot be null
70+
* @param type Used to obtain the component type. You needn't pass anything.
5971
* @return The component if exists, or {@code null}
72+
* @see #getComponent(Class)
6073
*/
6174
@SuppressWarnings("unchecked")
62-
public <C extends Component> C getComponent(@NotNull Class<? extends C> component) {
63-
return (C) components.get(component);
75+
public <C extends Component> C getComponent(C... type) {
76+
return getComponent((Class<C>) type.getClass().getComponentType());
6477
}
6578

6679
/**
67-
* Gets the component of given type if it is present, otherwise create a new component<br>
80+
* Gets the component of the given type if it is present, otherwise creates a new component<br>
6881
*
69-
* @param component Given component type. Cannot be null
82+
* @param component The given component type. Cannot be null
7083
* @return The component if exists, or the newly created component
7184
*/
7285
@SuppressWarnings("unchecked")
73-
public <C extends Component> @NotNull C getOrCreateComponent(@NotNull Class<? extends C> component, @NotNull Supplier<? extends C> factory) {
74-
var value = components.get(component);
86+
public <C extends Component> @NotNull C getOrCreateComponent(@NotNull Class<C> component, @NotNull Supplier<C> factory) {
87+
var value = components.get(Objects.requireNonNull(component));
7588
if (value == null) {
7689
value = Objects.requireNonNull(factory.get());
90+
if (value.getClass() != component) throw new IllegalArgumentException("Type mismatch");
7791
components.put(component, value);
7892
}
7993
return (C) value;
8094
}
8195

8296
/**
83-
* Gets the component of given type.
97+
* Gets the component of the given type if it is present, otherwise creates a new component<br>
98+
*
99+
* @param type Used to obtain the component type. You needn't pass anything.
100+
* @return The component if exists, or the newly created component
101+
*/
102+
@SuppressWarnings("unchecked")
103+
public <C extends Component> @NotNull C getOrCreateComponent(@NotNull Supplier<C> factory, C... type) {
104+
return getOrCreateComponent((Class<C>) type.getClass().getComponentType(), factory);
105+
}
106+
107+
/**
108+
* Gets the component of the given type.
84109
* <p>
85110
* For the {@link Owned} component, it is recommended to use {@link #getOwned()} instead of this method
86111
*
87-
* @param component Given component type. Cannot be null
88-
* @return The component
112+
* @param component The given component type. Cannot be null
113+
* @return The component optional
89114
*/
90-
public <C extends Component> @NotNull Optional<C> getComponentOptional(@NotNull Class<? extends C> component) {
115+
public <C extends Component> @NotNull Optional<C> getComponentOptional(@NotNull Class<C> component) {
91116
return Optional.ofNullable(getComponent(component));
92117
}
93118

119+
/**
120+
* Gets the component of the given type.
121+
* <p>
122+
* For the {@link Owned} component, it is recommended to use {@link #getOwned()} instead of this method
123+
*
124+
* @param type Used to obtain the component type. You needn't pass anything.
125+
* @return The component optional
126+
*/
127+
@SuppressWarnings("unchecked")
128+
public <C extends Component> @NotNull Optional<C> getComponentOptional(C... type) {
129+
return getComponentOptional((Class<C>) type.getClass().getComponentType());
130+
}
131+
94132
/**
95133
* Gets the {@link Owned} component if it is present
96134
*

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/NamespacedMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public NamespacedMapping(String[] namespaces, String name, Component... componen
196196
@SuppressWarnings("unchecked")
197197
@Override
198198
public Owned<NamespacedMapping> getOwned() {
199-
return getComponent(Owned.class);
199+
return getComponent();
200200
}
201201

202202
/**

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/PairedMapping.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public PairedMapping() {}
103103
@SuppressWarnings("unchecked")
104104
@Override
105105
public Owned<PairedMapping> getOwned() {
106-
return getComponent(Owned.class);
106+
return getComponent();
107107
}
108108

109109
/**

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/PdmeMappingGenerator.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, Ma
7676
synchronized (lines) {
7777
lines.add(String.join(PARA, "Def", unmappedName, method.mappedName, NIL, NIL, doc));
7878
}
79-
var lvt = method.getComponent(LocalVariableTable.Paired.class);
79+
LocalVariableTable.Paired lvt = method.getComponent();
8080
if (lvt != null) {
8181
IntIterator it = lvt.getLocalVariableIndexes().iterator();
8282
while (it.hasNext()) {
@@ -123,7 +123,7 @@ private static String nilWhenBlank(String s) {
123123
}
124124

125125
private static String getDoc(PairedMapping m) {
126-
var doc = m.getComponent(Documented.class);
126+
Documented doc = m.getComponent();
127127
return doc != null ? doc.getContentString() : "";
128128
}
129129

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/ProguardMappingGenerator.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ public ObjectList<String> generate(ClassifiedMapping<PairedMapping> mappings, Ma
7878
if (end > 1) args.deleteCharAt(args.length() - 1);
7979
String ret = NamingUtil.descriptor2Java(mappedDesc.substring(end + 1));
8080
if (method.hasComponent(LineNumber.class)) {
81-
LineNumber lineNumber = method.getComponent(LineNumber.class);
81+
LineNumber lineNumber = method.getComponent();
8282
lines.add(" " + lineNumber.startLineNumber + ':' + lineNumber.endLineNumber + ':' +
8383
ret + ' ' + method.mappedName + '(' + args + ") -> " + method.unmappedName);
8484
} else {

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/TinyV2MappingGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
5757
}
5858
for (ClassMapping<NamespacedMapping> cls : mappings.classes) {
5959
lines.add("c\t" + NamingUtil.concatNamespaces(namespaces, cls.mapping::getName, "\t"));
60-
var classDoc = cls.mapping.getComponent(Documented.class);
60+
Documented classDoc = cls.mapping.getComponent();
6161
if (classDoc != null) {
6262
String content = classDoc.getContentString();
6363
if (!content.isBlank()) lines.add("\tc\t" + TinyUtil.escape(content));
@@ -66,7 +66,7 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
6666
String desc = MappingUtils.Namespaced.checkTiny(namespace0, cls, field);
6767
synchronized (lines) {
6868
lines.add("\tf\t" + desc + '\t' + NamingUtil.concatNamespaces(namespaces, field::getName, "\t"));
69-
var fieldDoc = field.getComponent(Documented.class);
69+
Documented fieldDoc = field.getComponent();
7070
if (fieldDoc != null) {
7171
String doc = fieldDoc.getContentString();
7272
if (!doc.isBlank()) lines.add("\t\tc\t" + TinyUtil.escape(doc));
@@ -77,13 +77,13 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
7777
String desc = MappingUtils.Namespaced.checkTiny(namespace0, cls, method);
7878
synchronized (lines) {
7979
lines.add("\tm\t" + desc + '\t' + NamingUtil.concatNamespaces(namespaces, method::getName, "\t"));
80-
var methodDoc = method.getComponent(Documented.class);
80+
Documented methodDoc = method.getComponent();
8181
if (methodDoc != null) {
8282
String doc = methodDoc.getContentString();
8383
if (!doc.isBlank()) lines.add("\t\tc\t" + TinyUtil.escape(doc));
8484
}
8585
if (method.hasComponent(LocalVariableTable.Namespaced.class)) {
86-
LocalVariableTable.Namespaced lvt = method.getComponent(LocalVariableTable.Namespaced.class);
86+
LocalVariableTable.Namespaced lvt = method.getComponent();
8787
boolean omittedThis = method.hasComponent(StaticIdentifiable.class) &&
8888
!method.getComponent(StaticIdentifiable.class).isStatic;
8989
lvt.getLocalVariableIndexes().forEach(index -> {
@@ -93,7 +93,7 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
9393
return MappingUtils.isStringNotBlank(name) ? name : "";
9494
}, "\t");
9595
lines.add("\t\tp\t" + index + '\t' + names);
96-
var paramDoc = localVariable.getComponent(Documented.class);
96+
Documented paramDoc = localVariable.getComponent();
9797
if (paramDoc != null) {
9898
String doc = paramDoc.getContentString();
9999
if (!doc.isBlank()) lines.add("\t\t\tc\t" + TinyUtil.escape(doc));

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/generator/TsrgV2MappingGenerator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
6565
synchronized (lines) {
6666
genDescriptorLine(lines, namespace0, method, NamingUtil.concatNamespaces(namespaces,
6767
method::getName, " "));
68-
var si = method.getComponent(StaticIdentifiable.class);
68+
StaticIdentifiable si = method.getComponent();
6969
if (si != null && si.isStatic) lines.add("\t\tstatic");
7070
if (method.hasComponent(LocalVariableTable.Namespaced.class)) {
71-
LocalVariableTable.Namespaced lvt = method.getComponent(LocalVariableTable.Namespaced.class);
71+
LocalVariableTable.Namespaced lvt = method.getComponent();
7272
lvt.getLocalVariableIndexes().forEach(index -> {
7373
String names = NamingUtil.concatNamespaces(namespaces, namespace -> {
7474
String name = lvt.getLocalVariable(index).getName(namespace);
@@ -89,7 +89,7 @@ public ObjectList<String> generate(ClassifiedMapping<NamespacedMapping> mappings
8989
}
9090

9191
private static void genDescriptorLine(ObjectArrayList<String> lines, String namespace0, NamespacedMapping method, String names) {
92-
Descriptor.Namespaced desc = method.getComponent(Descriptor.Namespaced.class);
92+
Descriptor.Namespaced desc = method.getComponent();
9393
if (!namespace0.equals(desc.descriptorNamespace)) throw new IllegalArgumentException();
9494
int i = names.indexOf(' ');
9595
lines.add('\t' + names.substring(0, i + 1) + desc.descriptor + names.substring(i));

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/remapper/ClassifiedMappingRemapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ else if (mapping.hasComponent(Descriptor.Mapped.class))
136136

137137
public String getUnmappedDesc(Mapping mapping, String unmappedNamespace, Object2ObjectOpenHashMap<String, UniDescriptorRemapper> map,
138138
ClassifiedMapping<NamespacedMapping> mappings) {
139-
var desc = mapping.getComponent(Descriptor.Namespaced.class);
139+
Descriptor.Namespaced desc = mapping.getComponent();
140140
if (desc != null) return unmappedNamespace.equals(desc.descriptorNamespace) ? desc.descriptor : map
141141
.computeIfAbsent(desc.descriptorNamespace, (String n) -> new UniDescriptorRemapper(genMappingsByNamespaceMap(mappings.classes, n)))
142142
.unmapMethodDesc(desc.descriptor);

modules/mapping-api/src/main/java/cn/maxpixel/mcdecompiler/mapping/util/MappingUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static String checkTiny(String namespace0, ClassMapping<NamespacedMapping
8484
if (!mapping.hasComponent(Owned.class) || !mapping.hasComponent(Descriptor.Namespaced.class))
8585
throw new UnsupportedOperationException();
8686
checkOwner(mapping.getOwned(), cls);
87-
Descriptor.Namespaced desc = mapping.getComponent(Descriptor.Namespaced.class);
87+
Descriptor.Namespaced desc = mapping.getComponent();
8888
if (!namespace0.equals(desc.descriptorNamespace)) throw new IllegalArgumentException("Descriptor namespace mismatch");
8989
return desc.descriptor;
9090
}

0 commit comments

Comments
 (0)