List of usage examples for org.eclipse.jdt.internal.core.util Util setReadOnly
public static void setReadOnly(IResource resource, boolean readOnly)
From source file:net.sf.j2s.core.builder.AbstractImageBuilder.java
License:Open Source License
protected void copyResource(IResource source, IResource destination) throws CoreException { IPath destPath = destination.getFullPath(); try {//w w w .j a v a2 s . c o m source.copy(destPath, IResource.FORCE | IResource.DERIVED, null); } catch (CoreException e) { // handle the case when the source resource is deleted source.refreshLocal(0, null); if (!source.exists()) return; // source resource was deleted so skip it throw e; } Util.setReadOnly(destination, false); // just in case the original was read only }
From source file:org.eclipse.ajdt.core.builder.AJBuilder.java
License:Open Source License
/** * Copies over all non-excluded resources into the out folders. * // w w w.j a v a2 s . c o m * Called during a full build * * @param javaProject */ private void copyResources(IJavaProject project) throws CoreException { IClasspathEntry[] srcEntries = getSrcClasspathEntry(project); for (int i = 0, l = srcEntries.length; i < l; i++) { IClasspathEntry srcEntry = srcEntries[i]; IPath srcPath = srcEntry.getPath().removeFirstSegments(1); IPath outPath = srcEntry.getOutputLocation(); if (outPath == null) { outPath = project.getOutputLocation(); } outPath = outPath.removeFirstSegments(1); if (!srcPath.equals(outPath)) { final char[][] inclusionPatterns = ((ClasspathEntry) srcEntry).fullInclusionPatternChars(); final char[][] exclusionPatterns = ((ClasspathEntry) srcEntry).fullExclusionPatternChars(); final IContainer srcContainer = getContainerForGivenPath(srcPath, project.getProject()); if (!srcContainer.exists()) { continue; } final int segmentsToRemove = srcContainer.getLocation().segmentCount(); final IContainer outContainer = getContainerForGivenPath(outPath, project.getProject()); if (outContainer.getType() == IResource.FOLDER && (!outContainer.exists())) { // also ensure parent folders exist createFolder(outPath, getProject(), false); } IResourceVisitor copyVisitor = new IResourceVisitor() { public boolean visit(IResource resource) throws CoreException { if (Util.isExcluded(resource, inclusionPatterns, exclusionPatterns)) { return false; } else if (resource.getType() == IResource.PROJECT) { return true; } if (resource.getType() == IResource.FOLDER || !isSourceFile(resource)) { // refresh to ensure that resource has not been deleted from file system resource.refreshLocal(IResource.DEPTH_ZERO, null); if (resource.exists()) { switch (resource.getType()) { case IResource.FOLDER: // ensure folder exists and is derived IPath outPath = resource.getLocation().removeFirstSegments(segmentsToRemove); IFolder outFolder = (IFolder) createFolder(outPath, outContainer, true); // outfolder itself should not be derived if (outFolder.equals(outContainer)) { outFolder.setDerived(false, null); } break; case IResource.FILE: // if this is not a CU, then copy over and mark as derived if (!isSourceFile(resource)) { outPath = resource.getLocation().removeFirstSegments(segmentsToRemove); IFile outFile = outContainer.getFile(outPath); // check to make sure that resource has not been deleted from the file // system without a refresh if (!outFile.exists()) { try { resource.copy(outFile.getFullPath(), IResource.DERIVED | IResource.FORCE, null); Util.setReadOnly(outFile, false); } catch (ResourceException e) { resource.refreshLocal(IResource.DEPTH_ZERO, null); if (resource.exists()) { // probably hit https://bugs.eclipse.org/bugs/show_bug.cgi?id=331036 // We just checked to see if the outfile exists, but we get this exception // anyway. It might be that it has not been refreshed. if (e.getStatus() .getCode() == IResourceStatus.FAILED_WRITE_LOCAL) { AJLog.log(AJLog.BUILDER, "Could not write to resource '" + resource + "'. " + "It probbly already exists on disk. Try a clean build."); outFile.refreshLocal(IResource.DEPTH_ZERO, null); } else { throw e; } } else { // resource was deleted in the middle of the build. Can safely ignore this } } } } break; } return true; } } return false; } }; srcContainer.accept(copyVisitor); } } }
From source file:org.eclipse.ajdt.core.builder.AJBuilder.java
License:Open Source License
/** * Copies non-src resources to the output directory (bug 78579). The main * part of this method was taken from /*w w w. j av a 2 s . c o m*/ * org.eclipse.jdt.internal.core.builder.IncrementalImageBuilder.findSourceFiles(IResourceDelta,ClasspathMultiDirectory,int) * * @param IJavaProject - the project which is being built * @param IResourceDelta - the projects delta * @param IClasspathEntry - the src entry on the classpath * @param int - the segment count * * @throws CoreException */ private void copyResources(IJavaProject javaProject, IResourceDelta sourceDelta, IClasspathEntry srcEntry, int segmentCount) throws CoreException { IResource resource = sourceDelta.getResource(); // bug 161739: skip excluded resources char[][] inclusionPatterns = ((ClasspathEntry) srcEntry).fullInclusionPatternChars(); char[][] exclusionPatterns = ((ClasspathEntry) srcEntry).fullExclusionPatternChars(); if (Util.isExcluded(resource, inclusionPatterns, exclusionPatterns)) { return; } IPath outputPath = srcEntry.getOutputLocation(); if (outputPath == null) { outputPath = javaProject.getOutputLocation(); } outputPath = outputPath.removeFirstSegments(1).makeRelative(); IContainer outputFolder = getContainerForGivenPath(outputPath, javaProject.getProject()); IContainer srcContainer = getContainerForGivenPath(srcEntry.getPath().removeFirstSegments(1), javaProject.getProject()); IPath deltaPath = resource.getFullPath().removeFirstSegments(segmentCount); switch (resource.getType()) { case IResource.FOLDER: IContainer folderToRefresh = outputFolder.getFolder(deltaPath); switch (sourceDelta.getKind()) { case IResourceDelta.ADDED: createFolder(deltaPath, outputFolder, true); // ensure package exists in the output folder // fall through & collect all the resource files case IResourceDelta.CHANGED: IResourceDelta[] children = sourceDelta.getAffectedChildren(); for (int i = 0, l = children.length; i < l; i++) { copyResources(javaProject, children[i], srcEntry, segmentCount); } break; case IResourceDelta.REMOVED: IClasspathEntry[] srcEntries = getSrcClasspathEntry(javaProject); if (srcEntries.length > 1) { for (int i = 0, l = srcEntries.length; i < l; i++) { IPath srcPath = srcEntries[i].getPath().removeFirstSegments(1); IFolder srcFolder = javaProject.getProject().getFolder(srcPath); if (srcFolder.getFolder(deltaPath).exists()) { // only a package fragment was removed, same as removing multiple source files // ensure package exists in the output folder // ADE---wait...why are we doing this??? why not just delete and be done with it? // not going to change this because I don't know the ramifications. createFolder(deltaPath, outputFolder, true); IResourceDelta[] removedChildren = sourceDelta.getAffectedChildren(); for (int j = 0, m = removedChildren.length; j < m; j++) { copyResources(javaProject, removedChildren[j], srcEntry, segmentCount); } folderToRefresh.refreshLocal(IResource.DEPTH_ZERO, null); return; } } } IFolder removedPackageFolder = outputFolder.getFolder(deltaPath); if (removedPackageFolder.exists()) { removedPackageFolder.delete(IResource.FORCE, null); } break; } // switch(sourceDelta.getKind()) folderToRefresh.refreshLocal(IResource.DEPTH_ZERO, null); break; case IResource.FILE: // only do something if the output folder is different to the src folder if (!outputFolder.equals(srcContainer)) { // copy all resource deltas to the output folder if (deltaPath == null) return; // don't want to copy over .aj or .java files if (deltaPath.getFileExtension() != null && (deltaPath.getFileExtension().equals("aj") //$NON-NLS-1$ || deltaPath.getFileExtension().equals("java"))) { //$NON-NLS-1$ break; } IResource fileToRefresh = outputFolder.getFile(deltaPath); switch (sourceDelta.getKind()) { case IResourceDelta.ADDED: if (fileToRefresh.exists()) { AJLog.log(AJLog.BUILDER, "Deleting existing file " + deltaPath);//$NON-NLS-1$ fileToRefresh.delete(IResource.FORCE, null); } AJLog.log(AJLog.BUILDER, "Copying added file " + deltaPath);//$NON-NLS-1$ createFolder(deltaPath.removeLastSegments(1), outputFolder, true); resource.copy(fileToRefresh.getFullPath(), IResource.FORCE | IResource.DERIVED, null); Util.setReadOnly(fileToRefresh, false); // just in case the original was read only fileToRefresh.refreshLocal(IResource.DEPTH_ZERO, null); // mark this change so compiler knows about it. CoreCompilerConfiguration.getCompilerConfigurationForProject(getProject()) .configurationChanged(CompilerConfigurationChangeFlags.PROJECTSOURCERESOURCES_CHANGED); break; case IResourceDelta.REMOVED: if (fileToRefresh.exists()) { AJLog.log(AJLog.BUILDER, "Deleting removed file " + deltaPath);//$NON-NLS-1$ fileToRefresh.delete(IResource.FORCE, null); } // mark this change so compiler knows about it. CoreCompilerConfiguration.getCompilerConfigurationForProject(getProject()) .configurationChanged(CompilerConfigurationChangeFlags.PROJECTSOURCERESOURCES_CHANGED); break; case IResourceDelta.CHANGED: if ((sourceDelta.getFlags() & IResourceDelta.CONTENT) == 0 && (sourceDelta.getFlags() & IResourceDelta.ENCODING) == 0) { return; // skip it since it really isn't changed } if (fileToRefresh.exists()) { AJLog.log(AJLog.BUILDER, "Deleting existing file " + deltaPath);//$NON-NLS-1$ fileToRefresh.delete(IResource.FORCE, null); } AJLog.log(AJLog.BUILDER, "Copying changed file " + deltaPath);//$NON-NLS-1$ createFolder(deltaPath.removeLastSegments(1), outputFolder, true); resource.copy(fileToRefresh.getFullPath(), IResource.FORCE | IResource.DERIVED, null); Util.setReadOnly(fileToRefresh, false); // just in case the original was read only break; } fileToRefresh.refreshLocal(IResource.DEPTH_ZERO, null); } // switch (sourceDelta.getKind()) break; } // switch(resource.getType()) }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
/** * Copies/moves a compilation unit with the name <code>newCUName</code> * to the destination package.<br> * The package statement in the compilation unit is updated if necessary. * The main type of the compilation unit is renamed if necessary. * * @exception JavaModelException if the operation is unable to * complete// www.java2 s .c o m */ private void processCompilationUnitResource(ICompilationUnit source, PackageFragment dest) throws JavaModelException { String newCUName = getNewNameFor(source); String destName = (newCUName != null) ? newCUName : source.getElementName(); TextEdit edit = updateContent(source, dest, newCUName); // null if unchanged // TODO (frederic) remove when bug 67606 will be fixed (bug 67823) // store encoding (fix bug 66898) IFile sourceResource = (IFile) source.getResource(); String sourceEncoding = null; try { sourceEncoding = sourceResource.getCharset(false); } catch (CoreException ce) { // no problem, use default encoding } // end todo // copy resource IContainer destFolder = (IContainer) dest.getResource(); // can be an IFolder or an IProject IFile destFile = destFolder.getFile(new Path(destName)); // GROOVY start /* old { org.eclipse.jdt.internal.core.CompilationUnit destCU = new org.eclipse.jdt.internal.core.CompilationUnit(dest, destName, DefaultWorkingCopyOwner.PRIMARY); } new */ org.eclipse.jdt.internal.core.CompilationUnit destCU = LanguageSupportFactory.newCompilationUnit(dest, destName, DefaultWorkingCopyOwner.PRIMARY); // GROOVY end if (!destFile.equals(sourceResource)) { try { if (!destCU.isWorkingCopy()) { if (destFile.exists()) { if (this.force) { // we can remove it deleteResource(destFile, IResource.KEEP_HISTORY); destCU.close(); // ensure the in-memory buffer for the dest CU is closed } else { // abort throw new JavaModelException( new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind( Messages.status_nameCollision, destFile.getFullPath().toString()))); } } int flags = this.force ? IResource.FORCE : IResource.NONE; if (isMove()) { flags |= IResource.KEEP_HISTORY; sourceResource.move(destFile.getFullPath(), flags, getSubProgressMonitor(1)); } else { if (edit != null) flags |= IResource.KEEP_HISTORY; sourceResource.copy(destFile.getFullPath(), flags, getSubProgressMonitor(1)); } setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); } else { destCU.getBuffer().setContents(source.getBuffer().getContents()); } } catch (JavaModelException e) { throw e; } catch (CoreException e) { throw new JavaModelException(e); } // update new resource content if (edit != null) { boolean wasReadOnly = destFile.isReadOnly(); try { saveContent(dest, destName, edit, sourceEncoding, destFile); } catch (CoreException e) { if (e instanceof JavaModelException) throw (JavaModelException) e; throw new JavaModelException(e); } finally { Util.setReadOnly(destFile, wasReadOnly); } } // register the correct change deltas prepareDeltas(source, destCU, isMove()); if (newCUName != null) { //the main type has been renamed String oldName = Util.getNameWithoutJavaLikeExtension(source.getElementName()); String newName = Util.getNameWithoutJavaLikeExtension(newCUName); prepareDeltas(source.getType(oldName), destCU.getType(newName), isMove()); } } else { if (!this.force) { throw new JavaModelException(new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destFile.getFullPath().toString()))); } // update new resource content // in case we do a saveas on the same resource we have to simply update the contents // see http://dev.eclipse.org/bugs/show_bug.cgi?id=9351 if (edit != null) { saveContent(dest, destName, edit, sourceEncoding, destFile); } } }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
/** * Copies/moves a package fragment with the name <code>newName</code> * to the destination package.<br> * * @exception JavaModelException if the operation is unable to * complete// www . j a v a 2 s.c om */ private void processPackageFragmentResource(PackageFragment source, PackageFragmentRoot root, String newName) throws JavaModelException { try { String[] newFragName = (newName == null) ? source.names : Util.getTrimmedSimpleNames(newName); PackageFragment newFrag = root.getPackageFragment(newFragName); IResource[] resources = collectResourcesOfInterest(source); // if isMove() can we move the folder itself ? (see http://bugs.eclipse.org/bugs/show_bug.cgi?id=22458) boolean shouldMoveFolder = isMove() && !newFrag.resource().exists(); // if new pkg fragment exists, it is an override IFolder srcFolder = (IFolder) source.resource(); IPath destPath = newFrag.getPath(); if (shouldMoveFolder) { // check if destination is not included in source if (srcFolder.getFullPath().isPrefixOf(destPath)) { shouldMoveFolder = false; } else { // check if there are no sub-packages IResource[] members = srcFolder.members(); for (int i = 0; i < members.length; i++) { if (members[i] instanceof IFolder) { shouldMoveFolder = false; break; } } } } boolean containsReadOnlySubPackageFragments = createNeededPackageFragments( (IContainer) source.parent.resource(), root, newFragName, shouldMoveFolder); boolean sourceIsReadOnly = Util.isReadOnly(srcFolder); // Process resources if (shouldMoveFolder) { // move underlying resource // TODO Revisit once bug 43044 is fixed if (sourceIsReadOnly) { Util.setReadOnly(srcFolder, false); } srcFolder.move(destPath, this.force, true /* keep history */, getSubProgressMonitor(1)); if (sourceIsReadOnly) { Util.setReadOnly(srcFolder, true); } setAttribute(HAS_MODIFIED_RESOURCE_ATTR, TRUE); } else { // process the leaf resources if (resources.length > 0) { if (isRename()) { if (!destPath.equals(source.getPath())) { moveResources(resources, destPath); } } else if (isMove()) { // we need to delete this resource if this operation wants to override existing resources for (int i = 0, max = resources.length; i < max; i++) { IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot() .findMember(destPath.append(resources[i].getName())); if (destinationResource != null) { if (this.force) { deleteResource(destinationResource, IResource.KEEP_HISTORY); } else { throw new JavaModelException( new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destinationResource.getFullPath().toString()))); } } } moveResources(resources, destPath); } else { // we need to delete this resource if this operation wants to override existing resources for (int i = 0, max = resources.length; i < max; i++) { IResource destinationResource = ResourcesPlugin.getWorkspace().getRoot() .findMember(destPath.append(resources[i].getName())); if (destinationResource != null) { if (this.force) { // we need to delete this resource if this operation wants to override existing resources deleteResource(destinationResource, IResource.KEEP_HISTORY); } else { throw new JavaModelException( new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.status_nameCollision, destinationResource.getFullPath().toString()))); } } } copyResources(resources, destPath); } } } // Update package statement in compilation unit if needed if (!Util.equalArraysOrNull(newFragName, source.names)) { // if package has been renamed, update the compilation units char[][] inclusionPatterns = root.fullInclusionPatternChars(); char[][] exclusionPatterns = root.fullExclusionPatternChars(); for (int i = 0; i < resources.length; i++) { String resourceName = resources[i].getName(); if (Util.isJavaLikeFileName(resourceName)) { // we only consider potential compilation units ICompilationUnit cu = newFrag.getCompilationUnit(resourceName); if (Util.isExcluded(cu.getPath(), inclusionPatterns, exclusionPatterns, false/*not a folder*/)) continue; this.parser.setSource(cu); CompilationUnit astCU = (CompilationUnit) this.parser.createAST(this.progressMonitor); AST ast = astCU.getAST(); ASTRewrite rewrite = ASTRewrite.create(ast); updatePackageStatement(astCU, newFragName, rewrite, cu); TextEdit edits = rewrite.rewriteAST(); applyTextEdit(cu, edits); cu.save(null, false); } } } // Discard empty old package (if still empty after the rename) boolean isEmpty = true; if (isMove()) { // delete remaining files in this package (.class file in the case where Proj=src=bin) // in case of a copy updateReadOnlyPackageFragmentsForMove((IContainer) source.parent.resource(), root, newFragName, sourceIsReadOnly); if (srcFolder.exists()) { IResource[] remaining = srcFolder.members(); for (int i = 0, length = remaining.length; i < length; i++) { IResource file = remaining[i]; if (file instanceof IFile) { if (Util.isReadOnly(file)) { Util.setReadOnly(file, false); } deleteResource(file, IResource.FORCE | IResource.KEEP_HISTORY); } else { isEmpty = false; } } } if (isEmpty) { IResource rootResource; // check if source is included in destination if (destPath.isPrefixOf(srcFolder.getFullPath())) { rootResource = newFrag.resource(); } else { rootResource = source.parent.resource(); } // delete recursively empty folders deleteEmptyPackageFragment(source, false, rootResource); } } else if (containsReadOnlySubPackageFragments) { // in case of a copy updateReadOnlyPackageFragmentsForCopy((IContainer) source.parent.resource(), root, newFragName); } // workaround for bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=24505 if (isEmpty && isMove() && !(Util.isExcluded(source) || Util.isExcluded(newFrag))) { IJavaProject sourceProject = source.getJavaProject(); getDeltaFor(sourceProject).movedFrom(source, newFrag); IJavaProject destProject = newFrag.getJavaProject(); getDeltaFor(destProject).movedTo(newFrag, source); } } catch (JavaModelException e) { throw e; } catch (CoreException ce) { throw new JavaModelException(ce); } }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
private void saveContent(PackageFragment dest, String destName, TextEdit edits, String sourceEncoding, IFile destFile) throws JavaModelException { try {/*from w w w. java 2 s . c o m*/ // TODO (frederic) remove when bug 67606 will be fixed (bug 67823) // fix bug 66898 if (sourceEncoding != null) destFile.setCharset(sourceEncoding, this.progressMonitor); // end todo } catch (CoreException ce) { // use no encoding } // when the file was copied, its read-only flag was preserved -> temporary set it to false // note this doesn't interfere with repository providers as this is a new resource that cannot be under // version control yet Util.setReadOnly(destFile, false); ICompilationUnit destCU = dest.getCompilationUnit(destName); applyTextEdit(destCU, edits); destCU.save(getSubProgressMonitor(1), this.force); }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
private void updateReadOnlyPackageFragmentsForCopy(IContainer sourceFolder, PackageFragmentRoot root, String[] newFragName) {/*from w ww. jav a2 s.c o m*/ IContainer parentFolder = (IContainer) root.resource(); for (int i = 0, length = newFragName.length; i < length; i++) { String subFolderName = newFragName[i]; parentFolder = parentFolder.getFolder(new Path(subFolderName)); sourceFolder = sourceFolder.getFolder(new Path(subFolderName)); if (sourceFolder.exists() && Util.isReadOnly(sourceFolder)) { Util.setReadOnly(parentFolder, true); } } }
From source file:org.eclipse.jdt.internal.core.CopyResourceElementsOperation.java
License:Open Source License
private void updateReadOnlyPackageFragmentsForMove(IContainer sourceFolder, PackageFragmentRoot root, String[] newFragName, boolean sourceFolderIsReadOnly) { IContainer parentFolder = (IContainer) root.resource(); for (int i = 0, length = newFragName.length; i < length; i++) { String subFolderName = newFragName[i]; parentFolder = parentFolder.getFolder(new Path(subFolderName)); sourceFolder = sourceFolder.getFolder(new Path(subFolderName)); if ((sourceFolder.exists() && Util.isReadOnly(sourceFolder)) || (i == length - 1 && sourceFolderIsReadOnly)) { Util.setReadOnly(parentFolder, true); // the source folder will be deleted anyway (move operation) Util.setReadOnly(sourceFolder, false); }/*from w w w .j a v a 2 s . co m*/ } }