10

I'm interested in metaprogramming (i.e. programs that help programmers do tedious programming tasks). I'm looking for a tool which has the following properties:

  • usable both at compile time and runtime;
  • inspects program structure;
  • can add new classes, methods or fields and make them visible to Java compiler;
  • can change behavior of methods;
  • Java-based (well, Java is most popular programming language according to some rankings);
  • good integration with IDEs and build tools like Ant, Gradle or Maven;
  • actively maintained project;
  • easy to use and extend;

There are some solutions for this, like:

  • reflection
  • AspectJ
  • Annotation Processing Tool
  • bytecode manipulation (CGLIB, Javassist, java.lang.instrument)
  • Eclipse JDT
  • Project Lombok
  • Groovy, JRuby, Scala

But unfortunately none of them meets all the criteria above. Is there any complete metaprogramming solution for Java?

13
  • 1
    I'd remove the "will you start a new project for this with me?"-part. SO is not the right place to discuss this. Commented Sep 12, 2011 at 14:15
  • 7
    Groovy seems to fit the bill. The "Java compiler" doesn't run during runtime, aside from hotspot, so I'm not sure what you want there. Commented Sep 12, 2011 at 14:17
  • 1
    What's the better place then? :-) This is just a question, not project planning, so I think that this place is OK. Commented Sep 12, 2011 at 14:17
  • 3
    @iirekm: my suggestion is start the project and put it on Github. Nothing attracts contributors better than working code. Commented Sep 12, 2011 at 14:21
  • 2
    @iirekm: Intelli-J has great Groovy support. Eclipse is a piece of trash anyway, groovy aside. Commented Sep 12, 2011 at 14:43

4 Answers 4

2

There's JackPot, which is Java based but I don't think gets a lot a current attention. Has ASTs and symbol tables AFAIK. You can probably extend it; I doubt anybody will stop (or help) you.

There's the Java-based compiler APIs for the Sun, er, Oracle java compiler. They're likely actively maintained, but I don't think you can modify source code and regenerate it. Certainly has symbol tables; dunno about trees. Probably pretty hard to extend; you have to keep up with the compiler guys, not the other way round.

There is ANTLR, which has a Java implementation and a Java parser that will build ASTs. I don't think it has full symbol tables, so doing serious code analysis/revision is likely to be hard. ANTLR is certainly actively maintained, and nobody will object to you enhancing the Java grammar with symbol tables. Just know that will take you about 6 months for Java 1.6 if that's all you do. (That's how long it took our internal [smart] guy to do it for DMS, starting with symbol table support for 1.4).

Not in Java, and not easily integrated into IDEs, but capable of carrying massive analysis and transformation on Java code is our DMS Software Reengineering Toolkit with its Java Front End.

DMS is generic compiler machinery: parsing, AST building, symbol table machinery, flow analysis machinery, with that additional bonuses of source-to-source transformations and generic prettyprinting of ASTs back to legal text including retention of comments. It offers a set of APIs supporting these services, and additional tools for defining grammars and langauge-dependent flow analyzers.

The Java Front End gives crucial detail (using those APIs) to DMS to allow it process Java: a grammar/parser, full symbol table construction for Java 1.4-1.6 (with 1.7 due momentarily), as well as some control and data flow analysis (to be extended over time because this stuff is so useful).

By using the services provided by DMS and the Java Front end, one can reasonably contemplate building arbitrary Java anlaysis and transformation tools. (This makes the tool a "complete" metaprogramming tool, in that it can inspect any language structure, or change any language structure, as opposed to say template metaprogramming or reflection). We believe this to be much more effective than ad hoc tools because you don't have to build the infrastructure, the infrastructure provided is robust and handles cases you don't have the energy to implement, and it is designed to support such tasks. YMMV.

DMS/Java Front end have been used to construct a variety of Java tools: test coverage, profilers, dead code elimination, clone detection on scale, JavaDoc with hyperlinked source-code, fast XML parser/generators, etc.

Yes, its actively maintained; undergoing continuous enhancement since the first version in 1998.

Sign up to request clarification or add additional context in comments.

4 Comments

I've heard about ANTLR and javac API before - too hard to use, and javac APIs are very limited, so the only choice is to use private com.sun.tools.javac classes as e.g. Lombok does, but they probably can change at any moment, and are Oracle's javac specific.
As for JackPot - most links on the Internet about it seem to be dead. :-( DMS may be very interesting - I'll try it soon, although there's a bit of pity that it seems to be a paid tool.
@iirekm bitbucket.org/jlahoda/jackpot30/wiki/Home, it is now part of NetBeans.
@iirekm: "A bit of pity its a paid tool..." well, maybe. It wouldn't exist if it weren't a paid tool; there's nothing like it in open source community.
2

There's a Java metaprogramming framework that is part of Tapestry IOC, it's called Plastic. It munges class bytecodes using custom classloaders, I haven't tried it yet but it looks like it gives a simple interface that still enables the programmer to make powerful metaprogramming changes.

5 Comments

Thanks, I'll try it. I hope I'll have some time this weekend.
I believe the URL is builds.apache.org/job/tapestry-trunk-freestyle/javadoc/… -- also, one of the Tapestry committers has written a series of blogs on Plastic that are quite instructive: tawus.wordpress.com/category/plastic
I took a quick look at Plastic. There's a similar tool called Javassist (javassist.org ) - I don't know which of the two is better in terms of functionality, but Javassist is surely much more popular and has better documentation. I'm looking here for something more Lombok-ish (projectlombok.org ), but Lombok itself is poorly designed (eg in order to add custom transforms we have to do copy&paste to make it work on say: both javac and Eclipse).
@iirekm: javassist is at a lower level of abstraction. Plastic is designed to be easy to use.
Their APIs seem very similar to me, eg: PlasticClass -> CtClass, PlasticClassTransformer -> Transformer, pm.getClassInstantiator("plasticdemo.controlled.Foo").newInstance() -> classPool.get("plasticdemo.controlled.Foo").toClass().newInstance(), plasticClass.introduceField -> ctClass.addField, ... and so on
0

Check out the Meta Programming System:

http://www.jetbrains.com/mps/

It has great IDE support and is used quite frequently by the smart folks at JetBrains.

2 Comments

I haven't tried it yet. Does it support all (or at least most) of the requirements from my checklist?
I took a look at it. If I understood well, it's a framework for designing DSLs, and then generating Java code from it. Here by 'metaprogramming' I mean something more general, not only generating but also changing behavior of existing code, both at compile time and runtime.
0

Check out Spring Roo.

1 Comment

This was flagged as a low quality answer. If you are just going to provide a link then it would be better as a comment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.