Is there a way to programmatically generate a java class from a template?

I need a library or method to create a java class (just creating the class source code as a text format, without having to run or use it) from a template text file.

as an example, I have a class template

package packagename.name.abc; import lib.sub.sub; import lib.sub.sub2; public class templateClass { public String getTemplateText() { //some operations here. } PlaceController getPlaceController() { //some operations here. } } 

and I want to add a library import, import a function and add a parameter or line to the template and add the template to the project. // OPERATIONS

after import operations, the code will look like this:

 package packagename.name.abc; import lib.sub.sub; import lib.sub.sub2; import NEWLIB.NEWSUB.NEWSUB; // NEW LIBRARY public class templateClass { public String getTemplateText(String PARAMETER ) { // NEW PARAMETER //some operations here. String NEW_LINE = ""; // NEW LINE } PlaceController getPlaceController() { //some operations here. } public String getNEWText() { //NEW FUNCTION //some operations here. } } 

I was looking for some libraries for these operations. FreeMarker and ApacheVelocity are recommended for some of these issues. But I definitely don’t understand how to do this with FreeMarker. I think it is more likely to use a tag library (e.g. JSTL). I do not want to use the template keywords in the code (for example, $ (temp)), just the functions "doImport", "addFunction", "addParameterToFunction", etc. maybe in these libraries or can someone post some examples about these operations?

+6
source share
2 answers

Essentially, you want to somehow modify an arbitrary piece of code.

To do this in general, you pretty much have to be able to parse the text so that you can place structures in appropriate structured places. A commonly used pattern is text; There is no structure to hang a hat.

The most reliable way to do this is to use a source-to-source conversion system. Such a tool allows you to explicitly indicate: "If you see this, replace it with this." To achieve your goal, you would say something like: "If you see a set of class declarations in class X, add this class", usually indicated as

  a rewritesto b if condition c 

DMS Software Reengineering Toolkit - a program conversion tool that will read the source code, build compiler data structures (AST, symbol tables, flow graphs), allow you to apply source rewrites to the source of the code presented in the form of these structures, using the source templates for matching / replacement , and then regenerate a reliable source from the result.

DMS has a / prettyprinters parser for many languages, including Java (1.4 / 1.5 / 1.6), C, C ++, C #, COBOL, PHP, JavaScript, ...

For your task with adding a parameter using DMS, you should write the following conversion rule:

 add_string_parameter(r:result_type,m:IDENTIFIER,p:parameter_list): method_signature->method_signature = " \r \m ( \p ) " -> " \r \m ( \p , String PARAMETER ) " if m="getTemplateText"; 

( β†’ corresponds to "rewritesto"). This only recognizes method signatures (by searching for AST, not source code). Quotation markers are meta-quotes containing fragments of your target language, and you must distinguish between the text of the target language and the text in the language of the rules. r, m, p - meta-variables that must correspond to specific structures, as indicated by the signature of the rule; \ r \ m \ p are meta-expressions in the target text, which states that this structure must be present. The left side of "\ r \ m (\ p)" corresponds to the signatures and connects r, m, p with the AST structures supporting it; the right side indicates the substitution, in which the associated values ​​of r, m, p are substituted to obtain a replacement. The conditional "if" should insist that only the modified desired method be changed; you may need a more complicated condition if you have a large pile of code and want to use only a specific method.

+1
source

Will this help? - JET templates

+1
source

Source: https://habr.com/ru/post/893495/


All Articles