/** * Add an element to the script search scope. * * @param element The element we want to add to current script search scope * @throws ModelException May happen if some Script Model info are not available */ public void add(IModelElement element) throws ModelException { if (!natureFilter(element)) { return; } IPath containerPath = null; String containerPathToString = null; int includeMask = SOURCES | APPLICATION_LIBRARIES | SYSTEM_LIBRARIES; switch (element.getElementType()) { case IModelElement.SCRIPT_MODEL: // a workspace scope should be used break; case IModelElement.SCRIPT_PROJECT: add((ScriptProject) element, null, includeMask, new HashSet<IProject>(2), null); break; case IModelElement.PROJECT_FRAGMENT: IProjectFragment root = (IProjectFragment) element; String projectPath = null; if (!root.isExternal()) { IPath rootPath = root.getPath(); containerPath = root.getKind() == IProjectFragment.K_SOURCE ? root.getParent().getPath() : rootPath; containerPathToString = containerPath.toString(); IResource rootResource = root.getResource(); projectPath = root.getScriptProject().getPath().toString(); if (rootResource != null && rootResource.isAccessible()) { String relativePath = Util.relativePath(rootResource.getFullPath(), containerPath.segmentCount()); add(projectPath, relativePath, containerPathToString, false /* not a package */, null); } else { add( projectPath, org.eclipse.dltk.compiler.util.Util.EMPTY_STRING, containerPathToString, false /* not a package */, null); } } else { projectPath = root.getScriptProject().getPath().toString(); containerPath = root.getPath(); containerPathToString = containerPath.toString(); add( projectPath, org.eclipse.dltk.compiler.util.Util.EMPTY_STRING, containerPathToString, false /* not a package */, null); } break; case IModelElement.SCRIPT_FOLDER: root = (IProjectFragment) element.getParent(); projectPath = root.getScriptProject().getPath().toString(); if (root.isArchive()) { if (DLTKCore.DEBUG) { System.err.println("TODO: Check. Bug possible..."); // $NON-NLS-1$ } String relativePath = ((ModelElement) element).getPath().toString() + '/'; containerPath = root.getPath(); containerPathToString = containerPath.toString(); add(projectPath, relativePath, containerPathToString, true /* package */, null); } else { IResource resource = element.getResource(); if (resource != null) { if (resource.isAccessible()) { containerPath = root.getKind() == IProjectFragment.K_SOURCE ? root.getParent().getPath() : root.getPath(); } else { // for working copies, get resource container full path containerPath = resource.getParent().getFullPath(); } containerPathToString = containerPath.toString(); String relativePath = Util.relativePath(resource.getFullPath(), containerPath.segmentCount()); add(projectPath, relativePath, containerPathToString, true /* package */, null); } } break; default: // remember sub-cu (or sub-class file) script elements if (element instanceof IMember) { if (this.elements == null) { this.elements = new ArrayList<IModelElement>(); } this.elements.add(element); } root = (IProjectFragment) element.getAncestor(IModelElement.PROJECT_FRAGMENT); projectPath = root.getScriptProject().getPath().toString(); String relativePath; if (root.getKind() == IProjectFragment.K_SOURCE && !root.isExternal()) { containerPath = root.getParent().getPath(); relativePath = Util.relativePath(getPath(element, false /* full path */), 1 /* * remove * project * segmet */); } else { containerPath = root.getPath(); relativePath = getPath(element, true /* relative path */).toString(); } containerPathToString = containerPath.toString(); add(projectPath, relativePath, containerPathToString, false /* * not a * package */, null); } if (containerPath != null) addEnclosingProjectOrArchive(containerPath); }
/** * Add a path to current script search scope or all project fragment roots if null. Use project * resolved classpath to retrieve and store access restriction on each classpath entry. Recurse if * dependent projects are found. * * @param scriptProject Project used to get resolved classpath entries * @param pathToAdd Path to add in case of single element or null if user want to add all project * package fragment roots * @param includeMask Mask to apply on buildpath entries * @param visitedProjects Set to avoid infinite recursion * @param referringEntry Project raw entry in referring project buildpath * @throws ModelException May happen while getting script model info */ void add( ScriptProject scriptProject, IPath pathToAdd, int includeMask, HashSet<IProject> visitedProjects, IBuildpathEntry referringEntry) throws ModelException { if (!natureFilter(scriptProject)) { return; } IProject project = scriptProject.getProject(); if (!project.isAccessible() || !visitedProjects.add(project)) return; IPath projectPath = project.getFullPath(); String projectPathString = projectPath.toString(); this.addEnclosingProjectOrArchive(projectPath); // Iterate via project fragments without buildpath entries IProjectFragment[] fragments = scriptProject.getProjectFragments(); for (int i = 0; i < fragments.length; i++) { if (fragments[i].getRawBuildpathEntry() == null) { add(fragments[i]); } } IBuildpathEntry[] entries = scriptProject.getResolvedBuildpath(); IScriptModel model = scriptProject.getModel(); ModelManager.PerProjectInfo perProjectInfo = scriptProject.getPerProjectInfo(); for (int i = 0, length = entries.length; i < length; i++) { IBuildpathEntry entry = entries[i]; AccessRuleSet access = null; BuildpathEntry cpEntry = (BuildpathEntry) entry; if (referringEntry != null) { // Add only exported entries. // Source folder are implicitly exported. if (!entry.isExported() && entry.getEntryKind() != IBuildpathEntry.BPE_SOURCE) continue; cpEntry = cpEntry.combineWith((BuildpathEntry) referringEntry); // cpEntry = // ((BuildpathEntry)referringEntry).combineWith(cpEntry); } access = cpEntry.getAccessRuleSet(); switch (entry.getEntryKind()) { case IBuildpathEntry.BPE_LIBRARY: IBuildpathEntry rawEntry = null; Map<IPath, IBuildpathEntry> rootPathToRawEntries = perProjectInfo.rootPathToRawEntries; if (rootPathToRawEntries != null) { rawEntry = rootPathToRawEntries.get(entry.getPath()); } if (rawEntry == null) { break; } switch (rawEntry.getEntryKind()) { case IBuildpathEntry.BPE_LIBRARY: case IBuildpathEntry.BPE_VARIABLE: if ((includeMask & APPLICATION_LIBRARIES) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { String pathToString = path.toString(); add( projectPath.toString(), "", pathToString, false /* not a package */, access); //$NON-NLS-1$ addEnclosingProjectOrArchive(path); } } break; case IBuildpathEntry.BPE_CONTAINER: IBuildpathContainer container = DLTKCore.getBuildpathContainer(rawEntry.getPath(), scriptProject); if (container == null) break; if ((container.getKind() == IBuildpathContainer.K_APPLICATION && (includeMask & APPLICATION_LIBRARIES) != 0) || (includeMask & SYSTEM_LIBRARIES) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { String pathToString = path.toString(); add( projectPath.toString(), "", pathToString, false /* not a package */, access); //$NON-NLS-1$ addEnclosingProjectOrArchive(path); } } break; } break; case IBuildpathEntry.BPE_PROJECT: if ((includeMask & REFERENCED_PROJECTS) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { add( (ScriptProject) model.getScriptProject(entry.getPath().lastSegment()), null, includeMask, visitedProjects, cpEntry); } } break; case IBuildpathEntry.BPE_SOURCE: if ((includeMask & SOURCES) != 0) { IPath path = entry.getPath(); if (pathToAdd == null || pathToAdd.equals(path)) { add( projectPath.toString(), Util.relativePath(path, 1 /* * remove project * segment */), projectPathString, false /* * not a package */, access); } } break; } } }