How can I have Eclipse to automatically add method description block on method definition?
If I write: public int test(int hello){
And then press "Enter".
The Eclipse will add the end bracket two lines below, so the code ends up like this:
public int test(int hello){
}
If I then press ctrl+shift+j while the cursor is in the method name, the code ends up like this:
/**
* #param hello
* #return
*/
public int test(int hello){
}
Now my question is: Is it possible to let Eclipse add the method description (what is added on ctrl+shift+j) automatically, when I create the method? At the same time as it creates the end bracket.
What you have mentioned as method description is technically known as Javadoc comment. In Eclipse - Windows -> Preferences -> General -> Keys has the list of command and the keys binding to those commands. You can change the keys as per your comfort.
Related
I have code that looks like this, that I'm trying to format
Original code:
public int doThing(int a) // -incredibly useful comment here
{
int ab = a+1;
return ab;
}
I want it to look like this
public int doThing() { // -incredibly useful comment here
int ab = a+1;
return ab;
}
If I try to turn on the Brace position -> Method Declaration -> Same line option and run the formatter, any code with a comment in the position "breaks" the formatter, and I get an output for my example that looks the same as the original code, but methods without a comment have the correct formatting (meaning the results are inconsistent).
Is it possible with the eclipse formatter to get the style I want? I'm trying to run it against a large amount of code, and would prefer not to have to fix these all manually to get a consistent brace position.
The problem here is that is not formatting but rewriting. Using File Search + regular expression + Replace could do that in bulk.
Try this regex
^(\s*(?:public|private|protected)\s+[^(]+\([^)]*\))(\s*\/\/[^/]+)\R\s*\{
On File Search ( Ctrl + H)
Hit Replace and use $1 { $2\n as replacement
Code should compile after the refactoring.
UPDATE:
Fixed regex part that represents function arguments
\([^)]*\)
Full Regex matches these cases
public int doSmthg() // coment here
{
return 1;
}
private String doSmthgElse(String arg) // coment here
{
return arg;
}
I would like to write javadoc in Eclipse.
I have this piece of code:
private int variable;
When I want to add #value parametar in Eclipse above line I get {#value} instead of #value. When I want to add #author, I get output without {} brackets, as I want. How can I get #value without brackets?
Since the #value tag appears in running text, it should be in braces. It is not a tag like #author, which occurs at the end and basically considers the text up to the next (braceless) tag as its parameters.
So Eclipse's suggestion is correct. Why do you want to use that tag without braces?
The Javadoc tool will replace #value tags with the value of the constant it documents (if no parameter is given) or with the value of the constant mentioned in the parameter. For example:
/** The value {#value}. */
public static int aConstant = 1;
/**
* Does something.
* #param aValue should be {#value #aConstant}.
* #return 42
*/
public int aMethod(int aValue) {
...
}
Is there a quick way to replace this:
public static String ACCESSDENIED = Resources.strings.getString(Resources.ACCESS_DENIED);
with:
public stattic String getAccessDenied(){
return Resources.strings.getString(Resources.ACCESS_DENIED);
}
I need to replace all static references to getters in about 100 variables.
Use right-click --> Refactor --> Encapsulate Field... --> (new getter created), and everything is done automagically (and its a bulletproof solution).
You can also verify the results in a Preview window.
I managed to do it by using the find and replace with regex expressions.
Find: = (.+);
Replace: () { return $1; }
Eclipse does not find the correct variable for code completion as shown below.
int i = 0;
f(xyz); // f takes an int but eclipse won't fill it with i.
Under "Window" > "Preferences" > "Java" > "Editor" > "Content Assist", make sure "Fill method arguments and show guess arguments" is set and "Insert best guessed arguments" is selected.
Edit:
I tried this in my Eclipse (Version: Helios Service Release 1 - Build id: 20100917-0705):
public class BestGuessedParameter {
static int xyz = 1;
static void f(final int xyz) {
}
public static void main(final String[] args) {
final int i = 0;
f/*cursor here*/
}
}
Right after I typed the f, I hit space and selected f(xyz), Eclipse did supply f(i) with i highlighted and in a pop-down menu of i (highlighted), xyz, and 0. i was the default.
I couldn't find any info on how Eclipse selects the "best guessed parameters" (I have no idea where to look in the Eclipse source). I would guess that Eclipse "guesses" based on type, name, and visibility, and that Eclipse thinks there's a better match than your local variable. Perhaps if the local variable were closer in type and name, it would be a better match?
I'm looking for a way to filter files in an "Open" window. I'm using NetBeans IDE 6.5.
I did some research, and this is what i came up with, but for some reason it's not working.
//global variable
protected static FileFilter myfilter;
//in declaration of variables
fchoLoad.setFileFilter(myfilter);
//inside main
myfilter = .... (i actually delted this part by accident, i need to filter only .fwd files. can anybody tell me what goes here?)
If I understand it correctly, you want to create your own file chooser and be able to filter just some files (.fwd in your case). I guess this is more general Java question (not only NetBeans) and I suggest reading this tutorial
Anyway, your "myfilter" should look like this:
myfilter = new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".fwd")
|| f.isDirectory();
}
public String getDescription() {
return "FWD Files"; //type any description you want to display
}
};
Hope that helps