Constant pool

Constant pool — Constant pool structures and operations.

Synopsis


#include <jclass/constant_pool.h>


enum        ConstantTag;
enum        IntType;
typedef     ClassEntry;
typedef     ReferenceEntry;
typedef     StringEntry;
typedef     IntegerEntry;
typedef     LongEntry;
typedef     NameAndTypeEntry;
typedef     UTF8Entry;
typedef     ConstantPoolEntry;
typedef     ConstantPool;
ConstantPool* jclass_cp_new                 (const char *filename,
                                             const ClassPath *classpath);
ConstantPool* jclass_cp_new_from_buffer     (const char *data);
ConstantPool* jclass_cp_new_from_file       (FILE *classfile);
void        jclass_cp_free                  (ConstantPool *cpool);
char*       jclass_cp_get_this_class_name   (const ConstantPool *cpool);
char*       jclass_cp_get_super_class_name  (const ConstantPool *cpool);
char*       jclass_cp_get_class_name        (const ConstantPool *cpool,
                                             uint16_t index,
                                             int ignore_arrays);
char*       jclass_cp_get_method_signature  (const ConstantPool *cpool,
                                             uint16_t index,
                                             int return_type);
char*       jclass_cp_get_constant_value    (const ConstantPool *cpool,
                                             uint16_t index,
                                             IntType int_type);
ConstantTag jclass_cp_get_entry_type        (const ConstantPool *cpool,
                                             uint16_t index);

Description

The constant pool is a table in each class that contains the values for all constants. Typically any reference to a constant anywhere in a class file is using the index of that constant in the table.

Details

enum ConstantTag

typedef enum {
	CONSTANT_Empty = 0,
	CONSTANT_Utf8 =	1, 
	CONSTANT_Integer = 3,
	CONSTANT_Float = 4,
	CONSTANT_Long = 5,
	CONSTANT_Double	= 6,
	CONSTANT_Class = 7,
	CONSTANT_String = 8,
	CONSTANT_Fieldref = 9,
	CONSTANT_Methodref = 10,
	CONSTANT_InterfaceMethodref = 11,
	CONSTANT_NameAndType = 12
} ConstantTag;

Tag value for constant pool entries.

CONSTANT_EmptyEmpty.
CONSTANT_Utf8UTF-8 string.
CONSTANT_IntegerInteger.
CONSTANT_Floatfloat.
CONSTANT_Longlong.
CONSTANT_Doubledouble.
CONSTANT_Classreference to a class/interface.
CONSTANT_Stringreference to a string.
CONSTANT_Fieldrefreference to a field.
CONSTANT_Methodrefreference to a method.
CONSTANT_InterfaceMethodrefa reference to an interface method.
CONSTANT_NameAndTypename and type information.

enum IntType

typedef enum {
	INT_IS_INT,
	INT_IS_BOOLEAN,
	INT_IS_CHAR,
	INT_IS_SHORT,
	INT_IS_BYTE
} IntType;

Specifies the type for constants which are internally promoted to int.

INT_IS_INTint.
INT_IS_BOOLEANboolean.
INT_IS_CHARchar.
INT_IS_SHORTshort.
INT_IS_BYTEbyte.

ClassEntry

typedef struct {
		/* The index in the constant pool of the utf8_info entry 
		* with the name of the class. */
    	uint16_t name_index;
} ClassEntry;

Constant pool entry for a class reference. The tag value for this entry is CONSTANT_Class.


ReferenceEntry

typedef struct {
		/* The index in the constant pool of the ClassEntry 
		* of the class the field/method is a member of.
		*/
    	uint16_t class_index;
		/* The index in the constant pool of the NameAndTypeEntry 
		* with the description of the field/method.
		*/
    	uint16_t name_and_type_index;
} ReferenceEntry;

Constant pool entry for a field, method or interface method reference. Tag values for this type of entries are CONSTANT_Fieldref for fields, CONSTANT_Methodref for methods and CONSTANT_InterfaceMethodref for interface methods.


StringEntry

typedef struct {
		/* The index in the constant pool of the UTF8Entry
		* represending the initial value of the string.
		*/
    	uint16_t string_index;
} StringEntry;

Constant pool entry for a string reference. The tag value for this entry is CONSTANT_String.


IntegerEntry

typedef struct {
		/* The initial value of the int/float */
    	uint32_t bytes;
} IntegerEntry;

Constant pool entry for an integer or a float. Tag values for this type of entries are CONSTANT_Integer for integers and CONSTANT_Float for floats.


LongEntry

typedef struct {
    	uint64_t long_bytes;
} LongEntry;

Constant pool entry for a long or double. Tag values for this type of entries are CONSTANT_Long for longs and CONSTANT_Double for doubles.

Note

The next entry in the constant pool after each long/double is reserved for the VM. That extra entry is counted even though it is not physically present in the file.


NameAndTypeEntry

typedef struct {
		/* The index in the constant pool of the utf8_info entry 
		* with the name of the field/method.*/
    	uint16_t name_index;
		/* The index in the constant pool of the utf8_info entry 
		* with the coded field/method descriptor.*/
    	uint16_t descriptor_index;
} NameAndTypeEntry;

Constant pool entry for name and type information. The tag value for this entry is CONSTANT_NameAndType.


UTF8Entry

typedef struct {
		/* The length of the data in bytes */
    	uint16_t length;
		/* The UTF-8 data */
		uint8_t* contents;
} UTF8Entry;

Constant pool entry for UTF-8 data. The tag value for this entry is CONSTANT_Utf8


ConstantPoolEntry

typedef struct {
		/* It's value determines the type of the entry. */
		uint8_t tag;
		union {
			UTF8Entry* utf8;
			LongEntry* longinfo;
			/* structs with size <= size of pointer */
			NameAndTypeEntry nameandtype;
			IntegerEntry integer;
			ReferenceEntry ref;
			StringEntry stringinfo;
			ClassEntry classinfo;
		}info;
} ConstantPoolEntry;

Constant pool entry.


ConstantPool

typedef struct {
	/* Number of entries */
	uint16_t count;
	/* The index of this class in the constant pool */
	uint16_t this_class;
	/* The index of the super class in the constant pool */
	uint16_t super_class;
	/* An array with the entries */
	ConstantPoolEntry* entries;
} ConstantPool;

The Constant Pool. The number of entries also includes some entries that are reserved for the runtime environment and are not physically present in the file. Those entries are marked with a tag of CONSTANT_Empty.


jclass_cp_new ()

ConstantPool* jclass_cp_new                 (const char *filename,
                                             const ClassPath *classpath);

Constructs a ConstantPool struct with the constant pool of the given class. If parsing fails it returns NULL.

filename : The name of the class or filename of the class.
classpath : The classpath to use to locate the class.
Returns : A newly constructed ConstantPool struct.

jclass_cp_new_from_buffer ()

ConstantPool* jclass_cp_new_from_buffer     (const char *data);

Reads the constant pool of the class in the given buffer.

data : A memory buffer containing a class file.
Returns : A ConstantPool struct.

jclass_cp_new_from_file ()

ConstantPool* jclass_cp_new_from_file       (FILE *classfile);

Reads the constant pool of the class from the given class file. The file must be opened with "rb" permissions. The file will always be closed when the function returns.

classfile : The file containg the class.
Returns : A ConstantPool struct.

jclass_cp_free ()

void        jclass_cp_free                  (ConstantPool *cpool);

Frees a constant pool struct.

cpool : The constant pool to free.

jclass_cp_get_this_class_name ()

char*       jclass_cp_get_this_class_name   (const ConstantPool *cpool);

Gets the name of the class with the given constant pool.

cpool : The constant pool for the class.
Returns : A string allocated with malloc.

jclass_cp_get_super_class_name ()

char*       jclass_cp_get_super_class_name  (const ConstantPool *cpool);

Gets the name of the super class for the class with the given constant pool.

cpool : The constant pool for the class.
Returns : A string allocated with malloc.

jclass_cp_get_class_name ()

char*       jclass_cp_get_class_name        (const ConstantPool *cpool,
                                             uint16_t index,
                                             int ignore_arrays);

Gets the name of the class with the given constant pool index. The entry must be of type CONSTANT_Class. The class name is converted from the internal format to the fully qualifilied external (what it looks like in source code with no imports).

cpool : The constant pool.
index : The index of the class in the constant pool.
ignore_arrays : Set to 1 to report class arrays as classes, 0 otherwise.
Returns : The fully qualified name of the class.

jclass_cp_get_method_signature ()

char*       jclass_cp_get_method_signature  (const ConstantPool *cpool,
                                             uint16_t index,
                                             int return_type);

Gets the signature of a method as a string.

cpool : The constant pool.
index : The index of the method in the contant pool.
return_type : Set to 0 to prevent the return type from being included.
Returns : A newly created string allocated with malloc.

jclass_cp_get_constant_value ()

char*       jclass_cp_get_constant_value    (const ConstantPool *cpool,
                                             uint16_t index,
                                             IntType int_type);

Creates a string representation of a constant pool constant. This is for entries of type CONSTANT_Float, CONSTANT_Double, CONSTANT_Long CONSTANT_Integer, CONSTANT_String and CONSTANT_Utf8.

cpool : The constant pool.
index : The index of the entry on the constant pool.
int_type : The type of the entry for integer constants. If the entry is not of type CONSTANT_Integer this value is ignored.
Returns : A string allocated with malloc.

jclass_cp_get_entry_type ()

ConstantTag jclass_cp_get_entry_type        (const ConstantPool *cpool,
                                             uint16_t index);

Gets the type of the given constant pool entry.

Since: 0.3

cpool : The constant pool.
index : The index of the entry.
Returns : The entry type.