Is there asm nop equivalent in java?

前端 未结 5 1843
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-18 17:48

When I program C/C++ with Visual Studio I often use __asm nop; command to insert a noop code in order to have something to break on. For instance:

相关标签:
5条回答
  • 2020-12-18 18:05

    I did not find a perfect solution for this, but here is the solution I like most. Probably not efficient but it lets you place a breakpoint, Eclipse does not complain or warns and from the code reader's point of view, it is obvious what you want to do.

    // Silly method that allows me to make noop
    private static void noop() {
    
    }
    
    public otherMethod() { 
        // Here I need breakpoint
        noop();
    }
    

    If I want to precondition breakpoint I place the condition before:

    public otherMethod() { 
        // Here I need breakpoint
        if(someCondition())
            noop();
    }
    
    0 讨论(0)
  • 2020-12-18 18:13

    You can just put in any arbitrary assignment statement that doesn't do anything, e.g.

    if (someCondition()) {
      int t=0;
    }
    

    The debugger will be happy to break on this. Since t is local to the block, it can't possibly have any side effects (and will get JIT-compiled out of existence in production code).

    Alternatively, you can write a static function which has a breakpoint permanently set inside it, so you can just do:

    if (someCondition()) {
      breakPoint();
    }
    
    0 讨论(0)
  • 2020-12-18 18:19

    In bytecode you have a nop instruction, but there's no nop statement in the Java language.

    You can add an extra ; on a line by itself and the code will still compile, but that's not much more meaningful than adding an empty line.

    Another "does nothing" statement could be:

    assert true;
    

    which has no side-effects what so ever, and can be turned off when executing the program.

    As it turns out, assert true does not seem to generate any bytecode instructions, which causes break-points on assert true to be skipped all together. Eclipse is however able to break on a statement such as

    assert Boolean.TRUE;
    

    which is quite similar.

    0 讨论(0)
  • 2020-12-18 18:27

    Java interprets this as an empty statement:

    ;
    

    Though, as noted in comments, Eclipse won't let you set a breakpoint here. If you want something useless that you can put a breakpoint on that's also nice and easy to type, I suggest:

    if(false){}
    

    Your compiler might warn you that this is never entered, which can be useful for reminding you to take it out before compiling for production. Hope this helps!

    0 讨论(0)
  • 2020-12-18 18:29

    I'm separating this into a new answer because I'm surprised no one has said this, yet. Modern IDEs let you add logic to breakpoints! Instead of compiling a dedicated if statement just for debugging, place the breakpoint somewhere you actually care about, then right-click it and select Breakpoint Properties!

    0 讨论(0)
提交回复
热议问题