Java – how to integrate BeanShell

I am developing a Java based component game engine. Now when I make changes to the component to be rebuilt and restart the editor to make the changes take effect (or if the application is running, I can use some limited hot code injection debugging mode)

I'm looking for a way to allow users to modify the source of components and reload them without restarting the application (maybe just exit and enter game mode) Another important feature I need is that the final exported code should be local java code (so the interpreter should not be used in the final result)

Can you show me how to integrate the bean shell interpreter into the project? I can manually monitor the source folder for changes and provide it with updated Java classes, but how will HotSwap happen?

Solution

First, the title is a bit confusing You do not need to integrate BeanShell What you really need is:

>Define a correct architecture > use the java compiler API to handle Java classes

Architecture

Suppose you have an object graph There are many objects, references, etc., so replacing some instances with a new instance is really a tricky task Instead of solving this problem, you can hide the dynamic part behind the "static" agent The agent will handle all reloads (including source folder monitoring)

Before reloading:

After reassembly:

When done, you can easily track changes and update the dynamic section as needed

Java compiler API

Instead of using an interpreted language, you can use Java, compile on the fly, and use 'class Forname() 'loading There are many different examples because this method has been used for some time

Here are some details:

> Add dynamic Java code to your application > Compiling fully in memory with javax. tools. JavaCompiler

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>