Python-like decorators in Java?

One way to solve this in Java is to use an Aspect-oriented programming tool. One such tool is AspectJ You will probably find that this type of problem is an example that is commonly used to motivate AOP.

One way to solve this in Java is to use an Aspect-oriented programming tool. One such tool is AspectJ. You will probably find that this type of problem is an example that is commonly used to motivate AOP.

AOP might be a pretty heavyweight way to solve this particular problem, so you may want to explore just adding appropriate checks to each method. There isn't any direct equivalent to Python's decorators in native Java.

Thanks for the answer. I have a lot of other issues similar to this one, so it might not be too 'heavyweight'. I'll look into AspectJ as well as Guice.

– awegawef Dec 29 '10 at 17:23.

The simplest thing to do is to write a method like "assertCredentials" and call that method at the start of every method that needs credentials. If credentials are not set, the method should throw an exception, which will abort the parent method. Java has annotations that can be used to decorate methods, etc., but I don't think using annotations in this case would simplify things.

You should look into bean validation framework.

I don't think bean validation helps. 1) It is implied that other methods don't require credentials, so you cannot treat an object without credentials as "invalid". 2) The validation framework does not guarantee that objects are always valid anyway.

You have to call the "validate" method ... – Stephen C Dec 29 '10 at 6:19.

You can try Google Guice AOP which is relatively more lightweight than AOP framework like AspectJ.

Thanks, I'll look into it. +1 – awegawef Dec 29 '10 at 17:23.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions