• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 676
  • Last Modified:

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.



James Elliott
James Elliott
  • 2
2 Solutions
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: http://jmar777.blogspot.com/2008/02/java-reflection-hibernate-proxy-objects.html
Method[] methods = ClassType.class.getMethods();
for(Method method: methods) {
  Annotation[] annotations = method.getAnnotations();

Open in new window

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:

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now