Search in sources :

Example 91 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathEntry method elementDecode.

public static IClasspathEntry elementDecode(Element element, IJavaProject project, Map unknownElements) {
    IPath projectPath = project.getProject().getFullPath();
    NamedNodeMap attributes = element.getAttributes();
    NodeList children = element.getChildNodes();
    boolean[] foundChildren = new boolean[children.getLength()];
    String kindAttr = removeAttribute(TAG_KIND, attributes);
    String pathAttr = removeAttribute(TAG_PATH, attributes);
    // ensure path is absolute
    IPath path = new Path(pathAttr);
    int kind = kindFromString(kindAttr);
    if (kind != IClasspathEntry.CPE_VARIABLE && kind != IClasspathEntry.CPE_CONTAINER && !path.isAbsolute()) {
        if (!(path.segmentCount() > 0 && path.segment(0).equals(org.eclipse.jdt.internal.core.ClasspathEntry.DOT_DOT))) {
            path = projectPath.append(path);
        }
    }
    // source attachment info (optional)
    IPath sourceAttachmentPath = element.hasAttribute(TAG_SOURCEPATH) ? new Path(removeAttribute(TAG_SOURCEPATH, attributes)) : null;
    if (kind != IClasspathEntry.CPE_VARIABLE && sourceAttachmentPath != null && !sourceAttachmentPath.isAbsolute()) {
        sourceAttachmentPath = projectPath.append(sourceAttachmentPath);
    }
    IPath sourceAttachmentRootPath = element.hasAttribute(TAG_ROOTPATH) ? new Path(removeAttribute(TAG_ROOTPATH, attributes)) : null;
    // exported flag (optional)
    //$NON-NLS-1$
    boolean isExported = removeAttribute(TAG_EXPORTED, attributes).equals("true");
    // inclusion patterns (optional)
    IPath[] inclusionPatterns = decodePatterns(attributes, TAG_INCLUDING);
    if (inclusionPatterns == null)
        inclusionPatterns = INCLUDE_ALL;
    // exclusion patterns (optional)
    IPath[] exclusionPatterns = decodePatterns(attributes, TAG_EXCLUDING);
    if (exclusionPatterns == null)
        exclusionPatterns = EXCLUDE_NONE;
    // access rules (optional)
    NodeList attributeList = getChildAttributes(TAG_ACCESS_RULES, children, foundChildren);
    IAccessRule[] accessRules = decodeAccessRules(attributeList);
    // backward compatibility
    if (accessRules == null) {
        accessRules = getAccessRules(inclusionPatterns, exclusionPatterns);
    }
    // combine access rules (optional)
    //$NON-NLS-1$
    boolean combineAccessRestrictions = !removeAttribute(TAG_COMBINE_ACCESS_RULES, attributes).equals("false");
    // extra attributes (optional)
    attributeList = getChildAttributes(TAG_ATTRIBUTES, children, foundChildren);
    IClasspathAttribute[] extraAttributes = decodeExtraAttributes(attributeList);
    // custom output location
    IPath outputLocation = element.hasAttribute(TAG_OUTPUT) ? projectPath.append(removeAttribute(TAG_OUTPUT, attributes)) : null;
    String[] unknownAttributes = null;
    ArrayList unknownChildren = null;
    if (unknownElements != null) {
        // unknown attributes
        int unknownAttributeLength = attributes.getLength();
        if (unknownAttributeLength != 0) {
            unknownAttributes = new String[unknownAttributeLength * 2];
            for (int i = 0; i < unknownAttributeLength; i++) {
                Node attribute = attributes.item(i);
                unknownAttributes[i * 2] = attribute.getNodeName();
                unknownAttributes[i * 2 + 1] = attribute.getNodeValue();
            }
        }
        // unknown children
        for (int i = 0, length = foundChildren.length; i < length; i++) {
            if (!foundChildren[i]) {
                Node node = children.item(i);
                if (node.getNodeType() != Node.ELEMENT_NODE)
                    continue;
                if (unknownChildren == null)
                    unknownChildren = new ArrayList();
                StringBuffer buffer = new StringBuffer();
                decodeUnknownNode(node, buffer, project);
                unknownChildren.add(buffer.toString());
            }
        }
    }
    // recreate the CP entry
    IClasspathEntry entry = null;
    switch(kind) {
        case IClasspathEntry.CPE_PROJECT:
            entry = new org.eclipse.jdt.internal.core.ClasspathEntry(IPackageFragmentRoot.K_SOURCE, IClasspathEntry.CPE_PROJECT, path, // inclusion patterns
            org.eclipse.jdt.internal.core.ClasspathEntry.INCLUDE_ALL, // exclusion patterns
            org.eclipse.jdt.internal.core.ClasspathEntry.EXCLUDE_NONE, // source attachment
            null, // source attachment root
            null, // specific output folder
            null, isExported, accessRules, combineAccessRestrictions, extraAttributes);
            break;
        case IClasspathEntry.CPE_LIBRARY:
            entry = JavaCore.newLibraryEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported);
            break;
        case IClasspathEntry.CPE_SOURCE:
            // must be an entry in this project or specify another project
            String projSegment = path.segment(0);
            if (projSegment != null && path.toOSString().startsWith(project.getProject().getFullPath().toOSString())) {
                // this project
                entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes);
            } else {
                if (path.segmentCount() == 1 || path.segment(0).equals(project.getProject().getFullPath().segment(0))) {
                    // another project
                    entry = JavaCore.newProjectEntry(path, accessRules, combineAccessRestrictions, extraAttributes, isExported);
                } else {
                    // an invalid source folder
                    entry = JavaCore.newSourceEntry(path, inclusionPatterns, exclusionPatterns, outputLocation, extraAttributes);
                }
            }
            break;
        case IClasspathEntry.CPE_VARIABLE:
            entry = JavaCore.newVariableEntry(path, sourceAttachmentPath, sourceAttachmentRootPath, accessRules, extraAttributes, isExported);
            break;
        case IClasspathEntry.CPE_CONTAINER:
            entry = JavaCore.newContainerEntry(path, accessRules, extraAttributes, isExported);
            break;
        case org.eclipse.jdt.internal.core.ClasspathEntry.K_OUTPUT:
            if (!path.isAbsolute())
                return null;
            entry = new org.eclipse.jdt.internal.core.ClasspathEntry(org.eclipse.jdt.internal.core.ClasspathEntry.K_OUTPUT, IClasspathEntry.CPE_LIBRARY, path, INCLUDE_ALL, EXCLUDE_NONE, // source attachment
            null, // source attachment root
            null, // custom output location
            null, false, // no access rules
            null, // no accessible files to combine
            false, NO_EXTRA_ATTRIBUTES);
            break;
        default:
            throw new AssertionFailedException(Messages.bind(Messages.classpath_unknownKind, kindAttr));
    }
    if (unknownAttributes != null || unknownChildren != null) {
        UnknownXmlElements unknownXmlElements = new UnknownXmlElements();
        unknownXmlElements.attributes = unknownAttributes;
        unknownXmlElements.children = unknownChildren;
        unknownElements.put(path, unknownXmlElements);
    }
    return entry;
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) NamedNodeMap(org.w3c.dom.NamedNodeMap) IPath(org.eclipse.core.runtime.IPath) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) IClasspathAttribute(org.eclipse.jdt.core.IClasspathAttribute) IAccessRule(org.eclipse.jdt.core.IAccessRule)

Example 92 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class DeltaProcessingState method getRootInfos.

private HashMap[] getRootInfos(boolean usePreviousSession) {
    HashMap newRoots = new HashMap();
    HashMap newOtherRoots = new HashMap();
    HashMap newSourceAttachments = new HashMap();
    HashMap newProjectDependencies = new HashMap();
    IJavaModel model = manager.getJavaModel();
    IJavaProject[] projects;
    try {
        projects = model.getJavaProjects();
    } catch (JavaModelException e) {
        // nothing can be done
        return null;
    }
    for (int i = 0, length = projects.length; i < length; i++) {
        JavaProject project = (JavaProject) projects[i];
        IClasspathEntry[] classpath;
        try {
            //				if (usePreviousSession) {
            //					PerProjectInfo perProjectInfo = project.getPerProjectInfo();
            //					project.resolveClasspath(perProjectInfo, true/*use previous session values*/, false/*don't add classpath change*/);
            //					classpath = perProjectInfo.resolvedClasspath;
            //				} else {
            classpath = project.getResolvedClasspath();
        //				}
        } catch (JavaModelException e) {
            // continue with next project
            continue;
        }
        for (int j = 0, classpathLength = classpath.length; j < classpathLength; j++) {
            IClasspathEntry entry = classpath[j];
            if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
                // TODO (jerome) reuse handle
                IJavaProject key = model.getJavaProject(entry.getPath().segment(0));
                IJavaProject[] dependents = (IJavaProject[]) newProjectDependencies.get(key);
                if (dependents == null) {
                    dependents = new IJavaProject[] { project };
                } else {
                    int dependentsLength = dependents.length;
                    System.arraycopy(dependents, 0, dependents = new IJavaProject[dependentsLength + 1], 0, dependentsLength);
                    dependents[dependentsLength] = project;
                }
                newProjectDependencies.put(key, dependents);
                continue;
            }
            // root path
            IPath path = entry.getPath();
            if (newRoots.get(path) == null) {
                newRoots.put(path, new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            } else {
                ArrayList rootList = (ArrayList) newOtherRoots.get(path);
                if (rootList == null) {
                    rootList = new ArrayList();
                    newOtherRoots.put(path, rootList);
                }
                rootList.add(new DeltaProcessor.RootInfo(project, path, ((ClasspathEntry) entry).fullInclusionPatternChars(), ((ClasspathEntry) entry).fullExclusionPatternChars(), entry.getEntryKind()));
            }
            // source attachment path
            if (entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY)
                continue;
            String propertyString = null;
            //				try {
            //					propertyString = Util.getSourceAttachmentProperty(path);
            //				} catch (JavaModelException e) {
            //					e.printStackTrace();
            //				}
            IPath sourceAttachmentPath;
            if (propertyString != null) {
                int index = propertyString.lastIndexOf(PackageFragmentRoot.ATTACHMENT_PROPERTY_DELIMITER);
                sourceAttachmentPath = (index < 0) ? new Path(propertyString) : new Path(propertyString.substring(0, index));
            } else {
                sourceAttachmentPath = entry.getSourceAttachmentPath();
            }
            if (sourceAttachmentPath != null) {
                newSourceAttachments.put(sourceAttachmentPath, path);
            }
        }
    }
    return new HashMap[] { newRoots, newOtherRoots, newSourceAttachments, newProjectDependencies };
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) JavaModelException(org.eclipse.jdt.core.JavaModelException) IJavaProject(org.eclipse.jdt.core.IJavaProject) IPath(org.eclipse.core.runtime.IPath) HashMap(java.util.HashMap) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IJavaModel(org.eclipse.jdt.core.IJavaModel)

Example 93 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class JavaModelManager method containerPutIfInitializingWithSameEntries.

public boolean containerPutIfInitializingWithSameEntries(IPath containerPath, IJavaProject[] projects, IClasspathContainer[] respectiveContainers) {
    int projectLength = projects.length;
    if (projectLength != 1)
        return false;
    final IClasspathContainer container = respectiveContainers[0];
    IJavaProject project = projects[0];
    //        // optimize only if initializing, otherwise we are in a regular setContainer(...) call
    if (!containerIsInitializationInProgress(project, containerPath))
        return false;
    IClasspathContainer previousContainer = containerGetDefaultToPreviousSession(project, containerPath);
    if (container == null) {
        if (previousContainer == null) {
            containerPut(project, containerPath, null);
            return true;
        }
        return false;
    }
    final IClasspathEntry[] newEntries = container.getClasspathEntries();
    if (previousContainer == null)
        if (newEntries.length == 0) {
            containerPut(project, containerPath, container);
            return true;
        } else {
            if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                verbose_missbehaving_container(containerPath, projects, respectiveContainers, container, newEntries, null);
            return false;
        }
    final IClasspathEntry[] oldEntries = previousContainer.getClasspathEntries();
    if (oldEntries.length != newEntries.length) {
        if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
            verbose_missbehaving_container(containerPath, projects, respectiveContainers, container, newEntries, oldEntries);
        return false;
    }
    for (int i = 0, length = newEntries.length; i < length; i++) {
        if (newEntries[i] == null) {
            if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                verbose_missbehaving_container(project, containerPath, newEntries);
            return false;
        }
        if (!newEntries[i].equals(oldEntries[i])) {
            if (CP_RESOLVE_VERBOSE || CP_RESOLVE_VERBOSE_FAILURE)
                verbose_missbehaving_container(containerPath, projects, respectiveContainers, container, newEntries, oldEntries);
            return false;
        }
    }
    containerPut(project, containerPath, container);
    return true;
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) IClasspathContainer(org.eclipse.jdt.core.IClasspathContainer)

Example 94 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathService method getClasspath.

/**
     * Returns information about classpath.
     *
     * @param projectPath path to the current project
     * @return list of classpath entries
     * @throws JavaModelException when JavaModel has a failure
     */
@Override
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<ClasspathEntryDto> getClasspath(@QueryParam("projectpath") String projectPath) throws JavaModelException {
    IJavaProject javaProject = model.getJavaProject(projectPath);
    IClasspathEntry[] entries = javaProject.getRawClasspath();
    if (entries.length == 0) {
        return emptyList();
    }
    return convertClasspathEntriesToDTO(javaProject, entries);
}
Also used : IJavaProject(org.eclipse.jdt.core.IJavaProject) IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 95 with IClasspathEntry

use of org.eclipse.jdt.core.IClasspathEntry in project che by eclipse.

the class ClasspathService method convertClasspathEntriesToDTO.

private List<ClasspathEntryDto> convertClasspathEntriesToDTO(IJavaProject javaProject, IClasspathEntry[] entries) throws JavaModelException {
    List<ClasspathEntryDto> entriesDTO = new ArrayList<>(entries.length);
    for (IClasspathEntry entry : entries) {
        ClasspathEntryDto entryDTO = DtoFactory.getInstance().createDto(ClasspathEntryDto.class);
        entryDTO.withEntryKind(entry.getEntryKind()).withPath(entry.getPath().toOSString());
        if (IClasspathEntry.CPE_CONTAINER == entry.getEntryKind()) {
            IClasspathEntry[] subEntries = JavaCore.getClasspathContainer(entry.getPath(), javaProject).getClasspathEntries();
            entryDTO.withExpandedEntries(convertClasspathEntriesToDTO(javaProject, subEntries));
        }
        entriesDTO.add(entryDTO);
    }
    return entriesDTO;
}
Also used : IClasspathEntry(org.eclipse.jdt.core.IClasspathEntry) ArrayList(java.util.ArrayList) ClasspathEntryDto(org.eclipse.che.ide.ext.java.shared.dto.classpath.ClasspathEntryDto)

Aggregations

IClasspathEntry (org.eclipse.jdt.core.IClasspathEntry)123 IPath (org.eclipse.core.runtime.IPath)55 IJavaProject (org.eclipse.jdt.core.IJavaProject)44 ArrayList (java.util.ArrayList)35 Path (org.eclipse.core.runtime.Path)27 JavaModelException (org.eclipse.jdt.core.JavaModelException)23 IProject (org.eclipse.core.resources.IProject)20 IClasspathAttribute (org.eclipse.jdt.core.IClasspathAttribute)16 File (java.io.File)15 IFolder (org.eclipse.core.resources.IFolder)13 IPackageFragmentRoot (org.eclipse.jdt.core.IPackageFragmentRoot)13 CoreException (org.eclipse.core.runtime.CoreException)12 IJavaElement (org.eclipse.jdt.core.IJavaElement)12 JavaProject (org.eclipse.jdt.internal.core.JavaProject)11 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)10 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)8 IOException (java.io.IOException)7 URL (java.net.URL)7