Community
Participate
Working Groups
In M2. In the statement: URL url = new URL(null); Trying to extract a local variable from the "null" expression tells me "Cannot extract a single null literal". However, there's no ambiguity in this case: the only thing I can want is: String local = null; URL url = new URL(local);
We currently determine the type of the new local variable by inspecting the extracted expression. This does not work for null, so we would have to go up in the AST and analyze the parent to see what the upper bound of expected types is in that context. That can be somewhat tricky in the general case, e.g.: class Break { void use() { takeT(null, 42); } <T extends Number & Comparable<T>> void takeT(T t, T t2) { } }
Absolutely the general case may be quite difficult. I'd be happy if simple cases like the one in the original bug description were handled correctly.
For me it would be sufficient to make the new parameter/variable of type Object. In most cases this would leave me with a type error that is easily handled using the "Change Typ of XXX to XXX" quick fix. Bonus points for integrating the second step into the refactoring. Of course without the correct type this would move the extract operation out of the realm of refactorings but for me it would just work (TM) most of the times, which is more important for me.
Moving to correct owner
*** Bug 434346 has been marked as a duplicate of this bug. ***
The first part, creating Object x = null; Stream<Path> s = Files.walk(x); from Stream<Path> s = Files.walk(null); can be done simply by removing the check for NullLiteral in ExtractTempRefactoring.java on line 437: // if (selectedExpression instanceof NullLiteral) { // return RefactoringStatus.createFatalErrorStatus(RefactoringCoreMessages.ExtractTempRefactoring_null_literals); // } else
The variable created in this manner cannot be inlined again. Just as I suspected Inline Local Variable refactoring contains a similar check and is just as easily fixed. See bug 93850 for details. This bug has also annoyed me for a long time. Time to fix: 9 years and 10 minutes.
Link to commit which fixes this bug: https://github.com/Overruler/eclipse.jdt.ui/commit/eeb824de688bf288ed66fcf922ce2c8c38b91c99
The fix to this bug is included in Evening-IDE, which is available here: http://overruler.github.io/Evening-IDE/