请大家帮帮忙关于 如何解析class文件 应该不太可能.如果这样可以行的话,必须做java反编译,但即使是反编译出来的代码跟原代码还是有差距的.关注 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我在网上也查过不少资料,首先这样是可行的,不然反骗译软件就不会出来!我在网上也查到了class文件的结构,但是就是查找public函数时出现了困难,希望高手指教,有没有子,贴出来! public函数用反射就可以了,这没什么困难的. 非常容易就使用反射就可以,我记得《java编程思想》里面还有这么一个例子来着 Class c = Class.forName(yourclass);c.getMethods() 我说的不是解析一个类文件,而是解析程序编译后生成的class文件 Class c = Class.forName(yourclass);c.getMethods()this is for the .class files 一个class文件中可以写义很多类的呀,不知道可不可以,楼上的可以举个例子吗,谢谢啦,很急! 我也查过Class类的forName()这个方法了,它的参数是一个类的名字,而不是一个class文件呀 有谁有关于class文件反编译原理的资料呀,能不能给个地址呀!谢谢各位了! 我也查过Class类的forName()这个方法了,它的参数是一个类的名字,而不是一个class文件呀汗~!JDBC中的forName数据库驱动难道载入的是.java文件吗?晕啊你平时写的小程序在cmd下运行是难道用的是.java吗? java A.java???or java A ????? 这个帖子贴出来,首先谢谢大家的关注,虽然最终没有能解决问题!问题我也解决了,所以贴出来跟大家分享:里面用到的不是类的反射,而是根据class文件格式一步步读取出来的,关于class的格式我贴在此处,也是从网上搜的!注意点就是解析时要一个一个来,每一步都得读!定义:u1 1个字节为单位的非负值u2 2个字节为单位的非负值u3 . . . . . . . . (其他以此类推 )Java文件结构用类似struct的描述如下:ClassFile {u4 magic; // 必须为: 0xCAFEBABEu2 minor_version;u2 major_version; //CLASS文件结构主次版本号 JAVA2支持45.0-46.0u2 constant_pool_count; //记录常量信息cp_info constant_pool[constant_pool_count-1]; //计数从1开始u2 access_flags; //class/interface访问权限u2 this_class; //指向constant_poll中的有效索引值u2 super_class; //0或指向constant_poll中的有效索引值,对于interface必须为非0u2 interfaces_count; //superinterfaces的个数u2 interfaces[interfaces_count]; //计数[0,count-1) 对应constant_pool中的一个索引值u2 fields_count; field_info fields[fields_count]; //主要用于记录class及实例中的变量u2 methods_count;method_info methods[methods_count];u2 attributes_count;attribute_info attributes[attributes_count];}cp_info {u1 tag; u1 info[]; } tag 意义如下: CONSTANT_Class 7 CONSTANT_Fieldref 9 CONSTANT_Methodref 10 CONSTANT_InterfaceMethodref 11 CONSTANT_String 8 CONSTANT_Integer 3 CONSTANT_Float 4 CONSTANT_Long 5 CONSTANT_Double 6 CONSTANT_NameAndType 12 CONSTANT_Utf8 1 此时cp_info分别对应结构变化为1. CONSTANT_Class CONSTANT_Class_info { u1 tag; u2 name_index; } 2. CONSTANT_Fieldref CONSTANT_Fieldref_info { u1 tag; u2 class_index; //constant_pool的索引,对应CONSTANT_Class_info u2 name_and_type_index;//constant_pool的索引,对应CONSTANT_NameAndType_info }3. CONSTANT_Methodref CONSTANT_Methodref_info { u1 tag; u2 class_index;u2 name_and_type_index; } 4. CONSTANT_InterfaceMethodref CONSTANT_InterfaceMethodref_info { u1 tag; u2 class_index; u2 name_and_type_index; } 5. CONSTANT_String CONSTANT_String_info { u1 tag; u2 string_index; } 6. CONSTANT_Integer CONSTANT_Integer_info { u1 tag; u4 bytes; } 7. CONSTANT_Float CONSTANT_Float_info { u1 tag; u4 bytes; } 8. CONSTANT_Long CONSTANT_Long_info { u1 tag; u4 high_bytes; u4 low_bytes; } 9. CONSTANT_Double CONSTANT_Double_info { u1 tag; u4 high_bytes; u4 low_bytes } 10.CONSTANT_NameAndType CONSTANT_NameAndType_info { u1 tag; u2 name_index; u2 descriptor_index; } 11.CONSTANT_Utf8 CONSTANT_Utf8_info { u1 tag; u2 length; u1 bytes[length];}access_flags意义如下:ACC_PUBLIC 0x0001 ACC_FINAL 0x0010 ACC_SUPER 0x0020 ACC_INTERFACE 0x0200 ACC_ABSTRACT 0x0400 如果是interface那么必须置ACC_INTERFACE,如果没有置ACC_INTERFACE则定义的是一个类而非接口。如果设置了ACC_INTERFACE,那么ACC_ABSTRACT位也必须被设置,当然也可以设置ACC_PUBLIC。ACC_SUPER用以表明invokespecial语义,Sun公司老的JAVA编译器没有设置ACC_SUPER,并且老的JVM忽略ACC_SUPER位,但新的编译器应该实现invokespecial语义。其他未指明的位保留将来使用,并且编译器应当将其置为0,同时Java虚拟机应当忽略他们。this_class: constant_pool中的索引值,指向的元素的cp_info等价为CONSTANT_Class_info CONSTANT_Class_info {u1 tag; //必须为CONSTANT_Class (7)u2 name_index; //为指向constant_pool中的一个索引值}name_index :指向的元素的cp_info等价为CONSTANT_Utf8_info CONSTANT_Utf8_info {u1 tag; //必须为CONSTANT_Utf8 (1)u2 length; u1 bytes[length]; //Utf8编码的字符串}field_info {u2 access_flags; //访问控制权u2 name_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。u2 descriptor_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。u2 attributes_count;attribute_info attributes[attributes_count]; //attribute_info将在mothods后描述。}field_info中access_flages意义如下:ACC_PUBLIC 0x0001ACC_PRIVATE 0x0002ACC_PROTECTED 0x0004ACC_STATIC 0x0008ACC_FINAL 0x0010ACC_VOLATILE 0x0040ACC_TRANSIENT 0x0080其中很显然不能同时为ACC_FINAL和ACC_VOLATILE ;且前三项是互斥的。interface必须置ACC_PUBLIC, ACC_STATIC,ACC_FINAL位,且不能置其他位。其他未指明的位保留将来使用,并且编译器应当将其置为0,同时Java虚拟机应当忽略他们。methods指明了类中的所有方法。 method_info {u2 access_flags;u2 name_index; //指向constant_pool的入口,对应为CONSTANT_Utf8_info u2 descriptor_index; //指向constant_pool的入口,对应为CONSTANT_Utf8_infou2 attributes_count;attribute_info attributes[attributes_count];//此处只能出现Code、Exceptions、Synthetic、Deprecated四种类型的属性}access_flags访问权描述如下:ACC_PUBLIC 0x0001 ACC_PRIVATE 0x0002 ACC_PROTECTED 0x0004 ACC_STATIC 0x0008 ACC_FINAL 0x0010 ACC_SYNCHRONIZED 0x0020 ACC_NATIVE 0x0100 ACC_ABSTRACT 0x0400 ACC_STRICT 0x0800 attribute_info {u2 attribute_name_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。u4 attribute_length;u1 info[attribute_length];}现在已经预定义的属性有:1. SourceFile : attribute_info被替代为:SourceFile_attribute {u2 attribute_name_index;u4 attribute_length;u2 sourcefile_index; //指向constant_pool中的一个CONSTANT_Utf8_info 结构。}2. ConstantValue : attribute_info被替代为:ConstantValue_attribute {u2 attribute_name_index;u4 attribute_length; //必须为2u2 constantvalue_index;}对于constantvalue_index意义如下:long CONSTANT_Long float CONSTANT_Float double CONSTANT_Double int, short, char, byte, boolean CONSTANT_Integer String CONSTANT_String ConstantValue用于field_info 中,用于描述一个static常量,且此时field_info的access_flags应为ACC_STATIC 3. Code : attribute_info被替代为:Code_attribute {u2 attribute_name_index;u4 attribute_length;u2 max_stack; //执行此函数时可用的栈的最大深度u2 max_locals; //执行此函数可用到的最大本地变量数目,包括参数。// 注意:一个long/double相当于2个变量数目.u4 code_length; //本函数用到的代码长度。u1 code[code_length]; //实现本函数的真正字节码u2 exception_table_length;{ u2 start_pc;u2 end_pc; //捕获违例时执行代码数组中的[start_pc, end_pc)部分u2 handler_pc; //现在还不大明白他是干嘛的!!u2 catch_type; //指向constant_pool的索引,对应CONSTANT_Class_info }exception_table[exception_table_length];u2 attributes_count;attribute_info attributes[attributes_count];}CONSTANT_Class_info {u1 tag; //必须为CONSTANT_Class (7)u2 name_index; //不用我再说了吧?}Code属性用于method_info结构中。4. Exceptions : attribute_info被替代为:Exceptions_attribute {u2 attribute_name_index;u4 attribute_length;u2 number_of_exceptions;u2 exception_index_table[number_of_exceptions];}5. InnerClasses : attribute_info被替代为:InnerClasses_attribute {u2 attribute_name_index;u4 attribute_length;u2 number_of_classes;{ u2 inner_class_info_index; u2 outer_class_info_index; u2 inner_name_index; u2 inner_class_access_flags; } classes[number_of_classes];}6. Synthetic : attribute_info被替代为:Synthetic_attribute {u2 attribute_name_index; //不用废话了吧?u4 attribute_length; //必须为0}Synthetic用在 field_info、 method_info 中,一个没有出现在源程序中的变量必须使用Synthetic标记。7. LineNumberTable : attribute_info被替代为:LineNumberTable_attribute {u2 attribute_name_index;u4 attribute_length;u2 line_number_table_length;{ u2 start_pc; //代码数组中的开始处u2 line_number; //源文件中的行号(对于每一非空行都有这么一项) } line_number_table[line_number_table_length];}LineNumberTable用于Code属性中,通常用于调试。8. LocalVariableTable : attribute_info被替代为:LocalVariableTable_attribute {u2 attribute_name_index;u4 attribute_length;u2 local_variable_table_length;{ u2 start_pc;u2 length; //当解释到代码数组的[start_pc,start_pc+length]//时变量必须被赋值??u2 name_index;u2 descriptor_index;u2 index; //到本地变量数组的一个索引} local_variable_table[local_variable_table_length];} 9. Deprecated : attribute_info被替代为:Deprecated_attribute {u2 attribute_name_index;u4 attribute_length; //必须为0} 求将dom4j的document转换为String的方法 关于ssh的一些想法 谁能给我个禁止IE的前进 后退功能的activeX的控件 哪位大峡有wsad的下载地址提供一下,不甚感激啊. 求教高手: ejb程序部署成功了, 也可以执行,但就是不能向数据库插入数据?----------- 啊啊阿 一个soap示例程序的问题! 大家给推荐一本学习J2EE的好书???? 请问在JTable中的Jcheckbox状态如何改变?谢谢!!! EJB学习经验谈 srtuts中validate check后返回页面的问题,求高手来解答。 获取session错误
我在网上也查到了class文件的结构,但是就是查找public函数时出现了困难,希望高手指教,有没有子,贴出来!
c.getMethods()
c.getMethods()this is for the .class files
楼上的可以举个例子吗,谢谢啦,很急!
汗~!JDBC中的forName数据库驱动难道载入的是.java文件吗?晕啊你平时写的小程序在cmd下运行是难道用的是.java吗? java A.java???or java A ?????
问题我也解决了,所以贴出来跟大家分享:
里面用到的不是类的反射,而是根据class文件格式一步步读取出来的,关于class的格式我贴在此处,也是从网上搜的!注意点就是解析时要一个一个来,每一步都得读!
定义:u1 1个字节为单位的非负值
u2 2个字节为单位的非负值
u3 . . . . . . . . (其他以此类推 )Java文件结构用类似struct的描述如下:ClassFile {
u4 magic; // 必须为: 0xCAFEBABE
u2 minor_version;
u2 major_version; //CLASS文件结构主次版本号 JAVA2支持45.0-46.0
u2 constant_pool_count; //记录常量信息
cp_info constant_pool[constant_pool_count-1]; //计数从1开始
u2 access_flags; //class/interface访问权限
u2 this_class; //指向constant_poll中的有效索引值
u2 super_class; //0或指向constant_poll中的有效索引值,对于interface必须为非0
u2 interfaces_count; //superinterfaces的个数
u2 interfaces[interfaces_count]; //计数[0,count-1) 对应constant_pool中的一个索引值
u2 fields_count;
field_info fields[fields_count]; //主要用于记录class及实例中的变量
u2 methods_count;
method_info methods[methods_count];
u2 attributes_count;
attribute_info attributes[attributes_count];
}
cp_info {
u1 tag;
u1 info[];
}
tag 意义如下:
CONSTANT_Class 7
CONSTANT_Fieldref 9
CONSTANT_Methodref 10
CONSTANT_InterfaceMethodref 11
CONSTANT_String 8
CONSTANT_Integer 3
CONSTANT_Float 4
CONSTANT_Long 5
CONSTANT_Double 6
CONSTANT_NameAndType 12
CONSTANT_Utf8 1
此时cp_info分别对应结构变化为1. CONSTANT_Class
CONSTANT_Class_info {
u1 tag;
u2 name_index;
}
2. CONSTANT_Fieldref
CONSTANT_Fieldref_info {
u1 tag;
u2 class_index; //constant_pool的索引,对应CONSTANT_Class_info
u2 name_and_type_index;//constant_pool的索引,对应CONSTANT_NameAndType_info
}
3. CONSTANT_Methodref
CONSTANT_Methodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
} 4. CONSTANT_InterfaceMethodref
CONSTANT_InterfaceMethodref_info {
u1 tag;
u2 class_index;
u2 name_and_type_index;
} 5. CONSTANT_String
CONSTANT_String_info {
u1 tag;
u2 string_index;
} 6. CONSTANT_Integer
CONSTANT_Integer_info {
u1 tag;
u4 bytes;
}
7. CONSTANT_Float
CONSTANT_Float_info {
u1 tag;
u4 bytes;
} 8. CONSTANT_Long
CONSTANT_Long_info {
u1 tag;
u4 high_bytes;
u4 low_bytes;
}
9. CONSTANT_Double
CONSTANT_Double_info {
u1 tag;
u4 high_bytes;
u4 low_bytes
} 10.CONSTANT_NameAndType
CONSTANT_NameAndType_info {
u1 tag;
u2 name_index;
u2 descriptor_index;
} 11.CONSTANT_Utf8
CONSTANT_Utf8_info {
u1 tag;
u2 length;
u1 bytes[length];
}access_flags意义如下:
ACC_PUBLIC 0x0001
ACC_FINAL 0x0010
ACC_SUPER 0x0020
ACC_INTERFACE 0x0200
ACC_ABSTRACT 0x0400 如果是interface那么必须置ACC_INTERFACE,如果没有置ACC_INTERFACE则定义的是一个类而非接口。
如果设置了ACC_INTERFACE,那么ACC_ABSTRACT位也必须被设置,当然也可以设置ACC_PUBLIC。
ACC_SUPER用以表明invokespecial语义,Sun公司老的JAVA编译器没有设置ACC_SUPER,并且老的JVM
忽略ACC_SUPER位,但新的编译器应该实现invokespecial语义。
其他未指明的位保留将来使用,并且编译器应当将其置为0,同时Java虚拟机应当忽略他们。this_class: constant_pool中的索引值,指向的元素的cp_info等价为CONSTANT_Class_info CONSTANT_Class_info {
u1 tag; //必须为CONSTANT_Class (7)
u2 name_index; //为指向constant_pool中的一个索引值
}name_index :指向的元素的cp_info等价为CONSTANT_Utf8_info CONSTANT_Utf8_info {
u1 tag; //必须为CONSTANT_Utf8 (1)
u2 length;
u1 bytes[length]; //Utf8编码的字符串
}
field_info {
u2 access_flags; //访问控制权
u2 name_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。
u2 descriptor_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。
u2 attributes_count;
attribute_info attributes[attributes_count]; //attribute_info将在mothods后描述。
}
field_info中access_flages意义如下:ACC_PUBLIC 0x0001
ACC_PRIVATE 0x0002
ACC_PROTECTED 0x0004
ACC_STATIC 0x0008
ACC_FINAL 0x0010
ACC_VOLATILE 0x0040
ACC_TRANSIENT 0x0080其中很显然不能同时为ACC_FINAL和ACC_VOLATILE ;且前三项是互斥的。
interface必须置ACC_PUBLIC, ACC_STATIC,ACC_FINAL位,且不能置其他位。
其他未指明的位保留将来使用,并且编译器应当将其置为0,同时Java虚拟机应当忽略他们。
methods指明了类中的所有方法。
method_info {
u2 access_flags;
u2 name_index; //指向constant_pool的入口,对应为CONSTANT_Utf8_info
u2 descriptor_index; //指向constant_pool的入口,对应为CONSTANT_Utf8_info
u2 attributes_count;
attribute_info attributes[attributes_count];
//此处只能出现Code、Exceptions、Synthetic、Deprecated四种类型的属性
}
access_flags访问权描述如下:ACC_PUBLIC 0x0001
ACC_PRIVATE 0x0002
ACC_PROTECTED 0x0004
ACC_STATIC 0x0008
ACC_FINAL 0x0010
ACC_SYNCHRONIZED 0x0020
ACC_NATIVE 0x0100
ACC_ABSTRACT 0x0400
ACC_STRICT 0x0800
attribute_info {
u2 attribute_name_index; //constant_pool中的索引,对应于CONSTANT_Utf8_info描述。
u4 attribute_length;
u1 info[attribute_length];
}现在已经预定义的属性有:
1. SourceFile : attribute_info被替代为:SourceFile_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 sourcefile_index; //指向constant_pool中的一个CONSTANT_Utf8_info 结构。
}2. ConstantValue : attribute_info被替代为:ConstantValue_attribute {
u2 attribute_name_index;
u4 attribute_length; //必须为2
u2 constantvalue_index;
}对于constantvalue_index意义如下:
long CONSTANT_Long
float CONSTANT_Float
double CONSTANT_Double
int, short, char, byte, boolean CONSTANT_Integer
String CONSTANT_String ConstantValue用于field_info 中,用于描述一个static常量,
且此时field_info的access_flags应为ACC_STATIC
3. Code : attribute_info被替代为:Code_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 max_stack; //执行此函数时可用的栈的最大深度
u2 max_locals; //执行此函数可用到的最大本地变量数目,包括参数。
// 注意:一个long/double相当于2个变量数目.
u4 code_length; //本函数用到的代码长度。
u1 code[code_length]; //实现本函数的真正字节码
u2 exception_table_length;
{ u2 start_pc;
u2 end_pc; //捕获违例时执行代码数组中的[start_pc, end_pc)部分
u2 handler_pc; //现在还不大明白他是干嘛的!!
u2 catch_type; //指向constant_pool的索引,对应CONSTANT_Class_info
}exception_table[exception_table_length];
u2 attributes_count;
attribute_info attributes[attributes_count];
}CONSTANT_Class_info {
u1 tag; //必须为CONSTANT_Class (7)
u2 name_index; //不用我再说了吧?
}Code属性用于method_info结构中。4. Exceptions : attribute_info被替代为:Exceptions_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_exceptions;
u2 exception_index_table[number_of_exceptions];
}5. InnerClasses : attribute_info被替代为:InnerClasses_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 number_of_classes;
{ u2 inner_class_info_index;
u2 outer_class_info_index;
u2 inner_name_index;
u2 inner_class_access_flags;
} classes[number_of_classes];
}6. Synthetic : attribute_info被替代为:Synthetic_attribute {
u2 attribute_name_index; //不用废话了吧?
u4 attribute_length; //必须为0
}
Synthetic用在 field_info、 method_info 中,
一个没有出现在源程序中的变量必须使用Synthetic标记。7. LineNumberTable : attribute_info被替代为:LineNumberTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 line_number_table_length;
{ u2 start_pc; //代码数组中的开始处
u2 line_number; //源文件中的行号(对于每一非空行都有这么一项)
} line_number_table[line_number_table_length];
}
LineNumberTable用于Code属性中,通常用于调试。
8. LocalVariableTable : attribute_info被替代为:LocalVariableTable_attribute {
u2 attribute_name_index;
u4 attribute_length;
u2 local_variable_table_length;
{ u2 start_pc;
u2 length; //当解释到代码数组的[start_pc,start_pc+length]
//时变量必须被赋值??
u2 name_index;
u2 descriptor_index;
u2 index; //到本地变量数组的一个索引
} local_variable_table[local_variable_table_length];
} 9. Deprecated : attribute_info被替代为:Deprecated_attribute {
u2 attribute_name_index;
u4 attribute_length; //必须为0
}