去年為了公司的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://m.tkk7.com/Files/xmp123/CountMethodLocation.zip
posted on 2007-03-12 15:40
自己的小屋 閱讀(2260)
評論(9) 編輯 收藏