FileDocCategorySizeDatePackage
Rmic.javaAPI DocApache Ant 1.7024132Wed Dec 13 06:16:20 GMT 2006org.apache.tools.ant.taskdefs

Rmic

public class Rmic extends MatchingTask
Runs the rmic compiler against classes.

Rmic can be run on a single class (as specified with the classname attribute) or a number of classes at once (all classes below base that are neither _Stub nor _Skel classes). If you want to rmic a single class and this class is a class nested into another class, you have to specify the classname in the form Outer$$Inner instead of Outer.Inner.

It is possible to refine the set of files that are being rmiced. This can be done with the includes, includesfile, excludes, excludesfile and defaultexcludes attributes. With the includes or includesfile attribute you specify the files you want to have included by using patterns. The exclude or excludesfile attribute is used to specify the files you want to have excluded. This is also done with patterns. And finally with the defaultexcludes attribute, you can specify whether you want to use default exclusions or not. See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.

This task forms an implicit FileSet and supports all attributes of <fileset> (dir becomes base) as well as the nested <include>, <exclude> and <patternset> elements.

It is possible to use different compilers. This can be selected with the "build.rmic" property or the compiler attribute. There are three choices:

  • sun (the standard compiler of the JDK)
  • kaffe (the standard compiler of {@link Kaffe})
  • weblogic

The miniRMI project contains a compiler implementation for this task as well, please consult miniRMI's documentation to learn how to use it.

since
Ant 1.1
ant.task
category="java"

Fields Summary
public static final String
ERROR_RMIC_FAILED
rmic failed message
private File
baseDir
private String
classname
private File
sourceBase
private String
stubVersion
private org.apache.tools.ant.types.Path
compileClasspath
private org.apache.tools.ant.types.Path
extDirs
private boolean
verify
private boolean
filtering
private boolean
iiop
private String
iiopOpts
private boolean
idl
private String
idlOpts
private boolean
debug
private boolean
includeAntRuntime
private boolean
includeJavaRuntime
private Vector
compileList
private ClassLoader
loader
private org.apache.tools.ant.util.facade.FacadeTaskHelper
facade
public static final String
ERROR_UNABLE_TO_VERIFY_CLASS
unable to verify message
public static final String
ERROR_NOT_FOUND
could not be found message
public static final String
ERROR_NOT_DEFINED
not defined message
public static final String
ERROR_LOADING_CAUSED_EXCEPTION
loaded error message
public static final String
ERROR_NO_BASE_EXISTS
base not exists message
public static final String
ERROR_NOT_A_DIR
base not a directory message
public static final String
ERROR_BASE_NOT_SET
base attribute not set message
private static final org.apache.tools.ant.util.FileUtils
FILE_UTILS
Constructors Summary
public Rmic()
Constructor for Rmic.


            
      
        facade = new FacadeTaskHelper(RmicAdapterFactory.DEFAULT_COMPILER);
    

Methods Summary
public org.apache.tools.ant.types.PathcreateClasspath()
Creates a nested classpath element.

return
classpath

        if (compileClasspath == null) {
            compileClasspath = new Path(getProject());
        }
        return compileClasspath.createPath();
    
public org.apache.tools.ant.taskdefs.Rmic$ImplementationSpecificArgumentcreateCompilerArg()
Adds an implementation specific command line argument.

return
an object to be configured with a command line argument
since
Ant 1.5

        ImplementationSpecificArgument arg =
            new ImplementationSpecificArgument();
        facade.addImplementationArgument(arg);
        return arg;
    
public org.apache.tools.ant.types.PathcreateExtdirs()
Maybe creates a nested extdirs element.

return
path object to be configured with the extension directories

        if (extDirs == null) {
            extDirs = new Path(getProject());
        }
        return extDirs.createPath();
    
public voidexecute()
execute by creating an instance of an implementation class and getting to do the work

throws
org.apache.tools.ant.BuildException if there's a problem with baseDir or RMIC

        if (baseDir == null) {
            throw new BuildException(ERROR_BASE_NOT_SET, getLocation());
        }
        if (!baseDir.exists()) {
            throw new BuildException(ERROR_NO_BASE_EXISTS + baseDir, getLocation());
        }
        if (!baseDir.isDirectory()) {
            throw new BuildException(ERROR_NOT_A_DIR + baseDir, getLocation());
        }
        if (verify) {
            log("Verify has been turned on.", Project.MSG_VERBOSE);
        }

        RmicAdapter adapter = RmicAdapterFactory.getRmic(getCompiler(), this);

        // now we need to populate the compiler adapter
        adapter.setRmic(this);

        Path classpath = adapter.getClasspath();
        loader = getProject().createClassLoader(classpath);

        try {
            // scan base dirs to build up compile lists only if a
            // specific classname is not given
            if (classname == null) {
                DirectoryScanner ds = this.getDirectoryScanner(baseDir);
                String[] files = ds.getIncludedFiles();
                scanDir(baseDir, files, adapter.getMapper());
            } else {
                // otherwise perform a timestamp comparison - at least
                scanDir(baseDir,
                        new String[] {classname.replace('.",
                                                        File.separatorChar)
                                          + ".class"},
                        adapter.getMapper());
            }

            int fileCount = compileList.size();
            if (fileCount > 0) {
                log("RMI Compiling " + fileCount
                    + " class" + (fileCount > 1 ? "es" : "") + " to " + baseDir,
                    Project.MSG_INFO);

                // finally, lets execute the compiler!!
                if (!adapter.execute()) {
                    throw new BuildException(ERROR_RMIC_FAILED, getLocation());
                }
            }

            /*
             * Move the generated source file to the base directory.  If
             * base directory and sourcebase are the same, the generated
             * sources are already in place.
             */
            if (null != sourceBase && !baseDir.equals(sourceBase)
                && fileCount > 0) {
                if (idl) {
                    log("Cannot determine sourcefiles in idl mode, ",
                        Project.MSG_WARN);
                    log("sourcebase attribute will be ignored.",
                        Project.MSG_WARN);
                } else {
                    for (int j = 0; j < fileCount; j++) {
                        moveGeneratedFile(baseDir, sourceBase,
                                          (String) compileList.elementAt(j),
                                          adapter);
                    }
                }
            }
        } finally {
            compileList.removeAllElements();
        }
    
public java.io.FilegetBase()
Gets the base directory to output generated class.

return
the location of the compiled files

        return this.baseDir;
    
public java.lang.StringgetClassname()
Gets the class name to compile.

return
the name of the class to compile

        return classname;
    
public org.apache.tools.ant.types.PathgetClasspath()
Gets the classpath.

return
the classpath

        return compileClasspath;
    
public java.util.VectorgetCompileList()

return
the compile list.

        return compileList;
    
public java.lang.StringgetCompiler()
get the name of the current compiler

return
the name of the compiler
since
Ant 1.5

        facade.setMagicValue(getProject().getProperty("build.rmic"));
        return facade.getImplementation();
    
public java.lang.String[]getCurrentCompilerArgs()
Get the additional implementation specific command line arguments.

return
array of command line arguments, guaranteed to be non-null.
since
Ant 1.5

        getCompiler();
        return facade.getArgs();
    
public booleangetDebug()
Gets the debug flag.

return
debug

        return debug;
    
public org.apache.tools.ant.types.PathgetExtdirs()
Gets the extension directories that will be used during the compilation.

return
the extension directories to be used

        return extDirs;
    
public java.util.VectorgetFileList()
Gets file list to compile.

return
the list of files to compile.

        return compileList;
    
public booleangetFiltering()
Gets whether token filtering is set

return
filtering

        return filtering;
    
public booleangetIdl()
Gets IDL flags.

return
the idl flag

        return idl;
    
public java.lang.StringgetIdlopts()
Gets additional arguments for idl compile.

return
the idl options

        return idlOpts;
    
public booleangetIiop()
Gets iiop flags.

return
iiop

        return iiop;
    
public java.lang.StringgetIiopopts()
Gets additional arguments for iiop.

return
iiopOpts

        return iiopOpts;
    
public booleangetIncludeantruntime()
Gets whether or not the ant classpath is to be included in the task's classpath.

return
true if ant's classpath is to be included

        return includeAntRuntime;
    
public booleangetIncludejavaruntime()
Gets whether or not the java runtime should be included in this task's classpath.

return
true if default run-time libraries are included

        return includeJavaRuntime;
    
public java.lang.ClassLoadergetLoader()
Classloader for the user-specified classpath.

return
the classloader

        return loader;
    
public java.lang.ClassgetRemoteInterface(java.lang.Class testClass)
Returns the topmost interface that extends Remote for a given class - if one exists.

param
testClass the class to be tested
return
the topmost interface that extends Remote, or null if there is none.

        if (Remote.class.isAssignableFrom(testClass)) {
            Class [] interfaces = testClass.getInterfaces();
            if (interfaces != null) {
                for (int i = 0; i < interfaces.length; i++) {
                    if (Remote.class.isAssignableFrom(interfaces[i])) {
                        return interfaces[i];
                    }
                }
            }
        }
        return null;
    
public java.io.FilegetSourceBase()
Gets the source dirs to find the source java files.

return
sourceBase the directory containing the source files.

        return sourceBase;
    
public java.lang.StringgetStubVersion()
Gets the JDK version for the generated stub code.

return
stubVersion

        return stubVersion;
    
public booleangetVerify()
Get verify flag.

return
verify

        return verify;
    
public booleanisValidRmiRemote(java.lang.String classname)
Load named class and test whether it can be rmic'ed

param
classname the name of the class to be tested
return
true if the class can be rmic'ed

        try {
            Class testClass = loader.loadClass(classname);
            // One cannot RMIC an interface for "classic" RMI (JRMP)
            if (testClass.isInterface() && !iiop && !idl) {
                return false;
            }
            return isValidRmiRemote(testClass);
        } catch (ClassNotFoundException e) {
            log(ERROR_UNABLE_TO_VERIFY_CLASS + classname
                + ERROR_NOT_FOUND, Project.MSG_WARN);
        } catch (NoClassDefFoundError e) {
            log(ERROR_UNABLE_TO_VERIFY_CLASS + classname
                + ERROR_NOT_DEFINED, Project.MSG_WARN);
        } catch (Throwable t) {
            log(ERROR_UNABLE_TO_VERIFY_CLASS + classname
                + ERROR_LOADING_CAUSED_EXCEPTION
                + t.getMessage(), Project.MSG_WARN);
        }
        // we only get here if an exception has been thrown
        return false;
    
private booleanisValidRmiRemote(java.lang.Class testClass)
Check to see if the class or (super)interfaces implement java.rmi.Remote.

        return getRemoteInterface(testClass) != null;
    
private voidmoveGeneratedFile(java.io.File baseDir, java.io.File sourceBaseFile, java.lang.String classname, org.apache.tools.ant.taskdefs.rmic.RmicAdapter adapter)
Move the generated source file(s) to the base directory

throws
org.apache.tools.ant.BuildException When error copying/removing files.


        String classFileName =
            classname.replace('.", File.separatorChar) + ".class";
        String[] generatedFiles =
            adapter.getMapper().mapFileName(classFileName);

        for (int i = 0; i < generatedFiles.length; i++) {
            final String generatedFile = generatedFiles[i];
            if (!generatedFile.endsWith(".class")) {
                // don't know how to handle that - a IDL file doesn't
                // have a corresponding Java source for example.
                continue;
            }

            final int pos = generatedFile.length() - ".class".length();
            String sourceFileName =
                generatedFile.substring(0, pos) + ".java";

            File oldFile = new File(baseDir, sourceFileName);
            if (!oldFile.exists()) {
                // no source file generated, nothing to move
                continue;
            }

            File newFile = new File(sourceBaseFile, sourceFileName);
            try {
                if (filtering) {
                    FILE_UTILS.copyFile(oldFile, newFile,
                        new FilterSetCollection(getProject()
                                                .getGlobalFilterSet()));
                } else {
                    FILE_UTILS.copyFile(oldFile, newFile);
                }
                oldFile.delete();
            } catch (IOException ioe) {
                String msg = "Failed to copy " + oldFile + " to "
                    + newFile + " due to " + ioe.getMessage();
                throw new BuildException(msg, ioe, getLocation());
            }
        }
    
protected voidscanDir(java.io.File baseDir, java.lang.String[] files, org.apache.tools.ant.util.FileNameMapper mapper)
Scans the directory looking for class files to be compiled. The result is returned in the class variable compileList.

param
baseDir the base direction
param
files the list of files to scan
param
mapper the mapper of files to target files


        String[] newFiles = files;
        if (idl) {
            log("will leave uptodate test to rmic implementation in idl mode.",
                Project.MSG_VERBOSE);
        } else if (iiop
                   && iiopOpts != null && iiopOpts.indexOf("-always") > -1) {
            log("no uptodate test as -always option has been specified",
                Project.MSG_VERBOSE);
        } else {
            SourceFileScanner sfs = new SourceFileScanner(this);
            newFiles = sfs.restrict(files, baseDir, baseDir, mapper);
        }

        for (int i = 0; i < newFiles.length; i++) {
            String name = newFiles[i].replace(File.separatorChar, '.");
            name = name.substring(0, name.lastIndexOf(".class"));
            compileList.addElement(name);
        }
    
public voidsetBase(java.io.File base)
Sets the location to store the compiled files; required

param
base the location to store the compiled files

        this.baseDir = base;
    
public voidsetClassname(java.lang.String classname)
Sets the class to run rmic against; optional

param
classname the name of the class for rmic to create code for

        this.classname = classname;
    
public voidsetClasspath(org.apache.tools.ant.types.Path classpath)
Set the classpath to be used for this compilation.

param
classpath the classpath used for this compilation

        if (compileClasspath == null) {
            compileClasspath = classpath;
        } else {
            compileClasspath.append(classpath);
        }
    
public voidsetClasspathRef(org.apache.tools.ant.types.Reference pathRef)
Adds to the classpath a reference to a <path> defined elsewhere.

param
pathRef the reference to add to the classpath

        createClasspath().setRefid(pathRef);
    
public voidsetCompiler(java.lang.String compiler)
Sets the compiler implementation to use; optional, defaults to the value of the build.rmic property, or failing that, default compiler for the current VM

param
compiler the compiler implemention to use
since
Ant 1.5

        if (compiler.length() > 0) {
            facade.setImplementation(compiler);
        }
    
public voidsetDebug(boolean debug)
Generate debug info (passes -g to rmic); optional, defaults to false

param
debug turn on debug info

        this.debug = debug;
    
public voidsetExtdirs(org.apache.tools.ant.types.Path extDirs)
Sets the extension directories that will be used during the compilation; optional.

param
extDirs the extension directories to be used

        if (this.extDirs == null) {
            this.extDirs = extDirs;
        } else {
            this.extDirs.append(extDirs);
        }
    
public voidsetFiltering(boolean filter)
Sets token filtering [optional], default=false

param
filter turn on token filtering

        this.filtering = filter;
    
public voidsetIdl(boolean idl)
Indicates that IDL output should be generated. This defaults to false if not set.

param
idl generate IDL output

        this.idl = idl;
    
public voidsetIdlopts(java.lang.String idlOpts)
pass additional arguments for IDL compile

param
idlOpts additional IDL arguments

        this.idlOpts = idlOpts;
    
public voidsetIiop(boolean iiop)
Indicates that IIOP compatible stubs should be generated; optional, defaults to false if not set.

param
iiop generate IIOP compatible stubs

        this.iiop = iiop;
    
public voidsetIiopopts(java.lang.String iiopOpts)
Set additional arguments for iiop

param
iiopOpts additional arguments for iiop

        this.iiopOpts = iiopOpts;
    
public voidsetIncludeantruntime(boolean include)
Sets whether or not to include ant's own classpath in this task's classpath. Optional; default is true.

param
include if true include ant's classpath

        includeAntRuntime = include;
    
public voidsetIncludejavaruntime(boolean include)
task's classpath. Enables or disables including the default run-time libraries from the executing VM; optional, defaults to false

param
include if true include default run-time libraries

        includeJavaRuntime = include;
    
public voidsetSourceBase(java.io.File sourceBase)
optional directory to save generated source files to.

param
sourceBase the directory to save source files to.

        this.sourceBase = sourceBase;
    
public voidsetStubVersion(java.lang.String stubVersion)
Specify the JDK version for the generated stub code. Specify "1.1" to pass the "-v1.1" option to rmic.
param
stubVersion the JDK version

        this.stubVersion = stubVersion;
    
public voidsetVerify(boolean verify)
Flag to enable verification so that the classes found by the directory match are checked to see if they implement java.rmi.Remote. optional; This defaults to false if not set.

param
verify turn on verification for classes

        this.verify = verify;