From 42a2795f6ce12fd6165ba90fdea0431740d673b5 Mon Sep 17 00:00:00 2001 From: IzaiahSun Date: Mon, 13 Jun 2022 20:20:36 +0800 Subject: [PATCH] Fix hashmap overwrite when generate json file. --- .gitignore | 2 + src/main/java/TempOutput/DependsString.java | 8 ++- src/main/java/TempOutput/JsonMap.java | 74 +++------------------ src/main/java/TempOutput/JsonString.java | 9 ++- 4 files changed, 23 insertions(+), 70 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5bd05d6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +target +.idea \ No newline at end of file diff --git a/src/main/java/TempOutput/DependsString.java b/src/main/java/TempOutput/DependsString.java index 0bf53af..fd7b0e6 100644 --- a/src/main/java/TempOutput/DependsString.java +++ b/src/main/java/TempOutput/DependsString.java @@ -6,6 +6,7 @@ import entity.properties.Relation; import util.Configure; import util.SingleCollect; +import util.Tuple; import java.util.ArrayList; import java.util.HashMap; @@ -277,7 +278,7 @@ public DependsString getDependsString(String projectName, String projectPath, St dependsString.nodeNum = dependsString.variables.size(); JsonMap jsonMap = new JsonMap(); - Map> relationMap = jsonMap.getFinalRes(); + Map>> relationMap = jsonMap.getFinalRes(); for(int fromEntity:relationMap.keySet()) { if (singleCollect.getEntityById(fromEntity) instanceof PackageEntity){ continue; @@ -289,7 +290,8 @@ public DependsString getDependsString(String projectName, String projectPath, St catch (IndexOutOfBoundsException e){ continue; } - for (int toEntity : relationMap.get(fromEntity).keySet()) { + for (Tuple toEntityObj : relationMap.get(fromEntity)) { + int toEntity=toEntityObj.getL(); if (singleCollect.getEntityById(toEntity) instanceof PackageEntity){ continue; } @@ -314,7 +316,7 @@ public DependsString getDependsString(String projectName, String projectPath, St if (currentCell != null){ // for (String type : relationMap.get(fromEntity).get(toEntity)) { - Relation type = relationMap.get(fromEntity).get(toEntity); + Relation type = toEntityObj.getR(); DetailDTO detail = new DetailDTO(fromEntity, toEntity, type.getKind(), 0, 0); currentCell.addValue(type.getKind()); currentCell.details.add(detail); diff --git a/src/main/java/TempOutput/JsonMap.java b/src/main/java/TempOutput/JsonMap.java index 85a97a5..eae17b6 100644 --- a/src/main/java/TempOutput/JsonMap.java +++ b/src/main/java/TempOutput/JsonMap.java @@ -7,82 +7,28 @@ import util.SingleCollect; import util.Tuple; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; public class JsonMap { protected SingleCollect singleCollect = SingleCollect.getSingleCollectInstance(); - private Map> finalRes = new HashMap>(); + private Map>> finalRes = new HashMap<>(); - public Map> getFinalRes (){ + public Map>> getFinalRes (){ for(BaseEntity entity :singleCollect.getEntities()){ for(Relation relation : entity.getRelation()){ - switch (relation.getKind()){ - case Configure.RELATION_IMPORT: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_IMPORT)); - break; - case Configure.RELATION_INHERIT: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_INHERIT)); - break; - case Configure.RELATION_PARAMETER: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_PARAMETER)); - break; - case Configure.RELATION_CALL: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_CALL)); - break; - case Configure.RELATION_CALL_NON_DYNAMIC: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_CALL_NON_DYNAMIC)); -// finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_CALL)); - break; - case Configure.RELATION_IMPLEMENT: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_IMPLEMENT)); - break; - case Configure.RELATION_SET: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_SET)); - break; - case Configure.RELATION_USE: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_USE)); - break; - case Configure.RELATION_MODIFY: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_MODIFY)); - break; - case Configure.RELATION_CAST: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_CAST)); - break; - case Configure.RELATION_ANNOTATE: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_ANNOTATE)); - break; - case Configure.RELATION_CONTAIN: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_CONTAIN)); - break; - case Configure.RELATION_DEFINE: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_DEFINE)); - break; - case Configure.RELATION_OVERRIDE: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_OVERRIDE)); - break; - case Configure.RELATION_REFLECT: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation,Configure.RELATION_REFLECT)); - break; - case Configure.RELATION_TYPED: - finalRes.put(entity.getId(),supplementRes(entity.getId(), relation, Configure.RELATION_TYPED)); - } - } - } - return finalRes; - } - private Map supplementRes(int entityId, Relation relation, String type){ + if(!finalRes.containsKey(entity.getId())){ + finalRes.put(entity.getId(),new ArrayList<>()); + } + finalRes.get(entity.getId()).add(new Tuple(relation.getToEntity(),relation)); - Map dest = new HashMap<>(); - if(finalRes.containsKey(entityId)){ - dest = finalRes.get(entityId); - } - relation.setKind(type); - dest.put(relation.getToEntity(),relation); - return dest; + } + } + return finalRes; } diff --git a/src/main/java/TempOutput/JsonString.java b/src/main/java/TempOutput/JsonString.java index 4003d6e..e9b6985 100644 --- a/src/main/java/TempOutput/JsonString.java +++ b/src/main/java/TempOutput/JsonString.java @@ -7,6 +7,7 @@ import org.json.JSONObject; import util.SingleCollect; +import util.Tuple; import visitor.relationInf.RelationInf; public class JsonString { @@ -39,7 +40,7 @@ else if (singleCollect.getEntityById(singleCollect.getEntityById(id).getParentId } } - public static String JSONWriteRelation(Map> relationMap, String hiddenPath) throws Exception { + public static String JSONWriteRelation(Map>> relationMap, String hiddenPath) throws Exception { JSONObject obj=new JSONObject();//创建JSONObject对象 @@ -186,9 +187,11 @@ public static String JSONWriteRelation(Map> rela for(int fromEntity:relationMap.keySet()) { - for(int toEntity:relationMap.get(fromEntity).keySet()) { + for(Tuple toEntityObj:relationMap.get(fromEntity)) { + int toEntity=toEntityObj.getL(); + // for(Relation type : relationMap.get(fromEntity).get(toEntity)) { - Relation type = relationMap.get(fromEntity).get(toEntity); + Relation type = toEntityObj.getR(); JSONObject subObj=new JSONObject();//创建对象数组里的子对象 // JSONObject srcObj = new JSONObject();