Writing Java Annotations, Imports in Generator

I am trying to generate Java code that uses a library (a jar file) called ORMlite.  To use it, I need to generate code that does a Java import and then adds annotations to classes and fields as below.  

I can’t figure out how to get the MPS base language to insert the imports and annotations.  I believe what I may have to do is import the jar file into MPS, and that maybe MPS will know to generate the import of the referenced library in the Java source?  I assume that also when MPS is aware of the possible annotations and will allow them once it knows about this jar?

Anyways, I tried adding the directory that contains the jar file by going to the Library Manager (IDE) in the settings, and clicking add.  This didn’t seem to help.

I also tried going to the language properties “design time” tab and adding the jar to the libraries pane.  I still can’t add annotations to the fields in the generator like I need to as shown below.  I would appreciate any help.  Also, it would help tremendously if MPS had a more complete set of documentation.


import com.j256.ormlite.field.DatabaseField;
@DatabaseTable(tableName = "accounts")
public class Account {

    // for QueryBuilder to be able to find the fields
    public static final String NAME_FIELD_NAME = "name";
    public static final String PASSWORD_FIELD_NAME = "passwd";

    @DatabaseField(generatedId = true)
    private int id;

Comment actions Permalink
OK, after poking around I figured out a few things, but I still can't write the annotations.

I did figure out that there's the runtime tab as well as the design time tab.  It's not clear what the function is of each, but eventually after adding the jars to both tabs in the runtime libraries/libraries panes, I was able to see the classes from these libraries in the project navigation tree at Languge/all models/stubs.  At this point I was able to reference elements in the jar files from within my generated class, like I could extend my class from classes that are in the jar file.  This was not helpful to generate the code I pasted above, but it's good to know how to do this.

I also figured out how to generate annotations, by clicking on the class or field and using the pop-up intention menu to select "add annotation."  The only problem is that the annotations that I need (see above code) are not present, only the standard Java annotations like @Deprecated, @Overrides, etc.  I'm sure there's just something I am not understanding.  

Any help would be greatly appreciated.

Comment actions Permalink
Hi Jeff,

i m not a specialist in MPS, but i think the solution is:

after adding the jar in design-time you can add the library also in the generator.

at the generator "main@generator" -> model properties -> common -> imported models -> your library should already be selectable - then you could use your annotations

By the way, i m working on pretty much the same as you. I use hibernate instead of ormlite to define business objects. Should we share our experience somehow?

Comment actions Permalink
Thanks so much Dan; it worked beautifully!  MPS appears to be such a powerful tool, it just needs major improvements in documentation.  It seems as each of the generative tools has its problems.

I would love to collaborate somehow.  Where are you located?  If you want, you can send me an email and I can explain what I am working on.  

My email address is the same as my forum name with no spaces @gmail.com

I look forward to hearing from you.

Comment actions Permalink
The only problem is that the annotations that I need (see above code) are not present, only the standard Java annotations like @Deprecated, @Overrides, etc.  I'm sure there's just something I am not understanding.

To be able to reference annotations (and any other java elements) defined in some package you need to import corresponding model into a model you'd like to add this reference to. To import a model press Ctrl+M and type FQ name of java package -> you'll see the list of available packages suffixed with @java_stub. For example: choose "java.io@java_stub" for io package from JDK.

Please sign in to leave a comment.