Re: Method Proxy Adapter
Mark <[email protected]>
| Newsgroups | gmane.comp.java.objectweb.asm |
|---|---|
| Message-ID | <[email protected]> |
I fixed the issue (many) with my first attempt. I used the bytecode outline - to do all the work for me. However is there a better way to do write a MethodProxyAdapter ? On 17 February 2010 22:04, <[email protected]> wrote: > I have written an addapter that essentially renames a target method by > pre-pending an _ then creates a new method with the originall name that > calls > the renamed method > > eg: > > public String getName(){ > return name; > } > > gets re-written as: > > public String _getName(){ > return name; > } > > public String getName(){ > return this._getName(); > } > > my adapter creates: > > public class TestClass2 > { > private String name = "Test_Class_2"; > > public String getName() { return this._getName; } > public String _getName() { return this.name; > } > } > > _getName is missing () the brackets - a noob question as to what is wrong? > Also > is there a better way to do this ? > > public class MethodProxyAdapter extends ClassAdapter { > > private String matchDesc; > private String matchName; > > public MethodProxyAdapter(ClassVisitor cv, String matchDesc, String > matchName) { > super(cv); > this.matchDesc = matchDesc; > this.matchName = matchName; > } > > @Override > public MethodVisitor visitMethod(int access, String name, String > desc, > String signature, String[] exceptions) { > if (matchDesc.equals(desc) && matchName.equals(name)) { > System.out.println("Method Intercepted desc: " + > desc + > " name: " + name); > String newName = "_" + name; > createProxyMethod(access, name, desc, signature, > exceptions,newName); > MethodVisitor visitMethod = > super.visitMethod(access, > newName, desc, signature, exceptions); > return visitMethod; > } > return super.visitMethod(access, name, desc, signature, > exceptions); > } > > private void createProxyMethod(int access, String name, String desc, > String signature, String[] exceptions, String newName) { > // Honour the original method contract > MethodVisitor mv = cv.visitMethod(access, name, desc, > signature, exceptions); > // Write the new name > mv.visitCode(); > mv.visitVarInsn(Opcodes.ALOAD, 0); > mv.visitFieldInsn(Opcodes.GETFIELD, "this", newName, > "()Ljava/lang/String;"); > mv.visitInsn(Opcodes.IRETURN); > mv.visitMaxs(1, 1); > mv.visitEnd(); > } > > } > > > -- > You receive this message as a subscriber of the [email protected] mailing list. > To unsubscribe: mailto:[email protected] > For general help: mailto:[email protected]?subject=help > OW2 mailing lists service home page: http://www.ow2.org/wws > >
message-footer.txt
(text/plain, 238 B)
-- You receive this message as a subscriber of the [email protected] mailing list. To unsubscribe: mailto:[email protected] For general help: mailto:[email protected]?subject=help OW2 mailing lists service home page: http://www.ow2.org/wws