Community
Participate
Working Groups
Here is some old "legacy" code. I was using the refactoring tool that I've trusted very much until now. Start with the following code, then elect the last occurrence of "" + val import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class LegacyCode { public static void ugly() { List<Long> in = new ArrayList<Long>(); Map<String,Long> out = new HashMap<String,Long>(); Iterator<Long> it = null; it = in.iterator(); Long val = null; // Refactoring inserts new variable here: String key = "" + val; while (it.hasNext()) { val = it.next(); if (!out.containsKey("" + val)) out.put("" + val, val); } it = in.iterator(); while (it.hasNext()) { val = it.next(); // I would expect a local variable to be created here if (!out.containsKey("" + val)) out.put("" + val, val); } } } After: import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; public class LegacyCode { public static void ugly() { List<Long> in = new ArrayList<Long>(); Map<String,Long> out = new HashMap<String,Long>(); Iterator<Long> it = null; it = in.iterator(); Long val = null; String key = "" + val; // Wrong place while (it.hasNext()) { val = it.next(); if (!out.containsKey(key)) out.put(key, val); } it = in.iterator(); while (it.hasNext()) { val = it.next(); if (!out.containsKey(key)) out.put(key, val); } } }
Move to JDT/UI
The location is wrong because the value of variable 'val' changes in between, which is bug 27740. *** This bug has been marked as a duplicate of bug 27740 ***