+ Descriptor.userMethodResult(methodSig)
+ " " + methodName
+ Descriptor.userMethodArgs(methodSig));
// get method
final ClassMethod method = classFile.findMethod(methodName, methodSig);
affirm(method != null,
"Attempt to add code to a non-existing method.");
// check the found method
affirm(!method.isAbstract(),
"Attempt to add code to an abstract method.");
affirm(!method.isNative(),
"Attempt to add code to a native method.");
final CodeAttribute foundCodeAttr = method.codeAttribute();
affirm(foundCodeAttr != null); // by JVM spec
// prepend the new code to the current one
final Insn firstInsn = codeAttr.theCode();
affirm(firstInsn != null);
final Insn foundFirstInsn = foundCodeAttr.theCode();
affirm(foundFirstInsn != null);
final Insn lastInsn = firstInsn.append(foundFirstInsn);
affirm(lastInsn != null);
foundCodeAttr.setTheCode(firstInsn);
// ajust the method's stack and locals demand
foundCodeAttr.setStackUsed(max(foundCodeAttr.stackUsed(),
codeAttr.stackUsed()));
foundCodeAttr.setLocalsUsed(max(foundCodeAttr.localsUsed(),
codeAttr.localsUsed()));
// add the exception attribute or its exceptions
if (exceptAttr != null) {
affirm((exceptAttr.getExceptions().size()
== new HashSet(exceptAttr.getExceptions()).size()),
"Exception attribute contains duplicate exceptions.");
final ExceptionsAttribute foundExceptAttr
= method.exceptionsAttribute();
if (foundExceptAttr == null) {
// add the exception attribute
final AttributeVector methodAttrs = method.attributes();
affirm(methodAttrs != null);
methodAttrs.addElement(exceptAttr);
} else {
// add those exceptions not already present
final List foundEx = foundExceptAttr.getExceptions();