Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there asm nop equivalent in java?

Tags:

java

debugging

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:

if (someCondition()) {   __asm nop; } 

I have no idea what to do when the condition occurs, but I want to stop the execution and examine the current state. Sometimes someCondition() is simple enough to create a conditional breakpoint, but conditional breakpoints slow down the execution significantly, besides it is not always possible.

Now, in C# I break into the debugger directly either by calling System.Diagnostics.Debugger.Break() or System.Diagnostics.Debugger.Launch().

Now I am forced to program Java and until now I have found no better alternative than just do System.out.println("bla-bla") and set a breakpoint there. Again, please consider the case when a conditional breakpoint is not feasible.

So, I wonder - is there an __asm nop or System.Diagnostics.Debugger.Break() alternative in Java?

like image 933
mark Avatar asked May 24 '12 11:05

mark


People also ask

Is there a noop in java?

This is probably one of the simplest Java annotation processing libraries out there. It generates no-op implementations of your interfaces.

What is no-op in Java?

A no op (or no-op), for no operation , is a computer instruction that takes up a small amount of space but specifies no operation. The computer processor simply moves to the next sequential instruction.


2 Answers

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.

like image 54
aioobe Avatar answered Sep 20 '22 04:09

aioobe


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!

like image 25
Ky. Avatar answered Sep 23 '22 04:09

Ky.