jasm.io
Class ClassFileReader

java.lang.Object
  extended by jasm.io.ClassFileReader

public final class ClassFileReader
extends java.lang.Object

The ClassFileReader is responsible for reading in a class file and constructing a ClassFile object from it.

Author:
David J. Pearce

Nested Class Summary
protected static class ClassFileReader.ClassConstant
          This class represents a class constant.
 
Field Summary
static int ACC_ABSTRACT
           
static int ACC_ANNOTATION
           
static int ACC_BRIDGE
           
static int ACC_ENUM
           
static int ACC_FINAL
           
static int ACC_INTERFACE
           
static int ACC_NATIVE
           
static int ACC_PRIVATE
           
static int ACC_PROTECTED
           
static int ACC_PUBLIC
           
static int ACC_STATIC
           
static int ACC_STRICT
           
static int ACC_SUPER
           
static int ACC_SYNCHRONIZED
           
static int ACC_SYNTHETIC
           
static int ACC_TRANSIENT
           
static int ACC_VARARGS
           
static int ACC_VOLATILE
           
static int ADD
           
static int AND
           
protected static char ANNOTATION
           
protected static char ARRAY
           
static int ARRAYLENGTH
           
static int ARRAYLOAD
           
static int ARRAYSTORE
           
protected static char BOOLEAN
           
protected static char BYTE
           
protected static char CHAR
           
static int CHECKCAST
           
protected static char CLASS
           
static int CMP
           
static int CMPG
           
static int CMPL
           
protected static int CONSTANT_Class
           
protected static int CONSTANT_Double
           
protected static int CONSTANT_FieldRef
           
protected static int CONSTANT_Float
           
protected static int CONSTANT_Integer
           
protected static int CONSTANT_InterfaceMethodRef
           
protected static int CONSTANT_Long
           
protected static int CONSTANT_MethodRef
           
protected static int CONSTANT_NameAndType
           
protected static int CONSTANT_String
           
protected static int CONSTANT_Utf8
           
static int CONVERT
           
static int DIV
           
protected static char DOUBLE
           
static int DUP
           
static int DUPX1
           
static int DUPX2
           
protected static char ENUM
           
static int EQUALS
           
static int FIELDLOAD
           
static int FIELDSTORE
           
protected static char FLOAT
           
static int GOTO
           
static int GREATERTHAN
           
static int GREATERTHANEQUALS
           
static int IF
           
static int IFCMP
           
static int IINC
           
static int INSN_MASK
           
static int INSTANCEOF
           
protected static char INT
           
static int INVOKE
           
static int JSR
           
static int LESSTHAN
           
static int LESSTHANEQUALS
           
static int LOADCONST
           
static int LOADVAR
           
protected static char LONG
           
static int MONITORENTER
           
static int MONITOREXIT
           
static int MUL
           
static int NEG
           
static int NEW
           
static int NONNULL
           
static int NOP
           
static int NOTEQUALS
           
static int NULL
           
static int[] opmap
           
static int OR
           
static int POP
           
static int REM
           
static int RET
           
static int RETURN
           
static int SHL
           
protected static char SHORT
           
static int SHR
           
static int STORECONST
           
static int STOREVAR
           
protected static char STRING
           
static int SUB
           
static int SWAP
           
static int SWITCH
           
static int THROW
           
static int USHR
           
static byte VM_BOOLEAN
           
static byte VM_BYTE
           
static byte VM_CHAR
           
static byte VM_DOUBLE
           
static byte VM_FLOAT
           
static byte VM_INT
           
static byte VM_LONG
           
static byte VM_SHORT
           
static int WIDE_INSN
           
static int XOR
           
 
Constructor Summary
ClassFileReader(byte[] b, BytecodeAttribute.Reader... readers)
          Construct reader from byte array representing classfile.
ClassFileReader(byte[] b, java.util.Collection<BytecodeAttribute.Reader> readers)
          Construct reader from byte array representing classfile.
ClassFileReader(java.io.InputStream in, BytecodeAttribute.Reader... readers)
          Construct reader for classfile from InputStream
ClassFileReader(java.io.InputStream in, java.util.Collection<BytecodeAttribute.Reader> readers)
          Construct reader for classfile from InputStream
 
Method Summary
protected  int annotationLength(int offset)
           
protected static JvmType.Array buildArraytype(int atype)
           
 java.lang.Object convert(Constant.Info constant)
          Convert a constant pool item into a Java constant object (e.g.
protected  java.lang.String createBranchLabel(int target, java.util.HashMap<java.lang.Integer,java.lang.String> labels)
           
protected  JvmType.Primitive decodeConversionType(int offset)
           
protected  Bytecode decodeInstruction(int offset, int start, java.util.HashMap<java.lang.Integer,java.lang.String> labels, int line)
           
protected  java.lang.String decodeInstructionBranchTarget(int offset, int start, java.util.HashMap<java.lang.Integer,java.lang.String> labels, int line)
          Determine the relative offset for the destination of a branching instruction.
protected  java.lang.Object decodeInstructionConstant(int offset, int line)
           
protected  int decodeInstructionLength(int offset, int codeOffsetP14)
           
protected  Triple<JvmType.Clazz,java.lang.String,JvmType> decodeInstructionOwnerNameType(int offset, int line)
           
protected  JvmType decodeInstructionType(int offset)
           
protected  int decodeInstructionVariable(int offset, int line)
           
 Pair<java.lang.Integer,JvmType>[] exceptionHandlers(int offset, int exceptionTableOffset)
           
 java.lang.String getClassName(int index)
          This method is slightly ugly and it would be nice to get rid of it.
 Constant.Info getConstant(int index)
          Read constant value from this classfile's constant pool.
 JvmType getConstantType(int index)
          Get the type of the constant value from this classfile's constant pool.
 java.lang.String getString(int index)
          Read string from this classfile's constant pool.
protected  BytecodeAttribute parseAttribute(int offset, ClassFile.Method enclosingMethod, ClassFile enclosingClass)
           
protected  java.util.ArrayList<BytecodeAttribute> parseAttributes(int attributes, ClassFile.Method enclosingMethod, ClassFile enclosingClass)
          parse any attributes associated with this field.
static JvmType.Clazz parseClassDescriptor(java.lang.String descriptor)
          This method parses a descriptor of the form "Lxxx.yyy.zzz$aaa$bbb;"
protected  Triple<java.util.List<JvmType.Reference>,JvmType.Clazz,java.util.List<JvmType.Clazz>> parseClassSigDesc(java.lang.String descriptor)
          The class signature provides information about the generic type parameters declared for a class.
protected  Code parseCode(int offset, java.lang.String name, ClassFile.Method enclosingMethod)
           
protected  ConstantValue parseConstantValue(int offset, java.lang.String name)
           
static JvmType parseDescriptor(java.lang.String descriptor)
          This method parses a general type descriptor.
protected  Exceptions parseExceptions(int offset, java.lang.String name)
           
protected  ClassFile.Field parseField(int offset)
           
protected  java.util.ArrayList<ClassFile.Field> parseFields(int fields)
          parse array of fields defined in this class
static Pair<JvmType.Variable,java.lang.Integer> parseFormalType(java.lang.String descriptor, int pos)
           
protected  InnerClasses parseInnerClasses(int offset, java.lang.String name, JvmType.Clazz type)
           
protected  java.util.ArrayList<JvmType.Clazz> parseInterfaces(int interfaces)
          Get array of interfaces implemented by this class.
static Pair<JvmType.Clazz,java.lang.Integer> parseInternalClassDescriptor(java.lang.String descriptor, int pos)
           
static Pair<JvmType,java.lang.Integer> parseInternalDescriptor(java.lang.String descriptor, int pos)
           
protected  ClassFile.Method parseMethod(int offset, java.lang.String owner)
           
static JvmType.Function parseMethodDescriptor(java.lang.String descriptor)
           
protected  java.util.ArrayList<ClassFile.Method> parseMethods(int methods, java.lang.String owner)
          parse array of methods defined in this class
 ClassFile readClass()
          Parse classfile and construct ClassInfo object.
protected static byte[] readStream(java.io.InputStream is)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTE

protected static final char BYTE
See Also:
Constant Field Values

CHAR

protected static final char CHAR
See Also:
Constant Field Values

DOUBLE

protected static final char DOUBLE
See Also:
Constant Field Values

FLOAT

protected static final char FLOAT
See Also:
Constant Field Values

INT

protected static final char INT
See Also:
Constant Field Values

LONG

protected static final char LONG
See Also:
Constant Field Values

SHORT

protected static final char SHORT
See Also:
Constant Field Values

BOOLEAN

protected static final char BOOLEAN
See Also:
Constant Field Values

STRING

protected static final char STRING
See Also:
Constant Field Values

ENUM

protected static final char ENUM
See Also:
Constant Field Values

CLASS

protected static final char CLASS
See Also:
Constant Field Values

ANNOTATION

protected static final char ANNOTATION
See Also:
Constant Field Values

ARRAY

protected static final char ARRAY
See Also:
Constant Field Values

ACC_PUBLIC

public static final int ACC_PUBLIC
See Also:
Constant Field Values

ACC_PRIVATE

public static final int ACC_PRIVATE
See Also:
Constant Field Values

ACC_PROTECTED

public static final int ACC_PROTECTED
See Also:
Constant Field Values

ACC_STATIC

public static final int ACC_STATIC
See Also:
Constant Field Values

ACC_FINAL

public static final int ACC_FINAL
See Also:
Constant Field Values

ACC_VOLATILE

public static final int ACC_VOLATILE
See Also:
Constant Field Values

ACC_TRANSIENT

public static final int ACC_TRANSIENT
See Also:
Constant Field Values

ACC_SYNTHETIC

public static final int ACC_SYNTHETIC
See Also:
Constant Field Values

ACC_BRIDGE

public static final int ACC_BRIDGE
See Also:
Constant Field Values

ACC_VARARGS

public static final int ACC_VARARGS
See Also:
Constant Field Values

ACC_ANNOTATION

public static final int ACC_ANNOTATION
See Also:
Constant Field Values

ACC_ENUM

public static final int ACC_ENUM
See Also:
Constant Field Values

ACC_SYNCHRONIZED

public static final int ACC_SYNCHRONIZED
See Also:
Constant Field Values

ACC_NATIVE

public static final int ACC_NATIVE
See Also:
Constant Field Values

ACC_ABSTRACT

public static final int ACC_ABSTRACT
See Also:
Constant Field Values

ACC_STRICT

public static final int ACC_STRICT
See Also:
Constant Field Values

ACC_INTERFACE

public static final int ACC_INTERFACE
See Also:
Constant Field Values

ACC_SUPER

public static final int ACC_SUPER
See Also:
Constant Field Values

CONSTANT_Class

protected static final int CONSTANT_Class
See Also:
Constant Field Values

CONSTANT_FieldRef

protected static final int CONSTANT_FieldRef
See Also:
Constant Field Values

CONSTANT_MethodRef

protected static final int CONSTANT_MethodRef
See Also:
Constant Field Values

CONSTANT_InterfaceMethodRef

protected static final int CONSTANT_InterfaceMethodRef
See Also:
Constant Field Values

CONSTANT_String

protected static final int CONSTANT_String
See Also:
Constant Field Values

CONSTANT_Integer

protected static final int CONSTANT_Integer
See Also:
Constant Field Values

CONSTANT_Float

protected static final int CONSTANT_Float
See Also:
Constant Field Values

CONSTANT_Long

protected static final int CONSTANT_Long
See Also:
Constant Field Values

CONSTANT_Double

protected static final int CONSTANT_Double
See Also:
Constant Field Values

CONSTANT_NameAndType

protected static final int CONSTANT_NameAndType
See Also:
Constant Field Values

CONSTANT_Utf8

protected static final int CONSTANT_Utf8
See Also:
Constant Field Values

VM_BOOLEAN

public static final byte VM_BOOLEAN
See Also:
Constant Field Values

VM_CHAR

public static final byte VM_CHAR
See Also:
Constant Field Values

VM_FLOAT

public static final byte VM_FLOAT
See Also:
Constant Field Values

VM_DOUBLE

public static final byte VM_DOUBLE
See Also:
Constant Field Values

VM_BYTE

public static final byte VM_BYTE
See Also:
Constant Field Values

VM_SHORT

public static final byte VM_SHORT
See Also:
Constant Field Values

VM_INT

public static final byte VM_INT
See Also:
Constant Field Values

VM_LONG

public static final byte VM_LONG
See Also:
Constant Field Values

INSN_MASK

public static final int INSN_MASK
See Also:
Constant Field Values

WIDE_INSN

public static final int WIDE_INSN
See Also:
Constant Field Values

NOP

public static final int NOP
See Also:
Constant Field Values

LOADVAR

public static final int LOADVAR
See Also:
Constant Field Values

STOREVAR

public static final int STOREVAR
See Also:
Constant Field Values

LOADCONST

public static final int LOADCONST
See Also:
Constant Field Values

STORECONST

public static final int STORECONST
See Also:
Constant Field Values

ARRAYLOAD

public static final int ARRAYLOAD
See Also:
Constant Field Values

ARRAYSTORE

public static final int ARRAYSTORE
See Also:
Constant Field Values

ARRAYLENGTH

public static final int ARRAYLENGTH
See Also:
Constant Field Values

IINC

public static final int IINC
See Also:
Constant Field Values

NEW

public static final int NEW
See Also:
Constant Field Values

THROW

public static final int THROW
See Also:
Constant Field Values

CHECKCAST

public static final int CHECKCAST
See Also:
Constant Field Values

INSTANCEOF

public static final int INSTANCEOF
See Also:
Constant Field Values

MONITORENTER

public static final int MONITORENTER
See Also:
Constant Field Values

MONITOREXIT

public static final int MONITOREXIT
See Also:
Constant Field Values

SWITCH

public static final int SWITCH
See Also:
Constant Field Values

CONVERT

public static final int CONVERT
See Also:
Constant Field Values

POP

public static final int POP
See Also:
Constant Field Values

DUP

public static final int DUP
See Also:
Constant Field Values

DUPX1

public static final int DUPX1
See Also:
Constant Field Values

DUPX2

public static final int DUPX2
See Also:
Constant Field Values

SWAP

public static final int SWAP
See Also:
Constant Field Values

ADD

public static final int ADD
See Also:
Constant Field Values

SUB

public static final int SUB
See Also:
Constant Field Values

MUL

public static final int MUL
See Also:
Constant Field Values

DIV

public static final int DIV
See Also:
Constant Field Values

REM

public static final int REM
See Also:
Constant Field Values

NEG

public static final int NEG
See Also:
Constant Field Values

SHL

public static final int SHL
See Also:
Constant Field Values

SHR

public static final int SHR
See Also:
Constant Field Values

USHR

public static final int USHR
See Also:
Constant Field Values

AND

public static final int AND
See Also:
Constant Field Values

OR

public static final int OR
See Also:
Constant Field Values

XOR

public static final int XOR
See Also:
Constant Field Values

CMP

public static final int CMP
See Also:
Constant Field Values

CMPL

public static final int CMPL
See Also:
Constant Field Values

CMPG

public static final int CMPG
See Also:
Constant Field Values

IF

public static final int IF
See Also:
Constant Field Values

IFCMP

public static final int IFCMP
See Also:
Constant Field Values

GOTO

public static final int GOTO
See Also:
Constant Field Values

JSR

public static final int JSR
See Also:
Constant Field Values

RET

public static final int RET
See Also:
Constant Field Values

RETURN

public static final int RETURN
See Also:
Constant Field Values

FIELDLOAD

public static final int FIELDLOAD
See Also:
Constant Field Values

FIELDSTORE

public static final int FIELDSTORE
See Also:
Constant Field Values

INVOKE

public static final int INVOKE
See Also:
Constant Field Values

EQUALS

public static final int EQUALS
See Also:
Constant Field Values

NOTEQUALS

public static final int NOTEQUALS
See Also:
Constant Field Values

LESSTHAN

public static final int LESSTHAN
See Also:
Constant Field Values

GREATERTHANEQUALS

public static final int GREATERTHANEQUALS
See Also:
Constant Field Values

GREATERTHAN

public static final int GREATERTHAN
See Also:
Constant Field Values

LESSTHANEQUALS

public static final int LESSTHANEQUALS
See Also:
Constant Field Values

NULL

public static final int NULL
See Also:
Constant Field Values

NONNULL

public static final int NONNULL
See Also:
Constant Field Values

opmap

public static final int[] opmap
Constructor Detail

ClassFileReader

public ClassFileReader(java.io.InputStream in,
                       BytecodeAttribute.Reader... readers)
                throws java.io.IOException
Construct reader for classfile from InputStream

Parameters:
fileName - The filename of the java classfile. Use dot notation for specifying packages.
Throws:
java.io.IOException

ClassFileReader

public ClassFileReader(java.io.InputStream in,
                       java.util.Collection<BytecodeAttribute.Reader> readers)
                throws java.io.IOException
Construct reader for classfile from InputStream

Parameters:
fileName - The filename of the java classfile. Use dot notation for specifying packages.
Throws:
java.io.IOException

ClassFileReader

public ClassFileReader(byte[] b,
                       BytecodeAttribute.Reader... readers)
Construct reader from byte array representing classfile.

Parameters:
b - the byte array!
Throws:
java.lang.ClassFormatError - if the classfile is invalid.

ClassFileReader

public ClassFileReader(byte[] b,
                       java.util.Collection<BytecodeAttribute.Reader> readers)
Construct reader from byte array representing classfile.

Parameters:
b - the byte array!
Throws:
java.lang.ClassFormatError - if the classfile is invalid.
Method Detail

readClass

public ClassFile readClass()
Parse classfile and construct ClassInfo object. Currently, this does the same thing as readSkeletons.

Throws:
java.lang.ClassFormatError - if the classfile is invalid.

parseInterfaces

protected java.util.ArrayList<JvmType.Clazz> parseInterfaces(int interfaces)
Get array of interfaces implemented by this class.

Returns:

parseFields

protected java.util.ArrayList<ClassFile.Field> parseFields(int fields)
parse array of fields defined in this class

Returns:

parseField

protected ClassFile.Field parseField(int offset)

parseMethods

protected java.util.ArrayList<ClassFile.Method> parseMethods(int methods,
                                                             java.lang.String owner)
parse array of methods defined in this class

Returns:

parseMethod

protected ClassFile.Method parseMethod(int offset,
                                       java.lang.String owner)

parseAttributes

protected java.util.ArrayList<BytecodeAttribute> parseAttributes(int attributes,
                                                                 ClassFile.Method enclosingMethod,
                                                                 ClassFile enclosingClass)
parse any attributes associated with this field.

Returns:

parseAttribute

protected BytecodeAttribute parseAttribute(int offset,
                                           ClassFile.Method enclosingMethod,
                                           ClassFile enclosingClass)

parseExceptions

protected Exceptions parseExceptions(int offset,
                                     java.lang.String name)

parseConstantValue

protected ConstantValue parseConstantValue(int offset,
                                           java.lang.String name)

parseInnerClasses

protected InnerClasses parseInnerClasses(int offset,
                                         java.lang.String name,
                                         JvmType.Clazz type)

parseDescriptor

public static JvmType parseDescriptor(java.lang.String descriptor)
This method parses a general type descriptor.

Parameters:
descriptor -
Returns:

parseClassDescriptor

public static JvmType.Clazz parseClassDescriptor(java.lang.String descriptor)
This method parses a descriptor of the form "Lxxx.yyy.zzz$aaa$bbb;"

Parameters:
descriptor -
Returns:

parseClassSigDesc

protected Triple<java.util.List<JvmType.Reference>,JvmType.Clazz,java.util.List<JvmType.Clazz>> parseClassSigDesc(java.lang.String descriptor)
The class signature provides information about the generic type parameters declared for a class.

Returns:

parseInternalDescriptor

public static Pair<JvmType,java.lang.Integer> parseInternalDescriptor(java.lang.String descriptor,
                                                                      int pos)

parseInternalClassDescriptor

public static Pair<JvmType.Clazz,java.lang.Integer> parseInternalClassDescriptor(java.lang.String descriptor,
                                                                                 int pos)

parseFormalType

public static Pair<JvmType.Variable,java.lang.Integer> parseFormalType(java.lang.String descriptor,
                                                                       int pos)

parseMethodDescriptor

public static JvmType.Function parseMethodDescriptor(java.lang.String descriptor)

parseCode

protected Code parseCode(int offset,
                         java.lang.String name,
                         ClassFile.Method enclosingMethod)

decodeInstruction

protected Bytecode decodeInstruction(int offset,
                                     int start,
                                     java.util.HashMap<java.lang.Integer,java.lang.String> labels,
                                     int line)

decodeInstructionVariable

protected int decodeInstructionVariable(int offset,
                                        int line)

decodeInstructionLength

protected int decodeInstructionLength(int offset,
                                      int codeOffsetP14)

decodeInstructionBranchTarget

protected java.lang.String decodeInstructionBranchTarget(int offset,
                                                         int start,
                                                         java.util.HashMap<java.lang.Integer,java.lang.String> labels,
                                                         int line)
Determine the relative offset for the destination of a branching instruction.

Parameters:
offset -
start -
line -
Returns:

createBranchLabel

protected java.lang.String createBranchLabel(int target,
                                             java.util.HashMap<java.lang.Integer,java.lang.String> labels)

decodeInstructionType

protected JvmType decodeInstructionType(int offset)

buildArraytype

protected static final JvmType.Array buildArraytype(int atype)

decodeConversionType

protected JvmType.Primitive decodeConversionType(int offset)

decodeInstructionConstant

protected java.lang.Object decodeInstructionConstant(int offset,
                                                     int line)

convert

public java.lang.Object convert(Constant.Info constant)
Convert a constant pool item into a Java constant object (e.g. java.lang.Integer, java.lang.String, etc).

Parameters:
constant - Constant pool item to be converted. Should be an instance of Constant.String, Constant.Integer, Constant.Long, Constant.Float, Constant.Double.
Returns:

decodeInstructionOwnerNameType

protected Triple<JvmType.Clazz,java.lang.String,JvmType> decodeInstructionOwnerNameType(int offset,
                                                                                        int line)

exceptionHandlers

public Pair<java.lang.Integer,JvmType>[] exceptionHandlers(int offset,
                                                           int exceptionTableOffset)

annotationLength

protected int annotationLength(int offset)

getString

public final java.lang.String getString(int index)
Read string from this classfile's constant pool.

Parameters:
index - index into constant pool

getConstant

public final Constant.Info getConstant(int index)
Read constant value from this classfile's constant pool.

Parameters:
index -
Returns:
Object representing value (e.g. Integer for int etc).

getConstantType

public final JvmType getConstantType(int index)
Get the type of the constant value from this classfile's constant pool.

Parameters:
index -
Returns:
JvmType object representing type of value

getClassName

public final java.lang.String getClassName(int index)
This method is slightly ugly and it would be nice to get rid of it. It's used for getting the name of an exception.

Parameters:
index -
Returns:

readStream

protected static byte[] readStream(java.io.InputStream is)
                            throws java.io.IOException
Throws:
java.io.IOException


Copyright © 2013 David J. Pearce. All Rights Reserved.