未验证 提交 6ec7f789 编写于 作者: S Skylot

fix: restore usage data after class reload (#1281)

上级 31c0afe2
......@@ -108,6 +108,10 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
ListConsumer<IFieldData, FieldNode> fieldsConsumer = new ListConsumer<>(fld -> FieldNode.build(this, fld));
ListConsumer<IMethodData, MethodNode> methodsConsumer = new ListConsumer<>(mth -> MethodNode.build(this, mth));
cls.visitFieldsAndMethods(fieldsConsumer, methodsConsumer);
if (this.fields != null && this.methods != null) {
// TODO: temporary solution for restore usage info in reloaded methods and fields
restoreUsageData(this.fields, this.methods, fieldsConsumer.getResult(), methodsConsumer.getResult());
}
this.fields = fieldsConsumer.getResult();
this.methods = methodsConsumer.getResult();
......@@ -124,6 +128,24 @@ public class ClassNode extends NotificationAttrNode implements ILoadable, ICodeN
}
}
private void restoreUsageData(List<FieldNode> oldFields, List<MethodNode> oldMethods,
List<FieldNode> newFields, List<MethodNode> newMethods) {
Map<FieldInfo, FieldNode> oldFieldMap = Utils.groupBy(oldFields, FieldNode::getFieldInfo);
for (FieldNode newField : newFields) {
FieldNode oldField = oldFieldMap.get(newField.getFieldInfo());
if (oldField != null) {
newField.setUseIn(oldField.getUseIn());
}
}
Map<MethodInfo, MethodNode> oldMethodsMap = Utils.groupBy(oldMethods, MethodNode::getMethodInfo);
for (MethodNode newMethod : newMethods) {
MethodNode oldMethod = oldMethodsMap.get(newMethod.getMethodInfo());
if (oldMethod != null) {
newMethod.setUseIn(oldMethod.getUseIn());
}
}
}
private ArgType checkSuperType(IClassData cls) {
String superType = cls.getSuperType();
if (superType == null) {
......
......@@ -315,6 +315,21 @@ public class Utils {
return result;
}
/**
* Build map from list of values with value to key mapping function
* <br>
* Similar to:
* <br>
* {@code list.stream().collect(Collectors.toMap(mapKey, Function.identity())); }
*/
public static <K, V> Map<K, V> groupBy(List<V> list, Function<V, K> mapKey) {
Map<K, V> map = new HashMap<>(list.size());
for (V v : list) {
map.put(mapKey.apply(v), v);
}
return map;
}
@Nullable
public static <T> T getOne(@Nullable List<T> list) {
if (list == null || list.size() != 1) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册