GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. Language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


Splitting Java class files into multiple source files Free Stuff

Abstract

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.

1. The system

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
}
  
// File class-Foo-method-carrot.java
public void carrot()
{
   // Method body of carrot method goes here
}
  
To get the building of the preprocessor to work, you will need to add the following lines to your Makefile.
%.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:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. Language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Sun Sep 25 16:12:03 NZDT 2016
Best viewed at 800x600 or above resolution.
© Copyright 1999-2016 Davin Pearson.
Please report any broken links to