Class Munge
To avoid creating a separate Java dialect, the conditional tags are contained in Java comments. This allows one build to compile the source files without pre-processing, to facilitate faster incremental development. Other builds from the same source have their code contained within that comment. The format of the tags is a little verbose, so that the tags won't accidentally be used by other comment readers such as javadoc. Munge tags must be in C-style comments; C++-style comments may be used to comment code within a comment.
To demonstrate this, our sample source has 1.1 and 1.2-specific code, with 1.1 as the default build:
public void setSystemProperty(String key, String value) {
/*if[JDK1.1]*/
Properties props = System.getProperties();
props.setProperty(key, value);
System.setProperties(props);
/*end[JDK1.1]*/
/*if[JDK1.2]
// Use the new System method.
System.setProperty(key, value);
end[JDK1.2]*/
}
When the above code is directly compiled, the code bracketed by the JDK1.1 tags will be used. If the file is run through Munge with the JDK1.2 tag defined, the second code block will used instead. This code can also be written as:
public void setSystemProperty(String key, String value) {
/*if[JDK1.2]
// Use the new System method.
System.setProperty(key, value);
else[JDK1.2]*/
Properties props = System.getProperties();
props.setProperty(key, value);
System.setProperties(props);
/*end[JDK1.2]*/
}
Munge also performs text substitution; the Swing build uses this to
convert its package references from javax.swing
to java.awt.swing
, for example. This substitution is
has no knowledge of Java syntax, so only use it to convert strings
which are unambiguous. Substitutions are made in the same order as
the arguments are specified, so the first substitution is made over
the whole file before the second one, and so on.
Munge's command line takes one of the following forms:
java Munge [-D<symbol> ...] [-s <old>=<new> ...] [<in file>] [<out file>]
java Munge [-D<symbol> ...] [-s <old>=<new> ...] <file> ... <directory>
In the first form, if no output file is given, System.out is used. If
neither input nor output file are given, System.in and System.out are used.
Munge can also take an @<cmdfile>
argument. If one is
specified then the given file is read for additional command line arguments.
Like any preprocessor, developers must be careful not to abuse its capabilities so that their code becomes unreadable. Please use it as little as possible.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescription(package private) static class
This class was cut and pasted from the JDK1.2 sun.tools.util package. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) String
(package private) final int
(package private) final String[]
(package private) final int
(package private) final int
(package private) final int
(package private) final int
(package private) int
(package private) final int
(package private) final int
(package private) BufferedReader
(package private) String
(package private) int
(package private) static Vector
(package private) final int
(package private) static Vector
(package private) PrintWriter
(package private) boolean
(package private) String
(package private) Stack
(package private) static Hashtable
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
(package private) void
cmd_else()
(package private) void
cmd_end()
(package private) void
(package private) void
cmd_if_not
(String version) (package private) int
countLines
(String s) (package private) static int
countQuotes
(String input) void
(package private) static int
findCommentStart
(String source, int fromIndex) (package private) int
getCommand
(String s) boolean
static void
Munge's main entry point.(package private) int
(package private) void
void
void
process()
(package private) void
processComment
(String comment) (package private) void
static void
usage()
Report how this utility is used and exit.static void
-
Field Details
-
symbols
-
oldTextStrings
-
newTextStrings
-
errors
int errors -
line
int line -
inName
String inName -
in
-
out
PrintWriter out -
stack
Stack stack -
printing
boolean printing -
source
String source -
block
String block -
commands
-
IF
final int IF- See Also:
-
IF_NOT
final int IF_NOT- See Also:
-
ELSE
final int ELSE- See Also:
-
END
final int END- See Also:
-
numCommands
final int numCommands- See Also:
-
EOF
final int EOF- See Also:
-
COMMENT
final int COMMENT- See Also:
-
CODE
final int CODE- See Also:
-
-
Constructor Details
-
Munge
-
-
Method Details
-
getCommand
-
error
-
printErrorCount
public void printErrorCount() -
hasErrors
public boolean hasErrors() -
close
- Throws:
IOException
-
cmd_if
-
cmd_if_not
-
cmd_else
void cmd_else() -
cmd_end
- Throws:
EmptyStackException
-
print
- Throws:
IOException
-
countLines
-
processComment
- Throws:
IOException
-
nextBlock
- Throws:
IOException
-
findCommentStart
-
countQuotes
-
substitute
void substitute() -
process
- Throws:
IOException
-
usage
public static void usage()Report how this utility is used and exit. -
usage
-
main
Munge's main entry point.
-