Skip to content

Instantly share code, notes, and snippets.

@Frontear
Last active October 11, 2021 20:21
Show Gist options
  • Select an option

  • Save Frontear/b69879b679eca1e6b0b91ea2bdcf12dc to your computer and use it in GitHub Desktop.

Select an option

Save Frontear/b69879b679eca1e6b0b91ea2bdcf12dc to your computer and use it in GitHub Desktop.
A python script designed to output jvm descriptors with respect to the standards set by mixin.
"""
Implement the descriptor spec defined in Java 16: https://docs.oracle.com/javase/specs/jvms/se16/html/jvms-4.html#jvms-4.3
It is given respect to mixin standards, found at: https://github.com/SpongePowered/Mixin
package com.github.frontear;
class Main {
private int num;
private String str;
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
---
Lcom/github/frontear/Main;num:I
Lcom/github/frontear/Main;str:Ljava/lang/String;
Lcom/github/frontear/Main;getNum()I
Lcom/github/frontear/Main;setNum(I)V
Lcom/github/frontear/Main;getStr()Ljava/lang/String;
Lcom/github/frontear/Main;setStr(Ljava/lang/String;)V
"""
PRIMITIVES = {
"boolean": "Z",
"double": "D",
"float": "F",
"short": "S",
"byte": "B",
"char": "C",
"long": "J",
"void": "V",
"int": "I",
}
def type_desc(name, arr_dim = 0):
type = None
if (n := name.lower()) in PRIMITIVES:
type = PRIMITIVES[n]
else:
type = f"L{name.replace('.', '/')};"
if arr_dim > 0:
type = arr_dim * "[" + type
return type
def class_desc(package, name):
return type_desc(f"{package}.{name}")
def field_desc(class_desc, name, type_desc):
return f"{class_desc}{name}:{type_desc}"
def method_desc(class_desc, name, type_desc, *param_desc):
return f"{class_desc}{name}({''.join(param_desc)}){type_desc}"
def main():
c = class_desc("com.github.frontear", "Main")
s = type_desc("java.lang.String")
v = type_desc("void")
i = type_desc("int")
print(field_desc(c, "num", i))
print(field_desc(c, "str", s))
print()
print(method_desc(c, "getNum", i))
print(method_desc(c, "setNum", v, i))
print(method_desc(c, "getStr", s))
print(method_desc(c, "setStr", v, s))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment