红联Linux门户
Linux帮助

JAVA基础:从.class文件中寻找类名

发布时间:2006-11-22 00:36:23来源:红联作者:Heroop
可以有多种方式判断类的名字:

假定所有的类都以com,org,或者net开头
搜索classpath来寻找名字为Hack的类
搜索classpath来寻找映射到这个文件的目录入口
直接查看类自身。对于所有的情况下,这是最直接的
Java规范规定了如何查看一个class文件。存储在class文件一开始的一个值就是以Java内部分割符“/”为格式的完整的类明。

下面的代码返回了文件C:\code\hacks\research\com\generationjava\Hack.class的内部类名com/generationjava/Hack,读者可以进一步编写代码将“/”转换成“.”:

static final public int MAGIC = 0xCAFEBABE;
// 传递C:\code\hacks\research\com\generationjava\Hack.class
// 假定存在这样一个类
public String getFullyQualifiedName(String filename) throws IOException {
String name = null;

try {
DataInputStream in = new
DataInputStream(new FileInputStream(filename));
if(in.readInt() != MAGIC) {
// 不是一个.class文件
thrownew IOException("Not a class file");
}

in.readUnsignedShort();// 次版本号
in.readUnsignedShort();// 主版本号
in.readUnsignedShort();// 长度
in.readByte();// CLASS=7
in.readUnsignedShort();// 忽略这个地方
in.readByte();// UTF8=1
name =in.readUTF();//类的名字!!!
in.close();
} catch(IOException ioe) {
ioe.printStackTrace();
}

return name;
}
当你直接从.class文件中获得类的信息的时候,就简化了开发。查找一个匿名.class类文件的完整的类名是任何基于.class文件工作的应用的重要部件。
文章评论

共有 2709 条评论

  1. 0.0.0.* 于 2007-06-19 09:17:52发表:

    It is understood texas holdem poker online

  2. 205.251.92.* 于 2007-06-19 05:34:21发表:

    For a start poker game

  3. 76.29.81.* 于 2007-06-19 05:34:18发表:

    Viewed as free online video poker

  4. 0.0.0.* 于 2007-06-19 05:32:35发表:

    Who party poker com

  5. 0.0.0.* 于 2007-06-19 05:26:20发表:

    Come live online poker

  6. 74.116.35.* 于 2007-06-19 05:12:37发表:

    Choose free online poker game

  7. 201.27.116.* 于 2007-06-19 05:11:01发表:

    Viewed as free online video poker

  8. 75.17.198.* 于 2007-06-19 05:09:03发表:

    Find online video poker game

  9. 69.121.40.* 于 2007-06-19 05:06:51发表:

    Viewed as free online video poker

  10. 0.0.0.* 于 2007-06-19 05:01:43发表:

    Begin with texas hold em poker online

  11. 75.35.22.* 于 2007-06-19 04:55:44发表:

    Of the record texas hold em poker

  12. 71.60.104.* 于 2007-06-19 04:54:42发表:

    Breathe free online video poker

  13. 82.79.46.* 于 2007-06-19 04:51:45发表:

    But online video poker game

  14. 200.82.63.* 于 2007-06-19 04:45:42发表:

    Over pacfic poker

  15. 0.0.0.* 于 2007-06-19 04:45:31发表:

    Come free poker tournament

  16. 0.0.0.* 于 2007-06-19 04:44:14发表:

    Where poker table

  17. 201.17.27.* 于 2007-06-19 04:32:13发表:

    This party poker cheat

  18. 66.176.94.* 于 2007-06-19 04:29:24发表:

    Finally pacific poker bonus code

  19. 0.0.0.* 于 2007-06-19 04:20:20发表:

    Does pacific poker bonus code

  20. 65.103.28.* 于 2007-06-19 04:17:01发表:

    Shall video strip poker

  21. 68.188.171.* 于 2007-06-19 04:15:07发表:

    Just free online poker game

  22. 0.0.0.* 于 2007-06-19 01:52:47发表:

    In other words party poker cheat

  23. 67.86.68.* 于 2007-06-19 01:51:10发表:

    After poker table

  24. 0.0.0.* 于 2007-06-19 01:40:39发表:

    Come texas hold em poker

  25. 217.24.246.* 于 2007-06-19 01:27:22发表:

    Today party poker bonus

  26. 208.127.109.* 于 2007-06-19 01:19:35发表:

    As the online video poker game

  27. 216.218.252.* 于 2007-06-19 00:40:23发表:

    pacific poker download http://www.freewebs.com/poker-68/pacific-poker-5.html pacific poker download http://www.freewebs.com/poker-83/video-poker-7.html free video poker game poker video online video poker video poker game http://www.freewebs.com/poker-79/video-poker-5.html free texas holdem poker game http://www.freewebs.com/poker-98/texas-holdem-3.html free texas holdem poker game texas hold em poker free online texas hold em http://www.freewebs.com/poker-86/texas-hold-em-1.html