java – 如何将常量重定向到Eclipse中的枚举?
如何使用
eclipse将
Java常量重命名为枚举?
我发现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() { method(CONSTANT1); } } 步骤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; } 步骤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() { return value; } } 步骤4替换Constant getter中的返回值 public static String getConstant1() { return AnEnum.CONSTANT1.getValue(); } public static String getConstant2() { return AnEnum.CONSTANT2.getValue(); } 第5步将不断的吸气器插入 public void method(String aConstant) { if(aConstant.equals(AnEnum.CONSTANT2.getValue())) { //do something } } public void anotherMethod() { method(AnEnum.CONSTANT1.getValue()); } 步骤6内联常数 public static enum AnEnum { CONSTANT1("value1"),CONSTANT2("value2"); 步骤7a更改方法签名添加枚举作为参数. public void method(String aConstant,AnEnum theEnum) .... public void anotherMethod() { method(AnEnum.CONSTANT1.getValue(),null); } 步骤7b将枚举实例作为新参数传递给getValue调用 public void anotherMethod() { method(AnEnum.CONSTANT1.getValue(),AnEnum.CONSTANT1); } 步骤7c使用新的枚举参数,而不是旧的传递值. if(theEnum.getValue().equals(AnEnum.CONSTANT2.getValue())) { 步骤7d更改方法签名以删除旧常数 public void method(AnEnum theEnum) .... public void anotherMethod() { method(AnEnum.CONSTANT1); } 步骤8对于每个使用enum.getValue()在布尔逻辑中,确定是否可以使用枚举. if(theEnum.equals(AnEnum.CONSTANT2)) { //do something } 步骤9a删除未使用的getValue方法 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)) { //do something } } public void anotherMethod() { method(AnEnum.CONSTANT1); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |