銆奐ava瀛楄妭鐮佹牸寮忚瑙?銆?/a>銆?a href="http://www.tkk7.com/DLevin/archive/2011/09/05/358034.html">銆奐ava瀛楄妭鐮佹牸寮忚瑙?銆?/a>銆?a href="http://www.tkk7.com/DLevin/archive/2011/09/05/358035.html">銆奐ava瀛楄妭鐮佹牸寮忚瑙?銆?/a>
鍦ㄦ瀯寤篊lassReader瀹炰緥鏃訛紝瀹冮鍏堜繚瀛樺瓧鑺傜爜浜岃繘鍒舵暟緇刡錛岀劧鍚庡垱寤篿tems鏁扮粍錛屾暟緇勭殑闀垮害鍦ㄥ瓧鑺傜爜鏁扮粍鐨勭8銆?涓瓧鑺傛寚瀹氾紙鏈鍓嶉潰4涓瓧鑺傛槸欖旀暟CAFEBABE錛屼箣鍚?涓瓧鑺傛槸嬈$増鏈彿錛屽啀鍚?涓瓧鑺傛槸涓葷増鏈彿錛夛紝姣忎釜item琛ㄧず甯擱噺姹犻」鍦ㄥ瓧鑺傜爜鏁扮粍鐨勫亸縐婚噺鍔?錛堝父閲忔睜涓瘡涓」鐢?涓瓧鑺傜殑type鍜岀揣璺熺殑瀛楄妭鏁扮粍琛ㄧず錛屽父閲忔睜欏規湁12縐嶇被鍨嬶紝鍏朵腑CONSTANT_FieldRef_Info銆丆ONSTANT_MethodRef_Info銆丆ONSTANT_InterfaceMethodRef_Info銆丆ONSTANT_NameAndType_Info鍖呮嫭鍏剁被鍨嬪瓧鑺傚崰鐢?涓瓧鑺傦紝鍙﹀4涓瓧鑺傛瘡2涓瓧鑺備負瀛楁銆佹柟娉曠瓑鎵鍦ㄧ殑綾匯佸叾鍚嶇О銆佹弿榪扮鍦ㄥ綋鍓嶅父閲忔睜涓瑿ONSTANT_Utf8_Info綾誨瀷鐨勫紩鐢紱CONSTANT_Integer_Info銆丆ONSTANT_Float_Info鍖呮嫭鍏剁被鍨嬪瓧鑺傚崰鐢?涓瓧鑺傦紝鍙﹀鍥涗釜瀛楄妭涓哄叾瀵瑰簲鐨勫鹼紱CONSTANT_Class_Info銆丆ONSTANT_String_Info鍖呮嫭鍏剁被鍨嬪瓧鑺傚崰鐢?涓瓧鑺傦紝鍙﹀涓や釜瀛楄妭涓哄湪褰撳墠甯擱噺姹燙ONSTANT_Utf8_Info欏圭殑绱㈠紩錛汣ONSTANT_Utf8_Info綾誨瀷絎?涓瓧鑺傝〃紺虹被鍨嬶紝絎?銆?涓瓧鑺備負璇ラ」鎵琛ㄧず鐨勫瓧絎︿覆鐨勯暱搴︼級錛汣ONSTANT_Double_Info銆丆ONSTANT_Long_Info鍔犵被鍨嬪瓧鑺備負9涓瓧錛沵axStringLength琛ㄧず鏈闀跨殑UTF8綾誨瀷鐨勫父閲忔睜欏圭殑鍊鹼紝鐢ㄤ簬鍐沖畾鍦ㄨВ鏋怌ONSTANT_Utf8_Info綾誨瀷欏規椂鏈澶ч渶瑕佺殑瀛楃鏁扮粍錛沨eader琛ㄧず甯擱噺姹犱箣鍚庣殑瀛楄妭鐮佺殑絎竴涓瓧鑺傘?br />
鍦ㄨ皟鐢–lassReader鐨刟ccept鏂規硶鏃訛紝瀹冭В鏋愬瓧鑺傜爜涓父閲忔睜涔嬪悗鐨勬墍鏈夊厓绱犮傜揣鎺ョ潃甯擱噺姹犵殑2涓瓧鑺傛槸璇ョ被鐨刟ccess鏍囩錛欰CC_PUBLIC銆丄CC_FINAL絳夛紱涔嬪悗2涓瓧鑺備負褰撳墠綾誨悕鍦ㄥ父閲忔睜CONSTANT_Utf8_Info綾誨瀷鐨勭儲寮曪紱涔嬪悗2涓瓧鑺備負鍏剁埗綾誨悕鍦ㄥ父閲忔睜CONSTANT_Utf8_Info綾誨瀷鐨勭儲寮曪紙绱㈠紩鍊?琛ㄧず鐖剁被涓簄ull錛屽嵆鐩存帴緇ф壙鑷狾bject綾伙級錛涘啀涔嬪悗涓哄叾瀹炵幇鐨勬帴鍙f暟闀垮害鍜屽搴斿悇涓帴鍙e悕鍦ㄥ父閲忔睜涓瑿ONSTANT_Utf8_Info綾誨瀷鐨勭儲寮曞鹼紱鏆傛椂鍏堣煩榪嘑ield鍜孧ethod瀹氫箟淇℃伅錛岃В鏋愮被鐨刟ttribute琛紝瀹冪敤涓や釜瀛楄妭琛ㄨ揪attribute鏁扮粍鐨勯暱搴︼紝姣忎釜attribute欏逛腑鏈鍓嶉潰2涓瓧鑺傛槸attribute鍚嶇О錛歋ourceFile錛堣鍙杝ourceFile鍊鹼級銆両nnerClasses錛堟殏鏃剁邯褰曡搗濮嬬儲寮曪級銆丒nclosingMethod錛堢邯褰曞綋鍓嶅尶鍚嶇被銆佹湰鍦扮被鍖呭惈鑰呯被鍚嶄互鍙婂寘鍚呯殑鏂規硶鍚嶅拰鎻忚堪絎︼級銆丼ignature錛堢被鐨勭鍚嶄俊鎭紝鐢ㄤ簬鑼冨瀷錛夈丷untimeVisibleAnnotations錛堟殏鏃剁邯褰曡搗濮嬬儲寮曪級銆丏eprecated錛堣〃璇嗗睘鎬э級銆丼ynthetic錛堟爣璇嗗睘鎬э級銆丼ourceDebugExtension錛堜負璋冭瘯鍣ㄦ彁渚涚殑鑷畾涔夋墿灞曚俊鎭紝璇誨彇鎴愪竴涓瓧絎︿覆錛夈丷untimeInvisibleAnnotations錛堟殏鏃剁邯褰曡搗濮嬬儲寮曪級錛屽鍏朵粬涓嶈瘑鍒殑灞炴э紝綰綍鎴怉ttribute閾撅紝濡傛灉attribute鍚嶇О絎﹀悎鍦╝ccept涓璦ttribute鏁扮粍涓寚瀹氱殑attribute鍚嶏紝鍒欐浛鎹紶鍏ョ殑attribute鏁扮粍瀵瑰簲鐨勯」錛涙牴鎹В鏋愬嚭鏉ョ殑淇℃伅璋冪敤浠ヤ笅visit鏂規硶錛?br />void visit(int version, int access, String name, String signature, String superName, String[] interfaces);
// sourceFile, sourceDebug
void visitSource(String source, String debug);
// EnclosingMethod attribute: enclosingOwner, enclosingName, enclosingDesc.
// Note: only when the class has EnclosingMethod attribute, meaning the class is a local class or an anonymous class
void visitOuterClass(String owner, String name, String desc);
渚濇瑙f瀽RuntimeVisibleAnnotations鍜孯untimeInvisibleAnnotations灞炴э紝棣栧厛瑙f瀽瀹氫箟鐨凙nnotation鐨勬弿榪扮浠ュ強榪愯鏃跺彲瑙乫lag錛岃繑鍥炵敤鎴瘋嚜瀹氫箟鐨凙nnotationVisitor錛?br />AnnotationVisitor visitAnnotation(String desc, boolean visible);
瀵規瘡涓畾涔夌殑Annotation錛岃В鏋愬叾閿煎錛屽茍鏍規嵁涓嶅悓鐨凙nnotation瀛楁鍊艱皟鐢ˋnnotationVisitor涓殑鏂規硶錛屽湪鎵鏈夎В鏋愮粨鏉熷悗錛岃皟鐢ˋnnotationVisitor.visitEnd鏂規硶錛?br />public interface AnnotationVisitor {
// 瀵瑰熀鏈被鍨嬬殑鏁扮粍錛屼緷鐒墮噰鐢ㄨ鏂規硶錛寁isitArray鍙槸鍦ㄩ潪鍩烘湰綾誨瀷鏃惰皟鐢ㄣ?/span>
void visit(String name, Object value);
void visitEnum(String name, String desc, String value);
AnnotationVisitor visitAnnotation(String name, String desc);
AnnotationVisitor visitArray(String name);
void visitEnd();
}
涔嬪墠瑙f瀽鍑虹殑attribute閾捐〃錛堥潪鏍囧噯鐨凙ttribute瀹氫箟錛夛紝瀵規瘡涓狝ttribute瀹炰緥錛岃皟鐢–lassVisitor涓殑visitAttribute鏂規硶錛?br />void visitAttribute(Attribute attr);
Attribute綾誨寘鍚玹ype瀛楁鍜屼竴涓瓧鑺傛暟緇勶細
public class Attribute {
public final String type;
byte[] value;
Attribute next;
}
瀵規瘡涓狪nnerClasses灞炴э紝瑙f瀽騫惰皟鐢–lassVisitor鐨剉isitInnerClass鏂規硶錛堣灞炴т簨瀹炰笂淇濆瓨浜嗘墍鏈夊叾鐩存帴鍐呴儴綾諱互鍙婂畠鏈韓鍒版渶欏跺眰綾葷殑璺緞錛夛細
void visitInnerClass(String name, String outerName, String innerName, int access);
瑙f瀽瀛楁錛屽畠绱ц窡鎺ュ彛鏁扮粍瀹氫箟涔嬪悗錛屾渶鍓嶉潰鐨?涓瓧鑺備負瀛楁鏁扮粍鐨勯暱搴︼紝瀵規瘡涓瓧孌碉紝鍓嶉潰2涓瓧鑺備負璁塊棶flag瀹氫箟錛屽啀鍚?涓瓧鑺備負Name绱㈠紩錛屼互鍙?涓瓧鑺傜殑鎻忚堪絎︾儲寮曪紝鐒跺悗瑙f瀽鍏禔ttribute淇℃伅錛欳onstantValue銆丼ignature銆丏eprecated銆丼ynthetic銆丷untimeVisibleAnnotations銆丷untimeInvisibleAnnotations浠ュ強闈炴爣鍑嗗畾涔夌殑Attribute閾撅紝鑰屽悗璋冪敤ClassVisitor鐨剉isitField鏂規硶錛岃繑鍥濬ieldVisitor瀹炰緥錛?br />// 鍏朵腑value涓洪潤鎬佸瓧孌電殑鍒濆鍖栧鹼紙瀵歸潪闈欐佸瓧孌碉紝瀹冪殑鍒濆鍖栧繀欏葷敱鏋勯犲嚱鏁板疄鐜幫級錛屽鏋滄病鏈夊垵濮嬪寲鍊鹼紝璇ュ間負null銆?/span>
FieldVisitor visitField(int access, String name, String desc, String signature, Object value);
瀵硅繑鍥炵殑FieldVisitor渚濇瀵瑰叾Annotation浠ュ強闈炴爣鍑咥ttribute瑙f瀽錛岃皟鐢ㄥ叾visit鏂規硶錛屽茍鍦ㄥ畬鎴愬悗璋冪敤瀹冪殑visitEnd鏂規硶錛?br />public interface FieldVisitor {
AnnotationVisitor visitAnnotation(String desc, boolean visible);
void visitAttribute(Attribute attr);
void visitEnd();
}
瑙f瀽鏂規硶瀹氫箟錛屽畠绱ц窡瀛楁瀹氫箟涔嬪悗錛屾渶鍓嶉潰鐨?涓瓧鑺備負鏂規硶鏁扮粍闀垮害錛屽姣忎釜鏂規硶錛屽墠闈?涓瓧鑺備負璁塊棶flag瀹氫箟錛屽啀鍚?涓瓧鑺備負Name绱㈠紩錛屼互鍙?涓瓧鑺傜殑鏂規硶鎻忚堪絎︾儲寮曪紝鐒跺悗瑙f瀽鍏禔ttribute淇℃伅錛欳ode銆丒xceptions銆丼ignature銆丏eprecated銆丷untimeVisibleAnnotations銆丄nnotationDefault銆丼ynthetic銆丷untimeInvisibleAnnotations銆丷untimeVisibleParameterAnnotations銆丷untimeInvisibleParameterAnnotations浠ュ強闈炴爣鍑嗗畾涔夌殑Attribute閾撅紝濡傛灉瀛樺湪Exceptions灞炴э紝瑙f瀽鍏跺紓甯哥被鏁扮粍錛屼箣鍚庤皟鐢–lassVisitor鐨剉isitMethod鏂規硶錛岃繑鍥濵ethodVisitor瀹炰緥錛?br />MethodVisitor visitMethod(int access, String name, String desc, String signature, String[] exceptions);
AnnotationDefault涓哄Annotation瀹氫箟鏃舵寚瀹氶粯璁ゅ肩殑瑙f瀽錛涚劧鍚庝緷嬈¤В鏋怰untimeVisibleAnnotations銆丷untimeInvisibleAnnotations銆丷untimeVisibleParameterAnnotations銆丷untimeInvisibleParameterAnnotations絳夊睘鎬э紝璋冪敤鐩稿叧AnnotationVisitor鐨剉isit鏂規硶錛涘闈炴爣鍑嗗畾涔夌殑Attribute閾撅紝渚濇璋冪敤MethodVisitor鐨剉isitAttribute鏂規硶錛?br />public interface MethodVisitor {
AnnotationVisitor visitAnnotationDefault();
AnnotationVisitor visitAnnotation(String desc, boolean visible);
AnnotationVisitor visitParameterAnnotation(int parameter, String desc, boolean visible);
void visitAttribute(Attribute attr);
}
瀵笴ode灞炴цВ鏋愶紝璇誨彇2涓瓧鑺傜殑鏈娣辨爤澶у皬銆佹渶澶ocal鍙橀噺鏁般乧ode鍗犵敤瀛楄妭鏁幫紝璋冪敤MethodVisitor鐨剉isitCode()鏂規硶琛ㄧず寮濮嬭В鏋怌ode灞炴э紝瀵規瘡鏉℃寚浠わ紝鍒涘緩涓涓狶abel瀹炰緥騫舵瀯鎴怢abel鏁扮粍錛岃В鏋怌ode灞炴т腑鐨勫紓甯歌〃錛屽姣忎釜寮傚父欏癸紝璋冪敤visitTryCatchBlock鏂規硶錛?br />void visitTryCatchBlock(Label start, Label end, Label handler, String type);
Label鍖呭惈浠ヤ笅淇℃伅錛?br />/**
* A label represents a position in the bytecode of a method. Labels are used
* for jump, goto, and switch instructions, and for try catch blocks.
*
* @author Eric Bruneton
*/
public class Label {
public Object info;
int status;
int line;
int position;
private int referenceCount;
private int[] srcAndRefPositions;
int inputStackTop;
int outputStackMax;
Frame frame;
Label successor;
Edge successors;
Label next;
}
瑙f瀽Code灞炴т腑鐨勫唴閮ㄥ睘鎬т俊鎭細LocalVariableTable銆丩ocalVariableTypeTable銆丩ineNumberTable銆丼tackMapTable銆丼tackMap浠ュ強闈炴爣鍑嗗畾涔夌殑Attribute閾撅紝瀵規瘡涓狶abel璋冪敤鍏秜isitLineNumber鏂規硶浠ュ強瀵規瘡涓狥rame璋冪敤visitFrame鏂規硶錛屽茍涓斿鐩稿簲鐨勬寚浠よ皟鐢ㄧ浉搴旂殑鏂規硶錛?br />void visitFrame(int type, int nLocal, Object[] local, int nStack, Object[] stack);
// Visits a zero operand instruction.
void visitInsn(int opcode);
// Visits an instruction with a single int operand.
void visitIntInsn(int opcode, int operand);
// Visits a local variable instruction. A local variable instruction is an instruction that loads or stores the value of a local variable.
void visitVarInsn(int opcode, int var);
// Visits a type instruction. A type instruction is an instruction that takes the internal name of a class as parameter.
void visitTypeInsn(int opcode, String type);
// Visits a field instruction. A field instruction is an instruction that loads or stores the value of a field of an object.
void visitFieldInsn(int opcode, String owner, String name, String desc);
// Visits a method instruction. A method instruction is an instruction that invokes a method.
void visitMethodInsn(int opcode, String owner, String name, String desc);
// Visits a jump instruction. A jump instruction is an instruction that may jump to another instruction.
void visitJumpInsn(int opcode, Label label);
// Visits a label. A label designates the instruction that will be visited just after it.
void visitLabel(Label label);
// Visits a LDC instruction.
void visitLdcInsn(Object cst);
// Visits an IINC instruction.
void visitIincInsn(int var, int increment);
// Visits a TABLESWITCH instruction.
void visitTableSwitchInsn(int min, int max, Label dflt, Label[] labels);
// Visits a LOOKUPSWITCH instruction.
void visitLookupSwitchInsn(Label dflt, int[] keys, Label[] labels);
// Visits a MULTIANEWARRAY instruction.
void visitMultiANewArrayInsn(String desc, int dims);
// Visits a try catch block.
void visitTryCatchBlock(Label start, Label end, Label handler, String type);
void visitLocalVariable(String name, String desc, String signature, Label start, Label end, int index);
// Visits a line number declaration.
void visitLineNumber(int line, Label start);
// Visits the maximum stack size and the maximum number of local variables of the method.
void visitMaxs(int maxStack, int maxLocals);
鏈鍚庤皟鐢–lassVisitor鐨剉isitEnd鏂規硶錛?br />void visitEnd();
ClassWriter瀹炵幇
ClassWriter緇ф壙鑷狢lassVisitor鎺ュ彛錛屽彲浠ヤ嬌鐢ㄥ畠璋冪敤鍏剁浉搴旂殑visit鏂規硶鍔ㄦ佺殑鏋勯犱竴涓瓧鑺傜爜綾匯傚畠鍖呭惈浠ヤ笅瀛楁淇℃伅錛?br />public class ClassWriter implements ClassVisitor {
//The class reader from which this class writer was constructed, if any.
ClassReader cr;
//Minor and major version numbers of the class to be generated.
int version;
//Index of the next item to be added in the constant pool.
int index;
//The constant pool of this class.
final ByteVector pool;
//The constant pool's hash table data.
Item[] items;
//The threshold of the constant pool's hash table.
int threshold;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key2;
//A reusable key used to look for items in the {@link #items} hash table.
final Item key3;
//A type table used to temporarily store internal names that will not necessarily be stored in the constant pool.
Item[] typeTable;
//Number of elements in the {@link #typeTable} array.
private short typeCount;
//The access flags of this class.
private int access;
//The constant pool item that contains the internal name of this class.
private int name;
//The internal name of this class.
String thisName;
//The constant pool item that contains the signature of this class.
private int signature;
//The constant pool item that contains the internal name of the super class of this class.
private int superName;
// Number of interfaces implemented or extended by this class or interface.
private int interfaceCount;
//The interfaces implemented or extended by this class or interface.
private int[] interfaces;
//The index of the constant pool item that contains the name of the source file from which this class was compiled.
private int sourceFile;
//The SourceDebug attribute of this class.
private ByteVector sourceDebug;
//The constant pool item that contains the name of the enclosing class of this class.
private int enclosingMethodOwner;
//The constant pool item that contains the name and descriptor of the enclosing method of this class.
private int enclosingMethod;
//The runtime visible annotations of this class.
private AnnotationWriter anns;
//The runtime invisible annotations of this class.
private AnnotationWriter ianns;
//The non standard attributes of this class.
private Attribute attrs;
//The number of entries in the InnerClasses attribute.
private int innerClassesCount;
//The InnerClasses attribute.
private ByteVector innerClasses;
//The fields of this class. These fields are stored in a linked list of {@link FieldWriter} objects, linked to each other by their {@link FieldWriter#next} field. This field stores the first element of this list.
FieldWriter firstField;
//This field stores the last element of this list.
FieldWriter lastField;
//The methods of this class. These methods are stored in a linked list of {@link MethodWriter} objects, linked to each other by their {@link MethodWriter#next} field. This field stores the first element of this list.
MethodWriter firstMethod;
//This field stores the last element of this list.
MethodWriter lastMethod;
//true if the maximum stack size and number of local variables must be automatically computed.
private final boolean computeMaxs;
//true if the stack map frames must be recomputed from scratch.
private final boolean computeFrames;
//true if the stack map tables of this class are invalid.
boolean invalidFrames;
}

]]>