views:

95

answers:

2

After a bunch of XML config files, I've seen Java moving to Annotation based configurations.

Are annotations playing the role of DSL here?

Is it because the static nature of Java? I'm thinking in Ruby which doesn't have ( afaik ) things like that. Is it because Ruby has good metaprogramming capabilities?

Are there alternatives ( I mean other than using a bunch of .xml files )

+1  A: 

Basically annotations are a tool that allows you to process source files at compile-time and do action corresponding to annotations found in the file (possibily deriving a new source).

They are quite useful for many purposes like expliciting constraints while avoiding cluttering the code or enrich the behaviour of some methods.

I wouldn't say that they are so similar to DSLs of Ruby since in this case you annotate code with a particular syntax while in Ruby you can design your own DSL from scratches and use it as you wish.

Java ships a tool called apt (like the one you suspect) that is able also to work with annotations at run-time but they are usually used to give compile-time infos to your sources. This doesn't mean that in certain circumstances you can't easily adapt the annotation mechanism to work out the same things that you would obtain with a DSL but they don't exist for the same purpose.

Jack
I don't think it's accurate at all to say that annotations are usually used for compile-time processing. I think the main use of annotations lately has been as metadata that allows interesting and useful things to be done at runtime.
ColinD
I'm talking about what they were born for. Now the concept has been widely extended to overcome the lacks of Java language :) But I wouldn't say that they cover the purpose of DSLs.. would you?
Jack
What's the alternative ( in compiled programming languages in general ) ?
OscarRyz
I would say that in case of Java the best alternative is to use an extension like Groovy that allows metaprogramming with DSLs easily as in ruby..
Jack
A: 

As already said, annotation can be used to create DSLs quite efficiently, bacause they add some sort of metaprogramming capabilities to the langauge. However for that purpose you could use byte code injector or even any other Java language feature. However the primary purpose of annotations is to be able to annotate source code elements with metadata. If you are asking for alternatives for creating internal DSLs in Java, just look at the Fowler's DSL book WIP and choose from different concepts which can be used for implementing internal DSLs, many of them are present in Java. If you are asking for alternatives for metaprogramming, then there are also many: different byte code injectors, aspect oriented programming using AspectJ or Spring or code generation.

Gabriel Ščerbák