如何将常量重定向到Eclipse中的枚举? [英] How to refactor constants to enums in Eclipse?

查看:87
本文介绍了如何将常量重定向到Eclipse中的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现eclipse中没有内置的功能:
http://help.eclipse.org/juno/ index.jsp?topic =%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm



我发现一个插件:
http://code.google.com/p/constants-to-enum -eclipse-插件/ 。我想知道这个插件是否可以使用,或者任何人使用更好的方法。



我可以随时自己创建一个枚举类,并在2012年删除繁琐的常量。请不要指向另一个IDE,我太老了也改变不好的习惯; - )

解决方案

以下是一组自动化和手动的步骤来进行重构。



步骤1在常量上封装字段



步骤2(可选)重命名常量。如果要重用名称,请执行此操作。



步骤3(手动)使用常量值创建枚举。给枚举一个getValue方法返回常量。



步骤4(手动)将枚举中的getValue替换为getter的返回值。



步骤5将吸气剂放在一起。选择所有参考和删除方法声明。



步骤6内联常量。选择所有参考和删除常量声明。



如果你愿意,你可以在6之后停止,但是还有更多的功能要使用枚举的力量



步骤7对于使用enum.getValue()作为参数的每个方法,替换使用枚举传递的常量。



步骤7a更改方法签名以将枚举添加为参数。



步骤7b(手动)枚举实例作为getValue调用旁边的新参数。确保您找到所有的实例,或者稍后会出现问题。



步骤7c(手动)在该方法中,使用新的枚举参数,而不是常量。如果您在步骤7b中没有通话,您的测试将在此失败。



步骤7d更改方法签名以删除旧常数。



步骤8(手动)对于布尔逻辑中的每个使用enum.getValue(),都可以确定是否可以使用枚举。



步骤9如果getValue方法不再使用,可以被删除。



步骤9a(手动)删除未使用的getValue方法



步骤9b(手动)删除构造函数中的字段和赋值。



步骤9c更改方法签名以从枚举构造函数中删除该值。



步骤9d(手动)如果没有其他参数,请删除枚举构造函数。






例如:

  public class Initial {
public static final String CONSTANT1 =value1;
public static final String CONSTANT2 =value2;

public void method(String aConstant)
{
if(aConstant.equals(CONSTANT2))
{
// do something

}

public void anotherMethod()
{
方法(CONSTANT1);
}

}

步骤1

  private static final String CONSTANT1 =value1; 
private static final String CONSTANT2 =value2;

public void method(String aConstant)
{
if(aConstant.equals(getConstant2()))
{
//做某事
}
}

public void anotherMethod()
{
method(getConstant1());
}

public static String getConstant1(){
return CONSTANT1;
}

public static String getConstant2(){
return CONSTANT2;
}

步骤2重命名常量



private static final String _CONSTANT1 =value1;

  
private static final String _CONSTANT2 =value2;
...
public static String getConstant1(){
return _CONSTANT1;
}

public static String getConstant2(){
return _CONSTANT2;
}

步骤3创建枚举

  public static enum AnEnum {
CONSTANT1(_CONSTANT1),CONSTANT2(_CONSTANT2);

private final String value;

AnEnum(String aValue)
{
value = aValue;
}

public String getValue()
{
返回值;
}
}

步骤4在Constant getters中替换返回值

  public static String getConstant1(){
return AnEnum.CONSTANT1.getValue();
}

public static String getConstant2(){
return AnEnum.CONSTANT2.getValue();
}

步骤5内置不断的getters

  public void method(String aConstant)
{
if(aConstant.equals(AnEnum.CONSTANT2.getValue()))
{
//做某事
}
}

public void anotherMethod()
{
方法(AnEnum.CONSTANT1.getValue()) ;
}

步骤6内联常数

  public static enum AnEnum {
CONSTANT1(value1),CONSTANT2(value2);

步骤7a更改方法Signiture以添加枚举作为参数。

  public void method(String aConstant,AnEnum theEnum)
....
public void anotherMethod()
{
方法(AnEnum.CONSTANT1.getValue(),null);
}

步骤7b将枚举实例作为新参数传递给getValue调用



  public void anotherMethod()
{
方法(AnEnum.CONSTANT1.getValue(),AnEnum.CONSTANT1) ;
}

步骤7c使用新的枚举参数,而不是旧的传递值。 p>

  if(theEnum.getValue()。equals(AnEnum.CONSTANT2.getValue()))
{

步骤7d更改方法签名以删除旧常数

  public void method(AnEnum theEnum)
....

public void anotherMethod()
{
方法(AnEnum。 CONSTANT1);
}

步骤8每次使用enum.getValue()在布尔逻辑中确定如果您可以使用枚举。

  if(theEnum.equals(AnEnum.CONSTANT2))
{
//做某事
}

步骤9a删除未使用的getValue方法
步骤9b(手动)在构造函数中删除字段和赋值。
步骤9c更改方法签名以从枚举构造函数中删除该值。
步骤9d(手动)如果没有其他参数,请删除枚举构造函数。

  public static enum AnEnum {
CONSTANT1,CONSTANT2;
}

所以最后代码如下所示:

  public class Step9d {

public static enum AnEnum {
CONSTANT1,CONSTANT2;
}

public void method(AnEnum theEnum)
{
if(theEnum.equals(AnEnum.CONSTANT2))
{
/ /做某事
}
}

public void anotherMethod()
{
method(AnEnum.CONSTANT1);
}

}


How do I refactor Java constants to enums with eclipse?

I found no built-in functionality in eclipse: http://help.eclipse.org/juno/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Freference%2Fref-menu-refactor.htm

I found a plugin: http://code.google.com/p/constants-to-enum-eclipse-plugin/. I'm wondering whether the plugin is the way to go or anyone uses a better approach.

I could always create an enum class myself an cut&paste the constants which is tedious in 2012. Please don't point me to another IDE, I'm too old too change bad habits ;-)

解决方案

Here are a set of automated and manual steps to do this refactoring.

Step 1 Encapsulate field on the constants

Step 2 (Optional) Rename the constants. Do this if you want to reuse the names.

Step 3 (Manual) Create the enum using the values of the constants. Give the enum a getValue method that return the constant.

Step 4 (Manual) Replace the return value in the getters with getValue from the enum.

Step 5 Inline the getters. Choose "All References", and "Delete method declaration".

Step 6 Inline the Constants. Choose "All References", and "Delete constant declaration".

You can stop after 6 if you want, but there is more to be done to use the power of enums.

Step 7 For each method that uses enum.getValue() as a parameter, replace the constant was passed with the enum.

Step 7a Change Method Signature to add the Enum as a parameter.

Step 7b (Manual) Pass the enum instance as the new parameter alongside the getValue call. Make sure you find all the instances, or there will be problems later.

Step 7c (Manual) In the method, use the new enum parameter instead of the constant. If you missed a call in step 7b your tests will fail here.

Step 7d Change Method Signature to remove the old Constant.

Step 8 (Manual) For each use of the enum.getValue() in boolean logic determine if you can use the enum instead.

Step 9 If the getValue method is no longer used it can be removed.

Step 9a (Manual) Delete the unused getValue method

Step 9b (Manual) Delete field and assignment in the constructor.

Step 9c Change Method Signature to remove the value from the enum constructor.

Step 9d (Manual) If no other parameters, Remove the the enum constructor.


For example:

public class Initial {
public static final String CONSTANT1 = "value1";
public static final String CONSTANT2 = "value2";

public void method(String aConstant)
{
    if(aConstant.equals(CONSTANT2))
    {
        //do something
    }
}

public void anotherMethod()
{
    method(CONSTANT1);
}

}

Step 1

private static final String CONSTANT1 = "value1";
private static final String CONSTANT2 = "value2";

public void method(String aConstant)
{
    if(aConstant.equals(getConstant2()))
    {
        //do something
    }
}

public void anotherMethod()
{
    method(getConstant1());
}

public static String getConstant1() {
    return CONSTANT1;
}

public static String getConstant2() {
    return CONSTANT2;
}

Step 2 Rename Constants

private static final String _CONSTANT1 = "value1";
private static final String _CONSTANT2 = "value2";
...
public static String getConstant1() {
    return _CONSTANT1;
}

public static String getConstant2() {
    return _CONSTANT2;
}

Step 3 Create Enum

    public static enum AnEnum {
    CONSTANT1(_CONSTANT1), CONSTANT2(_CONSTANT2);

    private final String value;

    AnEnum(String aValue)
    {
        value = aValue;
    }

    public String getValue()
    {
        return value;
    }
}

Step 4 Replace return value in Constant getters

    public static String getConstant1() {
    return AnEnum.CONSTANT1.getValue();
}

public static String getConstant2() {
    return AnEnum.CONSTANT2.getValue();
}

Step 5 Inline the constant getters

public void method(String aConstant)
{
    if(aConstant.equals(AnEnum.CONSTANT2.getValue()))
    {
        //do something
    }
}

public void anotherMethod()
{
    method(AnEnum.CONSTANT1.getValue());
}

Step 6 Inline the Constants

    public static enum AnEnum {
    CONSTANT1("value1"), CONSTANT2("value2");

Step 7a Change Method Signiture to add enum as parameter.

    public void method(String aConstant, AnEnum theEnum)
    ....
    public void anotherMethod()
{
    method(AnEnum.CONSTANT1.getValue(), null);
}

Step 7b Pass the enum instance as the new parameter alongside the getValue call

    public void anotherMethod()
{
    method(AnEnum.CONSTANT1.getValue(), AnEnum.CONSTANT1);
}

Step 7c Us the new enum parameter instead of the old passed value.

        if(theEnum.getValue().equals(AnEnum.CONSTANT2.getValue()))
    {

Step 7d Change Method Signature to remove the old Constant

public void method(AnEnum theEnum)
....

public void anotherMethod()
{
    method(AnEnum.CONSTANT1);
}

Step 8 For each use of the enum.getValue() in boolean logic determine if you can use the enum instead.

        if(theEnum.equals(AnEnum.CONSTANT2))
    {
        //do something
    }

Step 9a delete the unused getValue method Step 9b (Manual) Delete field and assignment in the constructor. Step 9c Change Method Signature to remove the value from the enum constructor. Step 9d (Manual) If no other parameters, Remove the the enum constructor.

    public static enum AnEnum {
    CONSTANT1, CONSTANT2;
}

So finally the code looks like this:

public class Step9d {

public static enum AnEnum {
    CONSTANT1, CONSTANT2;
}

public void method(AnEnum theEnum)
{
    if(theEnum.equals(AnEnum.CONSTANT2))
    {
        //do something
    }
}

public void anotherMethod()
{
    method(AnEnum.CONSTANT1);
}

}

这篇关于如何将常量重定向到Eclipse中的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆