0

I want to modify a specific library with ASM for log or modify content. Simply I create an example at above two method for example.
I want to convert realMethod to iWantAsLikeThis method with ASM.

Usually add like INVOKESTATIC asmTest/Testt.logGet (Ljava/net/URL;)Ljava/net/URL; code enough but sometimes it is not enough as at bottom I highlight with ______ and there is like DUP and POP and maybe another things for different codes.

So what can I do for generic wrapping to modify classes of specific libraries.

Note: I don't want to modify constructer (<init>) method, I want to modify usages of its.

public class Testt {

    public void realMethod() throws Exception {
        URL u1 = new URL("http://1.1.1.1");
        URL u2 = new URL("http://2.2.2.2");
        System.out.println(u1);
    }

    public void iWantAsLikeThis() throws Exception {
        URL u1 = logGet(new URL("http://1.1.1.1"));
        URL u2 = logGet(new URL("http://2.2.2.2"));
        System.out.println(u1);
    }

    public static URL logGet(URL u) {
        System.out.println(u.getHost());
        return u;
    }
    /**
     * NEW java/net/URL
     * DUP
     * LDC "http://1.1.1.1"
     * INVOKESPECIAL java/net/URL.<init> (Ljava/lang/String;)V
     * _________INVOKESTATIC asmTest/Testt.logGet (Ljava/net/URL;)Ljava/net/URL;
     * ASTORE 1
     * NEW java/net/URL
     * _________DUP
     * LDC "http://2.2.2.2"
     * INVOKESPECIAL java/net/URL.<init> (Ljava/lang/String;)V
     * _________INVOKESTATIC asmTest/Testt.logGet (Ljava/net/URL;)Ljava/net/URL;
     * _________POP
     * GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
     * ALOAD 1
     * INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/Object;)V
     * RETURN
     * MAXSTACK = 3
     * MAXLOCALS = 2
     */


}
utrucceh
  • 1,076
  • 6
  • 11
  • Sorry for conflict, I replaced that 'real' word to 'constructer' – utrucceh Apr 30 '20 at 11:25
  • 1
    Injecting your `invokestatic` right after the `invokespecial` looks feasible; where does this unnecessary `pop` instruction come from? – Holger Apr 30 '20 at 11:55
  • When I compile class, that `DUP` and `POP` came automatically. When I try without those, JVM throwing `Exception` when class loading. – utrucceh Apr 30 '20 at 13:34
  • 1
    I didn’t say anything about the `DUP`. There is nothing wrong with the `DUP`. But since you are assigning the result of the second instantiation, i.e. `new URL("http://2.2.2.2")` to the variable `u2`, there should be an `ASTORE 2`, not a `POP`. Are you using a compiler which removes unused variables? But anyway, there should be no problem *with* these instructions. – Holger Apr 30 '20 at 14:09
  • I am compiling with intellij idea with eclipse compiler, it seems it is auto removing. But with `POP` or without `POP`, if we add `invokestatic` to `realMethod` for two `invokespecial`, jvm will throw exception, so can we find generic solution? – utrucceh Apr 30 '20 at 19:46
  • 1
    I don't see a reason for an exception. It would be helpful if you post exactly what you tried and which exception you got. – Holger May 01 '20 at 13:16
  • Okey. Simply, look to the Asmfied codes and forget lines whichs starting with `_____`. Now I am adding `invokestatics` after `invokespecial`. When I run jvm throwing that error: `Exception in thread "main" java.lang.VerifyError: Operand stack underflow Exception Details: Location: asmTest/Testt.realMethod()V @21: invokestatic Reason: Attempt to pop empty stack. `. So just add `invokestatic` after `invokespecial` is not solving problem. Is there any keyword for fix this problem??? – utrucceh May 02 '20 at 08:03
  • 1
    What do you mean with “forget lines whichs starting with _____”? These lines are needed, so they have to be present. Just injecting the invokestatic instructions would imply leaving them there. Besides that, you should show what you have done, i.e. post the actual code instead of discussing pseudo code. There is no proof that the problem is where you suspect it to be. Post the exact code that reproduces the problem. – Holger May 04 '20 at 07:03

0 Answers0