Recently I was investigating macros as a way to extend a language, and here this question came
There are several interesting projects that add macro functionality to Java at various levels.
Vishal's example is simple abbreviation (an advanced variant allows parameters),
Jatha can do that.
More interesting applications are where macros are a tool for building abstractions.
OpenJava extends Java's reflection system by providing a programmer with "metaobject" abstraction, via which he can query a state of the program (classes, methods...) and modify it at compile time, thus adding new features to the language. (syntax extensions have only limited support).
Even more advanced approach is to allow programmer extend syntax of the language with new constructs.
Java Syntactic Extender (JSE) is one such project,
Metamorphic Syntax Macros is another. (the latter paper has a nice general overview of macro functionality). You can, for example, amend Java with forEach operator:
forEach(Task elt in tasks)
elt.stop();
and whose expansion would be:
Iterator i = tasks.iterator();
while (i.hasNext()) {
Task elt = (Task)i.next();
elt.stop();
}
(this example is stolen from JSE paper)
As an extreme manifestation of power of macros, totally new, domain-specific languages can be build on base of a host language. On base of Java and "metamorphic syntax macros", a
DSL for Web services was built.
The Jakarta Tool Set (JTS) provides tools for creating domain specific languages.
[ August 11, 2002: Message edited by: Mapraputa Is ]