Search a code line in Eclipse - java

I'm developing a code in Eclipse (java). While debugging the code, I used a lot of System.out.println(...). Now I need to delete all these lines. To do this, I wrote System.out.println in Search->Java, but it hasn't provided any search result. So, how can I find all occurancies of this string System.out.println?

Search -> File. Input "System.out.println" and select the type of files you want to search in "*.java" (without quotes)
The "Java search" is something different. It doesn't allow you to find stuff in java files but methods/declarations/etc. in java files. For instance declare somewhere in your project "String canYou = "FindMe?";" and then do Search -> Java, input "String" and select "Type" and hit search. It should find all the String uses in you project, including the aforementioned one.

Try writing System.out.println anywhere in your project, select this text and afterwards press ctrl + alt + g (sorry don't know exactly what kind of search this stands for) and it will give you all the occurrences of this string in all your projects(workspace-wise). I'm 100% sure it stands for exactly something in search > java - you can take a closer look to see for exactly what. Other than that you can have a conditional execution for the logs - i.e
if(Consts.DEBUG){
Log("something");
}

press Control + H
this will open up the search window, go to File Search tab and look for the system.out.println(

just search for println. I bet eclipse is interpreting what you are putting in as a regex.

I would do Find usages on the println method (ctrl+shift+g)

Use the menu Search->File and in the "Containing Text" field type in "System.out.println*", in the "File name patterns" field type in "*.java". Now press the "Replace" button in the lower right corner. Leave blank the "With" field and click the "Preview" button. Uncheck any occurrences you want to keep and then press "OK".
You may want to make sure your code is in version control before attempting this in case you do it wrong and want to go back.

We have written an eclipse plugin to do the same. It will find the System.out.println in your java code and comment it out. We will soon have a version which will delete it also. Hope this is useful.
http://eclipseo.blogspot.in/2013/05/commenting-systemoutprintln-from.html

Related

Smart commenting in an IDE (e.g. Netbeans)

Is there a way to mark a line of code which you can afterwards easily find by color, click or something similar?
Use-case: Suppose I want to mark every occurence of the word "radius" as to investigate how many radii are used so that I can differentiate clearly which one is which. In this case I would want to go through multiple files and mark the lines with the occurence of "radius" and afterwards (having marked -let's say- 32 radii) smartly go through them.
I'm specifically programming with JAVA in Netbeans. But any other IDE or even language would also be welcome.
Both IntelliJ IDEA and Netbeans have a full project search ( ctrl + shift + f )
I don't know how Netbean's approach looks like, but IntelliJ lists all occurrences of the string you type in, letting you easily jump to any occurrence you select.

Automatic code formatting for Eclipse IDE

I use many brackets and braces when I code. Be it casting multiple times, casting multiple times in if blocks, etc. I sometimes get lost in the brackets and also, hate putting a lot of them.
Is there any short-cut key to format this selected part of code?
I have tried Ctrl-Shift-F, but that doesn't give me what I want.
Note: I work on Eclipse Mars.
What you want is Source menu | Cleanup, then customize the profile to add the remove extra parenthesis. From the Code Style tab check the Use parenthesis in expressions Only if necessary. And then complete the wizard:
That changes:
super.start(((BundleContext)(context)));
to:
super.start(context);
As an extra you can set your project to do code cleanup tasks on save automatically if you desire.
did you try ctrl+3 and type formate? for me Ctrl-Shift-F was bound by other app and cause eclipse to miss catching it.
search for formatter Java->code style->formatter
There is a tab Braces.
If you select specific lines and press Ctrl + Shift + F eclipse will only format the lines you selected according to your formatter.

Goto method of class shortcut

I have a tool that will list failed tests, and I would like a way to open a fully qualified class and goto a method in Eclipse with a single string I can copy from the results. It can use any pattern whatsoever (I can change the test results layout), but could be something like this example:
com.my.package.StringUtilTest:testValueOf
I shall use it for re-running tests, but could work with any methods. It could go to the first option when there are overloads (won't matter for me). It could also list all options and allow me to paste the string and enter to go to the first option (being the only one in my case), or selecting the correct one from the very narrowed down options, just like Ctrl + O will do (note Ctrl + O will work on current file only though). It could even accept (or require!) the parameter types, won't matter. Anything like that would be a huge thanks. Some place where I can paste a single string on (in any way that it may have to be) to open a specific method.
I've been looking for a while, could be built-in or configured shortcut, macro, plugin, et cetera, but could neither find nor wrap my head around doing something (not expert on eclipsetalities)... I tried searching for a lot different stuff, but as you can see my problem can be solved with a vast pletora of things (none of which I can find anywhere though).
Eclipse jdt doesn't have it yet, but I believe an open method dialog similar to the open type dialog is in the works.. Please create a bug at bugs.eclipse.org if one doesn't exist describing what you would want in such a dialog.

"syso" shortcut in Eclipse- changing a bit behaviour

I look for some trick which gives me possibility to change the syso behaviour in Eclipse (please assume that below there is Eclipse´s editor), now it works in this way:
syso%someVariable
% - means the plase when i typed ctrl + space, and result is:
System.out.println();someVariable
but I want to have of course without copying text..:
System.out.println(someVariable);
Any hints ? :-) Thanks in advance !
Reference
The feature is called "code templates" in Eclipse. You can add templates with Preferences->Java->Editor->Templates. Two good articles:
http://eclipse.dzone.com/news/effective-eclipse-dont-write-c
http://eclipse.dzone.com/news/effective-eclipse-custom-templ
Also, this SO question:
Useful Eclipse Java Code Templates
System.out.println() is already mapped to sysout, so you may save time by learning a few of the existing templates first.
If you wan´t to change current template you can press shift + end (to select 'someVariable') and then press ctrl + space and up, enter to select sysout.
Answering the original question or at least how I understood it:
Modify the existing template syntax for sysout (Preferences->Java->Editor->Templates find sysout and select edit) to read:
System.out.println(${cursor});
Now you can type syso and then ctr+space and instantly get the output:
System.out.println(<cursorHere>);
Or as you put it:
System.out.println(someVariable);
...with the cursor inside the brackets which is indeed more efficient than starting after the semicolon.

How to access Java API within netbeans and another question

I just have two specific questions that over the years I have never been able to figure out in netbeans.
How do I access the Java API documentation without having to use my web browser. It would be nice if I could have a window in netbeans of the Java API. Is this not possible? How do you normally access the API document? Note that I don't mean just access for the documentation for the current code (cntrl-space, I think)... I just mean for browsing.
Code completion will automatically generate right brackets (which I like). When this occurs, the cursor is still within the brackets. How do you jump out of the brackets without mousing? I know a command to move to the end of line, but sometimes the brackets aren't at the end of the line... it seems like there should be a way to just jump to the end of the autocompletion.
For 2) I honestly just use the right arrow.
re 1) I guess you are looking for: Windows -> Others -> Javadoc
Additionally if you use Shift-F1 (instead of Alt-F1) on e.g. a classname it will show the documentation in the internal browser as well.
re 2) I don't think there is a shortcut for this, but you might be able to do this with a (recorded) keyboard macro (by searching for a closing bracket).
You can open the JavaDoc window
with Window > Other > JavaDoc
Typing the closing bracket or
parenthesis just puts you outside of
the generated brackets

Categories

Resources