跟踪项目源码排查错误记录

97 阅读2分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第14天,点击查看活动详情


错误

查询接口,最后返回结果处报错,故进入代码一步步追踪排查错误。

image.png

2022-10-12 09:54:27.021 ERROR 23192 --- [io-29040-exec-8] c.e.c.e.EwsmpBootExceptionHandler        : class java.lang.Integer cannot be cast to class java.lang.Long (java.lang.Integer and java.lang.Long are in module java.base of loader 'bootstrap')

java.lang.ClassCastException: class java.lang.Integer cannot be cast to class java.lang.Long (java.lang.Integer and java.lang.Long are in module java.base of loader 'bootstrap')
	at com.ewsmp.common.aspect.DictAspect.parseDictText(DictAspect.java:147) ~[ewsmp-boot-base-core-3.1.0.jar:3.1.0]
	at com.ewsmp.common.aspect.DictAspect.doAround(DictAspect.java:62) ~[ewsmp-boot-base-core-3.1.0.jar:3.1.0]

追踪

image.png

发生错误的方法是 DictAspect #parseDictText(Object result)

参数 Object result

Result(
success=true, message=, code=200, 
result=com.baomidou.mybatisplus.extension.plugins.pagination.Page@3b5741f8, 
timestamp=1665540027650, onlTable=null
)

尝试阅读一下

  • 一个判断
if (result instanceof Result && ((Result)result).getResult() != null) {
    // 包含了所有代码
    ...
}
  • 声明一堆变量
ArrayList dictFieldList;
ArrayList dictFieldList;
HashMap dataListMap;
Iterator var5;
Object record;
ObjectMapper mapper;
String json;
JSONObject item;
Field[] var10;
int var11;
int var12;
Field field;
String value;
String code;
String text;
String table;
List dataList;
String dictCode;
Map translText;
Iterator var26;
JSONObject item;
Iterator var30;
Field field;
String code;
String code;
String value;
String code;
SimpleDateFormat aDate;
List dictModels;
  • 方法整体是分三类处理
if (((Result)result).getResult() instanceof IPage)

if (((Result)result).getResult() instanceof List)

if (((Result)result).getResult().getClass().getAnnotation(ApiModel.class) != null) 
  • 错误发生在第一类,我们阅读一下

image.png

错误如下

image.png

整体代码

if (((Result)result).getResult() instanceof IPage) {
    dictFieldList = new ArrayList();
    dictFieldList = new ArrayList();
    dataListMap = new HashMap();
    var5 = ((IPage)((Result)result).getResult()).getRecords().iterator();

    while(true) {
        if (!var5.hasNext()) {
            translText = this.translateAllDict(dataListMap);
            var26 = dictFieldList.iterator();

            while(var26.hasNext()) {
                item = (JSONObject)var26.next();
                var30 = dictFieldList.iterator();

                while(var30.hasNext()) {
                    field = (Field)var30.next();
                    code = ((Dict)field.getAnnotation(Dict.class)).dicCode();
                    code = ((Dict)field.getAnnotation(Dict.class)).dicText();
                    value = ((Dict)field.getAnnotation(Dict.class)).dictTable();
                    code = code;
                    if (!StringUtils.isEmpty(value)) {
                        code = String.format("%s,%s,%s", value, code, code);
                    }

                    value = item.getString(field.getName());
                    if (oConvertUtils.isNotEmpty(value)) {
                        dictModels = (List)translText.get(code);
                        if (dictModels != null && dictModels.size() != 0) {
                            text = this.translDictText(dictModels, value);
                            log.debug(" 字典Val : " + text);
                            log.debug(" __翻译字典字段__ " + field.getName() + "_dictText" + ": " + text);
                            log.debug(" ---- dictCode: " + code);
                            log.debug(" ---- value: " + value);
                            log.debug(" ----- text: " + text);
                            log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels));
                            item.put(field.getName() + "_dictText", text);
                        }
                    }
                }
            }

            ((IPage)((Result)result).getResult()).setRecords(dictFieldList);
            break;
        }

        record = var5.next();
        mapper = new ObjectMapper();
        json = "{}";

        try {
            json = mapper.writeValueAsString(record);
        } catch (JsonProcessingException var22) {
            log.error("json解析失败" + var22.getMessage(), var22);
        }

        item = JSONObject.parseObject(json, new Feature[]{Feature.OrderedField});
        var10 = oConvertUtils.getAllFields(record);
        var11 = var10.length;

        for(var12 = 0; var12 < var11; ++var12) {
            field = var10[var12];
            value = item.getString(field.getName());
            if (!oConvertUtils.isEmpty(value)) {
                if (field.getAnnotation(Dict.class) != null) {
                    if (!dictFieldList.contains(field)) {
                        dictFieldList.add(field);
                    }

                    code = ((Dict)field.getAnnotation(Dict.class)).dicCode();
                    text = ((Dict)field.getAnnotation(Dict.class)).dicText();
                    table = ((Dict)field.getAnnotation(Dict.class)).dictTable();
                    dictCode = code;
                    if (!StringUtils.isEmpty(table)) {
                        dictCode = String.format("%s,%s,%s", table, text, code);
                    }

                    dataList = (List)dataListMap.computeIfAbsent(dictCode, (k) -> {
                        return new ArrayList();
                    });
                    this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(",")));
                }

                if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
                    aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    item.put(field.getName(), aDate.format(new Date((Long)item.get(field.getName()))));
                }
            }
        }

        dictFieldList.add(item);
    }
}

另外,java的各种源码中为何使用var+数字做变量名,有啥规律吗? www.zhihu.com/question/41…

var 关键字来声明 局部变量。 这允许您声明一个没有类型的变量。

(1)声明变量

dictFieldList = new ArrayList();
dataListMap = new HashMap();
var5 = ((IPage)((Result)result).getResult()).getRecords().iterator();

(2)迭代

if (!var5.hasNext()) {
    translText = this.translateAllDict(dataListMap);
    var26 = dictFieldList.iterator();

    while(var26.hasNext()) {
        item = (JSONObject)var26.next();
        var30 = dictFieldList.iterator();

        while(var30.hasNext()) {
            field = (Field)var30.next();
            code = ((Dict)field.getAnnotation(Dict.class)).dicCode();
            code = ((Dict)field.getAnnotation(Dict.class)).dicText();
            value = ((Dict)field.getAnnotation(Dict.class)).dictTable();
            code = code;
            if (!StringUtils.isEmpty(value)) {
                code = String.format("%s,%s,%s", value, code, code);
            }

            value = item.getString(field.getName());
            if (oConvertUtils.isNotEmpty(value)) {
                dictModels = (List)translText.get(code);
                if (dictModels != null && dictModels.size() != 0) {
                    text = this.translDictText(dictModels, value);
                    log.debug(" 字典Val : " + text);
                    log.debug(" __翻译字典字段__ " + field.getName() + "_dictText" + ": " + text);
                    log.debug(" ---- dictCode: " + code);
                    log.debug(" ---- value: " + value);
                    log.debug(" ----- text: " + text);
                    log.debug(" ---- dictModels: " + JSON.toJSONString(dictModels));
                    item.put(field.getName() + "_dictText", text);
                }
            }
        }
    }

    ((IPage)((Result)result).getResult()).setRecords(dictFieldList);
    break;
}

(3)迭代处理

record = var5.next();
mapper = new ObjectMapper();
json = "{}";

try {
    json = mapper.writeValueAsString(record);
} catch (JsonProcessingException var22) {
    log.error("json解析失败" + var22.getMessage(), var22);
}

item = JSONObject.parseObject(json, new Feature[]{Feature.OrderedField});
var10 = oConvertUtils.getAllFields(record);
var11 = var10.length;

(4)遍历

for(var12 = 0; var12 < var11; ++var12) {
    field = var10[var12];
    value = item.getString(field.getName());
    if (!oConvertUtils.isEmpty(value)) {
        if (field.getAnnotation(Dict.class) != null) {
            if (!dictFieldList.contains(field)) {
                dictFieldList.add(field);
            }

            code = ((Dict)field.getAnnotation(Dict.class)).dicCode();
            text = ((Dict)field.getAnnotation(Dict.class)).dicText();
            table = ((Dict)field.getAnnotation(Dict.class)).dictTable();
            dictCode = code;
            if (!StringUtils.isEmpty(table)) {
                dictCode = String.format("%s,%s,%s", table, text, code);
            }

            dataList = (List)dataListMap.computeIfAbsent(dictCode, (k) -> {
                return new ArrayList();
            });
            this.listAddAllDeduplicate(dataList, Arrays.asList(value.split(",")));
        }

        if (field.getType().getName().equals("java.util.Date") && field.getAnnotation(JsonFormat.class) == null && item.get(field.getName()) != null) {
            aDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            item.put(field.getName(), aDate.format(new Date((Long)item.get(field.getName()))));
        }
    }
}

dictFieldList.add(item);

解决

@ApiModelProperty("创建时间")
@TableField("create_time")
private String createTime;