Chinaunix首页 | 论坛 | 博客
  • 博客访问: 442222
  • 博文数量: 1496
  • 博客积分: 79800
  • 博客等级: 大将
  • 技术积分: 9940
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 13:22
文章分类

全部博文(1496)

文章存档

2011年(1)

2008年(1495)

我的朋友

分类:

2008-09-09 17:21:52

    去年为了公司的Junit需求做了一个检测我们的代码是否有Junit Test Class的一个小Tool, 今天又翻出来看了下,觉得里面的有个小功能可能对大家有用,如果你也觉得有用,我觉得很荣幸。:)

1.用途:
指定一个类, 分析出类中所有方法的精确位置- 也就是某个方法在哪两行之间。
(大家可以考虑一下怎么做,然后再看看我的方法是不是太笨了。-_-!!)

2.用法:
新建一个类,将这几行代码加到mail方法,例如:



 1     public   static   void  main(String[] args)  {
 2          //  CVSParseMethod
 3          //  C:/Documents and
 4          //  Settings/hut/workspace/junitcheck/src/branding/src/java/com/webex/webapp/branding/common/BrandingTools.java
 5         CVSParseMethod x  =   new  CVSParseMethod( " C:\\Documents and Settings\\hut\\workspace\\AccountMethodLocation\\ " ,
 6                  " net.blogjava.xmp123.CVSClass " );
 7
 8          // src/java/com/webex/webapp/urlapi/biz/URLAPIMgrImpl.java
 9          // C:/Documents and Settings/hut/workspace/junitcheck/src/meetingcenter/src/java/com/webex/webapp/meetingcenter/action/common/QuickStartInviteAction.java
10         System.out.println(x.parseAction());
11     }

[补充]: CVSParseMethod的构造函数是CVSParseMethod(String String1,String String2), String1是目标的java文件在文件系统的位置,String2是类的全名..如net.blogjava.xmp123.CVSClass.
BTW:不要忘记将后面download的jar导过来。

结果会打在Console里,象这样:

Loading net.blogjava.xmp123.CVSClass
Loaded net.blogjava.xmp123.CVSClass
Found java source file in C:\Documents and Settings\hut\workspace\AccountMethodLocation
/ net / blogjava / xmp123 / CVSClass.java
Parsing net.blogjava.xmp123.CVSClass
Parsed a common 
class  and brackets can matched the methods correctly
MethodParseEngine have searched 
10  methods(Including constructors).
Completed :Spend 
0.187  seconds to parse  this  file.
[
public  CVSClass(String) locate between  52  and  123
public  CVSMethod[] getCvsMethodArray() locate between  128  and  130
public  CVSConstructor[] getCvsConstructorArray() locate between  135  and  137
public  Set getMethodSet() locate between  142  and  144
public  Set getConstructorSet() locate between  149  and  151
public  String getClasspath() locate between  156  and  158
public  List getMethodList() locate between  163  and  165
public  String getClassName() locate between  174  and  176
public   boolean  equals(Object) locate between  183  and  188
public   int  hashCode() locate between  193  and  196
]

3. 我是怎么做的。

.用java反射拿出所有的的类的构造函数和方法。
.读java源文件,通过匹配左右括号来匹配方法并记录出合适的位置。

4. 存在的问题:
到目前为止,这个小程序至少不能分析它自己的主类:CVSParseMethod(很多Exceptions), 因为这个类里面定义的一些String中含用 { }而导致匹配的时候出错。呵呵。但是我保证,它应该在>90%的概率下正常工作。:) 

5. 的地址:
http://www.blogjava.net/Files/xmp123/CountMethodLocation.zip
【责编:Peng】

--------------------next---------------------
Loading net.blogjava.xmp123.CVSClass
Loaded net.blogjava.xmp123.CVSClass
Found java source file in C:\Documents and Settings\hut\workspace\AccountMethodLocation
/ net / blogjava / xmp123 / CVSClass.java
Parsing net.blogjava.xmp123.CVSClass
Parsed a common 
class  and brackets can matched the methods correctly
MethodParseEngine have searched 
10  methods(Including constructors).
Completed :Spend 
0.187  seconds to parse  this  file.
[
public  CVSClass(String) locate between  52  and  123
public  CVSMethod[] getCvsMethodArray() locate between  128  and  130
public  CVSConstructor[] getCvsConstructorArray() locate between  135  and  137
public  Set getMethodSet() locate between  142  and  144
public  Set getConstructorSet() locate between  149  and  151
public  String getClasspath() locate between  156  and  158
public  List getMethodList() locate between  163  and  165
public  String getClassName() locate between  174  and  176
public   boolean  equals(Object) locate between  183  and  188
public   int  hashCode() locate between  193  and  196
]

--------------------next---------------------

阅读(155) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~