Skip to content

Commit 08d74ec

Browse files
authored
Merge pull request #20 from supersaiyansubtlety/validate-constant-proposer-names
prevent `ConstantFieldNameFinder` from finding invalid java identifiers
2 parents 86958d2 + 8aacedd commit 08d74ec

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

src/main/java/org/quiltmc/enigma_plugin/index/constant_fields/ConstantFieldNameFinder.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ private static boolean isInit(AbstractInsnNode insn) {
5353
return insn.getOpcode() == INVOKESPECIAL && ((MethodInsnNode) insn).name.equals("<init>");
5454
}
5555

56+
private static boolean isValidJavaIdentifier(String id) {
57+
if (id.isEmpty() || !Character.isJavaIdentifierStart(id.charAt(0))) {
58+
return false;
59+
}
60+
61+
for (int i = 1; i < id.length(); i++) {
62+
if (!Character.isJavaIdentifierPart(id.charAt(i))) {
63+
return false;
64+
}
65+
}
66+
67+
return true;
68+
}
69+
5670
private static String processIdentifierPath(String name) {
5771
if (name == null) {
5872
return null;
@@ -192,7 +206,7 @@ private void findNamesInInitializers(String clazz, List<MethodNode> initializers
192206
String s = processIdentifierPath(name);
193207
var fieldName = CasingUtil.toSafeScreamingSnakeCase(s);
194208

195-
if (fieldName == null || fieldName.isEmpty()) {
209+
if (fieldName == null || !isValidJavaIdentifier(fieldName)) {
196210
continue;
197211
}
198212

src/test/java/org/quiltmc/enigma_plugin/NameProposalTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public void testConstantFieldNames() {
136136
assertProposal("ONE", new FieldEntry(classEntry, "g", desc));
137137
assertProposal("TWO", new FieldEntry(classEntry, "h", desc));
138138
assertProposal("THREE", new FieldEntry(classEntry, "i", desc));
139+
// don't propose 123ABC for INVALID
140+
assertNotProposed(field(classEntry, "j", desc.toString()));
139141

140142
var class2Entry = new ClassEntry("com/a/a/a");
141143

src/testInputs/java/com/example/field_names/ClassTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class ClassTest {
1010
public static final Something ONE = create(new Something("One"));
1111
public static final Something TWO = create(new Something(new Something("Two")));
1212
public static final Something THREE = create(new Something(new Something(new Something("Three"))));
13+
public static final Something INVALID = create("123ABC");
1314

1415
private static Something create(String value) {
1516
return new Something(value);

0 commit comments

Comments
 (0)