Example usage for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots

List of usage examples for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaProject getPackageFragmentRoots.

Prototype

IPackageFragmentRoot[] getPackageFragmentRoots() throws JavaModelException;

Source Link

Document

Returns all of the package fragment roots contained in this project, identified on this project's resolved classpath.

Usage

From source file:in.cypal.studio.gwt.core.launch.LaunchConfigurationDelegate.java

License:Apache License

public String[] getClasspath(ILaunchConfiguration configuration) throws CoreException {

    String projectName = configuration.getAttribute(Constants.LAUNCH_ATTR_PROJECT_NAME, ""); //$NON-NLS-1$
    IJavaProject project = JavaCore.create(Util.getProject(projectName));
    List classpath = new ArrayList(4);
    IPackageFragmentRoot[] packageFragmentRoots = project.getPackageFragmentRoots();
    for (int i = 0; i < packageFragmentRoots.length; i++) {
        IPackageFragmentRoot packageFragmentRoot = packageFragmentRoots[i];
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
            classpath.add(packageFragmentRoot.getResource().getLocation().toOSString());
        }/*from  w  w  w .ja va 2s  .  c  o m*/
    }

    String[] classpath2 = super.getClasspath(configuration);
    for (int i = 0; i < classpath2.length; i++) {
        String aClasspath = classpath2[i];
        classpath.add(aClasspath);
    }

    classpath.add(Util.getGwtDevLibPath().toPortableString());

    return (String[]) classpath.toArray(new String[classpath.size()]);

}

From source file:io.sarl.eclipse.wizards.elements.AbstractNewSarlElementWizardPage.java

License:Apache License

/** Replies if the given filename is a SARL script or a generated Java file.
 *
 * @param packageFragment - the package in which the file should be search for.
 * @param filename - the filename to test.
 * @return <code>true</code> if a file (SALR or Java) with the given name exists.
 *///from ww w.  jav  a  2  s.  c o  m
protected boolean isSarlFile(IPackageFragment packageFragment, String filename) {
    if (isFileExists(packageFragment, filename, this.sarlFileExtension)) {
        return true;
    }
    final IJavaProject project = getPackageFragmentRoot().getJavaProject();
    if (project != null) {
        try {
            final String packageName = packageFragment.getElementName();
            for (final IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
                final IPackageFragment fragment = root.getPackageFragment(packageName);
                if (isFileExists(fragment, filename, JAVA_FILE_EXTENSION)) {
                    return true;
                }
            }
        } catch (JavaModelException exception) {
            // silent error
        }
    }
    return false;
}

From source file:io.sarl.lang.ui.contentassist.SARLProposalProvider.java

License:Apache License

private static String extractProjectPath(IPath fileWorkspacePath, IJavaProject javaProject) {
    if (javaProject != null && javaProject.exists() && javaProject.isOpen()) {
        try {// w  w  w.j  a v  a2s.c o  m
            for (final IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                if (!root.isArchive() && !root.isExternal()) {
                    final IResource resource = root.getResource();
                    if (resource != null) {
                        final IPath sourceFolderPath = resource.getFullPath();
                        if (sourceFolderPath.isPrefixOf(fileWorkspacePath)) {
                            final IPath claspathRelativePath = fileWorkspacePath
                                    .makeRelativeTo(sourceFolderPath);
                            return claspathRelativePath.removeLastSegments(1).toString().replace("/", "."); //$NON-NLS-1$//$NON-NLS-2$
                        }
                    }
                }
            }
        } catch (JavaModelException e) {
            Logger.getLogger(SARLProposalProvider.class).error(e.getLocalizedMessage(), e);
        }
    }
    return null;
}

From source file:io.sarl.lang.ui.validation.SARLUIValidator.java

License:Apache License

private static String getRelativeSourceFolder(IJavaProject javaProject, IPath rootPath)
        throws JavaModelException {
    for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
        if (!root.isArchive() && !root.isExternal()) {
            IResource resource = root.getResource();
            if (resource != null) {
                IPath sourceFolderPath = resource.getFullPath();
                if (sourceFolderPath.isPrefixOf(rootPath)) {
                    IPath classpathRelativePath = rootPath.makeRelativeTo(sourceFolderPath);
                    return classpathRelativePath.removeLastSegments(1).toString().replace("/", "."); //$NON-NLS-1$//$NON-NLS-2$
                }//from w ww .ja  va 2s .  c  o  m
            }
        }
    }
    return null;
}

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

private List<IPackageFragmentRoot> getJars(IJavaModel javaModel) throws JavaModelException {

    IJavaProject[] projects = javaModel.getJavaProjects();
    TreeSet<String> jarNames = new TreeSet<String>();
    LinkedList<IPackageFragmentRoot> jars = new LinkedList<IPackageFragmentRoot>();

    for (IJavaProject javaProj : projects) {

        IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots();

        for (IPackageFragmentRoot root : roots) {
            if (root.isArchive() && root.getSourceAttachmentPath() != null) {

                String name = root.getElementName();

                if (!jarNames.contains(name)) {
                    jarNames.add(name);/*  w  w  w  .j  av  a  2 s  .c o  m*/
                    jars.add(root);
                }
            }
        }
    }

    return jars;
}

From source file:it.unibz.instasearch.indexing.WorkspaceIndexerJDT.java

License:Open Source License

/**
 * @param doc/*  ww  w .j  a v a  2  s. c o  m*/
 * @return
 * @throws JavaModelException 
 */
private IStorage getNonJavaResource(SearchResultDoc doc) throws JavaModelException {

    IWorkspaceRoot workspaceRoot = InstaSearchPlugin.getWorkspaceRoot();
    IJavaModel javaModel = JavaCore.create(workspaceRoot);

    String javaProjectName = doc.getProject().segment(0);
    IJavaProject javaProj = javaModel.getJavaProject(javaProjectName);

    if (!javaProj.isOpen())
        javaProj.open(new NullProgressMonitor());

    javaModel.refreshExternalArchives(new IJavaElement[] { javaProj }, new NullProgressMonitor());

    String jarName = doc.getJarName();

    IPackageFragmentRoot[] roots = javaProj.getPackageFragmentRoots();
    IPackageFragmentRoot jar = null;

    for (IPackageFragmentRoot root : roots) {
        if (root.isArchive() && root.getSourceAttachmentPath() != null) {

            String name = root.getElementName();
            if (name.equals(jarName)) {
                jar = root;
                break;
            }
        }
    }

    if (jar == null)
        return null;

    String filePath = doc.getFilePath();
    IPath path = new Path(filePath);

    IJarEntryResource res = null;
    for (String segment : path.segments()) {
        if (res == null)
            res = findJarEntry(jar, segment);
        else
            res = findJarEntry(res.getChildren(), segment);
    }

    return res;
}

From source file:net.atos.jdt.ast.validation.engine.project.ValidationProjectBuilder.java

License:Open Source License

/**
 * Retrieves the compilation units within the provided project
 * /*from w  w w. jav  a2  s  .  com*/
 * @param javaProject
 * @return
 * @throws JavaModelException
 */
private Collection<? extends ICompilationUnit> retrieveCompilationUnits(final IJavaProject javaProject)
        throws JavaModelException {
    final Collection<ICompilationUnit> compilationUnits = new ArrayList<ICompilationUnit>();
    for (final IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
            for (final IJavaElement element : packageFragmentRoot.getChildren()) {
                if (element instanceof IPackageFragment) {
                    final IPackageFragment packageFragment = (IPackageFragment) element;
                    for (final ICompilationUnit compilationUnit : packageFragment.getCompilationUnits()) {
                        compilationUnits.add(compilationUnit);
                    }
                } else if (element instanceof ICompilationUnit) {
                    compilationUnits.add((ICompilationUnit) element);
                }
            }
        }
    }
    return compilationUnits;
}

From source file:net.harawata.mybatipse.mybatis.ConfigRegistry.java

License:Open Source License

/**
 * Scans the project and returns the MyBatis config file if found.<br>
 * If there are multiple files in the project, only the first one is returned.
 * /*from w  w w  .j a v  a  2s.c  o  m*/
 * @param project
 * @return MyBatis config file or <code>null</code> if none found.
 */
private Map<IFile, IContentType> search(IJavaProject project) {
    final Map<IFile, IContentType> configFiles = new ConcurrentHashMap<IFile, IContentType>();
    try {
        project.getResource().accept(new ConfigVisitor(configFiles), IContainer.NONE);

        for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
            if (root.getKind() != IPackageFragmentRoot.K_SOURCE)
                continue;
            root.getResource().accept(new ConfigVisitor(configFiles), IContainer.NONE);
        }
    } catch (CoreException e) {
        Activator.log(Status.ERROR, "Searching MyBatis Config xml failed.", e);
    }

    return configFiles;
}

From source file:net.hillsdon.testlink.model.conventions.FixedSourceFolderConvention.java

License:Open Source License

public IPackageFragmentRoot getSourceFolder(final IJavaProject project) throws JavaModelException {
    for (IPackageFragmentRoot root : project.getPackageFragmentRoots()) {
        final IResource resource = root.getResource();
        if (resource != null && _projectRelativePath.equals(resource.getProjectRelativePath().toString())) {
            return root;
        }//from   w  w  w .ja va2  s  .c o  m
    }
    return null;
}

From source file:net.sf.spindle.core.util.eclipse.JarEntryFileUtil.java

License:Mozilla Public License

private static IPackageFragmentRoot getPackageFragmentRoot(IJavaProject project, JarEntryFile entry,
        boolean includeOtherProjects) throws CoreException {
    String path = getJarPath(entry);
    IPackageFragmentRoot[] roots = includeOtherProjects ? project.getAllPackageFragmentRoots()
            : project.getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        if (roots[i].getKind() != IPackageFragmentRoot.K_BINARY)
            continue;

        if (((JarPackageFragmentRoot) roots[i]).getJar().getName().equals(path)) {
            return roots[i];
        }/*  ww w  .ja v a 2  s  .c o  m*/
    }
    return null;
}