Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[jdt-ui-dev] Bug in Extract Local Variable refactoring

Basically, ExtractLocalVariable ignores relevant type guards and side-effects
when computing where to move the extracted expression.

Bugzilla bug URL:

  http://bugs.eclipse.org/bugs/show_bug.cgi?id=35788

Here's a copy of the bug report, FYI.

=============================================================
Each of the three "main()" methods below provides a distinct test-case.
All of them fail under version 2.1M4.

I have a partial patch for the type-guard case (partial because the
already-existing createAndInsertTempDeclaration() isn't prepared to
create a block to surround the extracted temp variable declaration
statement, so I still have to fix that).  Also, I'm in the process
of creating a proper testcase for inclusion in the regression suite
in org.eclipse.jdt.ui.tests.refactoring.

How should I go about submitting these patches?

The side-effect case, on the other hand, requires some flow analysis
(computing and examining use/def chains) which I'm guessing someone
has already done somewhere inside the JDT.

public class ExtractVariableTest
{
    static public void main(String[] args)
    {
        Object x = System.getProperty("foo");

        if (x instanceof String)
            System.out.println(((String) x).substring(0, 5));
            // Select:         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            // and invoke Refactor->Extract Local Variable
            // Expression will be moved outside the type guard!
    }

    static public void main2(String[] args)
    {
        Object x;

        if ((x = System.getProperty("foo")) != null)
            System.out.println(x.toString());
            // Select:         ^^^^^^^^^^^^
            // and invoke Refactor->Extract Local Variable
            // Expression will be moved above the assignment to x!
    }

    static public void main3(String[] args)
    {
        Object x;
        System.out.println(((x = foo()) != null) ? x.toString() : "");
        // Select:                                 ^^^^^^^^^^^^
        // and invoke Refactor->Extract Local Variable
        // Expression will be moved above the assignment to x!
    }

    static public Object foo() { return null; }
}


Cheers,
-- Bob


Back to the top