This article explains how m4 can be used to achieve the result that different methods of the same class can be put in different files. This is advantageous if a class has lots of methods and/or many large methods. Keeping different methods in different files makes classes that use this system easier to maintain.
Suppose we have the following Java class:
// File Foo.java class Foo { // Some properties of the Foo class public int aProperty1; public int aProperty2; public int aProperty3; // Some methods of the Foo class public void apple() { // Method body of apple method goes here } public void banana() { // Method body of banana method goes here } public void carrot() { // Method body of carrot method goes here } }
If we make the following changes to the Foo class:
// File class-Foo.java m4_changecom() m4_changequote(,) class Foo { // Some properties of the Foo class public int aProperty1; public int aProperty2; public int aProperty3; // Some methods of the Foo class m4_include(class-Foo-method-apple.java); m4_include(class-Foo-method-banana.java); m4_include(class-Foo-method-carrot.java); }
The first two lines are needed to change the behaviour of m4 and can be ignored by users of this system. Note that the purpose of the semicolons after the include directive is so that indentation works correctly inside Emacs. If you don't use Emacs then you can avoid those semicolons. Using this system allows us to have methods of the Foo class in separate files, like so:
// File class-Foo-method-apple.java public void apple() { // Method body of apple method goes here }
// File class-Foo-method-banana.java public void banana() { // Method body of banana method goes here }
To get the building of the preprocessor to work, you will need to add the following lines to your Makefile.// File class-Foo-method-carrot.java public void carrot() { // Method body of carrot method goes here }
%.java: class-%.java class-%-method-*.java m4 -P class-$*.java >$*.java .PRECIOUS: %.java
Note that everything will still work if the Foo class is labelled as public, since the m4 generated file is Foo.java, the same name as the Foo class.
Back to Research Projects |
This page has the following hit count:
|