How to find a value with in method definition in netbeans? - java

Search an item with in Method
In net beans, i want to search for an item which should not search beyond method definition.
Generally i used to do this in eclipse by double clicking near the
open brace and it will select the entire method.
if i select CTRL+F, then i can search for a item with in that
particular method.
Similarly i want to do that in net beans, please help me.
I have tried double clicking the open brace inside a method, but it just highlights the start and end braces alone.

I can get the behavior you request by first selecting the whole method. Instead of double-clicking the braces,
Place the marker near the first brace
Hold down shift while clicking the second.
Ctrl+F to bring up search window.
It will now only search in the selected block while the search window is open.

Related

StringBuffer variable value not coming in full in eclipse debug [duplicate]

While debugging Java code, Strings in the views "Variables" and "Expressions" show up only till a certain length, after which Eclipse shows "..."
Is there any way to inspect the entire string? (This eases the pain of adding logging statements for debugging everywhere)
In the Variables view you can right click on Details pane (the section where the string content is displayed) and select "Max Length..." popup menu. The same length applies to expression inspector popup and few other places.
In the Variables view you right-click on the variable and select Change value. If your variable is huge you have to wait a few minutes (in this case Eclipse doesn't respond to commands) but in the end Eclipse will show your variable entirely.
If you have a really long string, I use a different strategy: dump it to a file. I have a snippet of code I use in the Display view, which is evaluated to dump the data to a file. I use Commons IO, but you can do it with JDK only methods.
org.apache.commons.io.FileUtils.writeStringToFile(new java.io.File("<filename>"), <expression to evaluate>);
Naturally, you will need the Commons IO JAR in your classpath for that to work. If you don't, resort to JDK work.
The best way to view a String value from eclipse debug view is as below.
1) Switch to debug view in Eclipse
2) Select the desired variable in variable pane.
3) Right click on display area of variable pane where value is shown and click on Max Length. Enter the maximum char value in Configure Details Pane .
4) Cheers
When debugger reaches the point where you want the String value, just type a sysOut statement
System.out.println("The value is : \n " + query);
Select the above the statement, right click-> Execute
It will print the value on the console
For javascript based debugging on eclipse, "change value" method and "Max length" method both failed for me, adding the required object to watch(Expressions) and then Right Clicking the watched expression to select "Load Full Value" is the only solution that works for me, but even this inserts unwanted "\n" in the output.
Note - "Max length" must be set for the "Load Full Value" to work as it loads values till max length(default in eclipse is 10000). Refer above answer to see how to set Max length.
There is no "maxLength" in Eclipse Mars. And, the "change value" only works with "Variables", not "Expressions", so none of above works for me.
And, the expression are cut in the middle, not at the end. I guess they changed the default behaviour.
The only thing working for me, is to expand the expression name column's width, and click the expression to select it all to see the full length content.
Nothing from the above worked for me, moreover, some user interface elements in my Eclipse can not be found as described. I'm using STS 4.3.1.
The value I needed was really big, it is part of a huge JSON request. Workaround is to use an expression in Eclipse Debug Shell console and to output the substring of the whole value. Since you can see the partial value, inspect it and use the last few literals as the position to output the next chunk of the string. Repeat this approach until get what you need:
String result = new String(reallyBigByteArrayValue, "UTF-8");
result.substring(result.indexOf("some-unique-text"));
In Eclipse IDE 2020-09 it's "Pretty print to console" on right click.
From the console the copying is possible at full length.

Eclipse editor code editing

First of all, I was unsure what the title of this question should be so I just gave it my best shot. I just started using Eclipse as my IDE, it's a great IDE and all but I get annoyed when I highlight a portion of code to be edited or replaced, any code after it gets deleted. So for example:
System.out.println(theScanner.nextLine());
I want to replace nextLine with nextDouble. I would highlight nextLine and type in nextDouble but then the ()()(); would be erased and I would have to type them in again. Is there a way to fix this?
Click Windows > Preferences. Type content assist in search box. Select Completion overwrites option as shown below
Keep cursor after character t then press Ctrl + Space.
Then type d then select nextDouble method press Enter

Java Eclipse evaluate expression

Is it possible to evaluate a expression in Eclipse similar to IntelliJ. Where you can dynamically type code and the result will be displayed during debug?. I know it does something similar "Display" but you need to highlight code that has been written (so you cant write any new code unless you re-compile).
Perhaps there are plugins that i could use?. I have just started using eclipse
There is a Display view as well that can do exactly what you want. Go to Window > Show view > Display (or Other... if the Display view is not there. In the Display view, you can type any code you want during debug (content assist is available). The current objects and variables are also available to use. After you wrote your code, you have to highlight it, right click and select Execute or hit CTRL-U. You may play around with the other possible actions as well.
Besides that, you can also write in the source code during debug and evaluate it using Display as you mentioned, if you have the source for the class you are debugging.
1 - Type your expression inside a method that you are debugging
2 - Select that code
3 - Press CTRL + SHIFT + I
4 - Eclipse will evaluate your expression and show the results in a floating window
You can change variable values on the fly using the Debug perspective.
The top right corner of Eclipse should have the variables view from which you can select a variable name and change its value. You can also set conditional breakpoints by selecting the breakpoints view in the top right corner of Eclipse. Right-click on the breakpoint and select breakpoint properties. Check the conditional checkbox and write an expression that only when it evaluates as true does the program suspend during Debugging.
Change Variable Values during Debug
Conditional Breakpoints
Although Display View works; There are two ways else to evaluate an expression.
In the context of the debugging session, we can write and run custom code to evaluate possibilities. This is done in the Debug Shell. For example, if we need to cross-check the correctness of the sqrt functionality, we could do it in the Debug Shell. On the code, Right-click -> Inspect to see the value.
You can select an expression then open Context Menu -> Inspect or press CTRL + Shift + I, during debugging, the result will be displayed.
Select any expression in your code. Then right-click and select "Display" (or press the associated key-combination).
For completely new expressions, use the Display view. It's like a scrapbook for expressions. If the Display view is not visible, select Window > Show view > Display to add it. Then type any expression in the view, and do the same thing as for expressions in your code: select the expression, right-click and select "Display".
For expressions that you want to always see the value of, every time execution is halted, use the Expressions view instead.

Eclipse custom hotkey - print current line to standard out

Eclipse has a useful hotkey to assign current line to a local variable - when I type for instance:
Math.random()
and press ALT + SHIFT + L (Extract local variable), I can quickly change the line to
double random = Math.random();
I would like to use the same trick for printing it to std out, so that the Math.random() is being changed to:
System.out.println(Math.random());
Currently the fastet way to to this is to type syso and use content assist to use a template, but that requires manual copy pasting. Anyone knows a better way to do this?
Two options come to my mind to achieve your goal, but both of them require the selection of statement first.
After you select statement, press CTRL+SPACE, then type syso and hit Enter.
Selected statement will be placed inside System.out block:
System.out.println(statement);
Also you can prepare eclipse template (Window->Preference->Java->Editor->Content Assist->Templates), and give it some name:
System.out.println(${line_selection});${cursor}
After you select statement, press ALT+SHIFT+Z or select menu option Source->Surround With (also in context menu). Template you have created should be there so select it. Selected statement will be wrapped inside desired block of code.
As far as I am aware there is no shortcut available from the keys section of preferences. Is content assist really not fast enough for you in this case?

Jump-To-Code-Line Eclipse Shortcuts

is it possible, in Eclipse, to mark certain lines with Shortcuts and be able to quickly jump to those lines?
Example: Let's say I have maintenanceHeavyMethod() at line 120 in my class, gameLoop() at line 800 and some listener at line 1460.
I'd like to f.ex. press CTRL+SHIFT+1, 2, 3 etc. to mark those positions, and then use f.ex. CTRL+1, 2, 3 to immediately jump to them. I don't like split-screens etc, but I need to jump around when writing.
Is there such a feature?
I'm using latest Eclipse to write Java-programs.
You can add Bookmarks in your code. Select the code fragment you want to bookmark and then go to Edit > Add Bookmark... (also possible via the menu available with a right-click in the left hand column of the editor, like breakpoints).
Then, add the Bookmarks view. Select Window > Show View > Other... > Bookmarks and you'll get something like this:
Sadly, I don't think you can bind a shortcut to a particular bookmark.
Just in case, the shortcut to jump to a particular line is CTRL+L.
That would be best taken care by mylyn:
Define a task with this three method, and you will be to see only those 3 in the package explorer view
To jump to a particular method, I use ctrl+o and then start typing the method. If you're a proficient typist, this shouldn't take any longer; keep in mind that you only have to start typing the name of the method.
Unfortunately this isn't a proper solution for jumping to a line of code within a method.
You can jump to methods by using the outline view. Outline view shows all the Methods, Fields, and Inner-Classes (and their methods and fields and inner-classes...etc) of the source file you currently have open. I personally prefer this method, unless I have a stacktrace and know which line I want to jump to, in which case I use ctrl+L.

Categories