⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class.h

📁 java 反射机制详解示例,实现类属性及方法修改
💻 H
字号:
#ifndef __CLASS_H__
#define __CLASS_H__

#include <stddef.h>
#include <stdio.h>

#include "type.h"


/**
 * Class description flags
 */
enum RTTIClassFlags { 
    RTTI_CLS_ABSTRACT  = 0x0001,
    RTTI_CLS_INTERNAL  = 0x0002,
    RTTI_CLS_TRANSIENT = 0x0004
};
    

/**
 * Class descriptor
 */
class RTTI_DLL_ENTRY RTTIClassDescriptor : public RTTIType { 
  public:
    typedef RTTIFieldDescriptor* (*RTTIDescribeFieldsFunc)();
    typedef RTTIMethodDescriptor* (*RTTIDescribeMethodsFunc)();
    typedef void  (*RTTIDefaultConstructor)(void* ptr);
    typedef void* (*RTTICreateInstanceFunc)();

    /**
     * Get type name 
     * @param buf buffer in which type name should be placed.
     */
    void getTypeName(char* buf);

    /**
     * Get next class. All classes in repository are linked in L1 list. 
     * Repository <code>getFirstClass</code> method can be used to get reference to first class 
     * descriptor in the list. And <code>getNext</code> method can be used to iterate through the      * list
     * @return next class descriptor in the list or <code>NULL</code> if this class descriptor is the last
     */
    RTTIClassDescriptor* getNext() { 
	return next;
    }

    /**
     * Get array of class fields.
     * @return array of pointers to field descriptors ordered by field name. 
     */
    RTTIFieldDescriptor** getFields() { 
	return fields;
    }

    /**
     * Get number of fields in the class
     * @return number of fields in the class (size of array returned by <code>getFields</code> method)
     */
    int getNumberOfFields() { 
	return nFields;
    }

    /**
     * Get name of the class
     * @return class name
     */
    char const* getName() { 
	return name;
    }
    
    /**
     * Get size f class instance
     * @return size in bytes f class instance
     */
    int  getSize() { 
	return size;
    }

    /**
     * Get class flags
     * @return combination of <code>RTTIClassFlags</code> flags
     */
    int  getFlags() { 
	return flags;
    }

    /**
     * Createnew instance of the class
     * @return newly created instance
     */
    void* newInstance();

    /**
     * Get base classes of this class
     * @return array of pointers to base classes descriptors
     */    
    RTTIClassDescriptor** getBaseClasses() { 
	return baseClasses;
    }

    /**
     * Get number of base classes
     * @return number of direct base classes
     */
    int getNumberOfBaseClasses() { 
	return nBaseClasses;
    }

    /**
     * Get array with the clas methods
     * @return array of pointers to method descriptors ordered by method name
     */
    RTTIMethodDescriptor** getMethods() { 
	return methods;
    }

    /**
     * Get number of methods in the class
     * @return number of methods in the class (size of array returned by <code>getMethods</code> method)
     */
    int getNumberOfMethods() { 
	return nMethods;
    }

    /**
     * Find field by name
     * @param name of the field
     * @return field with such name or <code>NULL</code> if not found
     */
    RTTIFieldDescriptor*  findField(char const* name);

    /**
     * Find method by name
     * @param name of the method
     * @return method with such name or <code>NULL</code> if not found. In case of polymorphic methods, 
     * reference to first once is returned. <code>RTTIMethodDescriptor.getIndex()</code> method can 
     * be used to get index of this method in array returned by <code>getMethods</code>.
     * You can then check name, number and type of parameters of method descriptors referenced by
     * following elements of the array. Code can look something like it:<BR>
     * <PRE>
     *     RTTIMethodDescriptor* mth = cls->findMethod(name);
     *     if (mth != NULL) { 
     *         RTTIMethodDescriptor** allMethods = cls->getMethods();
     *         for (int i = mth->getIndex(), 
                        n = cls->getNumberOfMethods(); 
     *              i < n && strcmp(allMethods[i]->getName, name) == 0; 
     *              i++) 
     *         { 
     *              if (allMethods[i]->getNumberOfParamters() == nParams 
     *                  && ... ) // check if method profile matches profile of searched method
     *              {
     *                  return allMethods[i];
     *              } 
     *         } 
     *     }
     *     return NULL;
     * </PRE>
     */
    RTTIMethodDescriptor* findMethod(char const* name);


    /**
     * Create and initialize class descriptor from information provided by programmer (typdecl.h)
     * This constructor requires functions returning list of components
     * instead of lists themselve, because these component descriptors should be created in context
     * this DLL. 
     * @param name name of the class. 
     * @param size size of the class instance.
     * @param describeFieldsFunc function which returns list of fields
     * @param describeMethodsFunc function which returns list of methods
     * @param function which invokes default ontructor to create instance of the class
     * @param flags flags associated with the class
     */
    RTTIClassDescriptor(char const* name, int size, 
			RTTIDescribeFieldsFunc  describeFieldsFunc,
			RTTIDescribeMethodsFunc describeMethodsFunc, 
			RTTICreateInstanceFunc  createInstanceFunc, 
			int flags); 

    /**
     * Create class descriptor for class loaded from debug information
     * @param name name of the class. 
     * @param size size of the class instance.
     * @param flags flags associated with the class
     */
    RTTIClassDescriptor(char const* name, int size, int flags);

    /**
     * Class descriptor desctructor
     */
    ~RTTIClassDescriptor();

  protected:
    friend class RTTIRepository;
    friend class RTTIBfdRepository;

    RTTIClassDescriptor*    next;
    RTTIClassDescriptor*    collisionChain;

    RTTIMethodDescriptor*   methodList;
    RTTIMethodDescriptor**  methods;
    int                     nMethods;

    RTTIFieldDescriptor*    fieldList;
    RTTIFieldDescriptor**   fields;
    int                     nFields;

    int                     flags;
    int                     size;

    bool                    initialized;
    
    char const*             name;
    unsigned                hashCode;

    int                     nBaseClasses;    
    RTTIClassDescriptor**   baseClasses;

    RTTIDefaultConstructor  defaultConstructor;
    RTTICreateInstanceFunc  createInstanceFunc;

    void buildClassDescriptor();
};


#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -