简体   繁体   中英

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

I need a library or method to generate a java class (just generating source code of class as text format, no need 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 an library import, a function import and a parameter or line addition to template and add template to a project. //OPERATIONS

after import operations the code will be like :

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 searched some libraries for this operations FreeMarker and ApacheVelocity are recomended for some similar problems. But I dont exactly understand the how to do this operation with FreeMarker. I think It's more likely have a usage as Tag Library ( like JSTL) I don't want to use template keywords in code (like $(temp) ) just functions "doImport", "addFunction", "addParameterToFunction" etc. Is it possible with this libraries or can anybody send some examples with about this operations?

Will this help? - JET templates

In effect, you want to modify an arbitrary piece of code, in arbitrary ways.

To do this in a general way, you pretty much need to be able to parse the text so that you can place structures in the appropriate structured places. A template as normally used is just text; there is no structure to hang your hat on.

The most reliable way to do this is to use a source-to-source program transformation system. Such a tool allows you to explicitly state, "If you see this, then replace it by that". To accomplish your purpose, you'd say something like, "If you see a set of class declarations in class X, then add this class", generally stated as

  a rewritesto b if condition c

The DMS Software Reengineering Toolkit is a program transformation tool that will read source code, build compiler data structures (ASTs, symbol tables, flow graphs), allow you to apply source-to-source rewrites to the code represented as those structures, using source patterns to match/replace, and then regenerate valid source from the result.

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

For your add-a-parameter task, with DMS you would write the following transformation 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 recognizes only method signatures (by searching the AST, not the raw text). The quote marks are meta-quotes containing your target language fragments, and are need to differentiate target-language text from rule-language text. r, m, p are metavariables that must match specific structures as given the the signature of the rule; \\r \\m \\p are meta escapes in the target text saying that these structure must be present. The left hand side " \\r \\m ( \\p ) " matches signatures and binds r, m, p to the AST structures support it; the right hand side specifies a replace in which the bound values of r, m, p are substittued to get the replacmement. The conditional "if" is there to insist the only the desired method gets modified; you might need a more complex condition if you have a big pile of code and want to hit just a specific method in it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM