Example usage for org.eclipse.jdt.core IClasspathEntry getPath

List of usage examples for org.eclipse.jdt.core IClasspathEntry getPath

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IClasspathEntry getPath.

Prototype

IPath getPath();

Source Link

Document

Returns the path of this classpath entry.

Usage

From source file:net.kbserve.pyjdt.properties.models.CPEAbstractContainer.java

License:Open Source License

/**
 * Alias for CPEAbstractContainer.makeStringPath(classpathEntry.getPath())
 * /*from w ww  .j  av  a 2 s.  c om*/
 * @param classpathEntry
 *            the classpath entry
 * @return the string
 * @deprecated Use CPEAbstractContainer.makeStringPath(IPath)
 */
@Deprecated
public static String makeStringPath(IClasspathEntry classpathEntry) {
    return makeStringPath(classpathEntry.getPath());
}

From source file:net.kbserve.pyjdt.properties.models.CPEAbstractContainer.java

License:Open Source License

/**
 * Update the list of children to include the ICPEType contains the giveb IClasspathEntry
 * /*from  ww  w. j  a va  2 s.  com*/
 * @param child
 *            the child
 * @param project
 *            the project
 * @return the iCPE type
 */
public synchronized ICPEType updateChild(IClasspathEntry child, IProject project) {

    String stringPath = makeStringPath(child.getPath());
    ICPEType icp = getChild(stringPath);
    if (icp == null) {
        switch (child.getEntryKind()) {
        case (IClasspathEntry.CPE_CONTAINER):
            icp = new CPEContainer();
            break;
        case (IClasspathEntry.CPE_LIBRARY):
            icp = new CPELibrary();
            break;
        case (IClasspathEntry.CPE_PROJECT):
            icp = new CPEProject();
            break;
        case (IClasspathEntry.CPE_SOURCE):
            icp = new CPESource();
            break;
        case (IClasspathEntry.CPE_VARIABLE):
            icp = new CPEVariable();
            break;
        default:
            throw new UnsupportedOperationException(
                    "Unsupported IClasspathEntry.getEntryKind() = '" + child.getEntryKind() + "' on " + child);
        }
        children.add(icp);
        icp.setPath(stringPath);
        icp.setParent(this.getPath());
    }

    return icp;
}

From source file:net.kbserve.pyjdt.properties.models.CPEContainer.java

License:Open Source License

@Override
public void update(IClasspathEntry classpathEntry, IProject project) {
    super.update(classpathEntry, project);
    IJavaProject javaProject = JavaCore.create(project);
    try {/*from ww  w. ja  v a2 s.co m*/
        IClasspathContainer classpathContainer = JavaCore.getClasspathContainer(classpathEntry.getPath(),
                javaProject);
        setDescription(classpathContainer.getDescription());
        setAvailable(true);
        for (ICPEType child : getChildren()) {
            child.setAvailable(false);
        }
        for (IClasspathEntry containerChild : classpathContainer.getClasspathEntries()) {
            ICPEType child = this.updateChild(containerChild, project);
            child.setAvailable(true);
            child.update(containerChild, project);
        }
    } catch (JavaModelException e) {
        e.printStackTrace();
    }
}

From source file:net.officefloor.eclipse.classpath.ClasspathUtil.java

License:Open Source License

/**
 * Obtains the children of the {@link IJavaElement}.
 * // www. j av  a2  s  .  c  om
 * @param javaElement
 *            {@link IJavaElement}.
 * @return Children.
 */
public static Object[] getChildren(IJavaElement javaElement) {
    try {
        // Children to return
        List<Object> children = new LinkedList<Object>();

        // Handle based on type of java element
        if (javaElement instanceof IJavaProject) {
            IJavaProject javaProject = (IJavaProject) javaElement;

            // Add the package fragment roots on the class path
            IClasspathEntry[] classPath = javaProject.getResolvedClasspath(true);
            for (IClasspathEntry entry : classPath) {
                // Obtain the Package Fragment Root of the class path entry
                IPath entryPath = entry.getPath();
                IPackageFragmentRoot fragmentRoot = javaProject.findPackageFragmentRoot(entryPath);

                // Add the package fragment root
                children.add(fragmentRoot);
            }

        } else if (javaElement instanceof IPackageFragmentRoot) {
            IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) javaElement;

            // Add the package fragment root children
            children.addAll(Arrays.asList(fragmentRoot.getChildren()));
            children.addAll(Arrays.asList(fragmentRoot.getNonJavaResources()));

        } else if (javaElement instanceof IPackageFragment) {
            IPackageFragment fragment = (IPackageFragment) javaElement;

            // Add the fragment children
            children.addAll(Arrays.asList(fragment.getClassFiles()));
            children.addAll(Arrays.asList(fragment.getCompilationUnits()));
            children.addAll(Arrays.asList(fragment.getNonJavaResources()));

        } else if (javaElement instanceof ITypeRoot) {

            // No children of class file

        } else {
            // Unhandled java type
            MessageDialog.openWarning(null, "Unhandled java element type",
                    "Unhandled java element type " + javaElement.getClass().getName());
        }

        // Return the children
        return children.toArray();

    } catch (CoreException ex) {
        MessageDialog.openError(null, "Error", ex.getMessage());
        return new Object[0];
    }
}

From source file:net.rim.ejde.internal.core.BasicClasspathElementChangedListener.java

License:Open Source License

static public boolean hasProjectDependencyProblem(IJavaProject javaProject) {
    IProject project = javaProject.getProject();
    try {//  w  w  w .  j a va 2 s.  c om
        ResourceBuilderUtils.cleanProblemMarkers(project,
                new String[] { IRIMMarker.PROJECT_DEPENDENCY_PROBLEM_MARKER }, IResource.DEPTH_ONE);
    } catch (CoreException e) {
        _log.error(e);
    }
    IClasspathEntry[] classpathEntries = null;
    try {
        classpathEntries = javaProject.getRawClasspath();
    } catch (JavaModelException e) {
        _log.error(e);
        return true;
    }
    IProject dependentProject = null;
    String projectName = null;
    boolean hasDependencyError = false;
    for (IClasspathEntry entry : classpathEntries) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            projectName = entry.getPath().lastSegment();
            dependentProject = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
            if (!isValidDependency(javaProject.getProject(), dependentProject) && !hasDependencyError) {
                hasDependencyError = true;
            }
        }
    }
    return hasDependencyError;
}

From source file:net.rim.ejde.internal.core.BasicClasspathElementChangedListener.java

License:Open Source License

/**
 * Checks if all source files are existing. If not, create them.
 *
 * @param javaProj/*w w w .  j a  v a  2 s  .  c om*/
 */
private void checkSourceFolders(final IJavaProject javaProj) {
    if (javaProj == null)
        return;
    if (javaProj.exists()) {
        try {
            if (!javaProj.isOpen()) {
                javaProj.open(new NullProgressMonitor());
            }
            IClasspathEntry[] entries = javaProj.getRawClasspath();
            for (IClasspathEntry entry : entries) {
                if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
                    IPath path = entry.getPath();
                    final IPath folderPath = path.removeFirstSegments(1);
                    if (!folderPath.isEmpty()) {
                        Display.getDefault().asyncExec(new Runnable() {
                            public void run() {
                                try {
                                    ImportUtils.createFolders(javaProj.getProject(), folderPath,
                                            IResource.FORCE);
                                } catch (CoreException e) {
                                    _log.error(e.getMessage());
                                }
                            }
                        });
                    }
                }
            }
        } catch (JavaModelException e) {
            _log.error("findProjectSources: Could not retrieve project sources:", e); //$NON-NLS-1$
        }
    }
}

From source file:net.rim.ejde.internal.core.RimIDEUtil.java

License:Open Source License

/**
 * Gets a file that exists in an Eclipse project.
 * <p>//from www.  jav a 2s . co  m
 * TODO: Someone can probably optimize this method better. Like using some of the IWorkspaceRoot.find*() methods...
 *
 * @param project
 *            the Eclipse project the file belongs to
 * @param file
 *            the File which is in the Eclipse project
 * @return the Eclipse resource file associated with the file
 */
public static IResource getResource(IProject project, File file) {
    IJavaProject javaProject = JavaCore.create(project);
    IPath filePath = new Path(file.getAbsolutePath());
    try {
        IClasspathEntry[] classpathEntries = javaProject.getResolvedClasspath(true);

        IFile input = null;
        // Look for a source folder
        for (IClasspathEntry classpathEntry : classpathEntries) {
            if (classpathEntry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {

                // Try to resolve the source container
                IWorkspaceRoot workspaceRoot = project.getWorkspace().getRoot();
                IResource resource = workspaceRoot.findMember(classpathEntry.getPath());
                if (resource instanceof IContainer) {
                    try {
                        IContainer sourceContainer = (IContainer) resource;
                        File sourceContainerFile = EFS.getStore(resource.getLocationURI()).toLocalFile(EFS.NONE,
                                null);
                        IPath sourceFolderPath = new Path(sourceContainerFile.getAbsolutePath());

                        // See if the file path is within this source folder
                        // path
                        if (sourceFolderPath.isPrefixOf(filePath)) {
                            int segmentCount = sourceFolderPath.segmentCount();
                            IPath relativePath = filePath.removeFirstSegments(segmentCount);
                            input = sourceContainer.getFile(relativePath);

                            break;
                        }
                    } catch (CoreException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        return input;
    } catch (JavaModelException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:net.rim.ejde.internal.packaging.PackagingManager.java

License:Open Source License

static private void getCompileImportsRecusively(IClasspathEntry[] entries, IJavaProject jProject,
        Vector<ImportedJar> imports, boolean isMainProject) throws CoreException {
    if (imports == null) {
        imports = new Vector<ImportedJar>();
    }//from  w  w  w . j a v a 2  s. com
    // Workspace imports; if there aren't any specified, default to
    // using the runtime libraries.
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    // String jarPathString;
    try {
        BlackBerryProperties properties = null;
        boolean needAddBBJar = false;
        IPath jarPath = null;
        ImportedJar importedJar = null;
        for (IClasspathEntry entry : entries) {
            switch (entry.getEntryKind()) {
            case IClasspathEntry.CPE_CONTAINER: {
                // libraries
                IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(),
                        jProject.getJavaProject());
                if (container == null) {
                    continue;
                }

                IVMInstall containerVM;
                if (!(container instanceof JREContainer)) {
                    // We need to verify the type of the container because the path of Maven container only has one
                    // segment and JavaRuntime.getVMInstall(IPath) return the default VM install if the entry path has one
                    // segment.
                    containerVM = null;
                } else {
                    containerVM = JavaRuntime.getVMInstall(entry.getPath());
                }

                try {
                    if (containerVM != null) {
                        if (containerVM.getVMInstallType().getId().equals(BlackBerryVMInstallType.VM_ID)) {
                            if (isMainProject) {
                                // Add jars to a list
                                IClasspathEntry[] classpathEntries = container.getClasspathEntries();
                                if (classpathEntries != null && classpathEntries.length > 0) {
                                    getCompileImportsRecusively(classpathEntries, jProject, imports, false);
                                }
                            }
                        } else {
                            if (!jProject.getProject().hasNature(BlackBerryProjectCoreNature.NATURE_ID)) {
                                needAddBBJar = true;
                                continue;
                            }
                        }
                    } else {
                        // Add jars to a list
                        IClasspathEntry[] classpathEntries = container.getClasspathEntries();
                        if (classpathEntries != null && classpathEntries.length > 0) {
                            getCompileImportsRecusively(classpathEntries, jProject, imports, false);
                        }
                    }
                } catch (CoreException e) {
                    _log.error(e.getMessage());
                    continue;
                }
                break;
            }
            case IClasspathEntry.CPE_LIBRARY: {
                // imported jars
                jarPath = PackageUtils.getAbsoluteEntryPath(entry);
                // the jar path can be null if the jar file does not exist
                if (jarPath == null) {
                    throw new CoreException(StatusFactory.createErrorStatus(
                            NLS.bind(Messages.PackagingManager_Entry_Not_Found_MSG, entry.getPath())));
                }
                if (jarPath.lastSegment().equals(IConstants.RIM_API_JAR) && needAddBBJar) {
                    needAddBBJar = false;
                }

                importedJar = null;
                if (PackagingUtils.getPackagExportedJar()) {
                    if (entry.isExported()) {
                        if (isMainProject) {
                            // if the exported jar is not in the main project but a dependent project, the classes it
                            // contains are packaged into the dependent project jar. We don't add it to classpath.
                            importedJar = new ImportedJar(jarPath.toOSString(), true,
                                    getJarFileType(jarPath.toFile()));
                        }
                    } else {
                        importedJar = new ImportedJar(jarPath.toOSString(), false,
                                getJarFileType(jarPath.toFile()));
                    }
                } else {
                    importedJar = new ImportedJar(jarPath.toOSString(), false,
                            getJarFileType(jarPath.toFile()));
                }
                if (importedJar != null && !existingJar(imports, importedJar)) {
                    imports.add(importedJar);
                }
                break;
            }
            case IClasspathEntry.CPE_PROJECT: {
                // dependency projects
                IProject project = workspaceRoot.getProject(entry.getPath().toString());
                IJavaProject javaProject = JavaCore.create(project);
                try {
                    if (project.hasNature(BlackBerryProjectCoreNature.NATURE_ID)) {
                        properties = ContextManager.PLUGIN.getBBProperties(javaProject.getProject().getName(),
                                false);
                        if (properties == null) {
                            _log.error("BlackBerry properties is null");
                            break;
                        }
                    } else {
                        properties = BlackBerryPropertiesFactory.createBlackBerryProperties(javaProject);
                    }
                } catch (CoreException e) {
                    _log.error(e.getMessage());
                    continue;
                }
                if (PackagingManager.getProjectTypeID(properties._application.getType()) == Project.LIBRARY) {
                    IPath absoluteJarPath = PackagingUtils
                            .getAbsoluteStandardOutputFilePath(new BlackBerryProject(javaProject, properties));
                    File jarFile = new File(
                            absoluteJarPath.toOSString() + IConstants.DOT_MARK + IConstants.JAR_EXTENSION);
                    importedJar = new ImportedJar(jarFile.getAbsolutePath(), false, getJarFileType(jarFile));
                    if (!existingJar(imports, importedJar)) {
                        imports.add(importedJar);
                    }
                    IClasspathEntry[] subEntries = javaProject.getRawClasspath();
                    if (subEntries != null && subEntries.length > 0) {
                        getCompileImportsRecusively(subEntries, javaProject, imports, false);
                    }
                }
                break;
            }
            case IClasspathEntry.CPE_VARIABLE: {
                // variables
                String e = entry.getPath().toString();
                int index = e.indexOf('/');
                if (index == -1) {
                    index = e.indexOf('\\');
                }
                String variable = e;
                IPath cpvar = JavaCore.getClasspathVariable(variable);
                if (cpvar == null) {
                    String msg = NLS.bind(Messages.PackagingManager_Variable_Not_Defined_MSG, variable);
                    throw new CoreException(StatusFactory.createErrorStatus(msg));
                }
                if (cpvar.lastSegment().equals(IConstants.RIM_API_JAR) && needAddBBJar) {
                    needAddBBJar = false;
                }
                // TODO RAPC does not support a class folder. We may support it later on
                if (cpvar.lastSegment().endsWith("." + IConstants.JAR_EXTENSION)) {
                    importedJar = new ImportedJar(cpvar.toOSString(), false, getJarFileType(cpvar.toFile()));
                    if (!existingJar(imports, importedJar)) {
                        imports.add(importedJar);
                    }
                }
                break;
            }
            }
        }
        if (needAddBBJar && isMainProject) {
            // insert the default BB jre lib if needed
            IVMInstall bbVM = VMUtils.getDefaultBBVM();
            if (bbVM != null) {
                LibraryLocation[] libLocations = bbVM.getLibraryLocations();
                if (libLocations != null) {
                    for (LibraryLocation location : libLocations) {
                        importedJar = new ImportedJar(location.getSystemLibraryPath().toOSString(), false,
                                getJarFileType(location.getSystemLibraryPath().toFile()));
                        if (!existingJar(imports, importedJar)) {
                            imports.add(importedJar);
                        }
                    }
                }
            }
        }
    } catch (JavaModelException e) {
        _log.error(e.getMessage());
    }
}

From source file:net.rim.ejde.internal.sourcelookup.RIMSourcePathProvider.java

License:Open Source License

/**
 * Computes and returns the default unresolved runtime classpath for the given project.
 *
 * @return runtime classpath entries//from  w w w  .  ja v  a  2 s.c o m
 * @exception CoreException
 *                if unable to compute the runtime classpath
 * @see IRuntimeClasspathEntry
 */
public static IRuntimeClasspathEntry[] computeUnresolvedRuntimeClasspath(IJavaProject project)
        throws CoreException {
    IClasspathEntry[] entries = project.getRawClasspath();
    List<IRuntimeClasspathEntry> classpathEntries = new ArrayList<IRuntimeClasspathEntry>();
    for (int i = 0; i < entries.length; i++) {
        IClasspathEntry entry = entries[i];
        switch (entry.getEntryKind()) {
        case IClasspathEntry.CPE_CONTAINER:
            IClasspathContainer container = JavaCore.getClasspathContainer(entry.getPath(), project);
            if (container != null) {
                switch (container.getKind()) {
                case IClasspathContainer.K_APPLICATION:
                    // don't look at application entries
                    break;
                case IClasspathContainer.K_DEFAULT_SYSTEM:
                    classpathEntries.add(JavaRuntime.newRuntimeContainerClasspathEntry(container.getPath(),
                            IRuntimeClasspathEntry.STANDARD_CLASSES, project));
                    break;
                case IClasspathContainer.K_SYSTEM:
                    classpathEntries.add(JavaRuntime.newRuntimeContainerClasspathEntry(container.getPath(),
                            IRuntimeClasspathEntry.BOOTSTRAP_CLASSES, project));
                    break;
                }
            }
            break;
        case IClasspathEntry.CPE_VARIABLE:
            if (JavaRuntime.JRELIB_VARIABLE.equals(entry.getPath().segment(0))) {
                IRuntimeClasspathEntry jre = JavaRuntime.newVariableRuntimeClasspathEntry(entry.getPath());
                jre.setClasspathProperty(IRuntimeClasspathEntry.STANDARD_CLASSES);
                classpathEntries.add(jre);
            }
            break;
        default:
            break;
        }
    }
    classpathEntries.add(JavaRuntime.newDefaultProjectClasspathEntry(project));
    return classpathEntries.toArray(new IRuntimeClasspathEntry[classpathEntries.size()]);
}

From source file:net.rim.ejde.internal.util.ImportUtils.java

License:Open Source License

/**
 * Applies the Java Path exclusion patterns to a given project and returns the list of filtered IClasspathEntry
 *
 * @param eclipseProject/*from w  w w.j av a 2  s .  com*/
 * @param legacyProject
 * @param originalClasspathEntries
 * @return
 */
static public List<IClasspathEntry> applyExclusionPatterns(IProject eclipseProject, Project legacyProject,
        List<IClasspathEntry> originalClasspathEntries) {
    if (null == eclipseProject) {
        throw new IllegalArgumentException("Can't process undefined Eclipse project!");
    }

    if (null == legacyProject) {
        throw new IllegalArgumentException("Can't process undefined legacy project!");
    }

    if (null == originalClasspathEntries) {
        throw new IllegalArgumentException("Can't process undefined Eclipse classpath entries!");
    }

    // TODO: call this when importing projects, rather than from the
    // Compilation Participant
    List<WorkspaceFile> excludedWorkspaceFiles = getFilesToBeExcluded(legacyProject);

    if (excludedWorkspaceFiles.isEmpty() && originalClasspathEntries.isEmpty()) {
        return originalClasspathEntries;
    }

    List<IClasspathEntry> excludedClasspathEntries = new ArrayList<IClasspathEntry>();
    HashMap<IPath, IClasspathEntry> filterMap = new HashMap<IPath, IClasspathEntry>();
    String projectNamePattern = IPath.SEPARATOR + eclipseProject.getName() + IPath.SEPARATOR;
    List<IPath> exclusionPatterns;
    IPath classpathEntryPath, exclusionPatternPath;
    boolean forProject;
    String lastSegment;
    IFolder folder;
    IPath srcLocation;
    IClasspathEntry newEntry;
    IPath[] excludedPaths;
    File file;
    String workspaceFilePath;
    String packageId;

    for (IClasspathEntry entry : originalClasspathEntries) {
        exclusionPatterns = new ArrayList<IPath>();

        classpathEntryPath = entry.getPath();

        if (IClasspathEntry.CPE_SOURCE == entry.getEntryKind()) {
            lastSegment = classpathEntryPath.lastSegment();

            if (lastSegment.equalsIgnoreCase(
                    ImportUtils.getImportPref(ResourceBuilder.LOCALE_INTERFACES_FOLDER_NAME))) {
                continue;
            }

            folder = eclipseProject.getFolder(lastSegment);

            if (folder.isDerived() || !folder.exists()) {
                continue;
            }

            forProject = classpathEntryPath.toString().startsWith(projectNamePattern);

            if (forProject) {
                srcLocation = folder.getLocation();

                if (srcLocation == null || srcLocation.isEmpty()) {
                    return originalClasspathEntries;
                }

                for (WorkspaceFile workspaceFile : excludedWorkspaceFiles) {
                    workspaceFilePath = workspaceFile.toString();
                    file = workspaceFile.getFile();

                    if (null != file && file.exists() && file.isFile()) {
                        // Fix for IDT 149988 - Check type of source folder and file type to prevent duplication for exclusion
                        // patterns
                        if (lastSegment.equalsIgnoreCase(
                                ImportUtils.getImportPref(LegacyImportHelper.PROJECT_SRC_FOLDER_NAME_KEY))) {
                            if (!workspaceFile.getIsJava()) {
                                continue;
                            }
                        } else {
                            if (workspaceFile.getIsJava()) {
                                continue;
                            }
                        }

                        if (workspaceFile.getIsJava() || workspaceFile.getIsResourceHeader()
                                || workspaceFile.getIsResource()) {
                            packageId = IConstants.EMPTY_STRING;
                            try {
                                packageId = PackageUtils.getFilePackageString(file, legacyProject);
                            } catch (CoreException e) {
                                _log.error(e.getMessage());
                                packageId = IConstants.EMPTY_STRING;
                            }
                            workspaceFilePath = File.separator + packageId + File.separator + workspaceFilePath;
                        }
                        exclusionPatternPath = getExclusionPattern(workspaceFile, lastSegment, eclipseProject,
                                legacyProject);

                        if (!exclusionPatternPath.isEmpty()) {
                            exclusionPatterns.add(exclusionPatternPath);
                        }
                    }
                }
            }

            if (exclusionPatterns.isEmpty()) {
                excludedPaths = new IPath[] {};
            } else {
                excludedPaths = exclusionPatterns.toArray(new IPath[exclusionPatterns.size()]);
            }

            newEntry = JavaCore.newSourceEntry(classpathEntryPath, entry.getInclusionPatterns(), excludedPaths,
                    entry.getOutputLocation(), entry.getExtraAttributes());
            filterMap.put(classpathEntryPath, newEntry);
        } else {// IClasspathEntry of type other than CPE_SOURCE
            filterMap.put(classpathEntryPath, entry);
        }
    }

    IPath elementPath;

    for (IClasspathEntry element : originalClasspathEntries) {
        elementPath = element.getPath();
        newEntry = filterMap.get(elementPath);
        if (null != newEntry) {
            excludedClasspathEntries.add(newEntry);
        }
    }

    return excludedClasspathEntries;
}