This repository has been archived by the owner on Jul 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Declare BCBClass to build classes composed of methods
Signed-off-by: Brad Wood <bradley.wood@ibm.com>
- Loading branch information
1 parent
581e5f7
commit a964b0e
Showing
3 changed files
with
123 additions
and
41 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package com.ibm.bcb; | ||
|
||
import com.ibm.bcb.tree.Block; | ||
import com.ibm.bcb.tree.MethodContext; | ||
import lombok.*; | ||
import org.objectweb.asm.*; | ||
|
||
import java.util.List; | ||
|
||
import static org.objectweb.asm.Opcodes.*; | ||
|
||
@Data | ||
@Builder | ||
@EqualsAndHashCode | ||
public class BCBClass { | ||
|
||
@Builder.Default | ||
private final String name = "AnonymousClass"; | ||
@Builder.Default | ||
private final String parent = "java/lang/Object"; | ||
@Singular | ||
private final List<BCBMethod> methods; | ||
|
||
public byte[] toByteArray() { | ||
final ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS | ClassWriter.COMPUTE_FRAMES); | ||
cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, name, null, parent, null); | ||
|
||
// Todo; Add default constructor if required | ||
// Modify existing default constructor to initial default fields | ||
|
||
// Todo; Add class initializer if required | ||
// Modify existing initializer to initial static fields | ||
|
||
for (final BCBMethod method : methods) { | ||
writeToClass(method, cw); | ||
} | ||
|
||
cw.visitEnd(); | ||
return cw.toByteArray(); | ||
} | ||
|
||
public void writeToClass(final BCBMethod method, final ClassVisitor cv) { | ||
final Label methodEnd = new Label(); | ||
final MethodVisitor mv = cv.visitMethod(method.getModifiers(), method.getName(), method.getMethodType().getDescriptor(), null, null); | ||
final Type clazzType = Type.getType("L" + name + ";"); | ||
final MethodContext ctx = new MethodContext(clazzType, method.getName(), ACC_PUBLIC + ACC_STATIC, method.getMethodType(), method.getArgNames(), methodEnd); | ||
|
||
Block.of(method.getStatements()).evaluate(ctx, mv); | ||
|
||
mv.visitLabel(methodEnd); | ||
|
||
for (final String argName : method.getArgNames()) { | ||
mv.visitParameter(argName, 0); | ||
} | ||
|
||
mv.visitMaxs(0, 0); | ||
mv.visitEnd(); | ||
} | ||
|
||
@SneakyThrows | ||
public Class<?> loadClass() { | ||
final byte[] bytes = toByteArray(); | ||
|
||
final ClassLoader loader = new ClassLoader() { | ||
@Override | ||
protected Class<?> findClass(final String name) { | ||
if (name.equals(name.replace("/", "."))) { | ||
return defineClass(name, bytes, 0, bytes.length); | ||
} | ||
|
||
return null; | ||
} | ||
}; | ||
|
||
return loader.loadClass(name.replace("/", ".")); | ||
} | ||
} |
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
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,35 @@ | ||
package com.ibm.bcb; | ||
|
||
import lombok.SneakyThrows; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.objectweb.asm.Type; | ||
|
||
import static com.ibm.bcb.BCB.constant; | ||
import static com.ibm.bcb.BCB.ret; | ||
|
||
public class TestBCBClass { | ||
|
||
@Test | ||
@SneakyThrows | ||
public void testInvokeMR() { | ||
final Class<?> clazz = BCBClass.builder() | ||
.method(BCBMethod.builder() | ||
.name("test") | ||
.returnType(Type.INT_TYPE) | ||
.body( | ||
ret(constant(100)) | ||
).build()) | ||
.method(BCBMethod.builder() | ||
.name("test2") | ||
.returnType(Type.INT_TYPE) | ||
.body( | ||
ret(constant(200)) | ||
).build()) | ||
.build() | ||
.loadClass(); | ||
|
||
Assert.assertEquals(100, (int) clazz.getMethod("test").invoke(null)); | ||
Assert.assertEquals(200, (int) clazz.getMethod("test2").invoke(null)); | ||
} | ||
} |