shadow$_monitor_和shadow$_klass_

简单来说就是Android sdk21以后 也就是Android5.0系统,google在Object中加入了2个字段

private transient Class<?> shadow$_klass_;
private transient int shadow$_monitor_;

这样在反射的时候就会多出来一个int类型和class类型的字段,
当给int类型字段赋值的时候,导致错误!!!!

/**
     * Android SDK21 在Object中增加了2个变量,这样在反射的时候就会多出来一个int类型和class类型的字段,
     * 导致错误。
     * private transient Class<?> shadow$_klass_;
     * private transient int shadow$_monitor_;
     */
    /*********************************************
     * 从,查询游标中取出查询数据
     * 
     * @param classz
     * @param cursor
     * @return List<Object>
     */
    private List<Object> rawQuery(Class classz, Cursor cursor) {
        List<Object> resultObj = new ArrayList<Object>();
        // 获取对象的字段
        Field[] fields = getField(classz);
        // 判断是否存在查询的字段
        if (fields.length == 0)
            return null;

        String fieldName = null;
        String fieldType = null;
        Object objName = null;

        int fieldIndex = 0;
        int i = 0;
        int max = fields.length;

        // 循环取出查询到的结果
        while (cursor.moveToNext()) {

        LogUtil.e(this, "max fields = "+ max);

            // 构造新的实例对象
            try {
                objName = classz.newInstance();
            } catch (IllegalAccessException e1) {
                e1.printStackTrace();
            } catch (InstantiationException e1) {
                e1.printStackTrace();
            }

            // fields = objName.getClass().getFields();
            try {
                for (i = 0; i < max; i++) {
                    fields[i].setAccessible(true);
                    fieldName = fields[i].getName();
                    fieldIndex = cursor.getColumnIndex(fieldName);
                    fieldType = fields[i].getType().getSimpleName();
                    LogUtil.e(this, " objName = "+ objName+"fieldType  = "+ fieldType + "  fieldIndex = "+ fieldIndex + " fieldName = "+ fieldName);
                    if ("byte".equals(fieldType))
                        fields[i].setByte(objName,
                                (byte) cursor.getInt(fieldIndex));

                    else if ("String".equals(fieldType))
                        fields[i].set(objName, cursor.getString(fieldIndex));

                    else if ("int".equals(fieldType))
                        // android 5.0 之后会有错误。
                        if(!fieldName.equals("shadow$_monitor_")){
                            fields[i].setInt(objName, cursor.getInt(fieldIndex));
                        }

                    else if ("boolean".equals(fieldType)) {
                        if ("true".equals(cursor.getString(fieldIndex)))
                            fields[i].setBoolean(objName, true);
                        else
                            fields[i].setBoolean(objName, false);
                    } else if ("long".equals(fieldType)) {
                        fields[i].setLong(objName, cursor.getLong(fieldIndex));
                    } else if ("float".equals(fieldType))
                        fields[i]
                                .setFloat(objName, cursor.getFloat(fieldIndex));

                    else if ("double".equals(fieldType))
                        fields[i].setDouble(objName,
                                cursor.getDouble(fieldIndex));

                    else if ("short".equals(fieldType))
                        fields[i]
                                .setShort(objName, cursor.getShort(fieldIndex));

                    else if ("char".equals(fieldType))
                        fields[i].setChar(objName, cursor.getString(fieldIndex)
                                .charAt(0));

                }

                // 保存当前的实例对象
                resultObj.add(objName);
//              showMsg("resultObj==" + resultObj.size());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }catch (Exception e) {
                e.printStackTrace();
            }
        }

        cursor.close();
        return resultObj;
    }
    /**
     * 
     * @Title: getField
     * @Description: 获得对象的所有字段,包括其父类(如果有)
     * @param @param clazz
     * @param @return
     * @return Field[]
     * @throws
     */
    private Field[] getField(Class clazz) {
        Field[] childfields = clazz.getDeclaredFields();
        Field[] superFields = clazz.getSuperclass().getDeclaredFields();
        Field[] fields = new Field[childfields.length + superFields.length];
        System.arraycopy(childfields, 0, fields, 0, childfields.length);
        System.arraycopy(superFields, 0, fields, childfields.length,
                superFields.length);
        return fields;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
以下是一个简单的 g_uevent_device_new 函数实现的示例代码: ```c #include <glib.h> #include <gio/gio.h> #define UEVENT_BUFFER_SIZE 2048 typedef struct _GUeventDevice GUeventDevice; struct _GUeventDevice { GObject parent_instance; gchar *syspath; gchar *subsystem; gchar *devtype; gchar **filters; GRegex **regex_filters; guint n_filters; GIOChannel *io_channel; guint io_watch_id; gchar io_buffer[UEVENT_BUFFER_SIZE]; }; G_DEFINE_TYPE(GUeventDevice, g_uevent_device, G_TYPE_OBJECT); static gboolean g_uevent_device_io_watch_callback(GIOChannel *channel, GIOCondition condition, gpointer user_data) { GUeventDevice *self = G_UEVENT_DEVICE(user_data); if (condition & G_IO_IN) { gsize bytes_read = 0; gsize io_buffer_size = sizeof(self->io_buffer) - 1; if (g_io_channel_read_chars(channel, self->io_buffer, io_buffer_size, &bytes_read, NULL) != G_IO_ERROR_NONE) { g_warning("Failed to read from uevent device"); return G_SOURCE_REMOVE; } /* Null-terminate the read buffer */ self->io_buffer[bytes_read] = '\0'; /* Process the uevent messages in the buffer */ gchar *line_start = self->io_buffer; gchar *line_end = NULL; while ((line_end = strchr(line_start, '\n'))) { *line_end = '\0'; gchar **kv_pairs = g_strsplit(line_start, "=", -1); /* Process the key-value pairs in the uevent message */ for (guint i = 0; kv_pairs[i]; i += 2) { gchar *key = kv_pairs[i]; gchar *value = kv_pairs[i + 1]; /* Filter out the key-value pairs that don't match any of the regex filters */ gboolean filter_match = FALSE; for (guint j = 0; j < self->n_filters; j++) { if (g_regex_match(self->regex_filters[j], key, 0, NULL)) { filter_match = TRUE; break; } } if (!filter_match) { continue; } g_message("Received uevent message: %s=%s", key, value); } g_strfreev(kv_pairs); line_start = line_end + 1; } } return G_SOURCE_CONTINUE; } static void g_uevent_device_init(GUeventDevice *self) { self->syspath = NULL; self->subsystem = NULL; self->devtype = NULL; self->filters = NULL; self->regex_filters = NULL; self->n_filters = 0; self->io_channel = NULL; self->io_watch_id = 0; } static void g_uevent_device_class_init(GUeventDeviceClass *klass) { GObjectClass *object_class = G_OBJECT_CLASS(klass); object_class->finalize = (GObjectFinalizeFunc) g_free; } GUeventDevice * g_uevent_device_new(const gchar *syspath, const gchar *subsystem, const gchar *devtype, gchar **filters) { GUeventDevice *self = g_object_new(G_TYPE_UEVENT_DEVICE, NULL); self->syspath = g_strdup(syspath); self->subsystem = g_strdup(subsystem); self->devtype = g_strdup(devtype); self->filters = g_strdupv(filters); /* Compile the regex filters */ for (guint i = 0; self->filters[i]; i++) { GRegex *regex_filter = g_regex_new(self->filters[i], 0, 0, NULL); if (!regex_filter) { g_warning("Failed to compile regex filter: %s", self->filters[i]); continue; } self->regex_filters = g_realloc(self->regex_filters, (self->n_filters + 1) * sizeof(GRegex *)); self->regex_filters[self->n_filters++] = regex_filter; } /* Open the uevent device file */ gchar *uevent_path = g_build_filename(self->syspath, "uevent", NULL); GError *error = NULL; self->io_channel = g_io_channel_new_file(uevent_path, "r", &error); if (!self->io_channel) { g_warning("Failed to open uevent device: %s", error->message); g_error_free(error); g_object_unref(self); return NULL; } /* Add a watch for the IO channel */ self->io_watch_id = g_io_add_watch(self->io_channel, G_IO_IN, g_uevent_device_io_watch_callback, self); g_free(uevent_path); return self; } ``` 这个示例代码演示了如何使用 GLib 和 GIO 库来创建一个 GUeventDevice 对象,该对象可以通过监听 Linux 内核的 uevent 消息来获取设备事件。GUeventDevice 对象可以指定一些过滤器,以只接收特定的 uevent 消息。在对象的销毁期间,将释放所有内存和资源。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值