I spend most of my time programming in Python, so forgive me if my approach to this problem is short-sited:
I want to have certain methods of a class require login credentials. Simply, each method should check whether the class variable user is set, and if so, continue, but if not, spit out a "you need to login" message.
In Python, I would just write a decorator to do this. How can I accomplish the same thing in java with as little redundant code as possible?
Thanks!
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.
Python-like decorators in Java?