Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to integrate beanshell

Tags:

java

beanshell

I am developing a component based game engine in java, right now when i have changes to the components I need to rebuild and restart the editor for the changes to take effect (or i can use some limited hot code injection if the application is running in debug mode).

I am looking for a way to allow the user to modify the source of the components and reload them without having to restart the application (maybe just exit and enter game mode instead). Also a important feature that I need is that the final exported code should be native Java code(so no interpreter should be used in the final result)).

Can you give me any pointers on how to integrate the beanshell interpreter in the project? I can manually monitor the source folder for changes and feed the updated java classes to it, but how is the hotswap going to occur really?

like image 695
Anton Banchev Avatar asked Dec 06 '14 11:12

Anton Banchev


People also ask

What is BeanShell code?

BeanShell is a small, free, embeddable Java source interpreter with object scripting language features, written in Java. BeanShell dynamically executes standard Java syntax and extends it with common scripting conveniences such as loose types, commands, and method closures like those in Perl and JavaScript.

Is BeanShell a programming language?

BeanShell is an open-source embeddable Java source interpreter which has object scripting language features developed in Java. Developed by Patrick Niemeyer, BeanShell runs in the Java Runtime Environment and makes use of a variation of the Java syntax.

How do I assign BeanShell VAR to JMeter VAR?

BeanShell vars.Create a sampler to request a product from the Amazon website. Then add a Post CSS/JQuery Extractor and extract the buying price. Whenever this request is executed, JMeter will create a variable on memory with the “Reference Name.” This variable is only available during that test.


1 Answers

First of all, the title is a bit confusing. You don't need to integrate a BeanShell. What you actually need are:

  1. to define a proper architecture
  2. to use Java Compiler API in order to work with java classes

Architecture

Let's say you have an object graph. There are lots of objects, references, etc. So it will be really a tricky task to replace some instance with the new one. Instead of solving this problem you can hide dynamic part behind a "static" proxy. Proxy will handle all reloading stuff (including source folder monitoring).

Before reload:

enter image description here

After reload:

enter image description here

Having that done you can easily track changes and update dynamic part when needed.

Java Compiler API

Instead of using interpreted languages you can use Java, compiling it on the fly and loading using 'Class.forName()'. There are a lot of different examples due to the fact this approach was there for a while.

Here are some details:

  • Add dynamic Java code to your application
  • Compiling fully in memory with javax.tools.JavaCompiler
like image 132
Renat Gilmanov Avatar answered Oct 30 '22 07:10

Renat Gilmanov