/* MIT License Copyright (c) 2018-2019 Gang ZHANG Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package depends.entity; import depends.importtypes.Import; import depends.relations.IBindingResolver; import java.util.*; public class FileEntity extends TypeEntity { private List importedNames = new ArrayList<>(); private boolean isInProjectScope = false; private Collection importedRelationEntities = new ArrayList<>(); private Collection importedFiles = new ArrayList<>(); private Collection importedTypes = new ArrayList<>(); private List declaredTypes = new ArrayList<>(); private ImportedFileCollector importedFileCollector = null; public FileEntity() {} public FileEntity(String fullName, int fileId, boolean isInProjectScope) { super(GenericName.build(fullName), null, fileId); setQualifiedName(fullName); this.isInProjectScope = isInProjectScope; } public FileEntity(String fullName, int fileId) { this(fullName, fileId, true); } public void addImport(Import imported) { if (!importedNames.contains(imported)) importedNames.add(imported); } /** * To match the imported name by suffix * for example: * import a.b.ClassX; * the b.ClassX, ClassX , a.b.classX should be matched * @param lastName * @return */ public String importedSuffixMatch(String lastName) { if (!lastName.startsWith(".")) lastName = "." + lastName; for (Entity imported : this.importedTypes) { String name = imported.getQualifiedName(true); if (!name.startsWith(".")) name = "." + name; if (imported.getQualifiedName(true).endsWith(lastName)) return imported.getQualifiedName(true); } return null; } @Override public String getQualifiedName(boolean overrideFileWithPackage) { if (!overrideFileWithPackage) { return super.getQualifiedName(); } if (this.getParent() == null) { return ""; } if (this.getParent() instanceof PackageEntity) return this.getParent().getQualifiedName(); else return super.getQualifiedName(); } @Override public void inferLocalLevelEntities(IBindingResolver bindingResolver) { this.importedRelationEntities = bindingResolver.getImportedRelationEntities(importedNames); this.importedTypes = bindingResolver.getImportedTypes(importedNames,this); this.importedFiles = bindingResolver.getImportedFiles(importedNames); super.inferLocalLevelEntities(bindingResolver); } public boolean isInProjectScope() { return isInProjectScope; } public void setInProjectScope(boolean isInProjectScope) { this.isInProjectScope = isInProjectScope; } public Collection getImportedRelationEntities() { return importedRelationEntities; } public Collection getImportedFiles() { return importedFiles; } public Collection getImportedTypes() { return importedTypes; } public List getDeclaredTypes() { return this.declaredTypes; } public void addType(TypeEntity currentTypeEntity) { this.declaredTypes.add(currentTypeEntity); } public Set getImportedFilesInAllLevel() { if (importedFileCollector==null) importedFileCollector = new ImportedFileCollector(this); return importedFileCollector.getFiles(); } public List getImportedNames() { return importedNames; } public void cacheAllExpressions() { this.cacheChildExpressions(); } @Override public Entity getByName(String name, HashSet searched) { Entity entity = super.getByName(name, searched); if (entity!=null) return entity; for (TypeEntity type:getDeclaredTypes()) { if (type.getRawName().getName().equals(name)|| suffixMatch(name,type.getQualifiedName())) { return type; } } return null; } private boolean suffixMatch(String name, String qualifiedName) { if (qualifiedName.contains(".")) { if (!name.startsWith(".")) name = "." +name; return qualifiedName.endsWith(name); } else { return qualifiedName.equals(name); } } }