Category: Code generation

Q

How do I import Java types into the code generator?

A

You have many ways of doing this. Assuming you're talking about the GUI version of the code generator, you can simply drag and drop .class, .jar, or .zip files onto it. If you had no model window open, this will create a new Java model and import the dropped types plus referenced types into the new model (this might require you to specify additional classpath roots if the imported files contain references to types in other, non-imported files). If you had a model open and dropped the files into the tree part of the model window, the dropped types will be added to the already open model. Alternatively, you can also use the File Import dialog to browse for files to import.

You can also import any type by name if it is available on the so-called model classpath. The model classpath consists of all classpath roots that you explicitly specified or that you added by importing files from the file system. Use the File Import Class from Classpath menuitem. This option allows you to import a specific type by its typename.

These two import variants are of course expressable both in ANT and in mdelfiles.

In ANT, they are represented by <modelfile> nested elements called <import> and <importclass>. The <import> element is a fileset and thereby grants you all of ANT's power of using inclusion and exclusion patterns to specify a set of files. The <importclass> element expects a single name attribute having the typename as its value.

ANT snippet of import elements

<modelfile name="test">
 
    <!-- import the Java types in two jarfiles -->
    <import dir="${basedir}/lib">
        <include name="myapi.jar"/>
        <include name="myutil.jar"/>
    </import>
 
    <!-- import the Java types in one package -->
    <importclass name="java.sql.*" />
 
    <!-- import a Java type by name -->
    <importclass name="java.util.Vector" />
 
</modelfile>

In a modelfile, they are represented by methods called importClassesJarsAndZips() and importClassNamed(). The former method takes an array of strings representing filenames, the latter method takes one string representing teh typename as an argument.

Modelfile snippet of import methods

tm=new JavaModelF("test", theClasspath, theLibdir); 
 
tm.importClassesJarsAndZips( new String[] { theBaseDir + "/myapi.jar", 
theBaseDir + "/myutil.jar" }; tm.importClassNamed( "java.sql.*" ); tm.importClassNamed( "java.util.Vector" );

Copyright 2006-2011 by Codemesh, Inc., ALL RIGHTS RESERVED

:
frequently asked questions
home products support customers partners newsroom about us contact us