Community
Participate
Working Groups
Create two classes like this package var.creation.test; public class b { public final void m(int x, String y) {} // private final void m(String x) {} // public final void m(boolean x, String y, String z) {} } package var.creation.test; public class a { public a() { new b().m(first, second); } } Now applying "Quick Fix">"Create Local Var first/Create field first" creates a String variable/field instead of "int first" and for the second parameter "String second". When the method signature is fully typed and available there is only one possibility from the 3 signatures. Cheers and goodwill ----------------------------------------------------------------------- Software configuration ---------------------------------------------------------------------- Windows XP Version 5.1 (Build 2600.xpsp_sp2_gdr.050301-1519: Service Pack 2) ----------------------------------------------------------------------- java version "1.5.0_05" Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_05-b05) Java HotSpot(TM) Client VM (build 1.5.0_05-b05, mixed mode) --------------------------------------------------------------------- eclipse-platform-3.2M3-win32 eclipse-PDE-3.2M3 eclipse-JDT-3.2M3 xsd-runtime-2.1.0 emf-sdo-runtime-2.1.0 GEF-runtime-3.1 VE-runtime-1.1.0.1 wtp-1.0M8 subclipse-0.9.36 -------------------------------------------------------------------
Could reproduce with 3.2 M3. The AST makes a guess for the reference of 'm' in a.a(). But it chooses m(String) instead of the better suited m(int, String) This is the same as bug 69471 which doesn't seem to be fixed. Moving to jdt.core
Martin, how does the UI make this case work: class Y { Y(int x, String y) {} Y(String x) {} Y(boolean x, String y, String z) {} void test() { new Y(first, second); } } You have no binding for the constructor, and we never even try to find a matching constructor, but the UI still finds the best guess. So why don't you do the same thing for this case: class X { void m(int x, String y) {} void m(String x) {} void m(boolean x, String y, String z) {} void test() { m(first, second); } }
Martin, the UI algorithm seems to be off with this case: public class Y { Y(int x, String y) {} Y(String x) {} Y(boolean x, Object y, String z) {} void test(boolean first) { new Y(first, second); } } It thinks second should be a String, not Object.
I started doing my own guessing for constructors ( a long time ago) because there we get no bindings at all. For methods there were guessings from the beginning which I think are also used by the compiler itself e.g. for hovers and for the error messages. Idealy, the AST would give the bindings for both method and constructors, as good as possible. In my opinion, the number of parameters should always be looked at first. But of course, you could also argue that a the match of a first parameter is to be considered as well.
Martin, I released changes to both method & constructor lookup. Please check with the UI behaviour to see if/when 'your best guess' can be turned off.
Sorry, still found a problem: public class Z { void foo(boolean x, String y, String z) {} // (a) void foo(int x, String y) {} // (b) void foo(String x, String y) {} // (c) void foo(String x) { foo(first, "second"); // maps to (a) should be (c) foo(first, second); // maps to (b) ok foo(true, second); // maps to (a) ok } It also doesn't seem ot work for the constructors yet. Maybe you have to enable it for the DOM AST? class Y { Y(boolean x, String y, String z) {} Y(int x, String y) {} Y(String x) { this(first, second); } void test() { new Y(first, second); } class Z extends Y { public Z() { super(first, second); } }
Olivier, can you take a look to see if the DOM needs to do something in this case?
public class Z { void foo(boolean x, String y, String z) {} // (a) void foo(int x, String y) {} // (b) void foo(String x, String y) {} // (c) void foo(String x) { foo(first, "second"); // maps to (a) should be (c) foo(first, second); // maps to (b) ok foo(true, second); // maps to (a) ok } For foo(first, "second"); // maps to (a) should be (c) I don't see why it should map to (c). It could map to either (b) or (c). And there is no way to find out which one is the best match. Kent, for now we do map it to (a) which looks strange. The constructor's bindings are null. So no matching is performed. I'll check the conversion to see if we need to do anything.
Agree, should be (b) or (c), but not (a). Releated question: Is code resolve using these guesses as well? It would be nice to have code resolve working for contructors with errors.
Enabled the test ASTConverterTestAST3_2 Released changes into HEAD
I verified that this works as expected for the submitted test cases. Martin, please double-check that all cases are covered.
verified, removed the workaround code in jdt.ui > 20051125
*** Bug 138010 has been marked as a duplicate of this bug. ***