public static void collectCompilationUnits( List<ICompilationUnit> result, IPackageFragmentRoot root) throws JavaModelException { IJavaElement[] elements = root.getChildren(); for (int j = 0; j < elements.length; ++j) { IPackageFragment p = (IPackageFragment) elements[j]; result.addAll(Arrays.asList(p.getCompilationUnits())); } }
private String getValue(Object[] objects) { StringBuffer buffer = new StringBuffer(); for (Object object : objects) { IPackageFragment fragment = (IPackageFragment) object; if (buffer.length() > 0) buffer.append("," + getLineDelimiter() + " "); // $NON-NLS-1$ //$NON-NLS-2$ buffer.append(fragment.getElementName()); } return buffer.toString(); }
protected boolean initialize(Object element) { try { if (element instanceof IPackageFragment) { IPackageFragment fragment = (IPackageFragment) element; if (!fragment.containsJavaResources()) return false; IJavaProject javaProject = (IJavaProject) fragment.getAncestor(IJavaElement.JAVA_PROJECT); IProject project = javaProject.getProject(); if (WorkspaceModelManager.isPluginProject(project)) { fProject = javaProject.getProject(); fElements = new HashMap(); fElements.put(fragment, getArguments().getNewName()); return true; } } } catch (JavaModelException e) { } return false; }
public IJavaElement getElementAtConsideringSibling(int position) throws JavaModelException { IPackageFragment fragment = (IPackageFragment) getParent(); PackageFragmentRoot root = (PackageFragmentRoot) fragment.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT); SourceMapper mapper = root.getSourceMapper(); if (mapper == null) { return null; } else { int index = this.name.indexOf('$'); int prefixLength = index < 0 ? this.name.length() : index; IType type = null; int start = -1; int end = Integer.MAX_VALUE; IJavaElement[] children = fragment.getChildren(); for (int i = 0; i < children.length; i++) { String childName = children[i].getElementName(); int childIndex = childName.indexOf('$'); int childPrefixLength = childIndex < 0 ? childName.indexOf('.') : childIndex; if (prefixLength == childPrefixLength && this.name.regionMatches(0, childName, 0, prefixLength)) { IClassFile classFile = (IClassFile) children[i]; // ensure this class file's buffer is open so that source ranges are computed classFile.getBuffer(); SourceRange range = mapper.getSourceRange(classFile.getType()); if (range == SourceMapper.UNKNOWN_RANGE) continue; int newStart = range.getOffset(); int newEnd = newStart + range.getLength() - 1; if (newStart > start && newEnd < end && newStart <= position && newEnd >= position) { type = classFile.getType(); start = newStart; end = newEnd; } } } if (type != null) { return findElement(type, position, mapper); } return null; } }
/** * Returns the children of <code>source</code> which are affected by this operation. If <code> * source</code> is a <code>K_SOURCE</code>, these are the <code>.java</code> files, if it is a * <code>K_BINARY</code>, they are the <code>.class</code> files. */ private IResource[] collectResourcesOfInterest(IPackageFragment source) throws JavaModelException { IJavaElement[] children = source.getChildren(); int childOfInterest = IJavaElement.COMPILATION_UNIT; if (source.getKind() == IPackageFragmentRoot.K_BINARY) { childOfInterest = IJavaElement.CLASS_FILE; } ArrayList correctKindChildren = new ArrayList(children.length); for (int i = 0; i < children.length; i++) { IJavaElement child = children[i]; if (child.getElementType() == childOfInterest) { correctKindChildren.add(((JavaElement) child).resource()); } } // Gather non-java resources Object[] nonJavaResources = source.getNonJavaResources(); int actualNonJavaResourceCount = 0; for (int i = 0, max = nonJavaResources.length; i < max; i++) { if (nonJavaResources[i] instanceof IResource) actualNonJavaResourceCount++; } IResource[] actualNonJavaResources = new IResource[actualNonJavaResourceCount]; for (int i = 0, max = nonJavaResources.length, index = 0; i < max; i++) { if (nonJavaResources[i] instanceof IResource) actualNonJavaResources[index++] = (IResource) nonJavaResources[i]; } if (actualNonJavaResourceCount != 0) { int correctKindChildrenSize = correctKindChildren.size(); IResource[] result = new IResource[correctKindChildrenSize + actualNonJavaResourceCount]; correctKindChildren.toArray(result); System.arraycopy( actualNonJavaResources, 0, result, correctKindChildrenSize, actualNonJavaResourceCount); return result; } else { IResource[] result = new IResource[correctKindChildren.size()]; correctKindChildren.toArray(result); return result; } }
private void checkForJavaDependencies(IPluginModelBase[] models, IProgressMonitor monitor) { try { if (!fProject.hasNature(JavaCore.NATURE_ID)) return; IJavaProject jProject = JavaCore.create(fProject); IPackageFragment[] packageFragments = PluginJavaSearchUtil.collectPackageFragments(models, jProject, true); monitor.beginTask("", packageFragments.length); // $NON-NLS-1$ SearchEngine engine = new SearchEngine(); for (int i = 0; i < packageFragments.length; i++) { if (monitor.isCanceled()) break; IPackageFragment pkgFragment = packageFragments[i]; monitor.subTask( PDEUIMessages.DependencyExtentOperation_inspecting + " " + pkgFragment.getElementName()); // $NON-NLS-1$ if (pkgFragment.hasChildren()) { IJavaElement[] children = pkgFragment.getChildren(); for (int j = 0; j < children.length; j++) { if (monitor.isCanceled()) break; IJavaElement child = children[j]; IType[] types = new IType[0]; if (child instanceof IClassFile) { types = new IType[] {((IClassFile) child).getType()}; } else if (child instanceof ICompilationUnit) { types = ((ICompilationUnit) child).getTypes(); } if (types.length > 0) searchForTypesUsed( engine, child, types, PluginJavaSearchUtil.createSeachScope(jProject)); } } monitor.worked(1); } } catch (CoreException e) { } finally { monitor.done(); } }
/* * Ensures that getPrimaryElement() on an package fragment returns the same handle. */ public void testGetPrimaryElement3() { IPackageFragment pkg = getPackage("P"); assertEquals("Unexpected element", pkg, pkg.getPrimaryElement()); }