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:com.liferay.ide.project.core.util.ProjectUtil.java

License:Open Source License

private static void fixExtProjectClasspathEntries(IProject project) {
    try {/* ww  w .  java2 s .c  o  m*/
        boolean fixedAttr = false;

        IJavaProject javaProject = JavaCore.create(project);

        List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

        IClasspathEntry[] entries = javaProject.getRawClasspath();

        for (IClasspathEntry entry : entries) {
            IClasspathEntry newEntry = null;

            if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                List<IClasspathAttribute> newAttrs = new ArrayList<IClasspathAttribute>();

                IClasspathAttribute[] attrs = entry.getExtraAttributes();

                if (!CoreUtil.isNullOrEmpty(attrs)) {
                    for (IClasspathAttribute attr : attrs) {
                        IClasspathAttribute newAttr = null;

                        if ("owner.project.facets".equals(attr.getName()) && //$NON-NLS-1$
                                "liferay.plugin".equals(attr.getValue())) //$NON-NLS-1$
                        {
                            newAttr = JavaCore.newClasspathAttribute(attr.getName(), "liferay.ext"); //$NON-NLS-1$
                            fixedAttr = true;
                        } else {
                            newAttr = attr;
                        }

                        newAttrs.add(newAttr);
                    }

                    newEntry = JavaCore.newSourceEntry(entry.getPath(), entry.getInclusionPatterns(),
                            entry.getExclusionPatterns(), entry.getOutputLocation(),
                            newAttrs.toArray(new IClasspathAttribute[0]));
                }
            }

            if (newEntry == null) {
                newEntry = entry;
            }

            newEntries.add(newEntry);
        }

        if (fixedAttr) {
            IProgressMonitor monitor = new NullProgressMonitor();

            javaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), monitor);

            try {
                javaProject.getProject().refreshLocal(IResource.DEPTH_INFINITE, monitor);
            } catch (Exception e) {
                ProjectCore.logError(e);
            }
        }

        fixExtProjectSrcFolderLinks(project);
    } catch (Exception ex) {
        ProjectCore.logError("Exception trying to fix Ext project classpath entries.", ex); //$NON-NLS-1$
    }
}

From source file:com.liferay.ide.project.core.util.ProjectUtil.java

License:Open Source License

/** IDE-270 */
public static void fixExtProjectSrcFolderLinks(IProject extProject) throws JavaModelException {
    if (extProject != null) {
        IJavaProject javaProject = JavaCore.create(extProject);

        if (javaProject != null) {
            final IVirtualComponent c = ComponentCore.createComponent(extProject, false);

            if (c != null) {
                final IVirtualFolder jsrc = c.getRootFolder().getFolder("/WEB-INF/classes"); //$NON-NLS-1$

                if (jsrc != null) {
                    final IClasspathEntry[] cp = javaProject.getRawClasspath();

                    for (int i = 0; i < cp.length; i++) {
                        final IClasspathEntry cpe = cp[i];

                        if (cpe.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
                            if (cpe.getPath().removeFirstSegments(1).segmentCount() > 0) {
                                try {
                                    IFolder srcFolder = ResourcesPlugin.getWorkspace().getRoot()
                                            .getFolder(cpe.getPath());

                                    IVirtualResource[] virtualResource = ComponentCore
                                            .createResources(srcFolder);

                                    // create link for source folder only when it is not mapped
                                    if (virtualResource.length == 0) {
                                        jsrc.createLink(cpe.getPath().removeFirstSegments(1), 0, null);
                                    }/*from  w w  w.  j av  a 2 s.  c o m*/
                                } catch (Exception e) {
                                    ProjectCore.logError(e);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:com.liferay.ide.project.ui.wizard.PluginClasspathContainerPage.java

License:Open Source License

public void setSelection(IClasspathEntry entry) {
    final IPath path = entry == null ? null : entry.getPath();

    if (path != null && path.segmentCount() == 2) {
        this.type = path.segment(1);
    }//w w w . j av  a  2  s.  c  om

}

From source file:com.liferay.ide.sdk.ui.SDKProjectRenameParticipant.java

License:Open Source License

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    final String newName = this.getArguments().getNewName();

    return new Change() {
        @Override//from  w  w w  .  j a v a2s.  c o m
        public String getName() {
            return "Update Ivy classpath entry"; //$NON-NLS-1$
        }

        @Override
        public void initializeValidationData(IProgressMonitor pm) {
            System.out.println();
        }

        @Override
        public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException, OperationCanceledException {
            return new RefactoringStatus();
        }

        @Override
        public Change perform(IProgressMonitor pm) throws CoreException {
            final RenameJavaProjectProcessor rjpp = (RenameJavaProjectProcessor) getProcessor();
            final IJavaProject newJavaProject = (IJavaProject) rjpp.getNewElement();
            final IvyClasspathContainerConfiguration conf = new IvyClasspathContainerConfiguration(
                    newJavaProject, ISDKConstants.IVY_XML_FILE, true);

            IClasspathEntry oldEntry = null;

            for (IClasspathEntry cpEntry : newJavaProject.getRawClasspath()) {
                if (cpEntry.getPath().segment(0).equals(IvyClasspathContainer.CONTAINER_ID)) {
                    oldEntry = cpEntry;
                    break;
                }
            }

            IvyClasspathContainerConfAdapter.load(conf, oldEntry.getPath(), oldEntry.getExtraAttributes());

            final String oldIvySettingsPath = conf.getIvySettingsSetup().getRawIvySettingsPath();
            final String oldIvyUserDir = conf.getIvySettingsSetup().getRawIvyUserDir();

            conf.setProject(newJavaProject);
            conf.getIvySettingsSetup().setIvySettingsPath(oldIvySettingsPath.replaceAll(oldName, newName));
            conf.getIvySettingsSetup().setIvyUserDir(oldIvyUserDir.replaceAll(oldName, newName));

            List<IClasspathEntry> newEntries = new ArrayList<IClasspathEntry>();

            for (IClasspathEntry cpEntry : newJavaProject.getRawClasspath()) {
                if (!cpEntry.getPath().segment(0).equals(IvyClasspathContainer.CONTAINER_ID)) {
                    newEntries.add(cpEntry);
                }
            }

            IPath newIvyPath = IvyClasspathContainerConfAdapter.getPath(conf);

            newJavaProject.setRawClasspath(newEntries.toArray(new IClasspathEntry[0]), pm);

            IClasspathEntry ivyEntry = JavaCore.newContainerEntry(newIvyPath, null,
                    oldEntry.getExtraAttributes(), false);

            IvyClasspathContainer ivycp = new IvyClasspathContainer(newJavaProject, newIvyPath,
                    new IClasspathEntry[0], new IClasspathAttribute[0]);
            JavaCore.setClasspathContainer(newIvyPath, new IJavaProject[] { newJavaProject },
                    new IClasspathContainer[] { ivycp }, pm);

            IClasspathEntry[] entries = newJavaProject.getRawClasspath();

            newEntries.add(ivyEntry);
            entries = (IClasspathEntry[]) newEntries.toArray(new IClasspathEntry[newEntries.size()]);
            newJavaProject.setRawClasspath(entries, newJavaProject.getOutputLocation(), pm);

            JavaCore.getClasspathContainerInitializer(IvyClasspathContainer.CONTAINER_ID)
                    .requestClasspathContainerUpdate(newIvyPath, newJavaProject, ivycp);

            ivycp.launchResolve(false, pm);

            return null;
        }

        @Override
        public Object getModifiedElement() {
            return null;
        }
    };
}

From source file:com.liferay.ide.server.remote.ModuleTraverser.java

License:Open Source License

private static void traverseWebComponentLocalEntries(WorkbenchComponent comp, IModuleVisitor visitor,
        IProgressMonitor monitor) throws CoreException {
    IProject warProject = StructureEdit.getContainingProject(comp);
    if (warProject == null || !warProject.hasNature(JavaCore.NATURE_ID)) {
        return;//  w  w  w.ja  v  a  2  s.c o  m
    }
    IJavaProject project = JavaCore.create(warProject);

    List res = comp.getResources();
    for (Iterator itorRes = res.iterator(); itorRes.hasNext();) {
        ComponentResource childComp = (ComponentResource) itorRes.next();
        IClasspathEntry cpe = getClasspathEntry(project, childComp.getSourcePath());
        if (cpe == null)
            continue;
        visitor.visitWebResource(childComp.getRuntimePath(),
                getOSPath(warProject, project, cpe.getOutputLocation()));
    }

    // Include tagged classpath entries
    Map classpathDeps = getComponentClasspathDependencies(project, true);
    for (Iterator iterator = classpathDeps.keySet().iterator(); iterator.hasNext();) {
        IClasspathEntry entry = (IClasspathEntry) iterator.next();
        IClasspathAttribute attrib = (IClasspathAttribute) classpathDeps.get(entry);
        boolean isClassFolder = isClassFolderEntry(entry);
        String rtFolder = attrib.getValue();
        if (rtFolder == null) {
            if (isClassFolder) {
                rtFolder = "/WEB-INF/classes"; //$NON-NLS-1$
            } else {
                rtFolder = "/WEB-INF/lib"; //$NON-NLS-1$
            }
        }
        IPath entryPath = entry.getPath();
        IResource entryRes = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
        if (entryRes != null) {
            entryPath = entryRes.getLocation();
        }
        // TODO Determine if different handling is needed for some use cases
        if (isClassFolder) {
            visitor.visitWebResource(new Path(rtFolder), getOSPath(warProject, project, entry.getPath()));
        } else {
            visitor.visitArchiveComponent(new Path(rtFolder), entryPath);
        }
    }
}

From source file:com.liferay.ide.server.remote.ModuleTraverser.java

License:Open Source License

private static void traverseDependentEntries(IModuleVisitor visitor, IPath runtimeFolder,
        WorkbenchComponent component, IProgressMonitor monitor) throws CoreException {
    IProject dependentProject = StructureEdit.getContainingProject(component);
    if (!dependentProject.hasNature(JavaCore.NATURE_ID))
        return;/*from   www .jav  a 2s  . co  m*/
    IJavaProject project = JavaCore.create(dependentProject);
    visitor.visitDependentJavaProject(project);

    String name = component.getName(); // assume it is the same as URI

    // go thru all entries
    List res = component.getResources();
    for (Iterator itorRes = res.iterator(); itorRes.hasNext();) {
        ComponentResource childComp = (ComponentResource) itorRes.next();
        IPath rtPath = childComp.getRuntimePath();
        IPath srcPath = childComp.getSourcePath();
        IClasspathEntry cpe = getClasspathEntry(project, srcPath);
        if (cpe != null) {
            visitor.visitDependentComponent(runtimeFolder.append(rtPath).append(name + ".jar"), //$NON-NLS-1$
                    getOSPath(dependentProject, project, cpe.getOutputLocation()));
        }
        // Handle META-INF/resources
        String path = rtPath.toString();
        IFolder resFolder = null;
        String targetPath = StringPool.EMPTY;
        if ("/".equals(path)) { //$NON-NLS-1$
            resFolder = dependentProject.getFolder(srcPath.append("META-INF/resources")); //$NON-NLS-1$
        } else if ("/META-INF".equals(path)) { //$NON-NLS-1$
            resFolder = dependentProject.getFolder(srcPath.append("resources")); //$NON-NLS-1$
        } else if ("/META-INF/resources".equals(path)) { //$NON-NLS-1$
            resFolder = dependentProject.getFolder(srcPath);
        } else if (path.startsWith("/META-INF/resources/")) { //$NON-NLS-1$
            resFolder = dependentProject.getFolder(srcPath);
            targetPath = path.substring("/META-INF/resources".length()); //$NON-NLS-1$
        }
        if (resFolder != null && resFolder.exists()) {
            visitor.visitDependentContentResource(new Path(targetPath), resFolder.getLocation());
        }
    }

    // Include tagged classpath entries
    Map classpathDeps = getComponentClasspathDependencies(project, false);
    for (Iterator iterator = classpathDeps.keySet().iterator(); iterator.hasNext();) {
        IClasspathEntry entry = (IClasspathEntry) iterator.next();
        boolean isClassFolder = isClassFolderEntry(entry);
        String rtFolder = null;
        if (isClassFolder) {
            rtFolder = "/"; //$NON-NLS-1$
        } else {
            rtFolder = "/WEB-INF/lib"; //$NON-NLS-1$
        }
        IPath entryPath = entry.getPath();
        IResource entryRes = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
        if (entryRes != null) {
            entryPath = entryRes.getLocation();
        }
        // TODO Determine if different handling is needed for some use cases
        if (isClassFolder) {
            visitor.visitDependentComponent(runtimeFolder.append(rtFolder).append(name + ".jar"), //$NON-NLS-1$
                    getOSPath(dependentProject, project, entry.getPath()));
        } else {
            visitor.visitArchiveComponent(new Path(rtFolder), entryPath);
        }
    }
}

From source file:com.liferay.ide.server.remote.ModuleTraverser.java

License:Open Source License

private static boolean isClassFolderEntry(final IClasspathEntry entry) {
    if (entry == null || entry.getEntryKind() != IClasspathEntry.CPE_LIBRARY) {
        return false;
    }/* w w w .ja  v a 2s. c om*/
    // does the path refer to a file or a folder?
    final IPath entryPath = entry.getPath();
    IPath entryLocation = entryPath;
    final IResource resource = ResourcesPlugin.getWorkspace().getRoot().findMember(entryPath);
    if (resource != null) {
        entryLocation = resource.getLocation();
    }
    boolean isFile = true; // by default, assume a jar file
    if (entryLocation.toFile().isDirectory()) {
        isFile = false;
    }
    return !isFile;
}

From source file:com.liferay.ide.server.tomcat.core.LiferayTomcatRuntimeClasspathProvider.java

License:Open Source License

private IClasspathEntry[] getUpdatedJavadocEntries(IClasspathEntry[] entries,
        ILiferayTomcatRuntime liferayTomcatRuntime) {
    List<IClasspathEntry> updatedEntries = new ArrayList<IClasspathEntry>();

    String javadocURL = liferayTomcatRuntime.getJavadocURL();

    if (javadocURL != null) {
        for (IClasspathEntry existingEntry : entries) {
            IPath path = existingEntry.getPath();

            IClasspathEntry newEntry = null;

            for (String javadocJar : JARS) {
                if (path.lastSegment().equalsIgnoreCase(javadocJar)) {
                    IClasspathAttribute[] extraAttrs = existingEntry.getExtraAttributes();

                    List<IClasspathAttribute> newExtraAttrs = new ArrayList<IClasspathAttribute>();

                    IClasspathAttribute javadocAttr = newJavadocAttr(javadocURL);

                    newExtraAttrs.add(javadocAttr);

                    if (!CoreUtil.isNullOrEmpty(extraAttrs)) {
                        for (IClasspathAttribute attr : extraAttrs) {
                            if (!attr.getName().equals(IClasspathAttribute.JAVADOC_LOCATION_ATTRIBUTE_NAME)) {
                                newExtraAttrs.add(attr);
                            }//from  www  .j ava  2 s.c  o m
                        }
                    }

                    newEntry = JavaCore.newLibraryEntry(existingEntry.getPath(),
                            existingEntry.getSourceAttachmentPath(),
                            existingEntry.getSourceAttachmentRootPath(), existingEntry.getAccessRules(),
                            newExtraAttrs.toArray(new IClasspathAttribute[0]), existingEntry.isExported());
                    break;
                }
            }

            if (newEntry != null) {
                updatedEntries.add(newEntry);
            } else {
                updatedEntries.add(existingEntry);
            }
        }
    } else {
        Collections.addAll(updatedEntries, entries);
    }

    return updatedEntries.toArray(new IClasspathEntry[0]);
}

From source file:com.liferay.ide.server.tomcat.core.LiferayTomcatRuntimeClasspathProvider.java

License:Open Source License

private IClasspathEntry[] getUpdatedSourceEntries(IClasspathEntry[] entries,
        ILiferayTomcatRuntime liferayTomcatRuntime) {
    List<IClasspathEntry> updatedEntries = new ArrayList<IClasspathEntry>();

    IPath sourceLocation = liferayTomcatRuntime.getSourceLocation();

    if (sourceLocation != null) {
        for (IClasspathEntry existingEntry : entries) {
            IPath path = existingEntry.getPath();

            IClasspathEntry newEntry = null;

            for (String sourceJar : JARS) {
                if (path.lastSegment().equalsIgnoreCase(sourceJar)) {
                    IPath sourcePath = existingEntry.getSourceAttachmentPath();

                    if (sourcePath == null) {
                        sourcePath = sourceLocation;
                    }/*w  w w  . j  av  a  2 s. c om*/

                    newEntry = JavaCore.newLibraryEntry(existingEntry.getPath(), sourcePath,
                            existingEntry.getSourceAttachmentRootPath(), existingEntry.getAccessRules(),
                            existingEntry.getExtraAttributes(), existingEntry.isExported());

                    break;
                }
            }

            if (newEntry != null) {
                updatedEntries.add(newEntry);
            } else {
                updatedEntries.add(existingEntry);
            }
        }
    } else {
        Collections.addAll(updatedEntries, entries);
    }

    return updatedEntries.toArray(new IClasspathEntry[0]);
}

From source file:com.liferay.ide.server.tomcat.core.LiferayTomcatRuntimeClasspathProvider.java

License:Open Source License

protected IClasspathEntry[] resolveClasspathContainerForPath(IPath installPath, String runtimeTypeId) {
    List<IClasspathEntry> list = new ArrayList<IClasspathEntry>();

    if (runtimeTypeId.endsWith("60") || runtimeTypeId.endsWith("70") || //$NON-NLS-1$ //$NON-NLS-2$
            installPath.append("lib").toFile().exists()) //$NON-NLS-1$
    {//from ww w.  ja va  2  s .co m
        IPath path = installPath.append("lib"); //$NON-NLS-1$

        addLibraryEntries(list, path.toFile(), true);
    }

    // go through all classpath entries and remove some unneeded ones
    List<IClasspathEntry> optimizedList = new ArrayList<IClasspathEntry>();

    List<String> excludes = Arrays.asList(ILiferayTomcatConstants.LIB_EXCLUDES);

    for (IClasspathEntry entry : list) {
        if (!excludes.contains(entry.getPath().lastSegment())) {
            optimizedList.add(entry);
        }
    }

    return (IClasspathEntry[]) optimizedList.toArray(new IClasspathEntry[optimizedList.size()]);
}