The simplest operation Moka can perform for you is reformatting Java source code. Used that way, Moka simply acts as a pretty printer. In the following examples, we will assume that your code originally looks like:
class Point {
// numPoints is a class variable
static int numPoints;
// x and y are instance variables
int x, y;
// w[0] is an array component
int[] w = new int[10];
// x is a method parameter
int setX(int x) {
// oldx is a local variable
int oldx = this.x;
this.x = x;
return oldx;
}
}
Generating source code
Moka can be used as a simple pretty-printer, as follows:
% moka source.java -out > source-new.java
This command line asks Moka to parse the file source.java and to re-emit it on the standard output, which is redirected to a file named source-new.java. Moka reconstructs a new source code that includes the original comments and a lot of the original formatting.
/* Generated by Moka using moka.stylesheet */
class Point
{
// numPoints is a class variable
static int numPoints;
// x and y are instance variables
int x, y;
// w[0] is an array component
int[] w = new int[10];
// x is a method parameter
int setX(int x)
{
// oldx is a local variable
int oldx = this .x;
this .x = x;
return oldx;
}
}
/* Thank you for using Moka. */
Generating colorized HTML
As the comments in the generated source code indicate, the formatting of your source is guided by a style sheet. The default style sheet is called moka.stylesheet. You can use another style sheet by using the -style option. For instance, you can format your source code as HTML using the html.stylesheet HTML stylesheet:
% moka source.java -style html -out > source.html
class Point
{
// numPoints is a class variable
static int numPoints;
// x and y are instance variables
int x, y;
// w[0] is an array component
int[] w = new int[10];
// x is a method parameter
int setX(int x)
{
// oldx is a local variable
int oldx = this .x;
this .x = x;
return oldx;
}
}
|