break;
}
}
} catch(IOException e) {
e。printStackTrace();
}
}
public String'' classNames() {
String'' result = new String'classes。size()';
Enumeration e = classes。keys();
int i = 0;
while(e。hasMoreElements())
result'i++' = (String)e。nextElement();
return result;
}
public void checkClassNames() {
Enumeration files = classMap。keys();
while(files。hasMoreElements()) {
String file = (String)files。nextElement();
Vector cls = classMap。getVector(file);
for(int i = 0; i 《 cls。size(); i++) {
String className =
(String)cls。elementAt(i);
if(Character。isLowerCase(
className。charAt(0)))
System。out。println(
〃class capitalization error; file: 〃
+ file + 〃; class: 〃
+ className);
}
}
}
public void checkIdentNames() {
Enumeration files = identMap。keys();
Vector reportSet = new Vector();
while(files。hasMoreElements()) {
String file = (String)files。nextElement();
Vector ids = identMap。getVector(file);
for(int i = 0; i 《 ids。size(); i++) {
String id =
(String)ids。elementAt(i);
if(!classes。contains(id)) {
// Ignore identifiers of length 3 or
// longer that are all uppercase
// (probably static final values):
if(id。length() 》= 3 &&
id。equals(
id。toUpperCase()))
continue;
// Check to see if first char is upper:
636
…………………………………………………………Page 638……………………………………………………………
if(Character。isUpperCase(id。charAt(0))){
if(reportSet。indexOf(file + id)
== …1){ // Not reported yet
reportSet。addElement(file + id);
System。out。println(
〃Ident capitalization error in:〃
+ file + 〃; ident: 〃 + id);
}
}
}
}
}
}
static final String usage =
〃Usage: n〃 +
〃ClassScanner classnames …an〃 +
〃tAdds all the class names in this n〃 +
〃tdirectory to the repository file n〃 +
〃tcalled "classnames"n〃 +
〃ClassScanner classnamesn〃 +
〃tChecks all the java files in this n〃 +
〃tdirectory for capitalization errors; n〃 +
〃tusing the repository file "classnames"〃;
private static void usage() {
System。err。println(usage);
System。exit(1);
}
public static void main(String'' args) {
if(args。length 《 1 || args。length 》 2)
usage();
ClassScanner c = new ClassScanner();
File old = new File(args'0');
if(old。exists()) {
try {
// Try to open an existing
// properties file:
InputStream oldlist =
new BufferedInputStream(
new FileInputStream(old));
c。classes。load(oldlist);
oldlist。close();
} catch(IOException e) {
System。err。println(〃Could not open 〃
+ old + 〃 for reading〃);
System。exit(1);
}
}
if(args。length == 1) {
c。checkClassNames();
c。checkIdentNames();
}
// Write the class names to a repository:
637
…………………………………………………………Page 639……………………………………………………………
if(args。length == 2) {
if(!args'1'。equals(〃…a〃))
usage();
try {
BufferedOutputStream out =
new BufferedOutputStream(
new FileOutputStream(args'0'));
c。classes。save(out;
〃Classes found by ClassScanner。java〃);
out。close();
} catch(IOException e) {
System。err。println(
〃Could not write 〃 + args'0');
System。exit(1);
}
}
}
}
class JavaFilter implements FilenameFilter {
public boolean accept(File dir; String name) {
// Strip path information:
String f = new File(name)。getName();
return f。trim()。endsWith(〃。java〃);
}
} ///:~
MultiStringMap 类是个特殊的工具,允许我们将一组字串与每个键项对应(映射)起来。和前例一样,这里
也使用了一个散列表(Hashtable),不过这次设置了继承。该散列表将键作为映射成为Vector 值的单一的
字串对待。add()方法的作用很简单,负责检查散列表里是否存在一个键。如果不存在,就在其中放置一个。
getVector()方法为一个特定的键产生一个 Vector;而printValues()将所有值逐个Vector 地打印出来,这
对程序的调试非常有用。
为简化程序,来自标准 Java 库的类名全都置入一个 Properties (属性)对象中(来自标准Java 库)。记住
Properties 对象实际是个散列表,其中只容纳了用于键和值项的 String 对象。然而仅需一次方法调用,我
们即可把它保存到磁盘,或者从磁盘中恢复。实际上,我们只需要一个名字列表,所以为键和值都使用了相
同的对象。
针对特定目录中的文件,为找出相应的类与标识符,我们使用了两个MultiStringMap:classMap 以及
identMap。此外在程序启动的时候,它会将标准类名仓库装载到名为classes 的Properties 对象中。一旦在
本地目录发现了一个新类名,也会将其加入classes 以及classMap 。这样一来,classMap 就可用于在本地目
录的所有类间遍历,而且可用 classes 检查当前标记是不是一个类名(它标记着对象或方法定义的开始,所
以收集接下去的记号——直到碰到一个分号——并将它们都置入 identMap )。
ClassScanner 的默认构建器会创建一个由文件名构成的列表(采用FilenameFilter 的JavaFilter 实现形
式,参见第 10章)。随后会为每个文件名都调用 scanListing()。
在 scanListing() 内部,会打开源码文件,并将其转换成一个StreamTokenizer。根据Java 帮助文档,将
true 传递给 slas
小说推荐
- 软件工程实践者的思想(PDF格式)
- -Page 1-大 道 至 简—软件工程实践者的思想周爱民(Aimingoo 著-Page 2-序2004 年 11 月初爱民(Aimingoo)第一次把他的书稿给我,我翻看了一下,第一反应讲的是感想。这不错,在技
- 最新章:第26章
- 深入浅出MFC第2版(PDF格式)
- -Page 1-Page 2-山高月小山高月小 水落石出水落石出山高月小山高月小 水落石出水落石出-Page 3-深入淺出MFC(第版 使用Visual C 5.0 MFC 4.2)Dissecting MFC(Second Edition Using Visual C 5.0 MFC 4.2)侯俊
- 最新章:第309章
- VB2008从入门到精通(PDF格式英文版)
- -Page 1(R)The eXperT’s Voice inBeginningVB 2008From Novice to ProfessionalChristian Gross-Page 2-Page 3-Beginning VB 2008From Novice to Professional■C
- 最新章:第214章
- C语言游戏编程从入门到精通(PDF格式)
- -Page 1-Page 2-Page 3-Page 4-Page 5-Page 6-Page 7-Page 8-Page 9-Page 10-Page 11-Page 12-Page 13-Page 14
- 最新章:第4章
- JMS简明教程(PDF格式)
- -Page 1-JMS1.1规范中文版卫建军2007‐11‐22-Page 2
- 最新章:第28章
- C语言实例教程(PDF格式)
- -Page 1-前 言Visual C+是开发运行于Windows 95和Windows NT环境下的Win32应用程序的可视化编程工具中最重要的成员之一,它为软件开发人员提供了完整的编辑、编译和调试工具和建立于Win32 API(ApplicationProgramming Interface)基
- 最新章:第143章
- 神祗之眼 (正式版)第7卷(全文完)作者:百里芜虚
- 第五十一章离12月25日的圣诞节还有两天,纽约已经换上了节日的盛装,几天前的一场大雪令整个城市一片银白。人们笑逐颜开,都纷纷开始为节日做准备,商家为了在节日打开销路纷纷推出各种优惠促销手段,纽约的大街小巷全都这样热闹。依沙那被老婆打发出来买过节要用的杂货,而女儿则和凯妮一起到百货商场去买衣服去了。对
- 最新章:第23章
- SQL语言艺术(PDF格式)
- -Page 1-SQLSSQQLL语言艺术内容介绍本书分为12章,每一章包含许多原则或准则,并通过举例的方式对原则进行解释说明。这些例子大多来自于实际案例,对九种SQL经典查询场景以及其性能影响讨论,非常便于实践,为你数据库应用维护人员阅读。资深 SQL 专家 Stéphane Faroult倾力打
- 最新章:第27章
- php程序设计简明教程(DOC格式)
- -Page 1-PHP 程序设计简明教程PHP 讲义 第 1 页 共 90 页-Page 2-目录序 4第一章 PHP 简介 6
- 最新章:第31章