Search code examples
javaparametersannotationsparameter-passingjava-6

modifying parameter prior to method invocation


I have a parameter called "server" in several methods:

public synchronized Client getClientByStreamId(String streamId, Server server) {
  //some logic
}

public Client getClientByPublicSID(String publicSID, boolean isAVClient, Server server) {
  //some logic
}

What I want is that everytime those methods are called there is a check, that is performed to the param server before the method is called:

if (server == null) {
  server = someServer; //someServer is a variable I get somewhere else
}

From what I know there must be some trick with an annotation in Java6 so that you can do something like (pseudo code):

@ManipulateArgs(MyMethod);
public synchronized Client getClientByStreamId(String streamId, Server server) {
  //some logic
}

@ManipulateArgs(MyMethod);
public Client getClientByPublicSID(String publicSID, boolean isAVClient, Server server) {
  //some logic
}

private MyMethod(Server server) {
  if (server == null) {
    server = someServer; //someServer is a variable I get somewhere else
  }
}

Some kind of "pre-processor" that is called before the actual method is invoked with the params. I just can't remember what the name of this pre-processing was. Or if it was annotaction based or something else. But I think it was an annotation.

Thanks
Sebastian


Solution

  • The general term of what you're looking for is a method interceptor. There are a variety of ways they are implemented. Aspect oriented programming is one way. See the tutorial http://www.javaworld.com/javaworld/jw-01-2002/jw-0118-aspect.html. If you're using a framework such as Spring or JBoss, there are interceptor annotations built in.