Metadata programming?

Hi Experts,

I do a fair amount of programming in .Net (VB, C#), and I design SQL Databases.

My boss has asked me to investigate Java as a new platform, and in particular, the strenghts of metadata programming. I've read the principals of this in various resources but I can't quite visualise the power of it.

Can someone please break it down for me.

What is it?
How is it different?
What IS the power of it?

Examples would be great.



LVL 12
James ElliottManaging DirectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Metadata programming is fairly straight forward in Java.  For the most part it is done through the use of annotations (which can be applied to classes, methods, properties, and more).

For example, if you wanted to specify that a property should not be null, then you could annotate the public accessor for it like this:

public ClassType getMyProperty() {
  return myProperty;

Of course you can annotate more than just public class members, but it is a good convention.  In the example above, the @NotNull annotation would presumably be consumed at runtime, but you can specify annotations that are only used in the source or during compilation, and then discarded.

Runtime annotation consumers will usually evaluate metadata through introspection and reflection.  For example, you can inspect all the method annotations in a class using the snippet below.

I did run into my own problem though, recently while trying to inspect annotations on a proxy object.  I wrote an overview of my solution to that problem here:
Method[] methods = ClassType.class.getMethods();
for(Method method: methods) {
  Annotation[] annotations = method.getAnnotations();

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Just to answer the third question, the power is HUGE if used correctly.  Annotations can be accessed from anywhere that you have access to the class, so you can make metadata based decisions in the view, the controller, your service layer..... anywhere.

Certain problems that have the potential to be big can be solved easily with annotations.  In the example I provided in the link above, I needed to mark certain form members as "Recommended."  I needed to be able to indicate these fields in the view, and process them appropriately in the service layer, and there was really no other practical approach that would allow me to mark these fields in one place and one place only.  Putting the annotations right into the model object was the most logical and convenient solution - and it was only possible through annotations.
James ElliottManaging DirectorAuthor Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.