Extension method of Java application, script engine and computing device

By embedding a script engine in the Java host environment and using Java reflection to build a dynamic syntax tree, the problems of Java object construction, method overriding, and parameter deduction are solved, enabling flexible expansion and efficient development of Java applications.

CN121807261APending Publication Date: 2026-04-07CHENGDU HUAWEI TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-09-30
Publication Date
2026-04-07

AI Technical Summary

Technical Problem

Existing JavaScript engines and scripting languages ​​cannot implement object construction, method overriding, and parameter deduction at the Java runtime, leading to increased development complexity and maintenance difficulty, and making it difficult to adapt to rapidly changing business needs.

Method used

By embedding a script engine in the Java host environment, the Java reflection mechanism is used to obtain authorized Java object or class member information, build a dynamic syntax tree environment, realize method overriding and parameter deduction, and simplify the object construction process.

Benefits of technology

Without modifying Java code, it improves the development efficiency and flexibility of Java applications, simplifies the process of object construction, method overriding and parameter deduction, reduces programming complexity and improves code readability and maintainability.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121807261A_ABST
    Figure CN121807261A_ABST
Patent Text Reader

Abstract

The invention provides a Java application extension method, a script engine and computing device.The script engine is embedded in a Java host environment, when a Java application runs, a first program element and an authorized Java object or class in a script file are obtained, and the Java application is extended by means of the script engine on the basis of the authorized Java object or class. And constructing a dynamic syntax tree corresponding to the first program element and a dynamic syntax tree environment. A Java reflection mechanism is adopted to obtain member information of an authorized Java object or class, and the member information of the Java object or class is added to a dynamic syntax tree in a dynamic syntax tree environment, so that a developer can perform method overwriting and parameter derivation. According to the method, dynamic interaction between the script engine and the Java host environment is achieved when the Java application runs, the operation processes of object construction, method overwriting and parameter derivation and the Java application architecture are simplified on the basis that Java codes are not changed, the development efficiency is improved, and the product development period is shortened.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer programming technology, and more particularly to an extension method for Java applications, a script engine, and a computing device. Background Technology

[0002] Due to the static nature of the Java language, the types of all variables in a Java application are determined at compile time. Therefore, providing Java with more flexible object interoperability has become an urgent need for Java application development.

[0003] To meet the needs of Java object interaction, some script engines that can be embedded into the Java host environment have recently been proposed, such as the Nashorn JavaScript engine and the Groovy scripting language.

[0004] However, the aforementioned script engines and scripting languages ​​can only solve the direct reference and manipulation of Java objects to a certain extent, and still cannot realize object construction, method overriding, and parameter deduction at the Java runtime.

[0005] We hope to find an improved solution that can better address direct references and manipulations of Java objects, as well as object construction, method overriding, and parameter deduction in the current Java runtime. Summary of the Invention

[0006] This application provides a method for extending Java applications, a script engine, and a computing device. By embedding a script engine in a Java host environment, it enables the running of Java applications and the dynamic interaction between the script engine and the Java host environment. Without changing the Java code, it simplifies the operation process of object construction, method overriding, and parameter deduction, simplifies the Java application architecture, improves development efficiency, and accelerates the product development cycle.

[0007] In a first aspect, this application provides a method for extending a Java application, comprising: obtaining a first instance and a script file; the first instance including a Java object or class authorized by a developer; the script file including a first program element; the first program element including a reference to the Java object or class; constructing a first object corresponding to the first program element based on the first instance, and generating a dynamic syntax tree (RTL) for the first object; constructing a RTL environment based on the RTL; the RTL environment including the RTL and the first instance; obtaining member information of the first instance; the member information including a first method of the first instance; adding the member information to the RTL in the RTL environment to obtain a second method of the first object, so that a developer can override the second method.

[0008] This application embeds a script engine within a Java host environment. During Java application runtime, it retrieves the first program element and authorized Java objects or classes from the script file. The script engine then constructs a dynamic syntax tree (RPL) corresponding to the first program element, along with the RPL environment, based on the authorized Java objects or classes. This enables developers to perform method overriding and parameter deduction. This method provides dynamic interaction between the script engine and the Java host environment during Java application runtime, simplifying object construction, method overriding, and parameter deduction capabilities without modifying the Java code, thereby improving development efficiency and accelerating the product development cycle.

[0009] In some embodiments, the method further includes: obtaining a third method for the first object, and adding the third method to the dynamic syntax tree in the dynamic syntax tree environment; the third method is a method obtained by a developer overriding the second method.

[0010] In this application, by constructing a dynamic syntax tree environment, developers can easily extend and rewrite the behavior of Java applications without modifying the Java code, thereby improving the dynamism and usability of the Java code.

[0011] In some embodiments, the method further includes: generating a proxy object of the first object for a developer to invoke; the proxy object includes the first method of the first instance and the third method of the first object.

[0012] In this application, generating a proxy object based on a first object and a first instance can help developers call methods overridden by the first object in a Java host environment and / or script engine.

[0013] In some embodiments, the method further includes: when a developer invokes the first method of the first instance, the proxy object invokes the third method that operates on the first object.

[0014] In this application, if a method in the first object is overridden by a developer, when the developer calls that method in the Java host environment, the proxy object will redirect the call to the overridden method in the first object. This allows the developer to flexibly add new implementations to the first instance without modifying the Java code.

[0015] In some embodiments, obtaining the member information of the first instance includes: obtaining the member information of the first instance using Java reflection.

[0016] In some embodiments, if the first instance is a Java object or class that has not been authorized by the developer, obtaining the member information of the first instance using Java reflection further includes: the Java application returning a result indicating that obtaining the member information of the first instance failed.

[0017] In this application, the script engine can obtain the member information of the first authorized instance through Java reflection mechanism. If it is not authorized, it cannot obtain the member information of the first instance. The above mechanism improves the security of the extension methods of Java applications.

[0018] In some embodiments, the member information includes the element declaration of the first instance, and the second method of adding the member information to the dynamic syntax tree in the dynamic syntax tree environment to obtain the first object further includes: determining the parameters of the first program element according to the parameters and attributes of the first instance, and generating a second program element; the second program element is used to help the Java application implement the first program element.

[0019] In this application, the automatic inference capability of parameter types provided by the script engine simplifies the interaction process between the script and Java objects, reducing programming complexity. By automatically identifying and adapting parameter types, developers do not need to manually specify complex type conversions, reducing errors and improving code readability and maintainability.

[0020] Secondly, this application provides a function construction module and a parameter derivation module; the script engine includes a function construction module and a parameter derivation module; the function construction module is used to obtain a first instance and a script file, and construct a first object corresponding to the first program element based on the first instance, and generate a dynamic syntax tree for the first object; wherein, the first instance includes a Java object or class authorized by the developer; the script file includes the first program element; the first program element includes a reference to the Java object or class; construct a dynamic syntax tree environment based on the dynamic syntax tree; wherein, the dynamic syntax tree environment includes the dynamic syntax tree and the first instance; obtain a first method from the member information of the first instance, and add the first method of the first instance to the dynamic syntax tree in the dynamic syntax tree environment to obtain a second method of the first object, so that the developer can override the second method; the parameter derivation module is used to obtain parameters and attributes from the member information of the first instance, determine the parameter type of the first program element according to the parameters and attributes of the first instance, and generate a second program element according to the parameter type of the first program element; the second program element is used to help the Java application implement the first program element.

[0021] Thirdly, this application provides a computing device including a processor and a memory, the memory storing program instructions executable on the processor, the processor executing the program instructions to cause the processor to perform the extended method of the Java application proposed in this application.

[0022] It should be understood that the beneficial effects achieved by the technical solutions of the second and third aspects of the embodiments of this application and the corresponding possible implementation methods can be referred to the above-described technical effects of the first aspect, and will not be repeated here. Attached Figure Description

[0023] Figure 1 This is a schematic diagram of the architecture of a script engine proposed in this application;

[0024] Figure 2 This is a schematic diagram illustrating the scenario of object construction and overriding methods in a Java application;

[0025] Figure 3 This is a schematic diagram illustrating a scenario of parameter deduction in a Java application;

[0026] Figure 4 This is a flowchart illustrating an extension method for a Java application disclosed in an embodiment of this application;

[0027] Figure 5 This is a schematic diagram of the structure of a script engine disclosed in an embodiment of this application;

[0028] Figure 6 This is a schematic diagram of the structure of a computing device disclosed in an embodiment of this application. Detailed Implementation

[0029] The terms "first," "second," etc., used in the specification, claims, and accompanying drawings of this application are used to distinguish similar objects and are not necessarily used to describe a specific order or sequence. It should be understood that such terms are interchangeable where appropriate; this is merely a way of distinguishing objects with the same attributes in the embodiments of this application. Furthermore, the terms "comprising" and "having," and any variations thereof, are intended to cover non-exclusive inclusion, so that a process, method, system, product, or apparatus that comprises a series of elements is not necessarily limited to those elements, but may include other elements not explicitly listed or inherent to those processes, methods, products, or apparatuses.

[0030] To facilitate understanding of the technical solution of this application, the relevant terms used in this document are explained below.

[0031] An Abstract Syntax Tree (AST) is a hierarchical representation of the abstract syntactic structure of source code. In compiler theory, it is used to represent the structure of a programming language's source code at an abstract level. An AST can represent the structure of code through a tree structure, where each node represents a construct in the code, such as an expression, statement, or declaration. Each node in the AST corresponds to a syntactic element in the source code, and a node in the AST can be an operator, operand, control flow statement, etc.

[0032] Java reflection is a mechanism provided by the Java language for dynamically accessing, inspecting, and modifying the structure of classes, such as interfaces, fields, and methods. It allows programs to access and inspect their own structure, such as classes, interfaces, fields (attributes), and methods, at runtime. In this application, due to the dynamic nature of Java reflection, the script engine can dynamically load classes at runtime and obtain their attributes and methods, as well as invoke methods.

[0033] Due to the static nature of the Java language, the types of all variables in a Java application are determined at compile time. Therefore, providing more flexible object interoperability to the Java language has become a pressing need for Java application development. While various technologies and tools exist to address runtime dynamic interoperability and object management in Java applications, these solutions often suffer from implementation complexity, high performance overhead, and poor compatibility with the native Java environment. Developers need a method that can seamlessly integrate into the Java environment while providing efficient and intuitive manipulation of Java objects to address rapidly changing business needs and enhance application flexibility.

[0034] To meet the needs of Java object interaction, some script engines that can be embedded into the Java host environment have recently been proposed, such as the Nashorn JavaScript engine and the Groovy scripting language.

[0035] However, the aforementioned scripting engines and languages ​​can only handle direct references and manipulations of Java objects to a certain extent; they still cannot implement object construction, method overriding, and parameter deduction at runtime. When faced with scenarios requiring dynamic modifications to the existing behavior of a Java application to adapt to changing environments, these engines and scripting languages ​​cannot dynamically adjust the application's method implementations at runtime based on new business logic. This may force developers to employ more complex design patterns, such as the decorator or proxy patterns, to indirectly implement functional changes, increasing development complexity and maintenance difficulty.

[0036] Furthermore, while the aforementioned scripting engines and languages ​​can access interfaces and classes in Java applications and create proxy objects for interfaces or instances of classes, they still cannot override the methods of these instances within the script. When a script calls a Java object, if the parameter is an object, the Java object must be explicitly constructed, such as by defining the class, using the `new` keyword, calling the constructor, and allocating memory. This not only increases the burden of script writing but also reduces code readability and maintainability. These limitations of scripting engines and languages ​​hinder truly dynamic and flexible programming practices in Java applications, especially in scenarios requiring frequent code updates and adjustments, making it difficult to adapt to rapidly changing business needs.

[0037] We hope to find an improved solution that can better address direct references and manipulations of Java objects, as well as object construction, method overriding, and parameter deduction in the current Java runtime.

[0038] This application provides a method for extending Java applications, a script engine, and a computing device. By embedding a script engine within a Java host environment, the application obtains a first program element and authorized Java objects or classes from a script file during runtime. The script engine then constructs a dynamic syntax tree (RPL) corresponding to the first program element based on the authorized Java objects or classes, along with a RTL environment. Java reflection is used to obtain member information of the authorized Java objects or classes, and this member information is added to the RTL environment to enable method overriding and parameter deduction by developers. This achieves dynamic interaction between the script engine and the Java host environment during Java application runtime, simplifying the operation process of object construction, method overriding, and parameter deduction, as well as the Java application architecture, without modifying the Java code. This improves development efficiency and accelerates the product development cycle.

[0039] The method provided in this specification is described below with reference to the accompanying drawings.

[0040] For example, Figure 1 The diagram shown illustrates the architecture of a script engine proposed in this application. Figure 1 As shown, this application proposes a script engine that can be embedded in a Java host environment. When a Java application runs, the script engine can be loaded and interpreted and executed by the script engine.

[0041] First, developers can delegate Java classes or objects to the script engine so that the engine can override methods or deduce parameters. Simultaneously, the developers send the script file they want to run to the script engine. The script engine loads the script file and obtains its first program element. This first program element includes a reference to the Java class or object.

[0042] Secondly, the script engine needs to analyze the lexical, semantic, and syntactic aspects of the first program element, construct the corresponding entity object (first object) based on the Java object, and generate the corresponding dynamic syntax tree for the entity object. Multiple nodes in the entity object's dynamic syntax tree include the first object's element declarations, method calls, parameters, and attributes.

[0043] Furthermore, the script engine constructs a dynamic syntax tree environment based on the dynamic syntax tree of the entity objects. This dynamic syntax tree environment includes the dynamic syntax tree of the aforementioned entity objects, as well as the Java classes or objects referenced by the first program element imported by the script engine.

[0044] Then, within the dynamic syntax tree environment, the script engine can use Java reflection to obtain member information of the Java class or object referenced by the first program element. This member information includes the parameters, properties, and methods of the Java class or object.

[0045] Finally, the script engine can dynamically add context information to the nodes of the dynamic syntax tree of the first program element based on the member information of the Java class or object referenced by the first program element. The context information includes the parameters, properties, and methods of the Java class or object referenced by the first program element.

[0046] The script engine can implement method overriding and parameter deduction in Java applications based on a dynamic syntax tree environment with added context information.

[0047] The script engine proposed in this application allows developers to dynamically interoperate with Ohscript script code and Java applications at runtime, execute script code, and directly reference and manipulate objects and classes in the Java application. The script engine provides significant flexibility and extensibility during Java application runtime, allowing the extension of Java application objects and methods through script code without modifying the original Java application code. Furthermore, the script engine can deduce the parameter types of a first program element and generate a second program element, which assists the Java application in implementing the first program element.

[0048] This application introduces the object construction and method overriding functions provided by the script engine through the first embodiment.

[0049] For example, Figure 2 This diagram illustrates a scenario of object construction and overriding methods in a Java application. For example... Figure 2 As shown, a script engine 200 is embedded in the Java host environment 100. Based on the function construction and method overriding capabilities provided by the script engine 200, this application abstracts a function construction module 210.

[0050] To illustrate, the first instance that the Java application authorizes to the script engine is the object `human`. The `human` object includes the methods `getName()` and `setName()`. The `getName()` method is used to retrieve the attribute "name" from the `human` object, and the `setName()` method is used to set the attribute "name" in the `human` object.

[0051] First, the function construction module 210 receives the object human and constructs an entity object based on the object Human.

[0052] Secondly, the function construction module 210 generates the dynamic syntax tree corresponding to the entity object, and generates a dynamic syntax tree environment based on the dynamic syntax tree corresponding to the entity object. The dynamic syntax tree environment includes the dynamic syntax tree corresponding to the entity object and the object Human.

[0053] Furthermore, the function construction module 210 uses Java reflection to obtain the member information of the object human, including the methods getName() and setName().

[0054] Then, the function construction module 210 dynamically adds context information to the nodes of the dynamic syntax tree of the entity object based on the member information of the object Human. This context information includes the methods getName() and setName() of the object Human. Therefore, the entity object constructed by the script engine 200 includes the same getName() and setName() method declarations as the object Human. In some possible implementations, the member information of the object Human also includes the program element declarations of the object Human.

[0055] Here, the object Human includes a first identity identifier, and the entity object entity includes a second identity identifier corresponding to the first identity identifier. In some possible implementations, the attribute or method name of the object Human can be used as the first identity identifier of the Java object, and the attribute or method name of the entity object entity can be used as the second identity identifier of the entity object.

[0056] In some possible implementations, when a developer calls the getName() and setName() methods of the entity object in the script engine 200, the getName() and setName() methods of the human object will be called directly based on the second identity corresponding to the first identity of the entity object.

[0057] When method overriding is performed, function building module 210 can support developers in overriding a method in an entity object.

[0058] As an illustration, the developer added a new function behavior to getName() in the entity object using function building module 210, returning "name", such as "return name;", thus obtaining the method overload of getName() in the entity object. The overloaded getName() method overrides the original getName() method body.

[0059] At this point, the function construction module 210 adds a new node to the getName() method in the dynamic syntax tree of the entity object based on the above operations. The node includes the function behavior of returning "name", such as "return", and the parameter "name". When the overridden method is called, it will return the obtained parameter "name".

[0060] In some possible implementations, the function building module 210 may also add a new function implementation "entity.getName=()=>{}" in entity.getName.

[0061] The line of code "entity.getName=()=>{}" defines an anonymous function with no parameters and no return value. When a method implementation needs to be added, the developer only needs to change the function implementation to "Entity.getName=()=>"Kimi", which means that when the "Entity.getName" method is called, it will output the string "Kimi".

[0062] In some possible implementations, when a developer calls the entity's getName() method in the script engine 200, the developer's call will point to the overridden getName() method.

[0063] When a developer calls the `setName()` method of an entity object in the script engine 200, the developer's call will be directed to the `setName()` method of the `Human` object, based on the second identity corresponding to the first identity of the entity object.

[0064] To facilitate developers calling the overridden methods of entity objects within the Java host environment, the script engine 200 generates a Java human proxy based on the `getName()` and `setName()` methods of the `Human` object, as well as the `getName()` and `setName()` methods of the entity object. Specifically, the `getName()` method of the entity object is overridden by the developer, while the `setName()` method is not.

[0065] The Java human proxy object includes the getName() and setName() methods of the human object, as well as the overridden getName() method and the non-overridden setName() method of the entity object.

[0066] When a Java application calls the `getName()` or `setName()` method of the `human` object, the proxy object `Java humanproxy` will redirect the call to the `getName()` or `setName()` method of the `entity` object.

[0067] The script engine 200 will judge the calling behavior of the Java application. If the called method is overridden, it will direct the Java application's call to the overridden method of the object entity.

[0068] If the method being called is not overridden, the Java application will direct the call to the method of the Human object.

[0069] To illustrate, when a developer calls the entity's getName() method in the Java host environment, because the getName() method is overridden, the proxy object will direct the call to the overridden getName() method of the Human object.

[0070] When a developer calls the `getName()` method of an entity in a Java host environment, because the `setName()` method is not overridden, the proxy object will direct the call to the `setName()` method of the `human` object.

[0071] The script engine 200 can extend and rewrite the behavior of JavaBeans without modifying the Java code through the function building module 210, thereby improving the dynamism and usability of the Java code.

[0072] This application describes the parameter derivation function provided by the script engine through a second embodiment.

[0073] The following is an illustrative code example from a Java application.

[0074]

[0075] The code snippet "public class Human{private String name;public void makeFriend(Human Human){}}" is a public class "class Human" defined in the Java application.

[0076] The class "Human" includes a private member variable "private String name" and a method "makeFriend", whose parameter is "Human Human".

[0077] The code snippet "OhScript.grant("Human",Human.class);" indicates that the Java application grants the script engine permission to use the class "classHuman".

[0078] The following is an illustrative example of code from a script file.

[0079] let human=Human{name: "Ivy"};

[0080] human.makeFriend(Human{name:“Alice”});

[0081] The code snippet "let human = Human{name:“Ivy”}; human.makeFriend(human);" indicates that the developer instantiates a human object in the scripting engine Ohscrip, which contains the method makeFriend(human) with the parameter "Ivy".

[0082] The code snippet "human.makeFriend({name:"Alice"});" indicates that the developer calls the makeFriend() method contained in the object human in the script engine Ohscrip, with the input parameter "{name:"Alice"}".

[0083] In a Java environment, the code snippet "let human = Human{name: "Ivy"}; human.makeFriend(human);" is code that the script engine can recognize.

[0084] However, Java applications cannot call the method "human.makeFriend()" using {name:"Alice"}.

[0085] In this application, the method calls in the above code can be implemented using the script engine 200.

[0086] For example, Figure 3 This diagram illustrates a scenario where parameter derivation is performed in a Java application. For example... Figure 3 As shown, in Figure 2 Based on the script engine 200 shown, this application abstracts a parameter derivation module 220 based on the parameter derivation capability provided by the script engine.

[0087] First, the developers entered a script file into the Java Hosting environment. This script file included the first program element: "human.makeFriend({name:“Alice”});". Correspondingly... Figure 3 Step ① in the process.

[0088] Secondly, the parameter derivation module 220 obtains the first program element "human.makeFriend({name:“Alice”});". Corresponding to... Figure 3 Step ② in the process.

[0089] Next, the parameter derivation module 220 parses the first program element and determines that the input parameter {name:“Alice”} of the first program element should be an object. Here, ““Alice”” is a string. The function construction module 210 constructs the entity object {name:“Alice”} and generates the corresponding dynamic syntax tree for the entity object {name:“Alice”}, thus constructing the dynamic syntax tree environment for the object {name:“Alice”}. Figure 3 Step ③ in the process.

[0090] Next, the parameter deduction module 220 parses the "makeFriend()" in the code segment, determines that makeFriend() is a method, and uses Java reflection to obtain the member information of the method makeFriend(). This member information includes the Java class "class Human" to which the method makeFriend() belongs, the parameters and attributes of "class Human", and the parameters "Human, Human" of the method makeFriend().

[0091] Parameter deduction module 220 generates entity objects in the dynamic syntax tree environment based on the member information of the makeFriend() method.

[0092] Add a node {name:“Alice”} to supplement the context information of the entity object's dynamic syntax tree.

[0093] The parameter deduction module 220 traverses the abstract syntax tree (AST) in the abstract syntax tree environment to obtain the context information of the method `makeFriend()`. This context information includes that the class to which the method belongs is the public class "class Human", and the parameter is `(Human Human)`. The public class "class Human" includes a private member variable "private String name". The process by which the parameter deduction module 220 parses the `makeFriend()` method and obtains its context information in the AST environment corresponds to... Figure 3 Step 4 in the process.

[0094] Then, the parameter deduction module 220 determines, based on the context information of the method `makeFriend()`, that the input parameter of this method is a Java-type `Human` object, and deduces from the entity object `{name:“Alice”}` to determine the second program element `“Human{name:“Alice”}` that the script engine 200 can recognize. Correspondingly... Figure 3 Step 5 in the process.

[0095] Finally, the parameter pushing module 210 replaces the parameters of the first program element "human.makeFriend(Human{name:"Alice"}) based on the second program element "Human{name:"Alice"}", enabling the script engine to run the above method. Among these, the corresponding... Figure 3 Step 6 in the process.

[0096] The execution of the above code indicates that a person named "ivy" wants to be friends with "Alice".

[0097] In some possible implementations, if the first instance is an unauthorized Java type, the Java application will return a result indicating that obtaining the member information of the first instance failed.

[0098] As one possible implementation, if the instantiated object is a static class, the script engine can directly run the above method.

[0099] The script engine's automatic parameter type inference capability simplifies the interaction between scripts and Java objects, reducing programming complexity. By automatically identifying and adapting parameter types, developers no longer need to manually specify complex type conversions, reducing errors and improving code readability and maintainability.

[0100] Based on the object construction and method overriding capabilities provided by the script engine described in the first embodiment, and the automatic parameter type deduction capability provided by the script engine described in the second embodiment, this application proposes an extension method for Java applications. This method embeds a script engine within a Java host environment. During Java application runtime, it obtains a first program element and an authorized Java object or class from a script file. The script engine then constructs a dynamic syntax tree (RTD) corresponding to the first program element, and a RTD environment, based on the authorized Java object or class. Java reflection is used to add the member information of the aforementioned Java object or class to the RTD environment, enabling developers to perform method overriding and parameter deduction.

[0101] Exemplary Figure 4 The diagram shown is a flowchart illustrating an extension method for a Java application disclosed in an embodiment of this application. Figure 4 As shown, this method extends Java applications through steps S410-450. It is understood that this method can be executed on any device platform or cluster of devices with computing and processing capabilities.

[0102] S410: Obtain the first instance and script file.

[0103] As mentioned earlier, this application embeds a script engine 200 into the Java host environment 100, allowing script files to be obtained during Java application runtime. Developers can also authorize Java classes or objects (first instances) to the script engine 200.

[0104] Script engine 200 loads the aforementioned script file and obtains the first program element within it. This first program element includes references to Java objects or classes.

[0105] The script engine 200 imports Java classes or objects authorized by developers into the script environment, enabling scripts to directly reference and manipulate Java objects or classes.

[0106] Illustratively, the first instance includes the object "human" in the first embodiment, and the class "class Human" in the second embodiment.

[0107] The first program element includes Entity.getName() and Entity.setName() in the first embodiment, and "human.makeFriend({name:“Alice”})" in the second embodiment.

[0108] S420: Construct the first object corresponding to the first program element based on the first instance and generate the dynamic syntax tree of the first object.

[0109] Taking the first instance as a Java object as an example, the script engine needs to analyze the lexical, semantic, and syntactic aspects of the first program element, construct the corresponding entity object of the first program element based on the Java object, and generate the corresponding dynamic syntax tree of the entity object.

[0110] The dynamic syntax tree of an entity object contains multiple nodes, including the element declarations, method calls, parameters, and properties of the first object.

[0111] Indicatively, in the first embodiment, the first object is an entity object "entity". The dynamic syntax tree corresponding to the entity object "entity" includes multiple nodes, and the multiple nodes include element declarations of the entity object "entity".

[0112] In the second embodiment, the first object is the entity object "{name:"Alice"}". The dynamic syntax tree of the entity object "{name:"Alice"}" includes multiple nodes, and the multiple nodes include the element declarations of the object "{name:"Alice"}".

[0113] S430: Construct a dynamic syntax tree environment based on the dynamic syntax tree.

[0114] Script Engine 200 can construct a dynamic syntax tree environment for entity objects based on their dynamic syntax trees. This dynamic syntax tree environment includes the entity object's dynamic syntax tree and the Java classes or objects authorized by the developer.

[0115] In a first embodiment, illustratively, the script engine 200 can generate a dynamic syntax tree environment based on the dynamic syntax tree corresponding to the entity object "entity". The dynamic syntax tree environment includes the dynamic syntax tree corresponding to the entity object "entity" and the object "human" authorized by the developer.

[0116] In the second embodiment, the script engine 200 can generate a dynamic syntax tree environment based on the dynamic syntax tree corresponding to the entity object "{name: "Alice"}" (the first object). The dynamic syntax tree environment includes the dynamic syntax tree corresponding to the entity object "{name: "Alice"}", and the developer-authorized class "class Human".

[0117] S440: Obtain the member information of the first instance.

[0118] The script engine 200 uses Java reflection to obtain member information of Java classes or objects authorized by the developer. This member information includes the parameters, properties, and methods of the Java class or object.

[0119] Indicatively, in the first embodiment, the script engine 200 uses Java reflection to obtain member information of the object "human" (first instance), including the methods getName() and setName() of the object "human".

[0120] In the second embodiment, the script engine 200 uses Java reflection to obtain the member information of the class "class Human" (first instance), including the attribute "name" of the object class "class Human", the parameter type String, and the parameter "Human, Human" of the method makeFriend().

[0121] In some possible implementations, if the first instance is a Java object or class that has not been authorized by the developer, when the script engine 200 uses the Java reflection mechanism to obtain the member information of the first instance, the Java application will return a result indicating that obtaining the member information of the first instance has failed.

[0122] S450: Add the member information to the dynamic syntax tree in the dynamic syntax tree environment to obtain a second method of the second object so that the developer can override the second method.

[0123] The script engine 200 can dynamically add context information to the nodes of the dynamic syntax tree of the first program element based on the member information of the Java class or object referenced by the first program element. The context information includes the parameters, element declarations, properties, and methods of the Java class or object referenced by the first program element.

[0124] In a first embodiment, the script engine 200 can add member information of an entity object to its dynamic syntax tree to obtain the method to which the entity object belongs, wherein the method to which the entity object belongs is the same as the method to which the Java class or object referenced by the first program element belongs.

[0125] Developers can override methods of entity objects to obtain the overridden methods.

[0126] To facilitate developers calling the methods overridden by the first object, the script engine 200 can also generate a proxy object for the first object.

[0127] If a method in the first object is overridden by the developer, when the developer calls that method in the Java host environment, the proxy object will redirect the call to the overridden method in the first object.

[0128] If the method in the first object is not overridden by the developer, when the developer calls the method in the Java host environment, the proxy object will direct the call operation to the method of the first instance.

[0129] For example, in the first embodiment, the developer adds a new function behavior "return name;" to getName() in entity object through function building module 210 of script engine 200, thereby obtaining method overload of getName() in entity object, and the method overload of getName() overrides the original method body of getName().

[0130] When a developer calls the entity's getName() method in the Java host environment, because the getName() method is overridden, the proxy object will direct the call to the overridden getName() method of the Human object.

[0131] When a developer calls the `getName()` method of an entity in a Java host environment, because the `setName()` method is not overridden, the proxy object will direct the call to the `setName()` method of the `human` object.

[0132] Script Engine 200 can also infer the parameter types of the first program element.

[0133] The parameter derivation module 220 parses the first instance referenced by the first program element, determines that the first instance is a method, and uses Java reflection mechanism to obtain the member information of the method. The member information includes the parameters, attributes and methods of the Java class to which the first instance belongs, and the parameters, attributes and methods of the first instance.

[0134] The parameter derivation module 220 adds new nodes to the entity object in the dynamic syntax tree environment based on the member information of the method, supplementing the context information of the entity object's dynamic syntax tree.

[0135] The parameter derivation module 220 traverses the abstract syntax tree in the abstract syntax tree runtime space to obtain the context information of the entity object. The context information of the entity object includes the parameters of the first instance and the element declarations.

[0136] The parameter derivation module 220 determines the parameter type of the first program element based on the parameters and attributes of the first instance, and generates a second program element that the Java application can recognize based on the parameter type of the first program element, thereby helping the Java application implement the first program element.

[0137] For example, in the second embodiment, the parameter derivation module 220 determines the second program element "Human{name:"Alice"} that the script engine 200 can recognize based on the context information of the method makeFriend(), where the input parameter of the method is a Java type Human object, and derives the entity object {name:"Alice"}.

[0138] The parameter push module 210 replaces the parameters of the first program element "human.makeFriend(Human{name:"Alice"}) based on the second program element "Human{name:"Alice"}", so that the script engine can run the above method.

[0139] In this way, Java applications can leverage scripting engines to implement dynamic functions, such as executing custom logic or interacting with external data sources, while maintaining the security and stability of the host environment. This approach can be widely applied in cloud computing and microservice architectures, especially in scenarios where services frequently exchange and process data. By implementing this technology in a microservice architecture, dynamic data interaction and automatic type matching between services can be achieved, greatly simplifying service development and integration. This not only improves development efficiency but also enhances system scalability and maintainability, enabling service components to interact more flexibly, supporting rapid deployment and updates, and thus better adapting to rapidly changing business needs.

[0140] It is understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application. In addition, in some possible implementations, each step in the above embodiments may be selectively executed according to the actual situation, and may be partially or fully executed, which is not limited here.

[0141] The above is a description of an extension method for a Java application provided in the embodiments of this application. Next, a script engine provided in the embodiments of this application will be described. It is understood that this computing device is proposed based on the content described above, and some or all of the content in this computing device can be found in the description above.

[0142] For example, Figure 5 The diagram shown is a structural schematic of a script engine disclosed in an embodiment of this application, such as... Figure 5 As shown, the script engine 200 includes a function construction module 210 and a parameter derivation module 220.

[0143] The function construction module 210 is used to obtain the first instance and the script file, construct the first object corresponding to the first program element based on the first instance, and generate the dynamic syntax tree of the first object. The first instance includes a Java object or class authorized by the developer. The script file includes the first program element; the first program element includes references to the Java object or class.

[0144] The function construction module 210 is also used to construct a dynamic syntax tree environment based on the dynamic syntax tree of the first object. The dynamic syntax tree environment includes the dynamic syntax tree of the first object and the first instance.

[0145] The function building module 210 is also used to obtain the first method from the member information of the first instance, and add the first method of the first instance to the dynamic syntax tree of the first object in the dynamic syntax tree environment to obtain the second method of the first object, so that the developer can override the second method.

[0146] The parameter derivation module 220 is used to obtain the parameters and attributes in the member information of the first instance, determine the parameter type of the first program element based on the parameters and attributes of the first instance, and generate a second program element based on the parameter type of the first program element; the second program element is used to help the Java application implement the first program element.

[0147] The above is a description of a script engine provided in the embodiments of this application. Next, a computing device provided in the embodiments of this application will be described. It is understood that this computing device is proposed based on the content described above, and some or all of the content in this computing device can be found in the description above.

[0148] For example, Figure 6 The diagram shown is a structural schematic of a computing device disclosed in an embodiment of this application, such as... Figure 6 As shown, the computing device 10 includes a processor 11 and a memory 12. The memory 12 stores program instructions that can be run on the processor 11. The processor 11 executes the program instructions to cause the processor 100 to execute the extended method of the Java application proposed in this application.

[0149] In addition to the methods, apparatus, and electronic devices described above, embodiments of this application may also provide a computer program product, comprising computer program instructions. When executed by a processor, these computer program instructions cause the processor to perform the steps of the extended methods of the Java application described in the "Methods" section of this specification for various embodiments of this application. The computer program product can be written in any combination of one or more programming languages ​​to perform the operations of the embodiments of this application. The programming languages ​​include object-oriented programming languages ​​such as Java and C++, as well as conventional procedural programming languages ​​such as C or similar languages. The computer program code can be in source code form, object code form, executable file, or some intermediate form. The computer program code can be executed entirely on a user's computing device, partially on a user's device, as a standalone software package, partially on a user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.

[0150] Furthermore, embodiments of this application may also provide a computer-readable storage medium storing computer program instructions thereon, which, when executed by a processor, cause the processor to perform the steps of the extended method of a Java application according to various embodiments of this disclosure as described in the "Method" section above. The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may include, for example, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, apparatus, or device, or any combination thereof. More specific examples of readable storage media (a non-exhaustive list) include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage device, magnetic storage device, or any suitable combination thereof. It should be noted that the content contained in the computer-readable medium may be appropriately added to or subtracted according to the requirements of legislation and patent practice in a jurisdiction. For example, in some jurisdictions, according to legislation and patent practice, a computer-readable medium may not include electrical carrier signals and telecommunication signals.

[0151] In the above embodiments, the descriptions of each embodiment have different focuses. For parts that are not described in detail or recorded in a certain embodiment, please refer to the relevant descriptions of other embodiments.

[0152] It should be understood that the sequence number of each step in the above embodiments does not imply the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiments of this application.

[0153] The basic principles of this application have been described above with reference to specific embodiments. However, it should be noted that the advantages, benefits, and effects mentioned in this application are merely examples and not limitations, and should not be considered as essential features of the various embodiments of this disclosure. Furthermore, the specific details disclosed above are for illustrative and facilitative purposes only, and are not limitations. These details do not limit the scope of this disclosure to the necessity of employing the specific details described above.

[0154] The block diagrams of devices, apparatuses, devices, and systems disclosed herein are merely illustrative examples and are not intended to require or imply that they must be connected, arranged, or configured in the manner shown in the block diagrams. As those skilled in the art will recognize, these devices, apparatuses, devices, and systems can be connected, arranged, and configured in any manner. Words such as “comprising,” “including,” “having,” etc., are open-ended terms meaning “including but not limited to,” and are used interchangeably with them. The terms “or” and “and” as used herein refer to the terms “and / or,” and are used interchangeably with them unless the context clearly indicates otherwise. The term “such as” as used herein refers to the phrase “such as but not limited to,” and is used interchangeably with it.

[0155] It should also be noted that in the apparatus, devices, and methods of this disclosure, the components or steps can be disassembled and / or recombined. These disassemblies and / or recombinations should be considered as equivalent solutions to this disclosure.

[0156] The above description has been given for purposes of illustration and description. Furthermore, this description is not intended to limit the embodiments of this disclosure to the forms disclosed herein. Although numerous exemplary aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, alterations, additions, and sub-combinations therein.

[0157] It is understood that the various numerical designations used in the embodiments of this application are merely for descriptive convenience and are not intended to limit the scope of the embodiments of this application. The specific embodiments described above further illustrate the purpose, technical solutions, and beneficial effects of the present invention. It should be understood that the above descriptions are merely specific embodiments of the present invention and are not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

[0158] The specific embodiments described above further illustrate the purpose, technical solution, and beneficial effects of the present invention. It should be understood that the above description is only a specific embodiment of the present invention and is not intended to limit the scope of protection of the present invention. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of the present invention should be included within the scope of protection of the present invention.

Claims

1. A method for extending a Java application, characterized in that, include: Obtain the first instance and script file; The first instance includes a Java object or class authorized by the developer; the script file includes a first program element; the first program element includes a reference to the Java object or class; Based on the first instance, construct the first object corresponding to the first program element, and generate the dynamic syntax tree of the first object; A dynamic syntax tree environment is constructed based on the dynamic syntax tree; the dynamic syntax tree environment includes the dynamic syntax tree and the first instance; Obtain the member information of the first instance; The member information includes the first method of the first instance; In the dynamic syntax tree environment, the member information is added to the dynamic syntax tree to obtain the second method of the first object, so that the developer can override the second method.

2. The method according to claim 1, characterized in that, The method further includes: Obtain a third method for the first object, and add the third method to the dynamic syntax tree in the dynamic syntax tree environment; the third method is a method obtained by the developer overriding the second method.

3. The method according to claim 2, characterized in that, The method further includes: Generate a proxy object for the first object so that developers can call it; the proxy object includes the first method of the first instance and the third method of the first object.

4. The method according to claim 3, characterized in that, The method further includes: When a developer calls the first method of the first instance, the proxy object will call the third method that is directed to the first object.

5. The method according to claim 3, characterized in that, The first instance further includes a fourth method, and the first object further includes a fifth method derived based on the fourth method, the fifth method being un-overridden by the developer, the method further including: When a developer invokes the fourth method of the first instance, the proxy object will invoke the fourth method of the first instance.

6. The method according to claim 1, characterized in that, The step of obtaining the member information of the first instance includes: The member information of the first instance is obtained using Java reflection.

7. The method according to claim 1, characterized in that, If the first instance is a Java object or class that has not been authorized by the developer, obtaining the member information of the first instance using Java reflection also includes: The Java application returned a result indicating that it failed to retrieve the member information of the first instance.

8. The method according to claim 1, characterized in that, The member information includes the parameters and attributes of the first instance. The second method of adding the member information to the dynamic syntax tree in the dynamic syntax tree environment to obtain the first object further includes: Based on the parameters and attributes of the first instance, the parameter type of the first program element is determined, and a second program element is generated based on the parameter type of the first program element; the second program element is used to help the Java application implement the first program element.

9. A script engine, characterized in that, The script engine includes a function construction module and a parameter deduction module; A function construction module is used to obtain a first instance and a script file, and construct a first object corresponding to the first program element based on the first instance, and generate a dynamic syntax tree for the first object; wherein, the first instance includes a Java object or class authorized by the developer; the script file includes the first program element; the first program element includes a reference to the Java object or class; a dynamic syntax tree environment is constructed based on the dynamic syntax tree; wherein, the dynamic syntax tree environment includes the dynamic syntax tree and the first instance; a first method is obtained from the member information of the first instance, and the first method of the first instance is added to the dynamic syntax tree in the dynamic syntax tree environment to obtain a second method of the first object, so that the developer can override the second method; The parameter derivation module is used to obtain the parameters and attributes in the member information of the first instance, determine the parameter type of the first program element based on the parameters and attributes of the first instance, and generate a second program element based on the parameter type of the first program element; the second program element is used to help the Java application implement the first program element.

10. A computing device, characterized in that, The computing device includes a processor and a memory, the memory storing program instructions executable on the processor, the processor executing the program instructions to cause the processor to perform the method as described in any one of claims 1-8.