You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Entity.java 7.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.entity;
  21. import depends.relations.IBindingResolver;
  22. import depends.relations.Relation;
  23. import java.util.*;
  24. /**
  25. * Entity is the root of all entities, including file, package, module,
  26. * class, method/function etc.
  27. * Each entity has unique id, name,qualifiedName, parent, children
  28. * We also use entity to record relations
  29. */
  30. public abstract class Entity {
  31. Integer id=-1;
  32. String qualifiedName = null;
  33. GenericName rawName = GenericName.build("");
  34. Entity parent;
  35. private MultiDeclareEntities mutliDeclare = null;
  36. private Set<Entity> children;
  37. ArrayList<Relation> relations;
  38. private Entity actualReferTo = null;
  39. private boolean inScope = true;
  40. protected HashMap<String, Entity> visibleNames = new HashMap<>();
  41. private Location location = new Location();
  42. public Entity() {};
  43. public Entity(GenericName rawName, Entity parent, Integer id) {
  44. this.qualifiedName = null;
  45. this.rawName = rawName;
  46. this.parent = parent;
  47. this.id = id;
  48. if (parent!=null)
  49. parent.addChild(this);
  50. deduceQualifiedName();
  51. visibleNames.put(rawName.getName(), this);
  52. visibleNames.put(qualifiedName, this);
  53. }
  54. private Set<Entity> children() {
  55. if (children==null)
  56. children = new HashSet<>();
  57. return children;
  58. }
  59. /**
  60. * Rule 1: if it start with '.' , then the name is equal to raw name
  61. * Rule 2: if parent not exists, the name is equal to raw name
  62. * Rule 3: if parent exists but no qualified name exists or empty, the name is equal to raw name
  63. * Rule 4: otherwise, qualified name = parent_qualfied_name + "."+rawName
  64. * Rule 5: make sure the qualified name do not start with '.'
  65. */
  66. private void deduceQualifiedName() {
  67. rawName = rawName.replace("::","." );
  68. if (this.rawName.startsWith(".")) {
  69. this.qualifiedName = this.rawName.uniqName().substring(1);
  70. return; //already qualified
  71. }
  72. if (parent==null) {
  73. this.qualifiedName = this.rawName.uniqName();
  74. return;
  75. }
  76. if (parent.getQualifiedName(true)==null) {
  77. this.qualifiedName = this.rawName.uniqName();
  78. return;
  79. }
  80. if (parent.getQualifiedName(true).isEmpty()) {
  81. this.qualifiedName = rawName.uniqName();
  82. return;
  83. }
  84. this.qualifiedName= parent.getQualifiedName(true)+"." + rawName.uniqName();
  85. }
  86. public GenericName getRawName() {
  87. return rawName;
  88. }
  89. public Integer getId() {
  90. return id;
  91. }
  92. public void addRelation(Relation relation) {
  93. if (relations==null)
  94. relations = new ArrayList<>();
  95. if (relation.getEntity()==null) return;
  96. relations.add(relation);
  97. }
  98. public ArrayList<Relation> getRelations() {
  99. if (relations==null)
  100. return new ArrayList<>();
  101. return relations;
  102. }
  103. public void addChild(Entity child) {
  104. children().add(child);
  105. visibleNames.put(child.getRawName().getName(), child);
  106. visibleNames.put(child.getQualifiedName(), child);
  107. }
  108. public Entity getParent() {
  109. return parent;
  110. }
  111. public void setParent(Entity parent) {
  112. this.parent = parent;
  113. }
  114. public Collection<Entity> getChildren() {
  115. if (children==null)
  116. return new HashSet<>();
  117. return children;
  118. }
  119. public void setQualifiedName(String qualifiedName) {
  120. this.qualifiedName = qualifiedName;
  121. }
  122. public void setRawName(GenericName rawName) {
  123. this.rawName = rawName;
  124. deduceQualifiedName();
  125. }
  126. public final String getQualifiedName() {
  127. return qualifiedName;
  128. }
  129. public String getQualifiedName(boolean overrideFileWithPackage) {
  130. return qualifiedName;
  131. }
  132. @Override
  133. public String toString() {
  134. return "Entity [id=" + id + ", qualifiedName=" + qualifiedName + ", rawName=" + rawName + "]";
  135. }
  136. /**
  137. * Get ancestor of type.
  138. * @param classType
  139. * @return null (if not exist) or the type
  140. */
  141. public Entity getAncestorOfType(@SuppressWarnings("rawtypes") Class classType) {
  142. Entity fromEntity = this;
  143. while(fromEntity!=null) {
  144. if (fromEntity.getClass().equals(classType))
  145. return fromEntity;
  146. if (fromEntity.getParent()==null) return null;
  147. fromEntity = fromEntity.getParent();
  148. }
  149. return null;
  150. }
  151. /**
  152. * Invoke inferer to resolve the entity type etc.
  153. * */
  154. public void inferEntities(IBindingResolver bindingResolver) {
  155. inferLocalLevelEntities(bindingResolver);
  156. for (Entity child:this.getChildren()) {
  157. child.inferEntities(bindingResolver);
  158. }
  159. }
  160. public abstract void inferLocalLevelEntities(IBindingResolver bindingResolver);
  161. public TypeEntity getType() {
  162. return null;
  163. }
  164. public String getDisplayName() {
  165. return getRawName().uniqName();
  166. }
  167. public MultiDeclareEntities getMutliDeclare() {
  168. return mutliDeclare;
  169. }
  170. public void setMutliDeclare(MultiDeclareEntities mutliDeclare) {
  171. this.mutliDeclare = mutliDeclare;
  172. }
  173. public Entity getActualReferTo() {
  174. if (this.actualReferTo ==null)
  175. return this;
  176. return actualReferTo;
  177. }
  178. public void setActualReferTo(Entity actualReferTo) {
  179. this.actualReferTo = actualReferTo;
  180. }
  181. public static void setParent(Entity child, Entity parent) {
  182. if (parent == null)
  183. return;
  184. if (child == null)
  185. return;
  186. if (parent.equals(child.getParent()))
  187. return;
  188. child.setParent(parent);
  189. parent.addChild(child);
  190. }
  191. @Override
  192. public int hashCode() {
  193. final int prime = 31;
  194. int result = 1;
  195. result = prime * result + ((id == null) ? 0 : id.hashCode());
  196. return result;
  197. }
  198. @Override
  199. public boolean equals(Object obj) {
  200. if (this == obj)
  201. return true;
  202. if (obj == null)
  203. return false;
  204. if (getClass() != obj.getClass())
  205. return false;
  206. Entity other = (Entity) obj;
  207. if (id == null) {
  208. if (other.id != null)
  209. return false;
  210. } else if (!id.equals(other.id))
  211. return false;
  212. return true;
  213. }
  214. public void setInScope(boolean value) {
  215. this.inScope = value;
  216. children().forEach(child->child.setInScope(value));
  217. }
  218. public boolean inScope() {
  219. return inScope;
  220. }
  221. public Entity getByName(String name, HashSet<Entity> searched) {
  222. if (searched.contains(this)) return null;
  223. searched.add(this);
  224. return visibleNames.get(name);
  225. }
  226. public Integer getLine() {
  227. return location.getLine();
  228. }
  229. public void setLine(int lineNumber) {
  230. this.location.setLine(lineNumber);
  231. }
  232. public Location getLocation() {
  233. return this.location;
  234. }
  235. }

No Description

Contributors (2)