去年為了公司的Junit需求做了一個(gè)檢測(cè)我們的代碼是否有Junit Test Class的一個(gè)小Tool, 今天又翻出來(lái)看了下,覺(jué)得里面的有個(gè)小功能可能對(duì)大家有用,如果你也覺(jué)得有用,我覺(jué)得很榮幸。:)
1.用途:
指定一個(gè)類, 分析出類中所有方法的精確位置- 也就是某個(gè)方法在哪兩行之間。
(大家可以考慮一下怎么做,然后再看看我的方法是不是太笨了。-_-!!)
2.用法:
新建一個(gè)類,將這幾行代碼加到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
????}
[補(bǔ)充]: CVSParseMethod的構(gòu)造函數(shù)是CVSParseMethod(String String1,String String2), String1是目標(biāo)的java文件在文件系統(tǒng)的位置,String2是類的全名..如net.blogjava.xmp123.CVSClass.
BTW:不要忘記將后面download的jar導(dǎo)過(guò)來(lái)。
結(jié)果會(huì)打在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反射拿出所有的的類的構(gòu)造函數(shù)和方法。
.讀java源文件,通過(guò)匹配左右括號(hào)來(lái)匹配方法并記錄出合適的位置。
4. 存在的問(wèn)題:
到目前為止,這個(gè)小程序至少不能分析它自己的主類:CVSParseMethod(很多Exceptions), 因?yàn)檫@個(gè)類里面定義的一些String中含用 { }而導(dǎo)致匹配的時(shí)候出錯(cuò)。呵呵。但是我保證,它應(yīng)該在>90%的概率下正常工作。:)?
5. 下載的地址:
http://www.aygfsteel.com/Files/xmp123/CountMethodLocation.zip
posted on 2007-03-12 15:40
自己的小屋 閱讀(2264)
評(píng)論(9) 編輯 收藏