/**
* Build a dummy method that delegates the call to the native method
*/
private void createNativeMethod(int access, String name, String desc,
String signature, String[] exceptions) {
Method newMethod = createNewMethod(name, desc);
final int newAccess = access & ~Opcodes.ACC_NATIVE;
MethodVisitor copyMethod = super.visitMethod(newAccess | Opcodes.ACC_SYNTHETIC, name, newMethod.getDescriptor(),
signature, exceptions);
copyMethod.visitCode();
// load the arguments before calling the original method
final boolean isStatic = (access & ~Opcodes.ACC_STATIC) != 0;
int place = 0; // place on the stack
if(!isStatic){
copyMethod.visitVarInsn(Opcodes.ALOAD, 0); // load this
place = 1;
}
Type[] argumentTypes = newMethod.getArgumentTypes();
for(int i=0 ; i<(argumentTypes.length-1) ; ++i){
Type type = argumentTypes[i];
copyMethod.visitVarInsn(type.getOpcode(Opcodes.ILOAD), place);
place += type.getSize();
}
// call the original method
copyMethod.visitMethodInsn(isStatic ? Opcodes.INVOKESTATIC : Opcodes.INVOKEVIRTUAL, className, name, desc);
TypeCodeResolver returnReolver = TypeCodeResolverFactory.getReolver(newMethod.getReturnType());
if( returnReolver == null) {
copyMethod.visitInsn( Opcodes.RETURN); // return;
}else {
copyMethod.visitInsn(returnReolver.returnCode());
}