Skip to content

Commit

Permalink
Bytecode: Add missing byte constant (#201)
Browse files Browse the repository at this point in the history
  • Loading branch information
dstepanov authored Dec 4, 2024
1 parent 2634ed1 commit bede34e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,22 @@ public void write(GeneratorAdapter generatorAdapter, MethodContext context) {
return;
}
if (type instanceof TypeDef.Primitive primitive) {
if (value instanceof Number number) {
switch (primitive.name()) {
case "long" -> generatorAdapter.push(number.longValue());
case "float" -> generatorAdapter.push(number.floatValue());
case "double" -> generatorAdapter.push(number.doubleValue());
case "byte" -> generatorAdapter.push(number.byteValue());
case "int" -> generatorAdapter.push(number.intValue());
case "short" -> generatorAdapter.push(number.shortValue());
default ->
throw new IllegalStateException("Unrecognized number primitive type: " + primitive.name());
}
return;
}
switch (primitive.name()) {
case "long" -> generatorAdapter.push((long) value);
case "float" -> generatorAdapter.push((float) value);
case "double" -> generatorAdapter.push((double) value);
case "boolean" -> generatorAdapter.push((boolean) value);
case "byte" -> generatorAdapter.push((byte) value);
case "int" -> generatorAdapter.push((int) value);
case "short" -> generatorAdapter.push((short) value);
case "boolean" -> generatorAdapter.push((Boolean) value);
case "char" -> generatorAdapter.push((Character) value);
default ->
throw new IllegalStateException("Unrecognized primitive type: " + primitive.name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1357,6 +1357,102 @@ boolean test() {
""", decompileToJava(bytes));
}

@Test
void testPrimitives() {

ClassDef classDef = ClassDef.builder("example.Test")
.addMethod(MethodDef.builder("testLong")
.returns(TypeDef.Primitive.LONG)
.build((aThis, methodParameters) ->
TypeDef.Primitive.LONG.constant(123445).returning()
)
)
.addMethod(MethodDef.builder("testInt")
.returns(TypeDef.Primitive.INT)
.build((aThis, methodParameters) ->
TypeDef.Primitive.INT.constant(334455).returning()
)
)
.addMethod(MethodDef.builder("testFloat")
.returns(TypeDef.Primitive.FLOAT)
.build((aThis, methodParameters) ->
TypeDef.Primitive.FLOAT.constant(123.456).returning()
)
)
.addMethod(MethodDef.builder("testShort")
.returns(TypeDef.Primitive.SHORT)
.build((aThis, methodParameters) ->
TypeDef.Primitive.SHORT.constant(345).returning()
)
)
.addMethod(MethodDef.builder("testChar")
.returns(TypeDef.Primitive.CHAR)
.build((aThis, methodParameters) ->
TypeDef.Primitive.CHAR.constant('c').returning()
)
)
.addMethod(MethodDef.builder("testBoolean")
.returns(TypeDef.Primitive.BOOLEAN)
.build((aThis, methodParameters) ->
TypeDef.Primitive.BOOLEAN.constant(true).returning()
)
)
.addMethod(MethodDef.builder("testByte")
.returns(TypeDef.Primitive.BYTE)
.build((aThis, methodParameters) ->
TypeDef.Primitive.BYTE.constant(45).returning()
)
)
.addMethod(MethodDef.builder("testDouble")
.returns(TypeDef.Primitive.DOUBLE)
.build((aThis, methodParameters) ->
TypeDef.Primitive.DOUBLE.constant(444.555).returning()
)
)
.build();

StringWriter bytecodeWriter = new StringWriter();
byte[] bytes = generateFile(classDef, bytecodeWriter);

Assertions.assertEquals("""
package example;
class Test {
long testLong() {
return 123445L;
}
int testInt() {
return 334455;
}
float testFloat() {
return 123.456F;
}
short testShort() {
return 345;
}
char testChar() {
return 'c';
}
boolean testBoolean() {
return true;
}
byte testByte() {
return 45;
}
double testDouble() {
return 444.555;
}
}
""", decompileToJava(bytes));
}

private String toBytecode(ObjectDef objectDef) {
StringWriter stringWriter = new StringWriter();
generateFile(objectDef, stringWriter);
Expand Down

0 comments on commit bede34e

Please sign in to comment.