Skip to content

Commit

Permalink
Cleanup the constant fields name finder (renamed from enum fields) (#9)
Browse files Browse the repository at this point in the history
* Rename enum_fields to constant_fields

* Move some code to `#stringToUpperSnakeCase`

* Organize imports

* Rename remaining "enum fields" instances

* Move some code to instance methods & fields

* More cleaning up

* Search field names recursively

* Search names and fields passed to constructors

* Remove some duplicate code

* Fix an exception when reloading the jar

* Fix linking to unnamed fields

* Fix names for enum fields with overridden methods

* Tweak snake case conversion

* Fix some unwanted cases
  • Loading branch information
IotaBread authored Jan 10, 2024
1 parent 2b6c133 commit 3ca9975
Show file tree
Hide file tree
Showing 10 changed files with 398 additions and 261 deletions.
2 changes: 1 addition & 1 deletion src/main/java/org/quiltmc/enigma_plugin/Arguments.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
*/
public class Arguments {
public static final String DISABLE_RECORDS = "disable_records";
public static final String DISABLE_ENUM_FIELDS = "disable_enum_fields";
public static final String DISABLE_CONSTANT_FIELDS = "disable_constant_fields";
public static final String DISABLE_EQUALS = "disable_equals";
public static final String DISABLE_LOGGER = "disable_logger";
public static final String DISABLE_CONSTRUCTOR_PARAMS = "disable_constructor_params";
Expand Down
22 changes: 12 additions & 10 deletions src/main/java/org/quiltmc/enigma_plugin/index/JarIndexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,30 +24,30 @@
import org.objectweb.asm.tree.ClassNode;
import org.quiltmc.enigma_plugin.Arguments;
import org.quiltmc.enigma_plugin.QuiltEnigmaPlugin;
import org.quiltmc.enigma_plugin.index.enum_fields.EnumFieldsIndex;
import org.quiltmc.enigma_plugin.index.constant_fields.ConstantFieldIndex;
import org.quiltmc.enigma_plugin.index.simple_type_single.SimpleTypeSingleIndex;

import java.util.List;
import java.util.Set;

public class JarIndexer implements JarIndexerService, Opcodes {
private final RecordIndex recordIndex = new RecordIndex();
private final EnumFieldsIndex enumFieldsIndex = new EnumFieldsIndex();
private final ConstantFieldIndex constantFieldIndex = new ConstantFieldIndex();
private final CodecIndex codecIndex = new CodecIndex();
private final LoggerIndex loggerIndex = new LoggerIndex();
private final SimpleTypeSingleIndex simpleTypeSingleIndex = new SimpleTypeSingleIndex();
private final ConstructorParametersIndex constructorParametersIndex = new ConstructorParametersIndex();
private final GetterSetterIndex getterSetterIndex = new GetterSetterIndex();
private boolean disableRecordIndexing = false;
private boolean disableEnumFieldsIndexing = false;
private boolean disableConstantFieldIndexing = false;
private boolean disableCodecsIndexing = false;
private boolean disableLoggerIndexing = false;
private boolean disableConstructorParametersIndexing = false;
private boolean disableGetterSetterIndexing = false;

public JarIndexer withContext(EnigmaServiceContext<JarIndexerService> context) {
this.disableRecordIndexing = Arguments.isDisabled(context, Arguments.DISABLE_RECORDS);
this.disableEnumFieldsIndexing = Arguments.isDisabled(context, Arguments.DISABLE_ENUM_FIELDS);
this.disableConstantFieldIndexing = Arguments.isDisabled(context, Arguments.DISABLE_CONSTANT_FIELDS);
this.disableCodecsIndexing = Arguments.isDisabled(context, Arguments.DISABLE_CODECS);
this.disableLoggerIndexing = Arguments.isDisabled(context, Arguments.DISABLE_LOGGER);
this.disableConstructorParametersIndexing = Arguments.isDisabled(context, Arguments.DISABLE_CONSTRUCTOR_PARAMS);
Expand All @@ -64,6 +64,8 @@ public JarIndexer withContext(EnigmaServiceContext<JarIndexerService> context) {

@Override
public void acceptJar(Set<String> scope, ClassProvider classProvider, JarIndex jarIndex) {
this.constantFieldIndex.clear();

for (String className : scope) {
ClassNode node = classProvider.get(className);
if (node != null) {
Expand All @@ -72,8 +74,8 @@ public void acceptJar(Set<String> scope, ClassProvider classProvider, JarIndex j
}
}

if (!this.disableEnumFieldsIndexing) {
this.enumFieldsIndex.findFieldNames();
if (!this.disableConstantFieldIndexing) {
this.constantFieldIndex.findFieldNames();
}

this.simpleTypeSingleIndex.dropCache();
Expand All @@ -88,8 +90,8 @@ private void visitClassNode(ClassNode node) {
}
}

if (!this.disableEnumFieldsIndexing) {
this.enumFieldsIndex.visitClassNode(node);
if (!this.disableConstantFieldIndexing) {
this.constantFieldIndex.visitClassNode(node);
}

if (!this.disableCodecsIndexing) {
Expand All @@ -113,8 +115,8 @@ public RecordIndex getRecordIndex() {
return this.recordIndex;
}

public EnumFieldsIndex getEnumFieldsIndex() {
return this.enumFieldsIndex;
public ConstantFieldIndex getConstantFieldIndex() {
return this.constantFieldIndex;
}

public CodecIndex getCodecIndex() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.quiltmc.enigma_plugin.index.enum_fields;
package org.quiltmc.enigma_plugin.index.constant_fields;

import org.quiltmc.enigma.api.translation.representation.entry.FieldEntry;
import org.objectweb.asm.tree.ClassNode;
Expand All @@ -29,7 +29,7 @@
import java.util.Map;
import java.util.Set;

public class EnumFieldsIndex implements Index {
public class ConstantFieldIndex implements Index {
private final Map<String, Set<String>> enumFields = new HashMap<>();
private final Map<String, List<MethodNode>> staticInitializers = new HashMap<>();
private Map<FieldEntry, String> fieldNames;
Expand All @@ -39,7 +39,7 @@ public void visitClassNode(ClassNode node) {
for (FieldNode field : node.fields) {
if ((field.access & ACC_ENUM) != 0) {
if (!this.enumFields.computeIfAbsent(node.name, k -> new HashSet<>()).add(field.name + ":" + field.desc)) {
throw new IllegalStateException("Found a duplicate enum field with name \"" + field.name + "\"");
throw new IllegalStateException("Found a duplicate enum field with name \"" + field.name + "\" in class " + node.name);
}
}
}
Expand All @@ -53,12 +53,18 @@ public void visitClassNode(ClassNode node) {

public void findFieldNames() {
try {
this.fieldNames = new FieldNameFinder().findNames(this);
this.fieldNames = new ConstantFieldNameFinder().findNames(this);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public void clear() {
this.enumFields.clear();
this.staticInitializers.clear();
this.fieldNames = null;
}

public boolean hasName(FieldEntry field) {
return this.fieldNames.containsKey(field);
}
Expand Down
Loading

0 comments on commit 3ca9975

Please sign in to comment.