kambo / karsk
Karsk - java bytecode writer
v0.1.0
2020-02-22 11:01 UTC
Requires
- php: >=7.1
Requires (Dev)
- mikey179/vfsstream: ~1
- phpstan/phpstan: 0.11.*
- phpunit/phpunit: 7.*
- squizlabs/php_codesniffer: 3.*
This package is auto-updated.
Last update: 2024-12-22 21:33:59 UTC
README
Karsk is direct port of ASM "a very small and fast Java bytecode manipulation framework".
This is highly experimental and very much work in progress at this moment. Any help is welcome!
Install
Preferred way to install framework is with composer:
composer require kambo/karsk
Basic usage
public class Helloworld { public static void main (String[] args) { System.out.println("Hello world!"); } }
<?php use Kambo\Karsk\ClassWriter; use Kambo\Karsk\Opcodes; $cw = new ClassWriter(0); $cw->visit( Opcodes::V1_8, Opcodes::ACC_PUBLIC, "Helloworld", null, "java/lang/Object", null ); $mw = $cw->visitMethod(Opcodes::ACC_PUBLIC, "<init>", "()V", null, null); $mw->visitVarInsn(Opcodes::ALOAD, 0); $mw->visitMethodInsn(Opcodes::INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); $mw->visitInsn(Opcodes::RETURN_); $mw->visitMaxs(1, 1); $mw->visitEnd(); $mainMethod = $cw->visitMethod( (Opcodes::ACC_PUBLIC + Opcodes::ACC_STATIC), "main", "([Ljava/lang/String;)V", null, null ); $mainMethod->visitFieldInsn(Opcodes::GETSTATIC, "java/lang/System", "out", "Ljava/io/PrintStream;"); $mainMethod->visitLdcInsn("Hello world!"); $mainMethod->visitMethodInsn( Opcodes::INVOKEVIRTUAL, "java/io/PrintStream", "println", "(Ljava/lang/String;)V", false ); $mainMethod->visitInsn(Opcodes::RETURN_); $mainMethod->visitMaxs(2, 2); $mainMethod->visitEnd(); $code = $cw->toByteArray();
License
3-Clause BSD, https://opensource.org/licenses/BSD-3-Clause