-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Handle fluent getters for primitive return types
- Loading branch information
1 parent
9fbf232
commit 53e9d67
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
codebook-lvt/src/main/java/io/papermc/codebook/lvt/suggestion/FluentGetterSuggester.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.papermc.codebook.lvt.suggestion; | ||
|
||
import dev.denwav.hypo.model.data.types.PrimitiveType; | ||
import io.papermc.codebook.lvt.suggestion.context.ContainerContext; | ||
import io.papermc.codebook.lvt.suggestion.context.method.MethodCallContext; | ||
import io.papermc.codebook.lvt.suggestion.context.method.MethodInsnContext; | ||
import java.io.IOException; | ||
import java.util.function.IntPredicate; | ||
import org.checkerframework.checker.nullness.qual.Nullable; | ||
import org.objectweb.asm.Opcodes; | ||
import org.objectweb.asm.tree.AbstractInsnNode; | ||
import org.objectweb.asm.tree.InsnList; | ||
|
||
public class FluentGetterSuggester implements LvtSuggester { | ||
|
||
// 3 instructions, load "this" local var, getfield, return - TODO maybe if there is a CAST, | ||
private static final IntPredicate[] OPCODES_IN_ORDER = new IntPredicate[] { | ||
i -> i == Opcodes.ALOAD, i -> i == Opcodes.GETFIELD, i -> i >= Opcodes.IRETURN && i <= Opcodes.RETURN}; | ||
|
||
@Override | ||
public @Nullable String suggestFromMethod(final MethodCallContext call, final MethodInsnContext insn, final ContainerContext container) throws IOException { | ||
// I think it's best to only work with primitive types here, as other types should already have names | ||
// and this dramatically cuts down on the number of methods analyzed because we aren't filtering by | ||
// method name | ||
if (!(call.data().returnType() instanceof PrimitiveType) | ||
|| !call.data().params().isEmpty()) { | ||
return null; | ||
} | ||
int opcodeIndex = 0; | ||
final InsnList instructions = call.node().instructions; | ||
if (instructions.size() == 0) { | ||
return null; | ||
} | ||
for (final AbstractInsnNode methodInsn : instructions) { | ||
if (methodInsn.getOpcode() == -1) { | ||
continue; | ||
} | ||
if (opcodeIndex == OPCODES_IN_ORDER.length) { | ||
break; // matched the correct order | ||
} | ||
if (OPCODES_IN_ORDER[opcodeIndex].test(methodInsn.getOpcode())) { | ||
opcodeIndex++; | ||
} else { | ||
return null; | ||
} | ||
} | ||
if (call.data().isStatic()) { // limit static matches | ||
if ("java/lang/System".equals(insn.node().owner) && "currentTimeMillis".equals(insn.node().name)) { | ||
return "currentTimeMillis"; | ||
} | ||
} else { | ||
return call.data().name(); | ||
} | ||
return null; | ||
} | ||
} |