[英]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. 我需要一个库或方法来从模板文本文件生成一个java类(只是生成类的源代码作为文本格式,不需要运行或使用它)。
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. 我搜索了一些库以进行此操作FreeMarker和ApacheVelocity被推荐用于一些类似的问题。 But I dont exactly understand the how to do this operation with FreeMarker.
但我并不完全理解如何使用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?
我认为它很可能用作标记库(如JSTL)我不想在代码中使用模板关键字(如$(temp))只是函数“doImport”,“addFunction”,“addParameterToFunction”等等。是吗可以使用这个库,或者任何人都可以发送一些有关此操作的示例?
Will this help? 这会有帮助吗? - JET templates
- JET模板
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
为了实现你的目的,你会说,“如果你在类X中看到一组类声明,那么添加这个类”,通常表示为
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软件重新设计工具包是一个程序转换工具,它将读取源代码,构建编译器数据结构(AST,符号表,流程图),允许您将源到源重写应用于表示为这些结构的代码,使用源代码匹配/替换的模式,然后从结果中重新生成有效的源。
DMS has parser/prettyprinters for many languages, including Java (1.4/1.5/1.6), C, C++, C#, COBOL, PHP, JavaScript, ... DMS有许多语言的解析器/ prettyprinters,包括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-a-parameter任务,使用DMS可以编写以下转换规则:
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). ( - >对应于“rewritesto”)这个只能识别方法签名(通过搜索AST,而不是原始文本)。 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是在给定规则的签名时必须与特定结构匹配的元变量; \\r \\m \\p are meta escapes in the target text saying that these structure must be present.
\\ r \\ m \\ p是目标文本中的元转义,表示必须存在这些结构。 The left hand side " \\r \\m ( \\p ) " matches signatures and binds r, m, p to the AST structures support it;
左侧“\\ r \\ m(\\ p)”匹配签名并将r,m,p绑定到AST结构支持它; the right hand side specifies a replace in which the bound values of r, m, p are substittued to get the replacmement.
右侧指定替换,其中r,m,p的边界值被替代以获得替换。 The conditional "if" is there to insist the only the desired method gets modified;
条件“if”是坚持只有所需的方法被修改; 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.
如果您有大量代码并想要只使用其中的特定方法,则可能需要更复杂的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.