How to refactor code extract method eclipse

How to refactor code extract method eclipse

This demo will show you how to refactor you class method if its getting too long and extract new method from it.

  • Create class and method for test. Below class you could use for this demo
package com.javahonk.refactor;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class RefactorMethod {

    public static void main(String[] args) {

        RefactorMethod refactorMethod = new RefactorMethod();
        refactorMethod.refactorThisMethod();
    }

    private void refactorThisMethod(){
        Map<String, String> map = new HashMap<String,String>();

        map.put("Java", "Java");
        map.put("Honk", "Honk");

        System.out.println("HashMap example");
        Iterator<Entry<String, String>> it = 
                map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String,String> entry = 
                    (Map.Entry<String,String>) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            System.out.println("Key: " + key + "  Value: " + val);

        }

        System.out.println("\nHashTable example");
        Hashtable<String, String> hashtable = 
                new Hashtable<String, String>();
        hashtable.put("Java", "Java");  
        hashtable.put("Honk", "Honk");

        Enumeration<String> enumeration = hashtable.keys();

        while (enumeration.hasMoreElements()) {
            String value = (String) enumeration.nextElement();
            System.out.println("Key: "+value 
                    +"  Value: "+hashtable.get(value));

        }
    }

}

 

  • On above class we have created refactorThisMethod() for refactoring.
  • Create class RefactorMethod.java and copy paste above code. Select from line number 36-49 as below

How to refactor code extract method eclipse

 

  • Right click –> Refactor –> Extract Method…

How to refactor code extract method eclipse

 

  • Give Method name, Choose Access Modifier, Declare thrown runtime exception and Generate method comment based on your requirement. For demo selected below then click OK to extract method for selected code:

How to refactor code extract method eclipse

 

  • You will see our demo class method name refactorThisMethod() refactored with new method name extractMethodForTreeMap() as below:
package com.javahonk.refactor;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

public class RefactorMethod {

    public static void main(String[] args) {

        RefactorMethod refactorMethod = new RefactorMethod();
        refactorMethod.refactorThisMethod();
    }

    private void refactorThisMethod(){
        Map<String, String> map = new HashMap<String,String>();

        map.put("Java", "Java");
        map.put("Honk", "Honk");

        System.out.println("HashMap example");
        Iterator<Entry<String, String>> it = 
                map.entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry<String,String> entry = 
                    (Map.Entry<String,String>) it.next();
            String key = (String) entry.getKey();
            String val = (String) entry.getValue();
            System.out.println("Key: " + key + "  Value: " + val);

        }

        extractMethodForTreeMap();
    }

    /**
     * 
     */
    public void extractMethodForTreeMap() {
        System.out.println("\nHashTable example");
        Hashtable<String, String> hashtable = 
                new Hashtable<String, String>();
        hashtable.put("Java", "Java");  
        hashtable.put("Honk", "Honk");

        Enumeration<String> enumeration = hashtable.keys();

        while (enumeration.hasMoreElements()) {
            String value = (String) enumeration.nextElement();
            System.out.println("Key: "+value 
                    +"  Value: "+hashtable.get(value));

        }
    }

}

 

  • Now for test let’s run this program to see the output. To run Right click class –> Run As –> Java Application
  • You will see below output. That’s it.

How to refactor code extract method eclipse

Leave a Reply

Your email address will not be published. Required fields are marked *

I am not Robot *