How can I set a breakpoint in a single line lambda?
e.g. I'd like the (eclipse) debugger to halt, when outer.doSth(event) is called:
observable.addCallback(event-> outer.doSth(event));
You can't.
If you refactor it like this:
observable.addCallback(event-> {
return outer.doSth(event);
});
you can.
Well this answer is not for eclipse, but in intellij you can (15.x)
You have an option to set a break point either at the line (which is the first option in the image), or at the first lambda or at second one. And so on.
In Eclipse 4.14.0 when I set a breakpoint into a lambda it works automatically correct and I am able to debug the lambda expression. But be aware that Eclipse also stops at the breakpoint at the "line" itself (that's before the stream operation is executed, since it's lazy):
And here you see that I am able to see the value of the group variable:
So you don't have to reformat your code.
Just making #Holger 's comment on this answer an answer:
It is enough to break line after ->
observable.addCallback(event->
outer.doSth(event) //line break works here!
);
Related
I have been setting up the eclipse formatter and quite close to our desired outcome except for the following. I'm trying to ignore line wrapping for the conditions within the if statement
For example
My desired outcome is
if (callback.getType().equals("NameCallback")) {
callback.getInput()
.get(0)
.setValue(testUser.getUsername());
}
but instead I'm getting
if (callback.getType()
.equals("NameCallback")) {
callback.getInput()
.get(0)
.setValue(testUser.getUsername());
}
In the formatter I have set Line wrapping -> Function Calls -> Qualified Innovations to a line wrapping policy of 'Wrap all elements, except first element if not necessary' and force split. This works fine but our preference is to not have this rule invoked for if statement conditions or inside of () if easier. I know I can setup the On/Off tags for the formatter but hoping there is a way to get the same result without having to do this. Any ideas would be appreciated
Thanks
In order to customize the way you format an if else blocks, you need to jump to Control Statements :
I'm trying to see what's going on in a chain of recursive calls. Is there some way to set a breakpoint in the middle of a single line to see when the function is being called from each statement?
Example: fibonacci(term - 2) + (want to put a breakpoint here) fibonacci(term - 1)
I don't think it is possible for some of the major IDE's like (Eclipse, Netbeans).
However, you can actually update your codes to such that you can evaluate them :
result1 = fibonacci(term - 2);
result2 = fibonacci(term - 1);
sum = result1 + result2;
The above code might not give you an elegant way of presenting your codes but can actually help you solve your problem.
You can't do this, but in some IDE's (Eclipse, IntelliJ) you can evaluate an expression that you highlight during debugging.
This could be useful if you wanted to see what fibonaci(term-2), for example, produced.
I understand the importance of succinct, clear code and that code line wrapping should be avoided, if possible. However, the requirement for this project is that no line should go beyond column 80 and I'm being asked to use verbose variable naming. Therefore, something as simple as a for loop parenthetical will need to be wrapped and that's where I'm finding Eclipse falling short.
It doesn't appear that Eclipse is capable of wrapping the parenthetical of a for loop or preserving the wrapping set. For example, my initialization statement, expression and update/counter are on separate lines like so:
for (initialization;
expression;
update/counter;)
{
//code...
}
When pressing Ctrl+Shift+f, Eclipse makes it:
for (initialization; expression; update/counter;)
{
//code...
}
Is there a way to get Eclipse to preserve this formatting. I've created a custom Eclipse formatter, but can't find any setting that will wrap the for loop parenthetical. I did see one post that suggested using //, but that won't work inside of a for loop's parenthesis.
I do not remember of an option to preserve the line feeds after each of the for's "initialization", "expression", "update". The rest can be done, but not the wrapping inside the for loop's parenthesis.
If you really need to preserve such a wrapping, you may want to disable the formatter altogether on these lines? If you go to the formater settings (Window > preferences > java > code style > formatter, then click "edit..."), then on the "Off/On Tags" tab (appeared in Eclipse 3.6 IIRC), you can enable tags to disable the formatter on specific parts of the code.
With the default tags, that would give something like :
// #formatter:off
for (initialization;
expression;
update/counter)
// #formatter:on
{
//code...
}
You can also use this way:
for (/**/initialization;
/**/expression;
/**/update/counter;)
/**/{
//code...
}
This also works:
for (initialization; //
expression; //
update/counter) {
// code...
}
I have to read java file by java code and to determine the greatest nested count of if statements in it.
for example:
if (someCondition)
{
if (someCondition)
{
// Expression
}
}
In this case program should display greatest nested if depth is 2.
Now the problem is that position of curly brace after if is uncertain.
for example it can be like :
Curly brace start and end comes in same line
if (someCondition){}
OR
Curly brace start in next line
if (someCondition)
{
}
OR
Conditions without curly brace
if (someCondition)
if (someCondition) // Single line without curly brace
Can anybody suggest what will be the best way to get the required nested count?
You'll need to parse the Abstract Syntax Tree (AST) of the Java source code. See Java library for code analysis. Once you have the AST, you can do a search to find the longest path of nested conditionals.
As the answer already said, you should rely on the AST rather than viewing code manually for this. The AST will never be wrong, your own reading abilities most often will.
I don't know a complete solution right now, but I suggest you spend some time looking at existing tools for computing software metrics. Nesting depth is a typical metric and there should be tools around.
If you can't find anything, you can at least fall back to writing something like an Eclipse plugin. In that case, you could simply load the Java file in the Eclipse editor, and Eclipse performs all the hard work for you and gives you the AST for free. Determining the nesting depth of a given AST is then rendered a simple task. Developing a prototype for that shouldn't take more than a few hours. And it's easy to extend it to cover your whole project and have it answer questions like "which java file in our project has the maximum nesting depth and what depth is that?". But then again.. someone else will surely point out an existing tool that already does this and much more.
I82Much's answer will certainly get you there, but feels a little like cheating.
Knowing little about your project, I would think that a simple stack mechanism with a max value record would do the trick push on { and pop on }. Once you have that basic model working, simply add the special case of control statements with one line bodies (this is valid for if, for, while ...). In those cases, you'll be looking for those keywords, followed by ( and a ). Once you've encountered that combination, if the scan encounters either another control statement or a semi-colon before it encounters a { then this is one of those special cases and you should push (using a special marker indicating to pop on ; rather than }).
I'm tying to figure out how I can customize the Eclipse code formatter to break lines more to my liking. I'm trying to set the style for parameter lists, either in method declarations or calls. Looking for a mix of Wrap where necessary and Wrap all elements, every element on a new line. I want to Wrap where necessary, every element on a new line, which doesn't seem to exist. My logic is that no break is necessary for short lines, my eye can scan the parameter list horizontally:
public void myMethod(int p1, int p2, int p3) {
But for lists that do need to be broken, I would like every element on a new line, so I can scan vertically:
public void myMethodWithALotOfParams(
ReallyLongClassName param1,
AnotherLongName aLongParamName,
int p3) {
I can't seem to make this happen. I can wrap everything, including short lists. I can wrap only long lines, and continue stacking parameters on each line until I reach the margin. I can't trigger wrapping on long lines, then put each parameter on its own line.
This style can be seen in several places in Code Complete (2nd Ed).
UPDATE >>
I don't think there is anything built in to Eclipse to handle this, but I'm not afraid to write code. :) Eclipse is open source, so I tried to find the code that handles formatting, in hopes of building in the preferred behavior. Didn't have much luck on the first try, lots of abstraction, not much parsing and formatting. Hints?
look at this link
Window -> Preferences -> Java -> Code Style -> Formatter -> New (Profile) -> Edit -> Line Wrapping -> Never join already wrapped lines
Or change other parameters if you want to change line wrapping parameters.
I would like to have such a feature too, unfortunatly (as you already guessed) it's not possible, yet. If you like you can file a bug at the eclipse-bugzilla, here you will find some bugs about formatting in jdt: https://bugs.eclipse.org/bugs/buglist.cgi?quicksearch=jdt+formatter. Let us know if you file a new bug, so everyone interested can vote for it!