Java Code Examples for org.eclipse.jdt.core.IPackageFragment#getResource()
The following examples show how to use
org.eclipse.jdt.core.IPackageFragment#getResource() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: NewPackageWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
/** * Returns the resource handle that corresponds to the element that was created or * will be created. * @return A resource or null if the page contains illegal values. * @since 3.0 */ public IResource getModifiedResource() { IPackageFragmentRoot root= getPackageFragmentRoot(); if (root != null) { IPackageFragment pack= root.getPackageFragment(getPackageText()); IResource packRes= pack.getResource(); if (isCreatePackageDocumentation()) { if (JavaModelUtil.is50OrHigher(getJavaProject())) { return pack.getCompilationUnit(PACKAGE_INFO_JAVA_FILENAME).getResource(); } else if (packRes instanceof IFolder){ return ((IFolder) packRes).getFile(PACKAGE_HTML_FILENAME); } } return packRes; } return null; }
Example 2
Source File: RenameModifications.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 6 votes |
private void addAllResourceModifications(IPackageFragment rootPackage, RenameArguments args, boolean renameSubPackages, IPackageFragment[] allSubPackages) throws CoreException { IFolder target= addResourceModifications(rootPackage, args, rootPackage, renameSubPackages); if (renameSubPackages) { IContainer container= (IContainer) rootPackage.getResource(); if (container == null) { return; } boolean removeContainer= ! container.contains(target); for (int i= 0; i < allSubPackages.length; i++) { IPackageFragment pack= allSubPackages[i]; IFolder subTarget= addResourceModifications(rootPackage, args, pack, renameSubPackages); if (container.contains(subTarget)) { removeContainer= false; } } if (removeContainer) { getResourceModifications().addDelete(container); } } }
Example 3
Source File: JavaElementResourceMapping.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static ResourceTraversal[] getRemotePackageFragmentTraversals(IPackageFragment pack, RemoteResourceMappingContext context, IProgressMonitor monitor) throws CoreException { ArrayList<ResourceTraversal> res= new ArrayList<ResourceTraversal>(); IContainer container= (IContainer)pack.getResource(); if (container != null) { res.add(new ResourceTraversal(new IResource[] {container}, IResource.DEPTH_ONE, 0)); IResource[] remoteMembers= context.fetchRemoteMembers(container, monitor); if (remoteMembers == null) { remoteMembers= context.fetchMembers(container, monitor); } if (remoteMembers != null) { for (int i= 0; i < remoteMembers.length; i++) { IResource member= remoteMembers[i]; if (member instanceof IFolder && JavaConventionsUtil.validatePackageName(member.getName(), pack).getSeverity() == IStatus.ERROR) { res.add(new ResourceTraversal(new IResource[] { member }, IResource.DEPTH_INFINITE, 0)); } } } } return res.toArray(new ResourceTraversal[res.size()]); }
Example 4
Source File: JavaElementResourceMapping.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 6 votes |
private static ResourceTraversal[] getPackageFragmentTraversals(IPackageFragment pack) throws CoreException { ArrayList<ResourceTraversal> res= new ArrayList<ResourceTraversal>(); IContainer container= (IContainer)pack.getResource(); if (container != null) { res.add(new ResourceTraversal(new IResource[] { container }, IResource.DEPTH_ONE, 0)); if (pack.exists()) { // folder may not exist any more, see https://bugs.eclipse.org/bugs/show_bug.cgi?id=269167 Object[] nonJavaResources= pack.getNonJavaResources(); for (int i= 0; i < nonJavaResources.length; i++) { Object resource= nonJavaResources[i]; if (resource instanceof IFolder) { res.add(new ResourceTraversal(new IResource[] { (IResource)resource }, IResource.DEPTH_INFINITE, 0)); } } } } return res.toArray(new ResourceTraversal[res.size()]); }
Example 5
Source File: ReorgPolicyFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private Change createChange(IPackageFragment pack, IContainer container, NewNameProposer nameProposer, INewNameQueries copyQueries) { String newName= nameProposer.createNewName(pack.getResource(), container); if (newName == null) { return new CopyResourceChange(pack.getResource(), container, copyQueries.createNullQuery()); } else { IResource res= pack.getResource(); INewNameQuery nameQuery= copyQueries.createNewResourceNameQuery(res, newName); return new CopyResourceChange(res, container, nameQuery); } }
Example 6
Source File: MultiOrganizeImportsHandler.java From xtext-eclipse with Eclipse Public License 2.0 | 5 votes |
private void collectRelevantFiles(IPackageFragment element, Multimap<IProject, IFile> result) throws JavaModelException { if (!element.isDefaultPackage()) { collectIFiles(result, element.getNonJavaResources()); } else if (element.getResource() instanceof IFolder) { IFolder folder = (IFolder) element.getResource(); try { collectIFiles(result, folder.members()); } catch (CoreException e) { e.printStackTrace(); } } }
Example 7
Source File: PackagesViewLabelProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
private String getHierarchicalText(IPackageFragment fragment) { if (fragment.isDefaultPackage()) { return super.getText(fragment); } IResource res= fragment.getResource(); if(res != null && !(res.getType() == IResource.FILE)) return decorateText(res.getName(), fragment); else return decorateText(calculateName(fragment), fragment); }
Example 8
Source File: RenameModifications.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public void rename(IPackageFragment rootPackage, RenameArguments args, boolean renameSubPackages) throws CoreException { add(rootPackage, args, null); IPackageFragment[] allSubPackages= null; if (renameSubPackages) { allSubPackages= getSubpackages(rootPackage); for (int i= 0; i < allSubPackages.length; i++) { IPackageFragment pack= allSubPackages[i]; RenameArguments subArgs= new RenameArguments( getNewPackageName(rootPackage, args.getNewName(), pack.getElementName()), args.getUpdateReferences()); add(pack, subArgs, null); } } IContainer container= (IContainer)rootPackage.getResource(); if (container == null) return; IContainer target= (IContainer) ((IPackageFragmentRoot)rootPackage.getParent()). getPackageFragment(args.getNewName()).getResource(); if ((!rootPackage.hasSubpackages() || renameSubPackages) && canMove(container, target)) { createIncludingParents(target.getParent()); if (container.getParent().equals(target.getParent())) { getResourceModifications().addRename(container, new RenameArguments(target.getName(), args.getUpdateReferences())); } else { // This is a little tricky. The problem is that the refactoring participants // don't support a generic move like the resource API does. So for the delta // we generate one move, however for the participants we have to generate single // moves and deletes. try { getResourceModifications().ignoreForDelta(); addAllResourceModifications(rootPackage, args, renameSubPackages, allSubPackages); } finally { getResourceModifications().trackForDelta(); } getResourceModifications().addDelta(new ResourceModifications.MoveDescription(container, target.getFullPath())); } } else { addAllResourceModifications(rootPackage, args, renameSubPackages, allSubPackages); } }
Example 9
Source File: ClasspathResourceUtilities.java From gwt-eclipse-plugin with Eclipse Public License 1.0 | 5 votes |
/** * Determines if a resource is available on a project's classpath. This method searches both source paths and JAR/ZIP * files. */ public static boolean isResourceOnClasspath(IJavaProject javaProject, IPath resourcePath) throws JavaModelException { String pckg = JavaUtilities.getPackageNameFromPath(resourcePath.removeLastSegments(1)); String fileName = resourcePath.lastSegment(); for (IPackageFragment pckgFragment : JavaModelSearch.getPackageFragments(javaProject, pckg)) { if (ClasspathResourceUtilities.isFileOnPackageFragment(fileName, pckgFragment)) { return true; } } // If the resource doesn't follow normal java naming convention for resources, such as /resources/folder-name/file-name.js for (IPackageFragmentRoot root : javaProject.getAllPackageFragmentRoots()) { IPackageFragment folderFragment = root.getPackageFragment(pckg); IResource folder = folderFragment.getResource(); if (folder == null || !folder.exists() || !(folder instanceof IContainer)) { continue; } IResource resource = ((IContainer) folder).findMember(fileName); if (resource != null && resource.exists()) { return true; } } // Not there return false; }
Example 10
Source File: PackageReorgChange.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
@Override protected IResource getModifiedResource() { IPackageFragment pack= getPackage(); if (pack != null) { return pack.getResource(); } return null; }
Example 11
Source File: JavaSynchronizationLabelProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
/** * {@inheritDoc} */ @Override public Image getDelegateImage(final Object element) { if (element instanceof IPackageFragment) { final IPackageFragment fragment= (IPackageFragment) element; final IResource resource= fragment.getResource(); if (resource == null || !resource.exists()) { if (fPackageImage == null) fPackageImage= JavaPluginImages.DESC_OBJS_PACKAGE.createImage(); return fPackageImage; } } return super.getDelegateImage(element); }
Example 12
Source File: RenameModifications.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 5 votes |
private IFolder addResourceModifications(IPackageFragment rootPackage, RenameArguments args, IPackageFragment pack, boolean renameSubPackages) throws CoreException { IContainer container= (IContainer)pack.getResource(); if (container == null) { return null; } IFolder target= computeTargetFolder(rootPackage, args, pack); createIncludingParents(target); MoveArguments arguments= new MoveArguments(target, args.getUpdateReferences()); IResource[] resourcesToMove= collectResourcesOfInterest(pack); Set<IResource> allMembers= new HashSet<>(Arrays.asList(container.members())); for (int i= 0; i < resourcesToMove.length; i++) { IResource toMove= resourcesToMove[i]; getResourceModifications().addMove(toMove, arguments); allMembers.remove(toMove); } for (Iterator<IResource> iter= allMembers.iterator(); iter.hasNext();) { IResource element= iter.next(); if (element instanceof IFile) { getResourceModifications().addDelete(element); iter.remove(); } } if (! renameSubPackages && allMembers.isEmpty()) { getResourceModifications().addDelete(container); } return target; }
Example 13
Source File: AbstractNewSarlElementWizardPage.java From sarl with Apache License 2.0 | 5 votes |
/** Replies if the given filename is a SARL script in the given package. * * @param packageFragment the package in which the file should be search for. * @param filename the filename to test. * @param extension the filename extension to search for. * @return <code>true</code> if a file (SARL or Java) with the given name exists. */ protected static boolean isFileExists(IPackageFragment packageFragment, String filename, String extension) { if (packageFragment != null) { final IResource resource = packageFragment.getResource(); if (resource instanceof IFolder) { final IFolder folder = (IFolder) resource; if (folder.getFile(filename + "." + extension).exists()) { //$NON-NLS-1$ return true; } } } return false; }
Example 14
Source File: PackageExplorerContentProvider.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
public Object getHierarchicalPackageParent(IPackageFragment child) { String name= child.getElementName(); IPackageFragmentRoot parent= (IPackageFragmentRoot) child.getParent(); int index= name.lastIndexOf('.'); if (index != -1) { String realParentName= name.substring(0, index); IPackageFragment element= parent.getPackageFragment(realParentName); if (element.exists()) { try { if (fFoldPackages && isEmpty(element) && findSinglePackageChild(element, parent.getChildren()) != null) { return getHierarchicalPackageParent(element); } } catch (JavaModelException e) { // ignore } return element; } else { // bug 65240 IResource resource= element.getResource(); if (resource != null) { return resource; } } } if (parent.getResource() instanceof IProject) { return parent.getJavaProject(); } return parent; }
Example 15
Source File: AddFolderToBuildpathAction.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 5 votes |
@Override protected boolean canHandle(IStructuredSelection elements) { if (elements.size() == 0) return false; try { for (Iterator<?> iter= elements.iterator(); iter.hasNext();) { Object element= iter.next(); if (element instanceof IJavaProject) { if (ClasspathModifier.isSourceFolder((IJavaProject)element)) return false; } else if (element instanceof IPackageFragment) { IPackageFragment fragment= (IPackageFragment)element; if (ClasspathModifier.isDefaultFragment(fragment)) return false; if (ClasspathModifier.isInExternalOrArchive(fragment)) return false; IResource res; if ((res= fragment.getResource()) != null && res.isVirtual()) return false; } else if (element instanceof IFolder) { IProject project= ((IFolder)element).getProject(); IJavaProject javaProject= JavaCore.create(project); if (javaProject == null || !javaProject.exists() || ((IFolder)element).isVirtual()) return false; } else { return false; } } return true; } catch (CoreException e) { } return false; }
Example 16
Source File: ReorgPolicyFactory.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private Change createChange(IPackageFragment pack, IContainer destination) { return new MoveResourceChange(pack.getResource(), destination); }
Example 17
Source File: FileEventHandler.java From eclipse.jdt.ls with Eclipse Public License 2.0 | 4 votes |
private static WorkspaceEdit computePackageRenameEdit(FileRenameEvent[] renameEvents, SourcePath[] sourcePaths, IProgressMonitor monitor) { WorkspaceEdit[] root = new WorkspaceEdit[1]; SubMonitor submonitor = SubMonitor.convert(monitor, "Computing package rename updates...", 100 * renameEvents.length); for (FileRenameEvent event : renameEvents) { IPath oldLocation = ResourceUtils.filePathFromURI(event.oldUri); IPath newLocation = ResourceUtils.filePathFromURI(event.newUri); for (SourcePath sourcePath : sourcePaths) { IPath sourceLocation = Path.fromOSString(sourcePath.path); IPath sourceEntry = Path.fromOSString(sourcePath.classpathEntry); if (sourceLocation.isPrefixOf(oldLocation)) { SubMonitor renameMonitor = submonitor.split(100); try { IJavaProject javaProject = ProjectUtils.getJavaProject(sourcePath.projectName); if (javaProject == null) { break; } IPackageFragmentRoot packageRoot = javaProject.findPackageFragmentRoot(sourceEntry); if (packageRoot == null) { break; } String oldPackageName = String.join(".", oldLocation.makeRelativeTo(sourceLocation).segments()); String newPackageName = String.join(".", newLocation.makeRelativeTo(sourceLocation).segments()); IPackageFragment oldPackageFragment = packageRoot.getPackageFragment(oldPackageName); if (oldPackageFragment != null && !oldPackageFragment.isDefaultPackage() && oldPackageFragment.getResource() != null) { oldPackageFragment.getResource().refreshLocal(IResource.DEPTH_INFINITE, null); if (oldPackageFragment.exists()) { ResourcesPlugin.getWorkspace().run((pm) -> { WorkspaceEdit edit = getRenameEdit(oldPackageFragment, newPackageName, pm); root[0] = ChangeUtil.mergeChanges(root[0], edit, true); }, oldPackageFragment.getSchedulingRule(), IResource.NONE, renameMonitor); } } } catch (CoreException e) { JavaLanguageServerPlugin.logException("Failed to compute the package rename update", e); } finally { renameMonitor.done(); } break; } } } submonitor.done(); return ChangeUtil.hasChanges(root[0]) ? root[0] : null; }
Example 18
Source File: CreateCompilationUnitOperation.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Creates a compilation unit. * * @exception JavaModelException if unable to create the compilation unit. */ protected void executeOperation() throws JavaModelException { try { beginTask(Messages.operation_createUnitProgress, 2); JavaElementDelta delta = newJavaElementDelta(); ICompilationUnit unit = getCompilationUnit(); IPackageFragment pkg = (IPackageFragment) getParentElement(); IContainer folder = (IContainer) pkg.getResource(); worked(1); IFile compilationUnitFile = folder.getFile(new Path(this.name)); if (compilationUnitFile.exists()) { // update the contents of the existing unit if fForce is true if (this.force) { IBuffer buffer = unit.getBuffer(); if (buffer == null) return; buffer.setContents(this.source); unit.save(new NullProgressMonitor(), false); this.resultElements = new IJavaElement[] {unit}; if (!Util.isExcluded(unit) && unit.getParent().exists()) { for (int i = 0; i < this.resultElements.length; i++) { delta.changed(this.resultElements[i], IJavaElementDelta.F_CONTENT); } addDelta(delta); } } else { throw new JavaModelException(new JavaModelStatus( IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, compilationUnitFile.getFullPath().toString()))); } } else { try { String encoding = null; try { encoding = folder.getDefaultCharset(); // get folder encoding as file is not accessible } catch (CoreException ce) { // use no encoding } InputStream stream = new ByteArrayInputStream(encoding == null ? this.source.getBytes() : this.source.getBytes(encoding)); createFile(folder, unit.getElementName(), stream, this.force); this.resultElements = new IJavaElement[] {unit}; if (!Util.isExcluded(unit) && unit.getParent().exists()) { for (int i = 0; i < this.resultElements.length; i++) { delta.added(this.resultElements[i]); } addDelta(delta); } } catch (IOException e) { throw new JavaModelException(e, IJavaModelStatusConstants.IO_EXCEPTION); } } worked(1); } finally { done(); } }
Example 19
Source File: NewPackageWizardPage.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
/** * Validates the package name and returns the status of the validation. * * @param packName the package name * * @return the status of the validation */ private IStatus getPackageStatus(String packName) { StatusInfo status= new StatusInfo(); if (packName.length() > 0) { IStatus val= validatePackageName(packName); if (val.getSeverity() == IStatus.ERROR) { status.setError(Messages.format(NewWizardMessages.NewPackageWizardPage_error_InvalidPackageName, val.getMessage())); return status; } else if (val.getSeverity() == IStatus.WARNING) { status.setWarning(Messages.format(NewWizardMessages.NewPackageWizardPage_warning_DiscouragedPackageName, val.getMessage())); } } else { status.setError(NewWizardMessages.NewPackageWizardPage_error_EnterName); return status; } IPackageFragmentRoot root= getPackageFragmentRoot(); if (root != null && root.getJavaProject().exists()) { IPackageFragment pack= root.getPackageFragment(packName); try { IPath rootPath= root.getPath(); IPath outputPath= root.getJavaProject().getOutputLocation(); if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) { // if the bin folder is inside of our root, don't allow to name a package // like the bin folder IPath packagePath= pack.getPath(); if (outputPath.isPrefixOf(packagePath)) { status.setError(NewWizardMessages.NewPackageWizardPage_error_IsOutputFolder); return status; } } if (pack.exists()) { // it's ok for the package to exist as long we want to create package level documentation // and the package level documentation doesn't exist if (!isCreatePackageDocumentation() || packageDocumentationAlreadyExists(pack)) { if (pack.containsJavaResources() || !pack.hasSubpackages()) { status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExists); } else { status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageNotShown); } } } else { IResource resource= pack.getResource(); if (resource != null && !ResourcesPlugin.getWorkspace().validateFiltered(resource).isOK()) { status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageNameFiltered); return status; } URI location= pack.getResource().getLocationURI(); if (location != null) { IFileStore store= EFS.getStore(location); if (store.fetchInfo().exists()) { status.setError(NewWizardMessages.NewPackageWizardPage_error_PackageExistsDifferentCase); } } } } catch (CoreException e) { JavaPlugin.log(e); } } return status; }
Example 20
Source File: ReorgPolicyFactory.java From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 | 4 votes |
private Change createChange(IPackageFragment pack, IContainer destination) { return new MoveResourceChange(pack.getResource(), destination); }