Class Precompile

java.lang.Object
  extended by Precompile

public class Precompile
extends java.lang.Object

A simple class to precompile Java source code similar to C precompilation.

The idea behind this is in having an option to generate similar sources out of one code base. So you can comment / uncomment different parts of the code via precompile switches in the source files or as arguments to this class (-D).

Syntax:

java Precompile [-q] [-f] -i [-i ...] [-o [-o ...]] {-D...}
java Precompile -l -i [-i ...]
In the first mode, you give this class at least one directory as parameter, this is the directory, where the input source files reside (-i). Optionally, you specify with the -o switch, where Precompile.java should put the precompiled source files. If the output directory does already exist, and it is not empty, Precompile will stop. Use the -f switch, to force overwriting existing files in the output directory.
Optionally, you give any amount of defines with -Ddefine-name -Ddefine-name ... as additional parameters.

In the second mode, Precompile will list (-l or --list) all used define switches in all the source files only. Supported precompile switches are:


Constructor Summary
Precompile()
           
 
Method Summary
static void main(java.lang.String[] params)
          You can run the class stand alone via this method.
 java.lang.String[] precompile(java.lang.String[] inputDirs, java.lang.String[] outputDirs, boolean force, java.lang.String[] switches)
          This is the routine to precompile all *.java files in a given directory.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Precompile

public Precompile()
Method Detail

precompile

public java.lang.String[] precompile(java.lang.String[] inputDirs,
                                     java.lang.String[] outputDirs,
                                     boolean force,
                                     java.lang.String[] switches)
                              throws java.security.GeneralSecurityException,
                                     java.io.IOException
This is the routine to precompile all *.java files in a given directory. It takes directories as input parameter: in which directories do we look for the source files. Optionally, you can say, into which directory to put the precompiled sources. By default, Precompile.java will parse the input file for a package statement, and output the file to that directory.

Parameters:
inputDirs - In which directories are the java source files? ALL Java source files in these directories will be precompiled.
outputDirs - In which directories to put the precompiled source files. They do not have to exist, they will be created automatically if needed.
This parameter may be null or empty. In this case, the method will try to determine the output directory by parsing the input file for a package statement. This method is not absolutely fool prove, but should work in most cases, where the package statement is not wrapped up in some unusual context.
force - A boolean to force overwriting the contents of any existing output directories before writing to them. If force is false, and an output directory is not empty, a GeneralSecurityException is thrown.
switches - Any <DEFINES> to be used as precompile options. Can be null if none should be applied.
Returns:
A String array with warnings. Each warning String consists of three parts, first the filename, then the lineNumber and then the error message. All three parts are separated by '\n'.
Throws:
java.security.GeneralSecurityException
java.io.IOException

main

public static void main(java.lang.String[] params)
You can run the class stand alone via this method. As parameters, you have to specify at least one directory, optional are further input directories, output directories, and any number of switches for precompilation.

Parameters in detail:

  • -i <directory> The directory containing the input source files. All *.java files in this directory will be precompiled. You can give multiple directories with -idir1 -idir2 -idir3 ....
  • -o <directory> (Optional) The directory to where the precompiled source files will be written. You can give multiple directories in the same way as it is explained above for the -i switch.
    If you don't give output directories, Precompile.java will try to find a "package" statement in the source file, and use the package name as the output directory for that file. The method of finding the package name should be rather robust, but in case it fails, this is, you don't find the resulting file in the correct directory, specify the output directories with the -o switch(es). Also, if you prefer to put the output files into another directory, than the package name implies, you have to use -o .
  • -q (Optional) Quiet mode. Don't output anything except for errors.
  • -f (Optional) Force overwriting existing files in the output directories. Precompile.java will not overwrite contents in the output directory by default.
    When using Precompile.java without -o switch, you might first want to test without -f to see, if Precompile.java outputs to the directories you think. In following precompiles, you can then add the -f switch.
  • -D<switch> (Optional) Any switch to be used for precompilation. You can have any number of -D options.

Parameters:
params - An array of Strings. See above for a more detailed explanation of possible parameters.