CS520
Fall 2018
Laboratory 6
Friday October 5
The lab must be submitted prior to 12noon on Sunday October 7.


The goal for this lab is to continue the program you began in Lab 5. If your Lab 5 is incomplete or has bugs, you should first complete it. As well as determining how many constant pool entries, interfaces and fields the class file contains, in Lab 6 you will determine how many methods are in the class file, as well as the amount of bytecode for each method.

You should continue to consult Chapter 4 of the Java Virtual Machine specification.

Lab 5 terminated after locating and printing the number of fields. The first step of Lab 6 is to skip over the descriptors for the fields. As with constant pool entries, the field descriptors are variable length. Each field descriptor can contain a different number of attributes, and the attribute descriptors are of various lengths. However, each attribute descriptor has a common structure, with a length in the second field of the descriptor. Each descriptor begins with a two-byte name index (into the constant pool), followed by a four-byte length, followed by that many bytes. This makes it fairly easy to skip from descriptor to descriptor.

Next retrieve and display to stderr the number of methods in decimal on a line by itself. Label this output appropriately, such as "method count is 3".

Next iterate through each method, locate the code attribute for each method, and display to stderr the code length for each method along with the method name. Label this output appropriately, such as "method computeBlah has code length 198".

The tricky part is that the attribute are identified by name, and the names are stored in the constant pool. Also, method names are also stored in the constant pool. These names are stored in UTF-8 constant pool entries.

You will need to come up with a method to retrieve the names from the constant pool. For instance, you could modify the code you wrote in Lab 5 that skipped over the constant pool entries to retrieve and store into your own data structure the UTF-8 entries along with their constant pool index. You may assume that the names are expressed with ASCII characters (encoded in UTF-8).

The code attribute name is always "Code".

Note that some methods (e.g. native or abstract methods) will not have code attributes. In this case report a code length of zero.

After printing the method names and their code lengths, terminate the program.

To complete Program 3, continue this code from this point, to analyze the bytecode in each method.

Put all your source code in a single file, "prog3.c".

If you find it helpful, you can include /usr/java/jdk/include/classfile_constants.h. But you should write all the other code yourself.

There are a variety of class files in ~cs520/public/prog3.

Also test with bad input files. In particular, in addition to detecting files that do not begin with the Java class file magic number, also detect class files that are truncated or that contain invalid constant pool entry tags. These are the only file validations that you need to perform.

You can use the javap command, as well as od, to examine class files to determine if your program is working correctly.

Points will be awarded for this lab in the following way:

Turn in this laboratory by typing:

% ~cs520/bin/submit lab6 prog3.c

Submissions can be checked by typing:

% ~cs520/bin/scheck lab6

Remember that there are no late submissions of labs. Submit what you have prior to 12noon on Sunday October 7. Be careful: one minute late is late.


Last modified on September 19, 2018.

Comments and questions should be directed to pjh@cs.unh.edu