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.

FileEntity.java 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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.importtypes.Import;
  22. import depends.relations.IBindingResolver;
  23. import java.util.*;
  24. public class FileEntity extends TypeEntity {
  25. private List<Import> importedNames = new ArrayList<>();
  26. private boolean isInProjectScope = false;
  27. private Collection<Entity> importedRelationEntities = new ArrayList<>();
  28. private Collection<Entity> importedFiles = new ArrayList<>();
  29. private Collection<Entity> importedTypes = new ArrayList<>();
  30. private List<TypeEntity> declaredTypes = new ArrayList<>();
  31. private ImportedFileCollector importedFileCollector = null;
  32. public FileEntity() {}
  33. public FileEntity(String fullName, int fileId, boolean isInProjectScope) {
  34. super(GenericName.build(fullName), null, fileId);
  35. setQualifiedName(fullName);
  36. this.isInProjectScope = isInProjectScope;
  37. }
  38. public FileEntity(String fullName, int fileId) {
  39. this(fullName, fileId, true);
  40. }
  41. public void addImport(Import imported) {
  42. if (!importedNames.contains(imported))
  43. importedNames.add(imported);
  44. }
  45. /**
  46. * To match the imported name by suffix
  47. * for example:
  48. * import a.b.ClassX;
  49. * the b.ClassX, ClassX , a.b.classX should be matched
  50. * @param lastName
  51. * @return
  52. */
  53. public String importedSuffixMatch(String lastName) {
  54. if (!lastName.startsWith("."))
  55. lastName = "." + lastName;
  56. for (Entity imported : this.importedTypes) {
  57. String name = imported.getQualifiedName(true);
  58. if (!name.startsWith("."))
  59. name = "." + name;
  60. if (imported.getQualifiedName(true).endsWith(lastName))
  61. return imported.getQualifiedName(true);
  62. }
  63. return null;
  64. }
  65. @Override
  66. public String getQualifiedName(boolean overrideFileWithPackage) {
  67. if (!overrideFileWithPackage) {
  68. return super.getQualifiedName();
  69. }
  70. if (this.getParent() == null) {
  71. return "";
  72. }
  73. if (this.getParent() instanceof PackageEntity)
  74. return this.getParent().getQualifiedName();
  75. else
  76. return super.getQualifiedName();
  77. }
  78. @Override
  79. public void inferLocalLevelEntities(IBindingResolver bindingResolver) {
  80. this.importedRelationEntities = bindingResolver.getImportedRelationEntities(importedNames);
  81. this.importedTypes = bindingResolver.getImportedTypes(importedNames,this);
  82. this.importedFiles = bindingResolver.getImportedFiles(importedNames);
  83. super.inferLocalLevelEntities(bindingResolver);
  84. }
  85. public boolean isInProjectScope() {
  86. return isInProjectScope;
  87. }
  88. public void setInProjectScope(boolean isInProjectScope) {
  89. this.isInProjectScope = isInProjectScope;
  90. }
  91. public Collection<Entity> getImportedRelationEntities() {
  92. return importedRelationEntities;
  93. }
  94. public Collection<Entity> getImportedFiles() {
  95. return importedFiles;
  96. }
  97. public Collection<Entity> getImportedTypes() {
  98. return importedTypes;
  99. }
  100. public List<TypeEntity> getDeclaredTypes() {
  101. return this.declaredTypes;
  102. }
  103. public void addType(TypeEntity currentTypeEntity) {
  104. this.declaredTypes.add(currentTypeEntity);
  105. }
  106. public Set<FileEntity> getImportedFilesInAllLevel() {
  107. if (importedFileCollector==null)
  108. importedFileCollector = new ImportedFileCollector(this);
  109. return importedFileCollector.getFiles();
  110. }
  111. public List<Import> getImportedNames() {
  112. return importedNames;
  113. }
  114. public void cacheAllExpressions() {
  115. this.cacheChildExpressions();
  116. }
  117. @Override
  118. public Entity getByName(String name, HashSet<Entity> searched) {
  119. Entity entity = super.getByName(name, searched);
  120. if (entity!=null) return entity;
  121. for (TypeEntity type:getDeclaredTypes()) {
  122. if (type.getRawName().getName().equals(name)||
  123. suffixMatch(name,type.getQualifiedName())) {
  124. return type;
  125. }
  126. }
  127. return null;
  128. }
  129. private boolean suffixMatch(String name, String qualifiedName) {
  130. if (qualifiedName.contains(".")) {
  131. if (!name.startsWith(".")) name = "." +name;
  132. return qualifiedName.endsWith(name);
  133. }
  134. else {
  135. return qualifiedName.equals(name);
  136. }
  137. }
  138. }

人工智能研发终端

Contributors (2)