利用myeclipse的重构自动抽取成方法
2013-06-06 11:03
176 查看
public class IntroSpectorDemo {
public static void main(String[] args) throws Exception {
ReflectPoint2 pt1 = new ReflectPoint2(3,5);
String propertyName="x";
//"x"-->"X"-->"getX"-->"MethodGetX"-->
PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(pt1);
System.out.println(retVal);
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX = pd2.getWriteMethod();
methodSetX.invoke(pt1, 7);
//选中pt1.getX(),ALT+/可以自动填上输出语句
System.out.println(pt1.getX());;
}
现在想把标绿色的部分抽取成方法:
第一步,选中这三行,点右键-->Refactor-->Extract Methos-->
![](http://img.blog.csdn.net/20130606105554234)
在Method name文本框里填上方法名就会自动生成方法了。如填上”getProperty“,点击OK后,代码就编程这样了:
public class IntroSpectorDemo {
public static void main(String[] args) throws Exception {
ReflectPoint2 pt1 = new ReflectPoint2(3,5);
String propertyName="x";
//"x"-->"X"-->"getX"-->"MethodGetX"-->
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
Object value = 7;
setProperties(pt1, propertyName, value);
//选中pt1.getX(),ALT+/可以自动填上输出语句
System.out.println(pt1.getX());;
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX = pd2.getWriteMethod();
methodSetX.invoke(pt1, value);
}
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(pt1);
return retVal;
}
}
setProperties()方法生成步骤类似,只是要注意一点:
methodSetX.invoke(pt1, 7);方法不会识别7这个常量,为了使方法能将7生成一个参数,需要建立一个变量,值设为7,把这个变量传给invoke()才行。
public static void main(String[] args) throws Exception {
ReflectPoint2 pt1 = new ReflectPoint2(3,5);
String propertyName="x";
//"x"-->"X"-->"getX"-->"MethodGetX"-->
PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(pt1);
System.out.println(retVal);
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX = pd2.getWriteMethod();
methodSetX.invoke(pt1, 7);
//选中pt1.getX(),ALT+/可以自动填上输出语句
System.out.println(pt1.getX());;
}
现在想把标绿色的部分抽取成方法:
第一步,选中这三行,点右键-->Refactor-->Extract Methos-->
在Method name文本框里填上方法名就会自动生成方法了。如填上”getProperty“,点击OK后,代码就编程这样了:
public class IntroSpectorDemo {
public static void main(String[] args) throws Exception {
ReflectPoint2 pt1 = new ReflectPoint2(3,5);
String propertyName="x";
//"x"-->"X"-->"getX"-->"MethodGetX"-->
Object retVal = getProperty(pt1, propertyName);
System.out.println(retVal);
Object value = 7;
setProperties(pt1, propertyName, value);
//选中pt1.getX(),ALT+/可以自动填上输出语句
System.out.println(pt1.getX());;
}
private static void setProperties(Object pt1, String propertyName,
Object value) throws IntrospectionException,
IllegalAccessException, InvocationTargetException {
PropertyDescriptor pd2 = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodSetX = pd2.getWriteMethod();
methodSetX.invoke(pt1, value);
}
private static Object getProperty(Object pt1, String propertyName)
throws IntrospectionException, IllegalAccessException,
InvocationTargetException {
PropertyDescriptor pd = new PropertyDescriptor(propertyName,pt1.getClass());
Method methodGetX = pd.getReadMethod();
Object retVal = methodGetX.invoke(pt1);
return retVal;
}
}
setProperties()方法生成步骤类似,只是要注意一点:
methodSetX.invoke(pt1, 7);方法不会识别7这个常量,为了使方法能将7生成一个参数,需要建立一个变量,值设为7,把这个变量传给invoke()才行。
相关文章推荐
- JAVA-----myeclipse快捷操作:一段代码自动生成方法 refactor重构extract method抽取方法
- JAVA-----myeclipse快捷操作:一段代码自动生成方法 refactor重构extract method抽取方法
- 能利用myeclipse自动生成WSDL对Java类中方法的返回值是有要求的
- myeclipse方法和类的自动注解
- Unity5序列帧动画实现方法及相关问题的解决方案(利用TexturePacker导出图集、自动生成AnimationClip)
- VS2005(excel2007)利用Automation(OLD Automation)方法。将Excel当成组件服务器的编译错误 我的系统盘和office都装在d盘。 自动生成的import有问题
- 利用eclipse(MyEclipse)快速生成set、get方法的方法
- Myeclipse自动生成的get set方法 加上文本注释
- 利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO
- MyEclipse 代码自动提示功能失效 提示No Default Proposals 或 no completions available 的解决方法
- MyEclipse 6.5 代码单词自动提示设置方法
- 利用libtool自动生成动态库的Makefile的生成方法
- MyEclipse8.5 自动生成注册码的破解方法
- beans.xml cannot be opened错误解决方法(MyEclipse不能自动编译)
- MyEclipse不能自动编译解决方法
- php利用cookie实现自动登录的方法
- MyEclipse 8.x 关闭自动更新方法
- 利用MyEclipse自动创建PO类、hbm文件(映射文件)、DAO
- 【技术贴】myeclipse自动提示代码很慢的解决方法 |自动提示卡慢
- 利用rsync自动备份 完全配置方法 增加了ip限制