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.

BindingResolver.java 9.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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.relations;
  21. import depends.entity.*;
  22. import depends.entity.repo.BuiltInType;
  23. import depends.entity.repo.EntityRepo;
  24. import depends.extractor.AbstractLangProcessor;
  25. import depends.extractor.UnsolvedBindings;
  26. import depends.extractor.empty.EmptyBuiltInType;
  27. import depends.importtypes.Import;
  28. import org.slf4j.Logger;
  29. import org.slf4j.LoggerFactory;
  30. import java.lang.management.ManagementFactory;
  31. import java.util.*;
  32. public class BindingResolver implements IBindingResolver{
  33. private BuiltInType buildInTypeManager = new EmptyBuiltInType();
  34. private ImportLookupStrategy importLookupStrategy;
  35. private Set<UnsolvedBindings> unsolvedSymbols = new HashSet<>();
  36. private EntityRepo repo;
  37. private boolean eagerExpressionResolve = false;
  38. private boolean isCollectUnsolvedBindings = false;
  39. private boolean isDuckTypingDeduce = true;
  40. private static Logger logger = LoggerFactory.getLogger(IBindingResolver.class);
  41. public BindingResolver(AbstractLangProcessor langProcessor,
  42. boolean isCollectUnsolvedBindings, boolean isDuckTypingDeduce) {
  43. this.repo = langProcessor.getEntityRepo();
  44. this.importLookupStrategy = langProcessor.getImportLookupStrategy();
  45. this.buildInTypeManager = langProcessor.getBuiltInType();
  46. this.isCollectUnsolvedBindings = isCollectUnsolvedBindings;
  47. this.isDuckTypingDeduce = isDuckTypingDeduce;
  48. unsolvedSymbols= new HashSet<>();
  49. importLookupStrategy.setBindingResolver(this);
  50. }
  51. @Override
  52. public Set<UnsolvedBindings> resolveAllBindings(boolean isEagerExpressionResolve) {
  53. System.out.println("Resolve type bindings....");
  54. if (logger.isInfoEnabled()) {
  55. logger.info("Resolve type bindings...");
  56. }
  57. resolveTypes(isEagerExpressionResolve);
  58. System.out.println("Dependency analaysing....");
  59. if (logger.isInfoEnabled()) {
  60. logger.info("Dependency analaysing...");
  61. }
  62. logger.info("Heap Information: " + ManagementFactory.getMemoryMXBean().getHeapMemoryUsage());
  63. return unsolvedSymbols;
  64. }
  65. private void resolveTypes(boolean eagerExpressionResolve) {
  66. this.eagerExpressionResolve = eagerExpressionResolve;
  67. Iterator<Entity> iterator = repo.sortedFileIterator();
  68. while(iterator.hasNext()) {
  69. Entity entity= iterator.next();
  70. entity.inferEntities(this);
  71. }
  72. }
  73. @Override
  74. public Collection<Entity> getImportedRelationEntities(List<Import> importedNames) {
  75. return importLookupStrategy.getImportedRelationEntities(importedNames);
  76. }
  77. @Override
  78. public Collection<Entity> getImportedTypes(List<Import> importedNames, FileEntity fileEntity) {
  79. HashSet<UnsolvedBindings> unsolved = new HashSet<UnsolvedBindings>();
  80. Collection<Entity> result = importLookupStrategy.getImportedTypes(importedNames,unsolved);
  81. for (UnsolvedBindings item:unsolved) {
  82. item.setFromEntity(fileEntity);
  83. addUnsolvedBinding(item);
  84. }
  85. return result;
  86. }
  87. private void addUnsolvedBinding(UnsolvedBindings item) {
  88. if (!isCollectUnsolvedBindings) return;
  89. this.unsolvedSymbols.add(item);
  90. }
  91. @Override
  92. public Collection<Entity> getImportedFiles(List<Import> importedNames) {
  93. return importLookupStrategy.getImportedFiles(importedNames);
  94. }
  95. @Override
  96. public TypeEntity inferTypeFromName(Entity fromEntity, GenericName rawName) {
  97. Entity data = resolveName(fromEntity, rawName, true);
  98. if (data == null)
  99. return null;
  100. return data.getType();
  101. }
  102. @Override
  103. public Entity resolveName(Entity fromEntity, GenericName rawName, boolean searchImport) {
  104. if (rawName==null) return null;
  105. Entity entity = resolveNameInternal(fromEntity,rawName,searchImport);
  106. if (entity==null ) {
  107. if (!this.buildInTypeManager.isBuiltInType(rawName.getName())) {
  108. addUnsolvedBinding(new UnsolvedBindings(rawName.getName(), fromEntity));
  109. }
  110. }
  111. return entity;
  112. }
  113. private Entity resolveNameInternal(Entity fromEntity, GenericName rawName, boolean searchImport) {
  114. if (rawName==null || rawName.getName()==null)
  115. return null;
  116. if (buildInTypeManager.isBuiltInType(rawName.getName())) {
  117. return TypeEntity.buildInType;
  118. }
  119. // qualified name will first try global name directly
  120. if (rawName.startsWith(".")) {
  121. rawName = rawName.substring(1);
  122. if (repo.getEntity(rawName) != null)
  123. return repo.getEntity(rawName);
  124. }
  125. Entity entity = null;
  126. int indexCount = 0;
  127. String name = rawName.getName();
  128. if (fromEntity==null) return null;
  129. do {
  130. entity = lookupEntity(fromEntity, name, searchImport);
  131. if (entity!=null ) {
  132. break;
  133. }
  134. if (importLookupStrategy.supportGlobalNameLookup()) {
  135. if (repo.getEntity(name) != null) {
  136. entity = repo.getEntity(name);
  137. break;
  138. }
  139. }
  140. indexCount++;
  141. if (name.contains("."))
  142. name = name.substring(0,name.lastIndexOf('.'));
  143. else
  144. break;
  145. }while (true);
  146. if (entity == null) {
  147. return null;
  148. }
  149. String[] names = rawName.getName().split("\\.");
  150. if (names.length == 0)
  151. return null;
  152. if (names.length == 1) {
  153. return entity;
  154. }
  155. // then find the subsequent symbols
  156. return findEntitySince(entity, names, names.length-indexCount);
  157. }
  158. private Entity lookupEntity(Entity fromEntity, String name, boolean searchImport) {
  159. if (name.equals("this") || name.equals("class") ) {
  160. TypeEntity entityType = (TypeEntity) (fromEntity.getAncestorOfType(TypeEntity.class));
  161. return entityType;
  162. } else if (name.equals("super")) {
  163. TypeEntity parent = (TypeEntity) (fromEntity.getAncestorOfType(TypeEntity.class));
  164. if (parent != null) {
  165. TypeEntity parentType = parent.getInheritedType();
  166. if (parentType!=null)
  167. return parentType;
  168. }
  169. }
  170. Entity inferData = findEntityUnderSamePackage(fromEntity, name);
  171. if (inferData != null) {
  172. return inferData;
  173. }
  174. if (searchImport)
  175. inferData = lookupTypeInImported((FileEntity)(fromEntity.getAncestorOfType(FileEntity.class)), name);
  176. return inferData;
  177. }
  178. /**
  179. * To lookup entity in case of a.b.c from a;
  180. * @param precendenceEntity
  181. * @param names
  182. * @param nameIndex
  183. * @return
  184. */
  185. private Entity findEntitySince(Entity precendenceEntity, String[] names, int nameIndex) {
  186. if (nameIndex >= names.length) {
  187. return precendenceEntity;
  188. }
  189. if (nameIndex == -1) {
  190. System.err.println("No expected symbols: names"+Arrays.toString(names) +", index=" + nameIndex);
  191. return null;
  192. }
  193. //If it is not an entity with types (not a type, var, function), fall back to itself
  194. if (precendenceEntity.getType()==null)
  195. return precendenceEntity;
  196. for (Entity child : precendenceEntity.getType().getChildren()) {
  197. if (child.getRawName().getName().equals(names[nameIndex])) {
  198. return findEntitySince(child, names, nameIndex + 1);
  199. }
  200. }
  201. return null;
  202. }
  203. @Override
  204. public Entity lookupTypeInImported(FileEntity fileEntity, String name) {
  205. if (fileEntity == null)
  206. return null;
  207. Entity type = importLookupStrategy.lookupImportedType(name, fileEntity);
  208. if (type != null)
  209. return type;
  210. return null;
  211. }
  212. /**
  213. * In Java/C++ etc, the same package names should take priority of resolving.
  214. * the entity lookup is implemented recursively.
  215. * @param fromEntity
  216. * @param name
  217. * @return
  218. */
  219. private Entity findEntityUnderSamePackage(Entity fromEntity, String name) {
  220. while (true) {
  221. Entity entity = fromEntity.getByName(name, new HashSet<>());
  222. if (entity!=null) return entity;
  223. fromEntity = fromEntity.getParent();
  224. if (fromEntity == null)
  225. break;
  226. }
  227. return null;
  228. }
  229. @Override
  230. public List<TypeEntity> calculateCandidateTypes(VarEntity fromEntity, List<FunctionCall> functionCalls) {
  231. if (buildInTypeManager.isBuildInTypeMethods(functionCalls)) {
  232. return new ArrayList<>();
  233. }
  234. if (!isDuckTypingDeduce)
  235. return new ArrayList<>();
  236. return searchTypesInRepo(fromEntity, functionCalls);
  237. }
  238. private List<TypeEntity> searchTypesInRepo(VarEntity fromEntity, List<FunctionCall> functionCalls) {
  239. List<TypeEntity> types = new ArrayList<>();
  240. Iterator<Entity> iterator = repo.sortedFileIterator();
  241. while(iterator.hasNext()) {
  242. Entity f = iterator.next();
  243. if (f instanceof FileEntity) {
  244. for (TypeEntity type:((FileEntity)f).getDeclaredTypes()) {
  245. FunctionMatcher functionMatcher = new FunctionMatcher(type.getFunctions());
  246. if (functionMatcher.containsAll(functionCalls)) {
  247. types.add(type);
  248. }
  249. }
  250. }
  251. }
  252. return types;
  253. }
  254. @Override
  255. public boolean isEagerExpressionResolve() {
  256. return eagerExpressionResolve;
  257. }
  258. @Override
  259. public EntityRepo getRepo() {
  260. return repo;
  261. }
  262. }

基于Vue的大屏可视化设计器,前后端一体化解决方案,几十种炫酷图表,支持多种数据来源接入,适用于大屏、低代码、BI场景,使用简单,代码完全开源。

Contributors (2)