去年为了公司的junit需求做了一个检测我们的代码是否有junit test class的一个小tool, 今天又翻出来看了下,觉得里面的有个小功能可能对大家有用,如果你也觉得有用,我觉得很荣幸。:)
1.用途:
指定一个类, 分析出类中所有方法的精确位置- 也就是某个方法在哪两行之间。
(大家可以考虑一下怎么做,然后再看看我的方法是不是太笨了。-_-!!)
2.用法:
新建一个类,将这几行代码加到mail方法,例如:

示例使用代码
code highlighting produced by actipro codehighlighter (freeware)
http://www.codehighlighter.com/
--> 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