Skip to content

Commit

Permalink
feat: also name new* and read*
Browse files Browse the repository at this point in the history
  • Loading branch information
MiniDigger committed Jul 25, 2023
1 parent 8a9d107 commit 3bd278f
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
52 changes: 52 additions & 0 deletions src/main/java/io/papermc/codebook/lvt/LvtAssignmentSuggester.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ private LvtAssignmentSuggester() {}
return suggested;
}

suggested = suggestNameFromNew(methodName, insn);
if (suggested != null) {
return suggested;
}

suggested = suggestNameFromRead(methodName);
if (suggested != null) {
return suggested;
}

suggested = suggestNameFromLine(methodName);
if (suggested != null) {
return suggested;
Expand Down Expand Up @@ -111,6 +121,48 @@ public static String suggestNameFromRecord(final String methodName) {
}
}

private static @Nullable String suggestNameFromNew(final String methodName, MethodInsnNode insn) {
if (!methodName.startsWith("new") || methodName.equals("new")) {
return null;
}

final @Nullable String result = switch (insn.owner) {
case "com/google/common/collect/Lists" -> "list";
case "com/google/common/collect/Maps" -> "map";
case "com/google/common/collect/Sets" -> "set";
default -> null;
};
if (result != null) {
return result;
}

final String baseName = methodName.substring(3);

if (Character.isUpperCase(baseName.charAt(0))) {
return Character.toLowerCase(baseName.charAt(0)) + baseName.substring(1);
} else {
// if the name doesn't follow the typical `newName` scheme we can't be confident it's
// really a "getter" method, so don't use it for a name
return null;
}
}

private static @Nullable String suggestNameFromRead(final String methodName) {
if (!methodName.startsWith("read") || methodName.equals("read")) {
return null;
}

final String baseName = methodName.substring(4);

if (Character.isUpperCase(baseName.charAt(0))) {
return Character.toLowerCase(baseName.charAt(0)) + baseName.substring(1);
} else {
// if the name doesn't follow the typical `readName` scheme we can't be confident it's
// really a "getter" method, so don't use it for a name
return null;
}
}

private static @Nullable String suggestNameFromLine(final String methodName) {
if (methodName.equals("readLine")) {
return "line";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,24 @@ getOrCreateSomething,io/paper/Paper,()Ljava/lang/String;,something
# as
asString,io/paper/Paper,()Ljava/lang/String;,string
asLevel,io/paper/paper,()Lnet/minecraft/Level;,level
# new
newArrayList,com/google/common/collect/Lists,()Ljava/util/ArrayList;,list
newHashMap,com/google/common/collect/Maps,()Ljava/util/HashMap;,map
newHashSet,com/google/common/collect/Sets,()Ljava/util/HashSet;,set
newLinkedHashMap,com/google/common/collect/Maps,()Ljava/util/LinkedHashMap;,map
newArrayListWithExpectedSize,com/google/common/collect/Lists,(I)Ljava/util/ArrayList;,list
newTree,some/tree/Maker,()Lcool/Tree;,tree
# read
readStruct,net/minecraft/nbt/TagParser,()Lnet/minecraft/nbt/CompoundTag;,struct
readBoolean,com/mojang/brigadier/StringReader,()Z,boolean
readUnquotedString,com/mojang/brigadier/StringReader,()Ljava/lang/String;,unquotedString
# readLine
readLine,some/stream,()Ljava/lang/String;,line
# strings
split,java/lang/String,()Ljava/lang/String;,parts
split,com/google/common/base/Splitter,()Ljava/lang/String;,parts
splitToStream,com/google/common/base/Splitter,()Ljava/util/stream/Stream;,parts
splitToList,com/google/common/base/Splitter,()Ljava/util/List;,parts
repeat,java/lang/String,()Ljava/lang/String;,repeated
indexOf,java/lang/String,()I,index
lastIndexOf,java/lang/String,()I,index
Expand Down

0 comments on commit 3bd278f

Please sign in to comment.