Giter VIP home page Giter VIP logo

jphp-cglib-ext's Introduction

jphp-cglib-ext

Code Generation Library, used to extend java classes and implements interfaces at runtime

Example (Swing UI)

<?php

use java\cglib\Enhancer;
use java\reflection\ReflectionClass;
use java\reflection\ReflectionTypes;

$frame_class = ReflectionClass::forName("javax.swing.JFrame"); // get ReflectionClass for javax.swing.JFrame
$frame = $frame_class->getConstructor([ ReflectionClass::forName("java.lang.String") ])->newInstance([ "Test!" ]); // get new java class instance from construcor
// getting setDefaultCloseOperation method & invoke it
$frame_class->getMethod("setDefaultCloseOperation", [ ReflectionTypes::typeInt() ])->invoke($frame, [ $frame_class->getField("EXIT_ON_CLOSE")->get($frame) ]);

// ...
$frame_class->getMethod("setSize", [
	ReflectionTypes::typeInt(), // getting ReflectionClass for int (int.class)
	ReflectionTypes::typeInt()
])->invoke($frame, [
	ReflectionTypes::toInt(300), // cast jPHP float to ReflectionObject(int)
	ReflectionTypes::toInt(300)
]);

$pane = $frame_class->getMethod("getContentPane", [])->invoke($frame, []); // getting ReflectionObject(java.awt.Container)
$pane_class = ReflectionClass::forName("java.awt.Container"); // ReflectionClass for java.awt.Container

$button_class = ReflectionClass::forName("javax.swing.JButton");
$button = $button_class->getConstructor([ ReflectionClass::forName("java.lang.String") ])->newInstance([ "Test swing button!" ]);

// create callback
$callback_class = ReflectionClass::forName("java.awt.event.ActionListener");
$callback = Enhancer::createWithInterfaces(ReflectionClass::forName("java.lang.Object"), [ $callback_class ], function ($obj, $method, $args) {
	if ($method->getName() == "actionPerformed") {
		echo "Cool!\n";

		return null;
	}

	return $method->invoke($obj, $args);
});

$button_class->getMethod("addActionListener", [ $callback_class ])->invoke($button, [ $callback ]);
$pane_class->getMethod("add", [ ReflectionClass::forName("java.awt.Component") ])->invoke($pane, [ $button ]); // ReflectionObject casts to java.lang.Object
$frame_class->getMethod("setVisible", [ ReflectionTypes::typeBool() ])->invoke($frame, [ true ]);

jphp-cglib-ext's People

Contributors

mwguy avatar zeonish avatar

Stargazers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.