0

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

seba.wagner
  • 3,800
  • 4
  • 28
  • 52

1 Answers1

3

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.

Jeff Storey
  • 56,312
  • 72
  • 233
  • 406
  • You are right. Method interceptor, that was exactly the term I was searching for. But it seems like I would need a dynamic one as I would like to modify/manipulate this argument on each method call. And that seems to be very resource hungry as there is a string comparison involved in the Interceptor/Annotate method, to map the method name to the right interceptor. I guess I will rather do it with some Decorator pattern. – seba.wagner Jan 27 '13 at 05:59
  • 1
    If you wanted to you could write an annotation processor that modified the byte code at compile time. – Jeff Storey Jan 27 '13 at 14:45