This content has been marked as final.
Show 1 reply
-
1. Re: Best Practice for 'Wrapping' a Method
parkinm Apr 23, 2008 8:16 AM (in response to parkinm)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.