Class

Class — Main class structure.

Synopsis


#include <jclass/class.h>


typedef     JavaClass;
#define     JAVA_CLASS_MAGIC
#define     ACC_PUBLIC
#define     ACC_PRIVATE
#define     ACC_PROTECTED
#define     ACC_STATIC
#define     ACC_FINAL
#define     ACC_SYNCHRONIZED
#define     ACC_VOLATILE
#define     ACC_TRANSIENT
#define     ACC_NATIVE
#define     ACC_INTERFACE
#define     ACC_ABSTRACT
#define     ACC_STRICTFP
JavaClass*  jclass_class_new                (const char *filename,
                                             const ClassPath *classpath);
JavaClass*  jclass_class_new_from_buffer    (const char *data);
JavaClass*  jclass_class_new_from_file      (FILE *classfile);
void        jclass_class_free               (JavaClass *javaclass);
const char* jclass_class_get_vm_spec        (JavaClass *javaclass);
char*       jclass_class_get_class_name     (JavaClass *javaclass);
char*       jclass_class_get_super_class_name
                                            (JavaClass *javaclass);
char*       jclass_class_get_package_name   (JavaClass *javaclass);
char**      jclass_class_get_interfaces     (JavaClass *class_struct);
char*       jclass_class_get_sourcefile_name
                                            (JavaClass *javaclass);

Description

Details

JavaClass

typedef struct {
	/* Minor version of the class file specification */
	uint16_t minor_version;
	/* Major version of the class file specification */
	uint16_t major_version;
	/* The constant pool */
	ConstantPool *constant_pool;
	/* A 16-bit access flag.*/
	uint16_t access_flags;
	/* The number of interfaces implemented by this class */
	uint16_t interfaces_count;
	/* An array with the constant pool indices of the ClassEntry entries 
	* for the interfaces implemented by this class */
	uint16_t *interfaces;
	/* The number of fields this class has */
	uint16_t fields_count;
	/* An array with the fields for this class */
	Field *fields;
	/* The number of methods this class has */
	uint16_t methods_count;
	/* An array with the method for this class */
	Field *methods;
	/* The number of attributes for this class */
	uint16_t attributes_count;
	/* An array with the attributes for this class */
	AttributeContainer *attributes;
} JavaClass;

Holds information about a class. Everything is stored in native order with the exception of the of attributes. They are stored big-endian inside their AttributeContainer.


JAVA_CLASS_MAGIC

#define JAVA_CLASS_MAGIC 0xCAFEBABE

The first 4 bytes of every java class file.


ACC_PUBLIC

#define ACC_PUBLIC 0x0001 

Mask for public visibility.


ACC_PRIVATE

#define ACC_PRIVATE	0x0002

Mask for private visibility.


ACC_PROTECTED

#define ACC_PROTECTED 0x0004

Mask for protected visibility.


ACC_STATIC

#define ACC_STATIC 0x0008

Mask for static.


ACC_FINAL

#define ACC_FINAL 0x0010

Mask for final.


ACC_SYNCHRONIZED

#define ACC_SYNCHRONIZED 0x0020

Mask for synchronized.

Note

The flag is always set for classes but it has a different meaning. It was used to indicate that the class has a super class which is of course always the case except for java.lang.Object. It is there for compatibility with 1.1 or older VMs. So you should ignore it in the context of classes.


ACC_VOLATILE

#define ACC_VOLATILE 0x0040

Mask for volatile.


ACC_TRANSIENT

#define ACC_TRANSIENT 0x0080

Mask for transient.


ACC_NATIVE

#define ACC_NATIVE 0x0100

Mask for native.


ACC_INTERFACE

#define ACC_INTERFACE 0x0200

Mask for interface.


ACC_ABSTRACT

#define ACC_ABSTRACT 0x0400

Mask for abstract.


ACC_STRICTFP

#define ACC_STRICTFP 0x0800

Mask to indicate that strict IEEE-754 floating point behavior is required.


jclass_class_new ()

JavaClass*  jclass_class_new                (const char *filename,
                                             const ClassPath *classpath);

Initializes a new JavaClass struct with info from the given file/class. If parsing fails it returns NULL. Use jclass_class_free() to free the class.

filename : The filename or classname for the class.
classpath : The classpath to use to locate the class.
Returns : A JavaClass struct allocated with malloc.

jclass_class_new_from_buffer ()

JavaClass*  jclass_class_new_from_buffer    (const char *data);

Creates a JavaClass struct from the given buffer. The buffer should be in the same format as a class file.

data : The buffer containing the class.
Returns : A JavaClass struct allocated with malloc.

jclass_class_new_from_file ()

JavaClass*  jclass_class_new_from_file      (FILE *classfile);

Creates a JavaClass struct 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 containing the class.
Returns : A JavaClass struct allocated with malloc.

jclass_class_free ()

void        jclass_class_free               (JavaClass *javaclass);

Frees a JavaClass struct.

javaclass : The JavaClass struct to free.

jclass_class_get_vm_spec ()

const char* jclass_class_get_vm_spec        (JavaClass *javaclass);

Gives the minimum VM spec needed to run this class. The function returns a pointer to a constant string. Do not free it!

javaclass : The class to get the VM spec for.
Returns : A statically allocated string.

jclass_class_get_class_name ()

char*       jclass_class_get_class_name     (JavaClass *javaclass);

Gives the fully qualified name of the class.

javaclass : The JavaClass that we want the class name for.
Returns : A string allocated with malloc.

jclass_class_get_super_class_name ()

char*       jclass_class_get_super_class_name
                                            (JavaClass *javaclass);

Gives the fully qualified name of the super class for the given class.

javaclass : The class that we want the super class name for.
Returns : A string allocated with malloc.

jclass_class_get_package_name ()

char*       jclass_class_get_package_name   (JavaClass *javaclass);

Gives the name of the package this class is part of. If the class is not in a package it returns NULL.

javaclass : The class to get its package name.
Returns : A string allocated with malloc.

jclass_class_get_interfaces ()

char**      jclass_class_get_interfaces     (JavaClass *class_struct);

Gives a null terminated array with the names of all interfaces implemented by the given class. If the class does not implement anything it returns NULL.

Since: 0.4

class_struct : The class to get its interfaces.
Returns : A string allocated with malloc.

jclass_class_get_sourcefile_name ()

char*       jclass_class_get_sourcefile_name
                                            (JavaClass *javaclass);

Gives the name of the source file used to compile this class. If the class does not have a SourceFile attribute it returns NULL.

javaclass : The class.
Returns : A string allocated with malloc.