Search in sources :

Example 46 with IPath

use of org.eclipse.core.runtime.IPath in project flux by eclipse.

the class JavaModelUtil method findTypeContainer.

/**
	 * Finds a type container by container name. The returned element will be of type
	 * <code>IType</code> or a <code>IPackageFragment</code>. <code>null</code> is returned if the
	 * type container could not be found.
	 * 
	 * @param jproject The Java project defining the context to search
	 * @param typeContainerName A dot separated name of the type container
	 * @return returns the container
	 * @throws JavaModelException thrown when the project can not be accessed
	 * @see #getTypeContainerName(IType)
	 */
public static IJavaElement findTypeContainer(IJavaProject jproject, String typeContainerName) throws JavaModelException {
    // try to find it as type
    IJavaElement result = jproject.findType(typeContainerName);
    if (result == null) {
        // find it as package
        IPath path = new Path(typeContainerName.replace('.', '/'));
        result = jproject.findElement(path);
        if (!(result instanceof IPackageFragment)) {
            result = null;
        }
    }
    return result;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IJavaElement(org.eclipse.jdt.core.IJavaElement) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) IPath(org.eclipse.core.runtime.IPath)

Example 47 with IPath

use of org.eclipse.core.runtime.IPath in project che by eclipse.

the class ClasspathEntry method fullExclusionPatternChars.

/*
     * Returns a char based representation of the exclusions patterns full path.
     */
public char[][] fullExclusionPatternChars() {
    if (this.fullExclusionPatternChars == UNINIT_PATTERNS) {
        int length = this.exclusionPatterns.length;
        this.fullExclusionPatternChars = new char[length][];
        IPath prefixPath = this.path.removeTrailingSeparator();
        for (int i = 0; i < length; i++) {
            this.fullExclusionPatternChars[i] = prefixPath.append(this.exclusionPatterns[i]).toString().toCharArray();
        }
    }
    return this.fullExclusionPatternChars;
}
Also used : IPath(org.eclipse.core.runtime.IPath)

Example 48 with IPath

use of org.eclipse.core.runtime.IPath in project che by eclipse.

the class ClasspathEntry method validateClasspathEntry.

//	/**
//	 * Returns a Java model status describing the problem related to this classpath entry if any,
//	 * a status object with code <code>IStatus.OK</code> if the entry is fine (that is, if the
//	 * given classpath entry denotes a valid element to be referenced onto a classpath).
//	 *
//	 * @param project the given java project
//	 * @param entry the given classpath entry
//	 * @param checkSourceAttachment a flag to determine if source attachment should be checked
//	 * @param referredByContainer flag indicating whether the given entry is referred by a classpath container
//	 * @return a java model status describing the problem related to this classpath entry if any, a status object with code <code>IStatus
// .OK</code> if the entry is fine
//	 */
//	public static IJavaModelStatus validateClasspathEntry(IJavaProject project, IClasspathEntry entry, boolean checkSourceAttachment,
// boolean referredByContainer){
//		if (entry.getEntryKind() == IClasspathEntry.CPE_LIBRARY) {
//			JavaModelManager.getJavaModelManager().removeFromInvalidArchiveCache(entry.getPath());
//		}
//		IJavaModelStatus status = validateClasspathEntry(project, entry, null, checkSourceAttachment, referredByContainer);
//		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=171136 and https://bugs.eclipse.org/bugs/show_bug.cgi?id=300136
//		// Ignore class path errors from optional entries.
//		int statusCode = status.getCode();
//		if ( (statusCode == IJavaModelStatusConstants.INVALID_CLASSPATH ||
//				statusCode == IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND ||
//				statusCode == IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND ||
//				statusCode == IJavaModelStatusConstants.INVALID_PATH) &&
//				((ClasspathEntry) entry).isOptional())
//			return JavaModelStatus.VERIFIED_OK;
//		return status;
//	}
private static IJavaModelStatus validateClasspathEntry(IJavaProject project, IClasspathEntry entry, IClasspathContainer entryContainer, boolean checkSourceAttachment, boolean referredByContainer) {
    IPath path = entry.getPath();
    // Build some common strings for status message
    String projectName = project.getElementName();
    String entryPathMsg = projectName.equals(path.segment(0)) ? path.removeFirstSegments(1).makeRelative().toString() : path.toString();
    switch(entry.getEntryKind()) {
        // container entry check
        case IClasspathEntry.CPE_CONTAINER:
            if (path.segmentCount() >= 1) {
                try {
                    IJavaModelStatus status = null;
                    // Validate extra attributes
                    IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
                    if (extraAttributes != null) {
                        int length = extraAttributes.length;
                        HashSet set = new HashSet(length);
                        for (int i = 0; i < length; i++) {
                            String attName = extraAttributes[i].getName();
                            if (!set.add(attName)) {
                                status = new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.classpath_duplicateEntryExtraAttribute, new String[] { attName, entryPathMsg, projectName }));
                                break;
                            }
                        }
                    }
                    IClasspathContainer container = JavaModelManager.getJavaModelManager().getClasspathContainer(path, project);
                    // container retrieval is performing validation check on container entry kinds.
                    if (container == null) {
                        if (status != null)
                            return status;
                        return new JavaModelStatus(IJavaModelStatusConstants.CP_CONTAINER_PATH_UNBOUND, project, path);
                    } else if (container == JavaModelManager.CONTAINER_INITIALIZATION_IN_PROGRESS) {
                        // don't create a marker if initialization is in progress (case of cp initialization batching)
                        return JavaModelStatus.VERIFIED_OK;
                    }
                    IClasspathEntry[] containerEntries = container.getClasspathEntries();
                    if (containerEntries != null) {
                        for (int i = 0, length = containerEntries.length; i < length; i++) {
                            IClasspathEntry containerEntry = containerEntries[i];
                            int kind = containerEntry == null ? 0 : containerEntry.getEntryKind();
                            if (containerEntry == null || kind == IClasspathEntry.CPE_SOURCE || kind == IClasspathEntry.CPE_VARIABLE || kind == IClasspathEntry.CPE_CONTAINER) {
                                return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CP_CONTAINER_ENTRY, project, path);
                            }
                            IJavaModelStatus containerEntryStatus = validateClasspathEntry(project, containerEntry, container, checkSourceAttachment, true);
                            if (!containerEntryStatus.isOK()) {
                                return containerEntryStatus;
                            }
                        }
                    }
                } catch (JavaModelException e) {
                    return new JavaModelStatus(e);
                }
            } else {
                return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_illegalContainerPath, new String[] { entryPathMsg, projectName }));
            }
            break;
        // variable entry check
        case IClasspathEntry.CPE_VARIABLE:
            if (path.segmentCount() >= 1) {
                try {
                    entry = JavaCore.getResolvedClasspathEntry(entry);
                } catch (AssertionFailedException e) {
                    // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=55992
                    return new JavaModelStatus(IJavaModelStatusConstants.INVALID_PATH, e.getMessage());
                }
                if (entry == null) {
                    return new JavaModelStatus(IJavaModelStatusConstants.CP_VARIABLE_PATH_UNBOUND, project, path);
                }
                // get validation status
                IJavaModelStatus status = validateClasspathEntry(project, entry, null, checkSourceAttachment, false);
                if (!status.isOK())
                    return status;
                // return deprecation status if any
                String variableName = path.segment(0);
                String deprecatedMessage = JavaCore.getClasspathVariableDeprecationMessage(variableName);
                if (deprecatedMessage != null) {
                    return new JavaModelStatus(IStatus.WARNING, IJavaModelStatusConstants.DEPRECATED_VARIABLE, project, path, deprecatedMessage);
                }
                return status;
            } else {
                return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_illegalVariablePath, new String[] { entryPathMsg, projectName }));
            }
        // library entry check
        case IClasspathEntry.CPE_LIBRARY:
            path = ClasspathEntry.resolveDotDot(project.getProject().getLocation(), path);
            // do not validate entries from Class-Path: in manifest
            // (these entries are considered optional since the user cannot act on them)
            // see https://bugs.eclipse.org/bugs/show_bug.cgi?id=252392
            String containerInfo = null;
            if (entryContainer != null) {
                if (entryContainer instanceof UserLibraryClasspathContainer) {
                    containerInfo = Messages.bind(Messages.classpath_userLibraryInfo, new String[] { entryContainer.getDescription() });
                } else {
                    containerInfo = Messages.bind(Messages.classpath_containerInfo, new String[] { entryContainer.getDescription() });
                }
            }
            IJavaModelStatus status = validateLibraryEntry(path, project, containerInfo, checkSourceAttachment ? entry.getSourceAttachmentPath() : null, entryPathMsg);
            // https://bugs.eclipse.org/bugs/show_bug.cgi?id=171136, ignore class path errors from optional entries
            if (status.getCode() == IJavaModelStatusConstants.INVALID_CLASSPATH && ((ClasspathEntry) entry).isOptional())
                status = JavaModelStatus.VERIFIED_OK;
            if (!status.isOK())
                return status;
            break;
        // project entry check
        case IClasspathEntry.CPE_PROJECT:
            if (path.isAbsolute() && path.segmentCount() == 1) {
                IProject prereqProjectRsc = workspaceRoot.getProject(path.segment(0));
                IJavaProject prereqProject = JavaCore.create(prereqProjectRsc);
                try {
                    if (!prereqProjectRsc.exists() || !prereqProjectRsc.hasNature(JavaCore.NATURE_ID)) {
                        return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_unboundProject, new String[] { path.segment(0), projectName }));
                    }
                    if (!prereqProjectRsc.isOpen()) {
                        return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_closedProject, new String[] { path.segment(0) }));
                    }
                    if (!JavaCore.IGNORE.equals(project.getOption(JavaCore.CORE_INCOMPATIBLE_JDK_LEVEL, true))) {
                        long projectTargetJDK = CompilerOptions.versionToJdkLevel(project.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true));
                        long prereqProjectTargetJDK = CompilerOptions.versionToJdkLevel(prereqProject.getOption(JavaCore.COMPILER_CODEGEN_TARGET_PLATFORM, true));
                        if (prereqProjectTargetJDK > projectTargetJDK) {
                            return new JavaModelStatus(IJavaModelStatusConstants.INCOMPATIBLE_JDK_LEVEL, project, path, Messages.bind(Messages.classpath_incompatibleLibraryJDKLevel, new String[] { project.getElementName(), CompilerOptions.versionFromJdkLevel(projectTargetJDK), path.makeRelative().toString(), CompilerOptions.versionFromJdkLevel(prereqProjectTargetJDK) }));
                        }
                    }
                } catch (CoreException e) {
                    return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_unboundProject, new String[] { path.segment(0), projectName }));
                }
            } else {
                return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_illegalProjectPath, new String[] { path.toString(), projectName }));
            }
            break;
        // project source folder
        case IClasspathEntry.CPE_SOURCE:
            if (((entry.getInclusionPatterns() != null && entry.getInclusionPatterns().length > 0) || (entry.getExclusionPatterns() != null && entry.getExclusionPatterns().length > 0)) && JavaCore.DISABLED.equals(project.getOption(JavaCore.CORE_ENABLE_CLASSPATH_EXCLUSION_PATTERNS, true))) {
                return new JavaModelStatus(IJavaModelStatusConstants.DISABLED_CP_EXCLUSION_PATTERNS, project, path);
            }
            if (entry.getOutputLocation() != null && JavaCore.DISABLED.equals(project.getOption(JavaCore.CORE_ENABLE_CLASSPATH_MULTIPLE_OUTPUT_LOCATIONS, true))) {
                return new JavaModelStatus(IJavaModelStatusConstants.DISABLED_CP_MULTIPLE_OUTPUT_LOCATIONS, project, path);
            }
            if (path.isAbsolute() && !path.isEmpty()) {
                IPath projectPath = project.getProject().getFullPath();
                if (!projectPath.isPrefixOf(path) || JavaModel.getTarget(path, true) == null) {
                    return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_unboundSourceFolder, new String[] { entryPathMsg, projectName }));
                }
            } else {
                return new JavaModelStatus(IJavaModelStatusConstants.INVALID_CLASSPATH, Messages.bind(Messages.classpath_illegalSourceFolderPath, new String[] { entryPathMsg, projectName }));
            }
            break;
    }
    // Validate extra attributes
    IClasspathAttribute[] extraAttributes = entry.getExtraAttributes();
    if (extraAttributes != null) {
        int length = extraAttributes.length;
        HashSet set = new HashSet(length);
        for (int i = 0; i < length; i++) {
            String attName = extraAttributes[i].getName();
            if (!set.add(attName)) {
                return new JavaModelStatus(IJavaModelStatusConstants.NAME_COLLISION, Messages.bind(Messages.classpath_duplicateEntryExtraAttribute, new String[] { attName, entryPathMsg, projectName }));
            }
        }
    }
    return JavaModelStatus.VERIFIED_OK;
}
Also used : JavaModelException(org.eclipse.jdt.core.JavaModelException) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaModelStatus(org.eclipse.jdt.core.IJavaModelStatus) IProject(org.eclipse.core.resources.IProject) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IJavaProject(org.eclipse.jdt.core.IJavaProject) CoreException(org.eclipse.core.runtime.CoreException) IJavaModelStatus(org.eclipse.jdt.core.IJavaModelStatus) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer) HashSet(java.util.HashSet)

Example 49 with IPath

use of org.eclipse.core.runtime.IPath in project che by eclipse.

the class ClasspathEntry method resolvedChainedLibraries.

/*
     * Read the Class-Path clause of the manifest of the jar pointed by this path, and return
     * the corresponding paths.
     */
public static IPath[] resolvedChainedLibraries(IPath jarPath) {
    ArrayList result = new ArrayList();
    resolvedChainedLibraries(jarPath, new HashSet(), result);
    if (result.size() == 0)
        return NO_PATHS;
    return (IPath[]) result.toArray(new IPath[result.size()]);
}
Also used : IPath(org.eclipse.core.runtime.IPath) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 50 with IPath

use of org.eclipse.core.runtime.IPath in project che by eclipse.

the class ClasspathEntry method resolvedChainedLibraries.

private static void resolvedChainedLibraries(IPath jarPath, HashSet visited, ArrayList result) {
    if (visited.contains(jarPath))
        return;
    visited.add(jarPath);
    JavaModelManager manager = JavaModelManager.getJavaModelManager();
    if (manager.isNonChainingJar(jarPath))
        return;
    List calledFileNames = getCalledFileNames(jarPath);
    if (calledFileNames == null) {
        manager.addNonChainingJar(jarPath);
    } else {
        Iterator calledFilesIterator = calledFileNames.iterator();
        IPath directoryPath = jarPath.removeLastSegments(1);
        while (calledFilesIterator.hasNext()) {
            String calledFileName = (String) calledFilesIterator.next();
            if (!directoryPath.isValidPath(calledFileName)) {
                if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                    Util.verbose("Invalid Class-Path entry " + calledFileName + " in manifest of jar file: " + //$NON-NLS-1$ //$NON-NLS-2$
                    jarPath.toOSString());
                }
            } else {
                IPath calledJar = directoryPath.append(new Path(calledFileName));
                // Ignore if segment count is Zero (https://bugs.eclipse.org/bugs/show_bug.cgi?id=308150)
                if (calledJar.segmentCount() == 0) {
                    if (JavaModelManager.CP_RESOLVE_VERBOSE_FAILURE) {
                        Util.verbose("Invalid Class-Path entry " + calledFileName + " in manifest of jar file: " + //$NON-NLS-1$ //$NON-NLS-2$
                        jarPath.toOSString());
                    }
                    continue;
                }
                resolvedChainedLibraries(calledJar, visited, result);
                result.add(calledJar);
            }
        }
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) Iterator(java.util.Iterator) List(java.util.List) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList)

Aggregations

IPath (org.eclipse.core.runtime.IPath)800 Path (org.eclipse.core.runtime.Path)219 File (java.io.File)161 IFile (org.eclipse.core.resources.IFile)150 CoreException (org.eclipse.core.runtime.CoreException)135 IResource (org.eclipse.core.resources.IResource)112 IOException (java.io.IOException)100 ArrayList (java.util.ArrayList)92 IProject (org.eclipse.core.resources.IProject)85 IFolder (org.eclipse.core.resources.IFolder)80 Test (org.junit.Test)72 IWorkspaceRoot (org.eclipse.core.resources.IWorkspaceRoot)58 IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)55 IStatus (org.eclipse.core.runtime.IStatus)48 InputStream (java.io.InputStream)44 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)42 Status (org.eclipse.core.runtime.Status)36 IJavaProject (org.eclipse.jdt.core.IJavaProject)36 URL (java.net.URL)33 IContainer (org.eclipse.core.resources.IContainer)33