📄 manual.html
字号:
<a href="#Figure 3">Figure 3</a> shows an UML diagram of the hierarchy of classes of the <font face="helvetica,arial">BCEL </font>API. <a href="#Figure 8">Figure 8</a> in the appendix also shows a detailed diagram of the <tt>ConstantPool</tt> components. </p> <p align="center"> <a name="Figure 3"> <img src="images/javaclass.gif" /> <br /> Figure 3: UML diagram for the JavaClass API</a> </p> <p> The top-level data structure is <tt>JavaClass</tt>, which in most cases is created by a <tt>ClassParser</tt> object that is capable of parsing binary class files. A <tt>JavaClass</tt> object basically consists of fields, methods, symbolic references to the super class and to the implemented interfaces. </p> <p> The constant pool serves as some kind of central repository and is thus of outstanding importance for all components. <tt>ConstantPool</tt> objects contain an array of fixed size of <tt>Constant</tt> entries, which may be retrieved via the <tt>getConstant()</tt> method taking an integer index as argument. Indexes to the constant pool may be contained in instructions as well as in other components of a class file and in constant pool entries themselves. </p> <p> Methods and fields contain a signature, symbolically defining their types. Access flags like <tt>public static final</tt> occur in several places and are encoded by an integer bit mask, e.g., <tt>public static final</tt> matches to the Java expression </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>int access_flags = ACC_PUBLIC | ACC_STATIC | ACC_FINAL;</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <p> As mentioned in <a href="#2.1 Java class file format">section 2.1</a> already, several components may contain <em>attribute</em> objects: classes, fields, methods, and <tt>Code</tt> objects (introduced in <a href="#2.3 Method code">section 2.3</a>). The latter is an attribute itself that contains the actual byte code array, the maximum stack size, the number of local variables, a table of handled exceptions, and some optional debugging information coded as <tt>LineNumberTable</tt> and <tt>LocalVariableTable</tt> attributes. Attributes are in general specific to some data structure, i.e., no two components share the same kind of attribute, though this is not explicitly forbidden. In the figure the <tt>Attribute</tt> classes are stereotyped with the component they belong to. </p> </blockquote> </p> </td></tr> <tr><td><br/></td></tr> </table> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr><td bgcolor="#525D76"> <font color="#ffffff" face="arial,helvetica,sanserif"> <a name="3.2 Class repository"><strong>3.2 Class repository</strong></a> </font> </td></tr> <tr><td> <blockquote> <p> Using the provided <tt>Repository</tt> class, reading class files into a <tt>JavaClass</tt> object is quite simple: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre>JavaClass clazz = Repository.lookupClass("java.lang.String");</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> <p> The repository also contains methods providing the dynamic equivalent of the <tt>instanceof</tt> operator, and other useful routines: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre> if(Repository.instanceOf(clazz, super_class) { ... }</pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> </blockquote> </p> </td></tr> <tr><td><br/></td></tr> </table> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr><td bgcolor="#525D76"> <font color="#ffffff" face="arial,helvetica,sanserif"> <a name="3.2.1 Accessing class file data"><strong>3.2.1 Accessing class file data</strong></a> </font> </td></tr> <tr><td> <blockquote> <p> Information within the class file components may be accessed like Java Beans via intuitive set/get methods. All of them also define a <tt>toString()</tt> method so that implementing a simple class viewer is very easy. In fact all of the examples used here have been produced this way: </p> <div align="left"> <table cellspacing="4" cellpadding="0" border="0"> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#ffffff"><pre> System.out.println(clazz); printCode(clazz.getMethods()); ... public static void printCode(Method[] methods) { for(int i=0; i < methods.length; i++) { System.out.println(methods[i]); Code code = methods[i].getCode(); if(code != null) // Non-abstract method System.out.println(code); } } </pre></td> <td bgcolor="#023264" width="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> <tr> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> <td bgcolor="#023264" width="1" height="1"><img src="/images/void.gif" width="1" height="1" vspace="0" hspace="0" border="0"/></td> </tr> </table> </div> </blockquote> </p> </td></tr> <tr><td><br/></td></tr> </table> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr><td bgcolor="#525D76"> <font color="#ffffff" face="arial,helvetica,sanserif"> <a name="3.2.2 Analyzing class data"><strong>3.2.2 Analyzing class data</strong></a> </font> </td></tr> <tr><td> <blockquote> <p> Last but not least, <font face="helvetica,arial">BCEL</font> supports the <em>Visitor</em> design pattern, so one can write visitor objects to traverse and analyze the contents of a class file. Included in the distribution is a class <tt>JasminVisitor</tt> that converts class files into the <a href="http://mrl.nyu.edu/~meyer/jasmin/">Jasmin</a> assembler language. </p> </blockquote> </p> </td></tr> <tr><td><br/></td></tr> </table> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr><td bgcolor="#525D76"> <font color="#ffffff" face="arial,helvetica,sanserif"> <a name="3.3 ClassGen"><strong>3.3 ClassGen</strong></a> </font> </td></tr> <tr><td> <blockquote> <p> This part of the API (package <tt>org.apache.bcel.generic</tt>) supplies an abstraction level for creating or transforming class files dynamically. It makes the static constraints of Java class files like the hard-coded byte code addresses "generic". The generic constant pool, for example, is implemented by the class <tt>ConstantPoolGen</tt> which offers methods for adding different types of constants. Accordingly, <tt>ClassGen</tt> offers an interface to add methods, fields, and attributes. <a href="#Figure 4">Figure 4</a> gives an overview of this part of the API. </p> <p align="center"> <a name="Figure 4"> <img src="images/classgen.gif" /> <br /> Figure 4: UML diagram of the ClassGen API</a> </p> </blockquote> </p> </td></tr> <tr><td><br/></td></tr> </table> <table border="0" cellspacing="0" cellpadding="2" width="100%"> <tr><td bgcolor="#525D76"> <font color="#ffffff" face="arial,helvetica,sanserif"> <a name="3.3.1 Types"><strong>3.3.1 Types</strong></a> </font> </td></tr> <tr><td> <blockquote> <p> We abstract from the concrete details of the type signature syntax (see <a href="#2.5 Type information">2.5</a>) by introducing the <tt>Type</tt> class, which is used, for example, by methods to define their return and argument types. Concrete sub-classes are <tt>BasicType</tt>, <tt>ObjectType</tt>, and <tt>ArrayType</tt> which consists of the element type and the number of dimensions. For commonly used types the class offers some predefined constants. For example, the method signature of the <tt>main</tt> method as shown in <a href="#2.5 Type information">section 2.5</a> is represented by: </p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -