org.apache.bcel.classfile

Class ClassParser


public final class ClassParser
extends java.lang.Object

Wrapper class that parses a given Java .class file. The method parse returns a JavaClass object on success. When an I/O error or an inconsistency occurs an appropiate exception is propagated back to the caller. The structure and the names comply, except for a few conveniences, exactly with the JVM specification 1.0. See this paper for further details about the structure of a bytecode file.
Version:
$Id: ClassParser.java 386056 2006-03-15 11:31:56Z tcurdt $
Author:
M. Dahm

Field Summary

private static int
BUFSIZE
private int
access_flags
private Attribute[]
attributes
private int
class_name_index
private ConstantPool
constant_pool
private Field[]
fields
private DataInputStream
file
private boolean
fileOwned
private String
file_name
private int[]
interfaces
private boolean
is_zip
private int
major
private Method[]
methods
private int
minor
private int
superclass_name_index
private String
zip_file

Constructor Summary

ClassParser(InputStream file, String file_name)
Parse class from the given stream.
ClassParser(String file_name)
Parse class from given .class file.
ClassParser(String zip_file, String file_name)
Parse class from given .class file in a ZIP-archive

Method Summary

JavaClass
parse()
Parse the given Java class file and return an object that represents the contained data, i.e., constants, methods, fields and commands.
private void
readAttributes()
Read information about the attributes of the class.
private void
readClassInfo()
Read information about the class and its super class.
private void
readConstantPool()
Read constant pool entries.
private void
readFields()
Read information about the fields of the class, i.e., its variables.
private void
readID()
Check whether the header of the file is ok.
private void
readInterfaces()
Read information about the interfaces implemented by this class.
private void
readMethods()
Read information about the methods of the class.
private void
readVersion()
Read major and minor version of compiler which created the file.

Field Details

BUFSIZE

private static final int BUFSIZE
Field Value:
8192

access_flags

private int access_flags

attributes

private Attribute[] attributes

class_name_index

private int class_name_index

constant_pool

private ConstantPool constant_pool

fields

private Field[] fields

file

private DataInputStream file

fileOwned

private boolean fileOwned

file_name

private String file_name

interfaces

private int[] interfaces

is_zip

private boolean is_zip

major

private int major

methods

private Method[] methods

minor

private int minor

superclass_name_index

private int superclass_name_index

zip_file

private String zip_file

Constructor Details

ClassParser

public ClassParser(InputStream file,
                   String file_name)
Parse class from the given stream.
Parameters:
file - Input stream
file_name - File name

ClassParser

public ClassParser(String file_name)
            throws IOException
Parse class from given .class file.
Parameters:
file_name - file name

ClassParser

public ClassParser(String zip_file,
                   String file_name)
Parse class from given .class file in a ZIP-archive
Parameters:
zip_file - zip file name
file_name - file name

Method Details

parse

public JavaClass parse()
            throws IOException,
                   ClassFormatException
Parse the given Java class file and return an object that represents the contained data, i.e., constants, methods, fields and commands. A ClassFormatException is raised, if the file is not a valid .class file. (This does not include verification of the byte code as it is performed by the java interpreter).
Returns:
Class object representing the parsed class file

readAttributes

private final void readAttributes()
            throws IOException,
                   ClassFormatException
Read information about the attributes of the class.

readClassInfo

private final void readClassInfo()
            throws IOException,
                   ClassFormatException
Read information about the class and its super class.

readConstantPool

private final void readConstantPool()
            throws IOException,
                   ClassFormatException
Read constant pool entries.

readFields

private final void readFields()
            throws IOException,
                   ClassFormatException
Read information about the fields of the class, i.e., its variables.

readID

private final void readID()
            throws IOException,
                   ClassFormatException
Check whether the header of the file is ok. Of course, this has to be the first action on successive file reads.

readInterfaces

private final void readInterfaces()
            throws IOException,
                   ClassFormatException
Read information about the interfaces implemented by this class.

readMethods

private final void readMethods()
            throws IOException,
                   ClassFormatException
Read information about the methods of the class.

readVersion

private final void readVersion()
            throws IOException,
                   ClassFormatException
Read major and minor version of compiler which created the file.