Search in sources :

Example 1 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class Util method getJdkLevel.

/**
     * Get the jdk level of this root.
     * The value can be:
     * <ul>
     * <li>major<<16 + minor : see predefined constants on ClassFileConstants </li>
     * <li><code>0</null> if the root is a source package fragment root or if a Java model exception occured</li>
     * </ul>
     * Returns the jdk level
     */
public static long getJdkLevel(Object targetLibrary) {
    try {
        ClassFileReader reader = null;
        if (targetLibrary instanceof IFolder) {
            // only internal classfolders are allowed
            IFile classFile = findFirstClassFile((IFolder) targetLibrary);
            if (classFile != null)
                reader = Util.newClassFileReader(classFile);
        } else {
            // root is a jar file or a zip file
            ZipFile jar = null;
            try {
                IPath path = null;
                if (targetLibrary instanceof IResource) {
                    path = ((IResource) targetLibrary).getFullPath();
                } else if (targetLibrary instanceof File) {
                    File f = (File) targetLibrary;
                    if (!f.isDirectory()) {
                        path = new Path(((File) targetLibrary).getPath());
                    }
                }
                if (path != null) {
                    jar = JavaModelManager.getJavaModelManager().getZipFile(path);
                    for (Enumeration e = jar.entries(); e.hasMoreElements(); ) {
                        ZipEntry member = (ZipEntry) e.nextElement();
                        String entryName = member.getName();
                        if (org.eclipse.jdt.internal.compiler.util.Util.isClassFileName(entryName)) {
                            reader = ClassFileReader.read(jar, entryName);
                            break;
                        }
                    }
                }
            } catch (CoreException e) {
            // ignore
            } finally {
                JavaModelManager.getJavaModelManager().closeZipFile(jar);
            }
        }
        if (reader != null) {
            return reader.getVersion();
        }
    } catch (CoreException e) {
    // ignore
    } catch (ClassFormatException e) {
    // ignore
    } catch (IOException e) {
    // ignore
    }
    return 0;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IFile(org.eclipse.core.resources.IFile) Enumeration(java.util.Enumeration) IPath(org.eclipse.core.runtime.IPath) IClassFileReader(org.eclipse.jdt.core.util.IClassFileReader) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) ZipEntry(java.util.zip.ZipEntry) IOException(java.io.IOException) ClassFormatException(org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException) ZipFile(java.util.zip.ZipFile) CoreException(org.eclipse.core.runtime.CoreException) IClassFile(org.eclipse.jdt.core.IClassFile) ClassFile(org.eclipse.jdt.internal.core.ClassFile) ZipFile(java.util.zip.ZipFile) IFile(org.eclipse.core.resources.IFile) File(java.io.File) IResource(org.eclipse.core.resources.IResource) IFolder(org.eclipse.core.resources.IFolder)

Example 2 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class Util method isExcluded.

/*
 * Returns whether the given java element is exluded from its root's classpath.
 * It doesn't check whether the root itself is on the classpath or not
 */
public static final boolean isExcluded(IJavaElement element) {
    int elementType = element.getElementType();
    switch(elementType) {
        case IJavaElement.JAVA_MODEL:
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return false;
        case IJavaElement.PACKAGE_FRAGMENT:
            PackageFragmentRoot root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            IResource resource = ((PackageFragment) element).resource();
            return resource != null && isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars());
        case IJavaElement.COMPILATION_UNIT:
            root = (PackageFragmentRoot) element.getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
            resource = element.getResource();
            if (resource == null)
                return false;
            if (isExcluded(resource, root.fullInclusionPatternChars(), root.fullExclusionPatternChars()))
                return true;
            return isExcluded(element.getParent());
        default:
            IJavaElement cu = element.getAncestor(IJavaElement.COMPILATION_UNIT);
            return cu != null && isExcluded(cu);
    }
}
Also used : IPackageFragment(org.eclipse.jdt.core.IPackageFragment) PackageFragment(org.eclipse.jdt.internal.core.PackageFragment) IJavaElement(org.eclipse.jdt.core.IJavaElement) PackageFragmentRoot(org.eclipse.jdt.internal.core.PackageFragmentRoot) IResource(org.eclipse.core.resources.IResource)

Example 3 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class RenameTypeProcessor method createChange.

@Override
public Change createChange(IProgressMonitor monitor) throws CoreException {
    try {
        monitor.beginTask(RefactoringCoreMessages.RenameTypeRefactoring_creating_change, 4);
        String project = null;
        IJavaProject javaProject = fType.getJavaProject();
        if (javaProject != null)
            project = javaProject.getElementName();
        int flags = JavaRefactoringDescriptor.JAR_MIGRATION | JavaRefactoringDescriptor.JAR_REFACTORING | RefactoringDescriptor.STRUCTURAL_CHANGE;
        try {
            if (!Flags.isPrivate(fType.getFlags()))
                flags |= RefactoringDescriptor.MULTI_CHANGE;
            if (fType.isAnonymous() || fType.isLocal())
                flags |= JavaRefactoringDescriptor.JAR_SOURCE_ATTACHMENT;
        } catch (JavaModelException exception) {
            JavaPlugin.log(exception);
        }
        final String description = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description_short, BasicElementLabels.getJavaElementName(fType.getElementName()));
        final String header = Messages.format(RefactoringCoreMessages.RenameTypeProcessor_descriptor_description, new String[] { JavaElementLabels.getElementLabel(fType, JavaElementLabels.ALL_FULLY_QUALIFIED), getNewElementLabel() });
        final String comment = new JDTRefactoringDescriptorComment(project, this, header).asString();
        final RenameJavaElementDescriptor descriptor = RefactoringSignatureDescriptorFactory.createRenameJavaElementDescriptor(IJavaRefactorings.RENAME_TYPE);
        descriptor.setProject(project);
        descriptor.setDescription(description);
        descriptor.setComment(comment);
        descriptor.setFlags(flags);
        descriptor.setJavaElement(fType);
        descriptor.setNewName(getNewElementName());
        descriptor.setUpdateQualifiedNames(fUpdateQualifiedNames);
        descriptor.setUpdateTextualOccurrences(fUpdateTextualMatches);
        descriptor.setUpdateReferences(fUpdateReferences);
        if (//$NON-NLS-1$
        fUpdateQualifiedNames && fFilePatterns != null && !"".equals(fFilePatterns))
            descriptor.setFileNamePatterns(fFilePatterns);
        descriptor.setUpdateSimilarDeclarations(fUpdateSimilarElements);
        descriptor.setMatchStrategy(fRenamingStrategy);
        final DynamicValidationRefactoringChange result = new DynamicValidationRefactoringChange(descriptor, RefactoringCoreMessages.RenameTypeProcessor_change_name);
        if (fChangeManager.containsChangesIn(fType.getCompilationUnit())) {
            TextChange textChange = fChangeManager.get(fType.getCompilationUnit());
            if (textChange instanceof TextFileChange) {
                ((TextFileChange) textChange).setSaveMode(TextFileChange.FORCE_SAVE);
            }
        }
        result.addAll(fChangeManager.getAllChanges());
        if (willRenameCU()) {
            IResource resource = fType.getCompilationUnit().getResource();
            if (resource != null && resource.isLinked()) {
                String ext = resource.getFileExtension();
                String renamedResourceName;
                if (ext == null)
                    renamedResourceName = getNewElementName();
                else
                    renamedResourceName = getNewElementName() + '.' + ext;
                result.add(new RenameResourceChange(fType.getCompilationUnit().getPath(), renamedResourceName));
            } else {
                String renamedCUName = JavaModelUtil.getRenamedCUName(fType.getCompilationUnit(), getNewElementName());
                result.add(new RenameCompilationUnitChange(fType.getCompilationUnit(), renamedCUName));
            }
        }
        monitor.worked(1);
        return result;
    } finally {
        fChangeManager = null;
    }
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) DynamicValidationRefactoringChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationRefactoringChange) RenameResourceChange(org.eclipse.ltk.core.refactoring.resource.RenameResourceChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) RenameJavaElementDescriptor(org.eclipse.jdt.core.refactoring.descriptors.RenameJavaElementDescriptor) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) IResource(org.eclipse.core.resources.IResource) JDTRefactoringDescriptorComment(org.eclipse.jdt.internal.corext.refactoring.JDTRefactoringDescriptorComment) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 4 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class RenameTypeProcessor method createChanges.

private void createChanges(IProgressMonitor pm) throws CoreException {
    try {
        //$NON-NLS-1$
        pm.beginTask("", 12);
        pm.setTaskName(RefactoringCoreMessages.RenameTypeProcessor_creating_changes);
        if (fUpdateReferences)
            addReferenceUpdates(fChangeManager, new SubProgressMonitor(pm, 3));
        // Similar names updates have already been added.
        pm.worked(1);
        IResource resource = fType.getCompilationUnit().getResource();
        // directly. So we have to update the code by ourselves.
        if ((resource != null && resource.isLinked()) || !willRenameCU()) {
            addTypeDeclarationUpdate(fChangeManager);
            pm.worked(1);
            addConstructorRenames(fChangeManager);
            pm.worked(1);
        } else {
            pm.worked(2);
        }
        if (fUpdateTextualMatches) {
            pm.subTask(RefactoringCoreMessages.RenameTypeRefactoring_searching_text);
            TextMatchUpdater.perform(new SubProgressMonitor(pm, 1), RefactoringScopeFactory.create(fType), this, fChangeManager, fReferences);
            if (fUpdateSimilarElements)
                addSimilarElementsTextualUpdates(fChangeManager, new SubProgressMonitor(pm, 3));
        }
    } finally {
        pm.done();
    }
}
Also used : SubProgressMonitor(org.eclipse.core.runtime.SubProgressMonitor) IResource(org.eclipse.core.resources.IResource)

Example 5 with IResource

use of org.eclipse.core.resources.IResource in project che by eclipse.

the class ResourceModifications method getParticipants.

public RefactoringParticipant[] getParticipants(RefactoringStatus status, RefactoringProcessor processor, String[] natures, SharableParticipants shared) {
    List<RefactoringParticipant> result = new ArrayList<RefactoringParticipant>(5);
    if (fDelete != null) {
        DeleteArguments arguments = new DeleteArguments();
        for (Iterator<IResource> iter = fDelete.iterator(); iter.hasNext(); ) {
            DeleteParticipant[] deletes = ParticipantManager.loadDeleteParticipants(status, processor, iter.next(), arguments, natures, shared);
            result.addAll(Arrays.asList(deletes));
        }
    }
    if (fCreate != null) {
        CreateArguments arguments = new CreateArguments();
        for (Iterator<IResource> iter = fCreate.iterator(); iter.hasNext(); ) {
            CreateParticipant[] creates = ParticipantManager.loadCreateParticipants(status, processor, iter.next(), arguments, natures, shared);
            result.addAll(Arrays.asList(creates));
        }
    }
    if (fMove != null) {
        for (int i = 0; i < fMove.size(); i++) {
            Object element = fMove.get(i);
            MoveArguments arguments = fMoveArguments.get(i);
            MoveParticipant[] moves = ParticipantManager.loadMoveParticipants(status, processor, element, arguments, natures, shared);
            result.addAll(Arrays.asList(moves));
        }
    }
    if (fCopy != null) {
        for (int i = 0; i < fCopy.size(); i++) {
            Object element = fCopy.get(i);
            CopyArguments arguments = fCopyArguments.get(i);
            CopyParticipant[] copies = ParticipantManager.loadCopyParticipants(status, processor, element, arguments, natures, shared);
            result.addAll(Arrays.asList(copies));
        }
    }
    if (fRename != null) {
        for (int i = 0; i < fRename.size(); i++) {
            Object resource = fRename.get(i);
            RenameArguments arguments = fRenameArguments.get(i);
            RenameParticipant[] renames = ParticipantManager.loadRenameParticipants(status, processor, resource, arguments, natures, shared);
            result.addAll(Arrays.asList(renames));
        }
    }
    return result.toArray(new RefactoringParticipant[result.size()]);
}
Also used : RefactoringParticipant(org.eclipse.ltk.core.refactoring.participants.RefactoringParticipant) RenameArguments(org.eclipse.ltk.core.refactoring.participants.RenameArguments) ArrayList(java.util.ArrayList) MoveParticipant(org.eclipse.ltk.core.refactoring.participants.MoveParticipant) DeleteParticipant(org.eclipse.ltk.core.refactoring.participants.DeleteParticipant) CreateArguments(org.eclipse.ltk.core.refactoring.participants.CreateArguments) DeleteArguments(org.eclipse.ltk.core.refactoring.participants.DeleteArguments) CopyArguments(org.eclipse.ltk.core.refactoring.participants.CopyArguments) CreateParticipant(org.eclipse.ltk.core.refactoring.participants.CreateParticipant) CopyParticipant(org.eclipse.ltk.core.refactoring.participants.CopyParticipant) MoveArguments(org.eclipse.ltk.core.refactoring.participants.MoveArguments) RenameParticipant(org.eclipse.ltk.core.refactoring.participants.RenameParticipant) IResource(org.eclipse.core.resources.IResource)

Aggregations

IResource (org.eclipse.core.resources.IResource)1508 IFile (org.eclipse.core.resources.IFile)399 CoreException (org.eclipse.core.runtime.CoreException)398 IPath (org.eclipse.core.runtime.IPath)319 IProject (org.eclipse.core.resources.IProject)291 ArrayList (java.util.ArrayList)240 IFolder (org.eclipse.core.resources.IFolder)191 IContainer (org.eclipse.core.resources.IContainer)178 Path (org.eclipse.core.runtime.Path)145 File (java.io.File)125 IStructuredSelection (org.eclipse.jface.viewers.IStructuredSelection)110 IOException (java.io.IOException)104 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)95 IStatus (org.eclipse.core.runtime.IStatus)90 List (java.util.List)85 IJavaProject (org.eclipse.jdt.core.IJavaProject)75 HashSet (java.util.HashSet)71 Status (org.eclipse.core.runtime.Status)66 HashMap (java.util.HashMap)61 ISelection (org.eclipse.jface.viewers.ISelection)61