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

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

Introduction

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

Prototype

IPath getPath();

Source Link

Document

Returns the path to the innermost resource enclosing this element.

Usage

From source file:org.eclipse.ajdt.core.tests.AJDTCoreTestCase.java

License:Open Source License

private IPackageFragmentRoot createDefaultSourceFolder(IJavaProject javaProject) throws CoreException {
    IProject project = javaProject.getProject();
    IFolder folder = project.getFolder("src");
    if (!folder.exists())
        ensureExists(folder);//from   ww  w.j a  v  a2 s  . c o  m

    // if already exists, do nothing
    final IClasspathEntry[] entries = javaProject.getResolvedClasspath(false);
    final IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    for (int i = 0; i < entries.length; i++) {
        final IClasspathEntry entry = entries[i];
        if (entry.getPath().equals(folder.getFullPath())) {
            return root;
        }
    }

    // else, remove old source folders and add this new one
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    List<IClasspathEntry> oldEntriesList = new ArrayList<IClasspathEntry>();
    oldEntriesList.add(JavaCore.newSourceEntry(root.getPath()));
    for (IClasspathEntry entry : oldEntries) {
        if (entry.getEntryKind() != IClasspathEntry.CPE_SOURCE) {
            oldEntriesList.add(entry);
        }
    }

    IClasspathEntry[] newEntries = oldEntriesList.toArray(new IClasspathEntry[0]);
    javaProject.setRawClasspath(newEntries, null);
    return root;
}

From source file:org.eclipse.ajdt.internal.core.ClasspathModifier.java

License:Open Source License

/**
 * Find out whether the <code>IResource</code> excluded or not.
 * /*  www . j a v a 2s .co  m*/
 * @param resource
 *            the resource to be checked
 * @param project
 *            the Java project
 * @return <code>true</code> if the resource is excluded, <code>
 * false</code>
 *         otherwise
 * @throws JavaModelException
 */
public static boolean isExcluded(IResource resource, IJavaProject project) throws JavaModelException {
    IPackageFragmentRoot root = getFragmentRoot(resource, project, null);
    if (root == null)
        return false;
    String fragmentName = getName(resource.getFullPath(), root.getPath());
    fragmentName = completeName(fragmentName);
    IClasspathEntry entry = root.getRawClasspathEntry();
    return entry != null && contains(new Path(fragmentName), entry.getExclusionPatterns());
}

From source file:org.eclipse.ajdt.internal.core.ClasspathModifier.java

License:Open Source License

/**
 * Find out whether one of the <code>IResource</code>'s parents
 * is excluded.//w ww .j a v a2 s  .  c  om
 * 
 * @param resource check the resources parents whether they are
 * excluded or not
 * @param project the Java project
 * @return <code>true</code> if there is an excluded parent, 
 * <code>false</code> otherwise
 * @throws JavaModelException
 */
public static boolean parentExcluded(IResource resource, IJavaProject project) throws JavaModelException {
    if (resource.getFullPath().equals(project.getPath()))
        return false;
    IPackageFragmentRoot root = getFragmentRoot(resource, project, null);
    if (root == null) {
        return true;
    }
    IPath path = resource.getFullPath().removeFirstSegments(root.getPath().segmentCount());
    IClasspathEntry entry = root.getRawClasspathEntry();
    if (entry == null)
        return true; // there is no build path entry, this is equal to the fact that the parent is excluded
    while (path.segmentCount() > 0) {
        if (contains(path, entry.getExclusionPatterns()))
            return true;
        path = path.removeLastSegments(1);
    }
    return false;
}

From source file:org.eclipse.ajdt.internal.corext.util.OpenTypeHistory.java

License:Open Source License

private long getContainerTimestamp(TypeNameMatch match) {
    try {/*from w ww.ja  v a 2s.c  o m*/
        IType type = match.getType();
        IResource resource = type.getResource();
        if (resource != null) {
            URI location = resource.getLocationURI();
            if (location != null) {
                IFileInfo info = EFS.getStore(location).fetchInfo();
                if (info.exists()) {
                    // The element could be removed from the build path. So check
                    // if the Java element still exists.
                    IJavaElement element = JavaCore.create(resource);
                    if (element != null && element.exists())
                        return info.getLastModified();
                }
            }
        } else { // external JAR
            IPackageFragmentRoot root = match.getPackageFragmentRoot();
            IFileInfo info = EFS.getLocalFileSystem().getStore(root.getPath()).fetchInfo();
            if (info.exists()) {
                return info.getLastModified();
            }
        }
    } catch (CoreException e) {
        // Fall through
    }
    return IResource.NULL_STAMP;
}

From source file:org.eclipse.ajdt.internal.ui.ajdocexport.AJdocProjectContentProvider.java

License:Open Source License

public Object getParent(Object element) {

    IJavaElement parent = ((IJavaElement) element).getParent();
    if (parent instanceof IPackageFragmentRoot) {
        IPackageFragmentRoot root = (IPackageFragmentRoot) parent;
        if (root.getPath().equals(root.getJavaProject().getProject().getFullPath())) {
            return root.getJavaProject();
        }/*from ww w  .  java2 s  . co  m*/
    }
    return parent;
}

From source file:org.eclipse.ajdt.internal.ui.wizards.exports.AJJarFileExportOperation.java

License:Open Source License

/**
 * Exports the passed resource to the JAR file
 *
 * @param element the resource or JavaElement to export
 *//*from   w  w w  .  jav a  2  s.co m*/
protected void exportElement(Object element, IProgressMonitor progressMonitor) throws InterruptedException {
    // AspectJ Change Begin
    if (!AspectJPlugin.USING_CU_PROVIDER) {
        // Don't export AJCompilationUnits because they are duplicates of files that we also export.
        if (element instanceof AJCompilationUnit) {
            return;
        }
    }
    // AspectJ Change End
    int leadSegmentsToRemove = 1;
    IPackageFragmentRoot pkgRoot = null;
    boolean isInJavaProject = false;
    IResource resource = null;
    IJavaProject jProject = null;
    if (element instanceof IJavaElement) {
        isInJavaProject = true;
        IJavaElement je = (IJavaElement) element;
        int type = je.getElementType();
        if (type != IJavaElement.CLASS_FILE && type != IJavaElement.COMPILATION_UNIT) {
            exportJavaElement(progressMonitor, je);
            return;
        }
        try {
            resource = je.getUnderlyingResource();
        } catch (JavaModelException ex) {
            addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_resourceNotFound,
                    je.getElementName()), ex);
            return;
        }
        jProject = je.getJavaProject();
        pkgRoot = JavaModelUtil.getPackageFragmentRoot(je);
    } else
        resource = (IResource) element;

    if (!resource.isAccessible()) {
        addWarning(Messages.format(JarPackagerMessages.JarFileExportOperation_resourceNotFound,
                resource.getFullPath()), null);
        return;
    }

    if (resource.getType() == IResource.FILE) {
        if (!isInJavaProject) {
            // check if it's a Java resource
            try {
                isInJavaProject = resource.getProject().hasNature(JavaCore.NATURE_ID);
            } catch (CoreException ex) {
                addWarning(
                        Messages.format(JarPackagerMessages.JarFileExportOperation_projectNatureNotDeterminable,
                                resource.getFullPath()),
                        ex);
                return;
            }
            if (isInJavaProject) {
                jProject = JavaCore.create(resource.getProject());
                try {
                    IPackageFragment pkgFragment = jProject
                            .findPackageFragment(resource.getFullPath().removeLastSegments(1));
                    if (pkgFragment != null)
                        pkgRoot = JavaModelUtil.getPackageFragmentRoot(pkgFragment);
                    else
                        pkgRoot = findPackageFragmentRoot(jProject,
                                resource.getFullPath().removeLastSegments(1));
                } catch (JavaModelException ex) {
                    addWarning(Messages.format(
                            JarPackagerMessages.JarFileExportOperation_javaPackageNotDeterminable,
                            resource.getFullPath()), ex);
                    return;
                }
            }
        }

        if (pkgRoot != null && jProject != null) {
            leadSegmentsToRemove = pkgRoot.getPath().segmentCount();
            boolean isOnBuildPath;
            isOnBuildPath = jProject.isOnClasspath(resource);
            if (!isOnBuildPath || (mustUseSourceFolderHierarchy()
                    && !pkgRoot.getElementName().equals(IPackageFragmentRoot.DEFAULT_PACKAGEROOT_PATH)))
                leadSegmentsToRemove--;
        }

        IPath destinationPath = resource.getFullPath().removeFirstSegments(leadSegmentsToRemove);

        boolean isInOutputFolder = false;
        if (isInJavaProject && jProject != null) {
            try {
                isInOutputFolder = jProject.getOutputLocation().isPrefixOf(resource.getFullPath());
            } catch (JavaModelException ex) {
                isInOutputFolder = false;
            }
        }

        exportClassFiles(progressMonitor, pkgRoot, resource, jProject, destinationPath);
        exportResource(progressMonitor, pkgRoot, isInJavaProject, resource, destinationPath, isInOutputFolder);

        progressMonitor.worked(1);
        ModalContext.checkCanceled(progressMonitor);

    } else
        exportContainer(progressMonitor, (IContainer) resource);
}

From source file:org.eclipse.ajdt.internal.ui.wizards.NewTypeWizardPage.java

License:Open Source License

/**
 * A hook method that gets called when the package field has changed. The method 
 * validates the package name and returns the status of the validation. The validation
 * also updates the package fragment model.
 * <p>//from www  .  j a va 2s  .  c o m
 * Subclasses may extend this method to perform their own validation.
 * </p>
 * 
 * @return the status of the validation
 */
protected IStatus packageChanged() {
    StatusInfo status = new StatusInfo();
    IPackageFragmentRoot root = getPackageFragmentRoot();
    fPackageDialogField.enableButton(root != null);

    IJavaProject project = root != null ? root.getJavaProject() : null;

    String packName = getPackageText();
    if (packName.length() > 0) {
        IStatus val = validatePackageName(packName, project);
        if (val.getSeverity() == IStatus.ERROR) {
            status.setError(Messages.format(NewWizardMessages.NewTypeWizardPage_error_InvalidPackageName,
                    val.getMessage()));
            return status;
        } else if (val.getSeverity() == IStatus.WARNING) {
            status.setWarning(Messages.format(
                    NewWizardMessages.NewTypeWizardPage_warning_DiscouragedPackageName, val.getMessage()));
            // continue
        }
    } else {
        status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_DefaultPackageDiscouraged);
    }

    if (project != null) {
        if (project.exists() && packName.length() > 0) {
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = project.getOutputLocation();
                if (rootPath.isPrefixOf(outputPath) && !rootPath.equals(outputPath)) {
                    // if the bin folder is inside of our root, don't allow to name a package
                    // like the bin folder
                    IPath packagePath = rootPath.append(packName.replace('.', '/'));
                    if (outputPath.isPrefixOf(packagePath)) {
                        status.setError(NewWizardMessages.NewTypeWizardPage_error_ClashOutputLocation);
                        return status;
                    }
                }
            } catch (JavaModelException e) {
                JavaPlugin.log(e);
                // let pass         
            }
        }

        fCurrPackage = root.getPackageFragment(packName);
    } else {
        status.setError(""); //$NON-NLS-1$
    }
    return status;
}

From source file:org.eclipse.andmore.internal.editors.binaryxml.BinaryXMLMultiPageEditorPart.java

License:Open Source License

@Override
protected void setInput(IEditorInput input) {
    if (input instanceof JarEntryEditorInput) {
        JarEntryEditorInput jarInput = (JarEntryEditorInput) input;
        IStorage storage = jarInput.getStorage();
        if (storage instanceof JarEntryFile) {
            JarEntryFile jarEntryFile = (JarEntryFile) storage;
            IPackageFragmentRoot fragmentRoot = jarEntryFile.getPackageFragmentRoot();
            if (fragmentRoot == null) {
                super.setInput(input);
                return;
            }//w  w w. ja  v a  2s.  com
            IPath path = fragmentRoot.getPath();
            if (path == null) {
                super.setInput(input);
                return;
            }
            path = path.removeLastSegments(1);
            IPath filePath = path.append(SdkConstants.FD_DATA)
                    .append(jarEntryFile.getFullPath().toPortableString());
            File file = new File(filePath.toOSString());
            if (!(file.isFile())) {
                super.setInput(input);
                return;
            }
            try {
                XmlStorageEditorInput newInput = new XmlStorageEditorInput(new FileStorage(file));
                super.setInput(newInput);
                return;
            } catch (Exception e) {
                AndmoreAndroidPlugin.log(e, e.getMessage(), null);
            }
        }
    }
    super.setInput(input);
}

From source file:org.eclipse.ant.internal.ui.datatransfer.ProjectCreator.java

License:Open Source License

/**
 * Adds a source container to a IJavaProject.
 *//*from  www  .j a va  2 s  .co m*/
private void addSourceContainer(IJavaProject jproject, String srcName, String srcPath, String outputName,
        String outputPath, IProgressMonitor monitor) throws CoreException {
    IProject project = jproject.getProject();
    IContainer container = null;
    if (srcName == null || srcName.length() == 0) {
        container = project;
    } else {
        IFolder folder = project.getFolder(srcName);
        if (!folder.exists()) {
            folder.createLink(new Path(srcPath), IResource.ALLOW_MISSING_LOCAL, monitor);
        }
        container = folder;
    }
    IPackageFragmentRoot root = jproject.getPackageFragmentRoot(container);

    IPath output = null;
    if (outputName != null) {
        IFolder outputFolder = project.getFolder(outputName);
        if (!outputFolder.exists()) {
            outputFolder.createLink(new Path(outputPath), IResource.ALLOW_MISSING_LOCAL, monitor);
        }
        output = outputFolder.getFullPath();
    }

    IClasspathEntry cpe = JavaCore.newSourceEntry(root.getPath(), new IPath[0], output);

    addToClasspath(jproject, cpe, monitor);
}

From source file:org.eclipse.buildship.core.workspace.internal.DefaultWorkspaceOperations.java

License:Open Source License

private List<IClasspathEntry> collectSourceDirectories(ClasspathDefinition classpath,
        final IJavaProject javaProject) {
    return FluentIterable.from(classpath.getSourceDirectories())
            .transform(new Function<String, IClasspathEntry>() {

                @Override/*from   www . java2 s. co m*/
                public IClasspathEntry apply(String directory) {
                    IFolder sourceDirectory = javaProject.getProject().getFolder(Path.fromOSString(directory));
                    ensureFolderHierarchyExists(sourceDirectory);
                    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceDirectory);
                    return JavaCore.newSourceEntry(root.getPath());
                }
            }).toList();
}