This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

Bug 237497

Summary: Extract method: Generates uncompileable code when there are return values
Product: java Reporter: markiewb
Component: HintsAssignee: Svata Dedic <sdedic>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P2    
Version: 7.4   
Hardware: PC   
OS: Windows 7   
Issue Type: DEFECT Exception Reporter:
Attachments: Shows the issue
Simplified example

Description markiewb 2013-10-22 20:33:37 UTC
Created attachment 141429 [details]
Shows the issue

* Take this code
package javaapplication17;

public class JavaApplication17 {

    public static void main(String[] args) {
        new JavaApplication17().doit();
    }

    private String doit() {
        [if ("1".equals("2")) {
            return "";
        }
        return "bla";]
    }

    private String doit2() {
        if ("1".equals("2")) {
            return "";
        }
        return "bla";
    }

    private String doit3() {
        if ("1".equals("2")) {
            return "";
        }
        return "bla";
    }

}
* extract a method from the selected code (marked via []) 
* choose to refactor also other occurrences

ACTUAL: generated code cannot be compiled. The return value is not returned. See screenshot. For example

    private String doit2() {
        extractedMethod();
    }

EXPECTED: return values are supported too. No compile errors.

Product Version: NetBeans IDE 7.4 (Build 201310012201)
Java: 1.7.0_40; Java HotSpot(TM) 64-Bit Server VM 24.0-b56
Runtime: Java(TM) SE Runtime Environment 1.7.0_40-b43
System: Windows 7 version 6.1 running on amd64; Cp1252; en_US (nb)
User directory: C:\Users\markiewb\AppData\Roaming\NetBeans\7.4
Cache directory: C:\Users\markiewb\AppData\Local\NetBeans\Cache\7.4
Comment 1 markiewb 2013-10-22 20:34:20 UTC
Created attachment 141430 [details]
Simplified example

Here is another simplified example.
Comment 2 Jiri Prox 2013-10-22 20:37:19 UTC
reproducible
Comment 3 Svata Dedic 2013-10-30 12:58:33 UTC
The 'introduce hint' method computes the replacements in a wrong way; it carefully analyzes the selection and its surroundings to detect later usages of values computed within the selection. Such analysis is not done for the duplicates. The code should be rewritten to perform the analysis for each potential duplicate found. Also each of the duplicates may provide a different break/continue exit target, which does not seem to be handled properly now.
Comment 4 Svata Dedic 2013-11-22 17:02:28 UTC
Fixed in http://hg.netbeans.org/jet-main/rev/d4cbb394d97c
Comment 5 Quality Engineering 2013-11-24 02:19:49 UTC
Integrated into 'main-silver', will be available in build *201311240002* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)

Changeset: http://hg.netbeans.org/main-silver/rev/d4cbb394d97c
User: Svata Dedic <sdedic@netbeans.org>
Log: #235783, #237497: fixed behaviour if extracted method's outcome is further used. Refactored, code reused, de-optimized to make more readable