Search code examples
androidc++java-native-interfacecode-generationtemplate-engine

Tool to generate C++, Java, XML from a template


I am looking for a tool that would allow me to:

  • Define code templates with replaceable strings. Example - <NAMESPACE> would be replaceable:

    jclass pJNIActivityClass = pJNIEnv->FindClass("<NAMESPACE>");

  • Automate code snippet insertion based on a regex. Example - new code would be inserted at //##:

    public void onCreate ( Bundle savedInstanceState ){
        super.onCreate  ( savedInstanceState ) ;
        //##
    

The existing tools I've found are language specific. I don't really need the tool to understand a specific language, although it would make life easier if it could parse XML.

For some background. I'm working in an environment where a closed-source application exports an eclipse or visual studio project (android or windows). This exported code is always identical with exception to the project namespace.

The idea here is to be able to run a tool on these stock exported projects to automate the implementation of common extra functionality.


Solution

  • For a FOSS solution you should take a look at Acceleo from the Eclipse Foundation. It uses languages like UML as input and allows you to generate output for any text based language. It may be a bit heavy in terms of size and features and it requires an installation of Eclipse but it's very good at code generation. It also handles large and complex template source very well and provides the type of functionality you expect to have in an IDE. I have only used older versions but my experience with it was good.

    If you are unable to find a FOSS solution and a commercial solution is acceptable I recommend EntiPro. It has been a while since I used it but my experience with it was very positive. It's powerful and flexible and seems to meet the requirements you outlined in your question. It's also inexpensive at $40 for a single seat developer license.

    In the event that you are unable to find a FOSS solution or don't want to drop the $40 on EntiPro you may be left to implementing a solution yourself. If that turns out to be the case I highly recommend using the String Template Engine by Terrance Parr.