[jboss-user] [Javassist user questions] - Re: Best Practice for 'Wrapping' a Method

parkinm do-not-reply at jboss.com
Wed Apr 23 08:16:40 EDT 2008


So here's what I'm doing to achieve the above. Assume that m is the original CtMethod, c is the class it's declared in, pool is a ClassPool and that we're adding a new parameter of type String.


  | // Add a new parameter of type string to the list of parameters
  | CtClass[] parameters = m.getParameterTypes();
  | CtClass[] newParameters = new CtClass[parameters.length + 1];
  | System.arraycopy(parameters, 0, newParameters, 0, parameters.length);
  | newParameters[parameters.length] = pool.get("java.lang.String");
  | 
  | // Create and configure method
  | CtClass returnType = m.getReturnType();
  | CtMethod newM = new CtMethod(returnType, m.getName(), newParameters, c);
  | newM.setExceptionTypes(m.getExceptionTypes());
  | newM.setModifiers(m.getModifiers());
  | 
  | // Add and configure method body
  | newM.setBody(m, null);
  | newM.insertBefore("// string parameter is $args[$args.length-1]");
  | 
  | // Add new method to class
  | c.addMethod(newM);
  | 

Is this the best way to do this?

Thanks,

Michael.

View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4146141#4146141

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4146141



More information about the jboss-user mailing list