Viewing complete strings while debugging in Eclipse - java

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.

Related

How can I change the post fix template in Eclipse? [duplicate]

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Format specific part of Java code in Eclipse Mars

I am able to format my Java code by configuring it in Save Actions.
What I get after Saving the file is that the whole code gets formatted according to my settings. What I need is, that only a part of code gets formatted according to the settings.
Say, there are 10 methods in my code and I add one more to it. What I want is that only my 11th method gets formatted and the previous 10 gets untouched.
Is that possible? I am using Eclipse Mars
NOTE:
The format includes, removing unnecessary casts, parenthesis, etc.
Source code formatting on save can be limited to edited lines only:
The other save actions however are applied to the whole file.
Select the text you want to format, and press Ctrl + Shift + F to format the selection.
Alternatively you can do Ctrl + I on the selected text to just correct the indentation.
I always use CTRL + SHIFT + F to format my code. In eclipse you can just drag your mouse and select the code you want, then press CTRL + SHIFT + F. That way it will just format the selected code.
Normal the Ctrl+Shift+F will format the current class. Or when a selection is made, only that selected part will be formated.
To make a quick selection, from the current place, use Ctrl+Shift+Arrow-Up to make it bigger. Or Ctrl+Shift+Arrow-Down to make it smaller again.
Telling Eclipse "manually" which parts of your code to format and which not can be a daunting task. Especially if others work on the same project and don't know which parts of the code have to be left out.
Because of this Eclipse allows you to set "markers", simply surround your code with these tags:
// #formatter:off
Here goes your code
// #formatter:on
In order to make this work you have to adjust the Java Code Formatter (it can be reached via Window / Preferences) settings like this:
As far as I know this has been in Eclipse since version 3.6.

I am not able to see the value of variable in jsp page when i hover the mouse on it

I am not able to see the value of the variable in JSP page. When I hover the mouse on it. It shows the type of the class. I am in debug mode and I am checking the variable after the line is executed. It works absolutely fine. When I see variables in java file. both the files are in the same project.
window >> preference>>java/j2ee>>editor>>hovers
....combined hovers are checked...
I ran into same issue recently. I found this. It might be helpful for you.
Also one quick workaround is that you can punch out System.out.println() statements to check the values. It might be painful though, and make sure to remove them once you are done.
select The variable and
press Cntrl + shift + i
and you will get value of selected variable in debug mode

Eclipse parameter dialog/hint when using functions in code

By default In Eclipse when you use a function or create objects it helps with parameters like this:
But once it's done, it'll never show up again. Is there any way to call this parameter helper on code that's already written when I point the cursor on the method?
The Image I uploaded only appears while writing code.
Ctrl + Shift + Space will do the trick. It is named Context information in Key preferences.
Place the cursor just inside the left parenthesis and press Ctrl+Space (Command+Space on Mac) again; Eclipse will show Content Assist again.
When the mouse cursor is in the paranthesis, click Ctrl+Space. It will pop up menu (content assist). If you enter it, it will show the context information as it was at the beginning.
You can't get place holders again once they disappeared. But you can get content assist help as said by #E-Riz
Check this answer What is Eclipse shortcut key to turn on feature that allows when pressing TAB key, for cursor to go to expected position? and also this What's this box around my function input?
i was looking for the same shortcut and didn't find it ;
now i'm using
*ctrl+Arrows to move between words
*alt+shift+arrows to select the world and replace it
JDT-Codemining is a new project (as of Aug 2018) that supports parameter hints, along with many other features, such as:
General
Show references
Show implementations
Show method parameter names
Show method parameter types
Show end statement
JUnit
Show JUnit status
Show JUnit run
Show JUnit debug
Debugging
Show variable values inline while debugging

How to use the "sysout" snippet in Eclipse with selected text?

I am aware of the Eclipse snippet "sysout" which is neatly replaced with System.out.println(); when tab-completed.
This is very useful but sometimes, I need to wrap some existing code in a System.out.println();
In Eclipse internals, the template is defined as being able to wrap a "selected word". But how can I use the snippet with selected text since typing "sysout" obviously removes the selected text.
Any idea?
The sysout template acts upon entire Java statements.
Highlight a statement in the editor.
Hit CTRL-SPACE (or whatever you have set up for content assist.)
Type sysout then hit enter. Note that when you're typing sysout it will temporarily overwrite your statement (but it will come back wrapped by System.out.println when you hit enter.)
Eclipse has "Surround Width" Option which can do this for you.
SHIFT + ALT + Z should get you that to see how that templates meta-data layout.
I don't think you can do it in one go, but what about cutting the selected text and then: tab+space, ctrl+v
it's just one key combination more.
if you use content assist (ctrl-space on Windows), at the end of the list will be the sysout option. you might want to augment the template with quotes around the word selection so you dont need to type them in.
Preference>General>Keys.
then search "content assist" or "content" in "type filter text".
don't press Enter.
choice "content assist".
type Binding and set your own shortcuts.
such as "control+space".
The main part is into:
Java->Editor->Templates
This can accessed by:
Preferences window: into Java->Editor->Templates.
Surrounding a text and press:
Windows: SHIFT + ALT + Z (as sadhasivam said)
Mac: CMD + OPT + Z
to display the Preferences Window

Categories

Resources