How can I improve eclipse code completion? - eclipse

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?

Related

eclipse - Always view object when debugging

I have an object in a Set that has it's guts updated by reference. Is there a way I can use the object hash ID (MyObject#93efa2ed) or Eclipse ID (id=356) to always watch in in my Expressions view even when it's out-of-scope?
I am using Eclipse Neon.
ur question is not quite clear to me, but i guess u mean this:
u have an object O that is contained in a set S, that contains more objects
O is updated in ur code (ie. its internal properties change) and u want to keep an eye on these changes thru the expressions view while u step thru the code
u also want this to happen in code lines/methods that dont a have a straitforward path to O
i dont know of any way (internal eclipse goody) that lets u ref' O by its eclipse debug ID or object hash.
but it can be done fairly simple: u just need to be able to reach O by some (lengthy) way of references from the current frame that is selected in the debugger to accomplish this.
the easiest way is to add some extra code to set O to some static var of some sort.
U can even assign the static var while debugging manually, with the help of the "Display" view that lets u execute java code, that runs in the context of the current stack frame.
Steps:
create/add the class MyWatcher to ur code base.
step thru ur code to where u get ur handle on O (eg. in the sample code, on the 5th break when adding o to the set)
open the Display view and add this line MyWatcher.watches.put("a", o) (replace 'o' by the expression that references O at ur breakpoint)
Execute the line with Ctrl+U or thru the context menu
add this to ur "expressions" view: MyWatcher.watches.get("a")
now O will remain visible at all times, eg. in the example below: even when u are in foo(), b/c the map holds ar ref on O.
public class MyCode {
public static void main(final String[] args) {
bar();
foo();
}
static void bar() {
final Set<Object> set = new HashSet<>();
for (int i = 0; i < 10; i++) {
final List<Integer> o = Arrays.asList(i);
set.add(o);
}
}
static void foo() {
System.out.println("bar");
}
}
public class MyWatcher {
public static final Map<String, Object> watches = new HashMap<>();
}
Add a toString() method in you MyObject class.
You can do it through Eclipse using:
right click(in MyObject) -> Source -> Generate toString().
Now you will be able to see the contents rather than hash id of MyObject.

Can Eclipse add method description on {+enter

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.

GWT Super Dev Mode variable referenceError: variable is not defined

I'm using GWT Super Dev Mode and the Chrome debugger. Some (not all) variables won't let me examine their values, either by hovering or by right-click -> Evaluate in Console. The latter gives "ReferenceError: is not defined. It reminds me of an earlier environment where the compiler optimizer simply removed variables of short duration. Does anyone know why this happens or how to stop it? Here is a code example:
public AbstractModal( final String title, final String width )
{
this( title, width, ( int )( Window.getClientHeight( ) * AbstractModal.MaxHeightPercentage ) + "px" );
}

Can I generate fields automatically in eclipse from a constructor?

When I'm coding in eclipse, I like to be as lazy as possible. So I frequently type something like:
myObject = new MyClass(myParam1, myParam2, myParam3);
Even though MyClass doesn't exist and neither does it's constructor. A few clicks later and eclipse has created MyClass with a constructor inferred from what I typed. My question is, is it possible to also get eclipse to generate fields in the class which correspond to what I passed to the constructor? I realize it's super lazy, but that's the whole joy of eclipse!
If you have a class A.
class A{
A(int a |){}
}
| is the cursor. Crtl + 1 "assign parameter to new field"
Result:
class A{
private final int a;
A(int a){
this.a = a;
}
}
This works also for methods:
void method(int b){}
Will result in:
private int b;
void method(int b){
this.b = b;
}
I know you can do the other way round. Define the fields and let Eclipse generate a constructor using these fields for you: Source | Generate Constructor using Fields
Since Eclipse Neon it is possible to assign all parameters to fields.
Using the quick assist Ctrl + 1 it suggest Assign all parameters to new fields. You can call for the quick assist if the cursor is anywhere between the parenthesis of the constructor.
This option is available for methods as well.
source

How to filter files when opening using NetBeans?

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