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 206193

Summary: [71cat] malfunction of extract method if duplicate code exists
Product: java Reporter: muellermi <muellermi>
Component: HintsAssignee: Jan Lahoda <jlahoda>
Status: RESOLVED FIXED    
Severity: normal    
Priority: P3    
Version: 7.1   
Hardware: PC   
OS: Windows XP   
Issue Type: DEFECT Exception Reporter:

Description muellermi 2011-12-09 13:52:41 UTC
Product Version = NetBeans IDE 7.1 (Build 201112051121)
Operating System = Windows XP version 5.1 running on x86
Java; VM; Vendor = 1.7.0_01
Runtime = Java HotSpot(TM) Client VM 21.1-b02

given this portion of code:
method1:
[...]
        try {
            FileInputStream fis = new FileInputStream(encryptedFile);
            Pair<Integer, byte[]> versionAndKey = readHeader(fis);
(x)            SecretKeySpec key = new SecretKeySpec(rsaDecrypt(versionAndKey.getValue2()), "AES");
(x)            Cipher cipher = Cipher.getInstance("AES");
(x)            cipher.init(Cipher.DECRYPT_MODE, key);
            decryptStream(fis, cipher, new File(decryptedFile));
[...]

method2:
[...]
        try {
            FileInputStream fis = new FileInputStream(encryptedFile);
            Pair<Integer, byte[]> versionAndKey = readHeader(fis);
(x)           SecretKeySpec key = new SecretKeySpec(rsaDecrypt(versionAndKey.getValue2()), "AES");
(x)            Cipher cipher = Cipher.getInstance("AES");
(x)            cipher.init(Cipher.DECRYPT_MODE, key);
            if (versionAndKey.getValue1() != 2) {
[...]

- In method1, mark the lines indicated with (x)
- Press Alt-Shift-m
- name new Method getCipher
--> NB asks whether you would replace the duplicate code of method2 too
- answer yes
--> in method1 these three lines are replaced by
            Cipher cipher = getCipher(versionAndKey);
whilst in method2 it is
            Cipher cipher;

### Method call is missing. ###




--------------------------------------------------------------------------------------------------------------------------
BTW:

The automatic introduced method is

    private Cipher getCipher(Pair<Integer, byte[]> versionAndKey) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
        SecretKeySpec key = new SecretKeySpec(rsaDecrypt(versionAndKey.getValue2()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher;
    }

SecretKeySpec needs an array of byte, versionAndKey.getValue2() is the one. Thus refactoring might better result in

    private Cipher getCipher(byte[]> value2) throws NoSuchAlgorithmException, InvalidKeyException, NoSuchPaddingException {
        SecretKeySpec key = new SecretKeySpec(rsaDecrypt(value2()), "AES");
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher;
    }

whereas value2  is a derived variable name. Is this feasable?
If the refactoring works like this, the developer only needs to change this ugly name.
Comment 1 Jiri Prox 2011-12-12 12:34:41 UTC
reproducible
Comment 2 Jan Lahoda 2012-04-23 13:34:47 UTC
Fixed in NetBeans daily builds, thanks for the report:
http://hg.netbeans.org/jet-main/rev/8b5a083160a0
Comment 3 Quality Engineering 2012-04-26 10:35:53 UTC
Integrated into 'main-golden', will be available in build *201204260400* on http://bits.netbeans.org/dev/nightly/ (upload may still be in progress)
Changeset: http://hg.netbeans.org/main-golden/rev/8b5a083160a0
User: Jan Lahoda <jlahoda@netbeans.org>
Log: #206193: introduce method sometimes forgets to invoke the newly created method due to a flag clash - adding a new flag to keep track if the method was or was not already invoked.