Is there any way in IntelliJ's debugger to always:
a) set result to true or
b) make foo() return true
I know that I can change the variable when I am in a breakpoint, but I want it to happen always and automatically, without changing the code because it is a decompiled class.
public void foo() {
boolean result = false;
..
return result;
}
Add a breakpoint on the line with return, in breakpoint properties unset "suspend", set "evaluate and log" to result = true.
If there are more than one return in the method, you'll need to modify the result value before all of them.
Related
While deleting data from Firebase, I use the remove value function and this function overrides the condition block. My code;
boolean deleteDelayControl=true;
if (deleteDelayControl) {
deleteDelayControl = false;
reference = FirebaseDatabase.getInstance().getReference("taken_foods").child(food_id);
reference.removeValue();
}
As you can see when I run this code, although I set the boolean variable to false, the control block works every time. What could be the problem?
When I delete this function, the control block works properly: "reference.removeValue();"
Using Byte Buddy's advice API, is it possible to return from the instrumented method without actually executing it?
One use case would be to implement a cache and to return the cached value, if present, instead of computing the value again.
#Advice.OnMethodEnter
public static Object returnCachedValue(#Advice.Argument(0) String query) {
if (cache.containsKey(query)) {
// should "abort" method call
return cache.get(query);
}
}
I know that this code sample above just creates a local variable which I can get in a #Advice.OnMethodExit method. But is there a way to abort the method call on an explicit return? If yes, is this also possible for void methods?
No, this is not possible, a return value can only be set from exit advice. But it can be emulated by skipping the original method in case that a value already exists and by setting this value from the exit advice in case that the enter advice defines a value:
class MyAdvice {
#Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class)
public static Object returnCachedValue(#Advice.Argument(0) String query) {
if (cache.containsKey(query)) {
return cache.get(query);
} else {
return null;
}
}
#Advice.OnMethodExit
public static void processCachedValue(
#Advice.Return(readOnly = false, typing = DYNAMIC) Object returned,
#Advice.Enter Object enter) {
if (enter != null) {
returned = enter;
} else {
cache.put(query, returned);
}
}
}
Of course, this does not work if the cached value is null. To avoid this, you could wrap the value in some instance to make sure that the enter value is never null. Doing so would also allow to use the above pattern to void methods.
This might look inconvenient to program but the idea of advice is that Byte Buddy can use the advice class as a template and inline the byte code without much work to avoid a runtime overhead.
I want to exit from a function if length of a string( originalPath.length) is equal to some specific number and this is restriction as well I cant do any other thing any idea?
String original = originalPath.substring(0, originalPath.length() - separatorPos);
this snippet is part of a function and not loop.
Make a void method and then
You can do it using if condition and return statement like
if(condition){
return;
}
return is used to stop further execution and return any value back from where it was called.
There are other options like throwing exceptions for the same and break,continue for loops in java
You can use return; if your method is a void, otherwise just return a default value like return false to finish your method if the condition fails.
The return statement will finish your method.
Given the following Java code, how can I use the IntelliJ or Eclipse debugger to return false from the condition() method?
public boolean condition() {
return (4 == add());
}
public int add() {
return 2 + 2;
}
In Eclipse you can use Force Return.
Just place a breakpoint on return (4 == add()); and type false on the Display view-tab. Then make a selection of your false, right click and hit "Force Return".
In IntelliJ (since IDEA 15 EAP) you can use Force return.
Extract from jetbrains' blog
You can force the return from the current method without executing any more instructions from it:
NB :
If the method returns a value, you’ll have to specify it (smart code completion provided).
If the method has try-finally blocks, you’ll be able to choose whether to execute them or not.
You can first change the code for condition to something like this:
public boolean condition() {
boolean result = (4 == add());
return result;
}
Then, you can set a breakpoint on the return statement. When the breakpoint is hit, you can use the debugger to change the value of result to false.
I'm trying to call a boolean method in another class and Eclipse is reporting the above error on the second line in the following code:
CCR ccrFlags = new CCR();
if (ccrFlags.cBit() = set)
The method being called from the class called "CCR" is:
public boolean cBit() {
boolean set = false;
return set;
}
I imagine I'm probably going about this in an idiotic way and would be grateful for any advice. Thanks, Robert.
Comparison should use == (double-equal):
CCR ccrFlags = new CCR();
if (ccrFlags.cBit() == set)
in an if, the condition has to be always true or false.
your error is, that = only assigns the values, but it is not a logical operation which can be true or false.
So you have to use == in conditions.