I see the use of ASM to print the values of all parameters passed in a method. I have got some examples, but I can not sense it. To be honest, maybe I have not done my "homework" in the sense that I have studied this as much as it has been done to make it, I have not studied it. But if someone is ready to help me, then it will be great.
For example, the meaning of the method is zero and takes an integer as the parameter.
(And I have seen the example on)
OK I understand it Here we go
Here the method is injector class I read it in a class file (print.class) and also adds instructions to print whenever the print ing method is called
asm package; Import java.io.file; Import java.io.FileInputStream; Import java.io.FileOutputStream; Import java.io.IOException; Import org.objectweb.asm.AnnotationVisitor; Import org.objectweb.asm.Attribute; Import org.objectweb.asm.ClassReader; Import org.objectweb.asm.ClassVisitor; Import org.objectweb.asm.ClassWriter; Import org.objectweb.asm.Label; Import org.objectweb.asm.MethodVisitor; Import org.objectweb.asm.opcodes; Import org.objectweb.asm.commons.AdviceAdapter; Public Class Print Injector Extends Class Visitor {Public Print Injector (IPI, Class Visitor MV) {Super (API, MV); } @ Override public method visitor visitMethod (int access, string name, string dave, string signature, string [] exception) {Method Viewer MV = CV. Visit method (access, name, des, signature, exception); System.out.println (string format ("% s% s% s", name, description, signature)); If (name.equals ("PrintInt")) mv = new PrintSingleIntParameter (Opcodes.ASM5, mv, access, name, desc); Return MV; } Public static zero main throws IOException {class reader CR = new class reader (new file inputstream (new file ("bin \\ print.class"))); Classwater cw = new classware (class wt.compute_mx | classdriver.compute_fAMS); PrintInjector pi = new print injector (Opcodes.ASM5, cw); Cr.accept (p, 0); FileOutputStream fos = New FileOutputStream (new file ("print category")); Fos.write (cw.toByteArray ()); Fos.flush (); Fos.close (); } Class PrintSingleIntParameter AdviceAdapter {Protects PrintSingleIntParameter (AP API, MethodViceter MV, Int Access, String Name, String Des) Expands {Super (API, MV, Access, Name, Dera); } @ Override Protected Zero to MathIntre () {Label L = New Label (); Mv.visitLabel (l0); Mv.visitFieldInsn (GETSTATIC, "Java / Lang / System", "Out", "Light / io / PrintStream;"); Mv.visitInsn (ILOAD, 0); // if 0 print instead of 1 static mv.visitMethodInsn (INVOKEVIRTUAL, "java / io / printstream", "println", "(i) v", was not wrong); }}}}
Here's my print.close
public class print {public static print print (int i) {System.out.println (i ); Return i; } Public static zero main (string [] args) {int a = PrintInt (10); Println ("full"); }}
Output
10 a: 10
You will need to adjust the code onMethodEveryone Now, I am realizing that the code has not been kept bytecode by me (as I use it in other programs when the registers are used after this code then it breaks. ).
No comments:
Post a Comment