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.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @param project/*from w  w  w . java 2 s  .  c o  m*/
 * @return
 */
public static String[][] getSourcePathsForProject(final String project) {
    final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

    final List<String> paths = new ArrayList<String>();
    try {
        for (final IProject prjct : projects) {
            if (project == null || prjct.getName().equals(project)) {
                final IJavaProject javaProject = JavaCore.create(prjct);
                if (javaProject == null || !javaProject.exists()) {
                    continue;
                }
                if (isProjectBinary(javaProject)) {
                    continue;
                }
                for (final IPackageFragmentRoot packageFragmentRoot : javaProject
                        .getAllPackageFragmentRoots()) {
                    if (packageFragmentRoot == null || !packageFragmentRoot.exists()
                            || packageFragmentRoot.isArchive() || packageFragmentRoot.isExternal()) {
                        continue;
                    }
                    if (!packageFragmentRoot.getParent().equals(javaProject)) { // discard
                        // roots
                        // which
                        // come
                        // from
                        // another
                        // project.
                        continue;
                    }
                    final String fullSrcPath = packageFragmentRoot.getPath().toString();
                    final String srcPath = fullSrcPath
                            .replaceFirst(FORWARD_SLASH + javaProject.getElementName(), EMPTY_STR);
                    if (!paths.contains(srcPath)) {
                        paths.add(srcPath);
                    }
                }
            }
        }
    } catch (final Exception ex) {
        ex.printStackTrace();
    }

    return getStringArrayFromList(paths, true);
}

From source file:org.fastcode.util.SourceUtil.java

License:Open Source License

/**
 *
 * @return//from w w  w . j  av a 2  s  . co m
 */
public static String[][] getAllSourcePaths(final String filter) {
    final IProject[] projects = ResourcesPlugin.getWorkspace().getRoot().getProjects();

    final List<String> paths = new ArrayList<String>();

    try {
        for (final IProject project : projects) {
            final IJavaProject javaProject = JavaCore.create(project);
            if (javaProject != null && javaProject.exists() && isJunitEnabled(javaProject)
                    && javaProject.isOpen() && !javaProject.isReadOnly()) {
                for (final IPackageFragmentRoot packageFragmentRoot : javaProject
                        .getAllPackageFragmentRoots()) {
                    if (!(packageFragmentRoot.isArchive() || packageFragmentRoot.isReadOnly())) {
                        String sourcePath = packageFragmentRoot.getPath().toString();
                        sourcePath = sourcePath.replace(FORWARD_SLASH + project.getName(), "${project}");
                        if (filter == null || sourcePath.toUpperCase().contains(filter.toUpperCase())) {
                            boolean include = true;
                            for (final String path : paths) {
                                if (path.equals(sourcePath)) {
                                    include = false;
                                    break;
                                }
                            }
                            if (include) {
                                paths.add(sourcePath);
                            }
                        }
                    }
                }
            }
        }
    } catch (final JavaModelException ex) {
        ex.printStackTrace();
        return new String[0][0];
    }

    return getStringArrayFromList(paths, false);
}

From source file:org.fusesource.ide.camel.editor.propertysheet.CamelComponentUtils.java

License:Open Source License

public static URLClassLoader getProjectClassLoader() {
    try {// ww  w. j  a  v a  2  s.  co m
        IProject project = Activator.getDiagramEditor().getCamelContextFile().getProject();
        IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        IPackageFragmentRoot[] pfroots = javaProject.getAllPackageFragmentRoots();
        ArrayList<URL> urls = new ArrayList<URL>();
        for (IPackageFragmentRoot root : pfroots) {
            URL rUrl = root.getPath().toFile().toURI().toURL();
            urls.add(rUrl);
        }
        return new URLClassLoader(urls.toArray(new URL[urls.size()]),
                CamelComponentUtils.class.getClassLoader());
    } catch (Exception ex) {
        Activator.getLogger().error(ex);
    }
    return null;
}

From source file:org.fusesource.ide.camel.editor.utils.CamelComponentUtils.java

License:Open Source License

public static URLClassLoader getProjectClassLoader() {
    try {//from   w  ww .j a v a  2 s  . c o m
        IProject project = CamelUtils.getDiagramEditor().getModel().getResource().getProject();
        IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        IPackageFragmentRoot[] pfroots = javaProject.getAllPackageFragmentRoots();
        ArrayList<URL> urls = new ArrayList<URL>();
        for (IPackageFragmentRoot root : pfroots) {
            URL rUrl = root.getPath().toFile().toURI().toURL();
            urls.add(rUrl);
        }
        return new URLClassLoader(urls.toArray(new URL[urls.size()]),
                CamelComponentUtils.class.getClassLoader());
    } catch (Exception ex) {
        CamelEditorUIActivator.pluginLog().logError(ex);
    }
    return null;
}

From source file:org.fusesource.ide.camel.model.service.core.util.CamelComponentUtils.java

License:Open Source License

public static URLClassLoader getProjectClassLoader(IProject project) {
    try {/*  www  .  j  a  v  a 2  s .  com*/
        IJavaProject javaProject = (IJavaProject) project.getNature(JavaCore.NATURE_ID);
        IPackageFragmentRoot[] pfroots = javaProject.getAllPackageFragmentRoots();
        List<URL> urls = new ArrayList<>();
        for (IPackageFragmentRoot root : pfroots) {
            URL rUrl = root.getPath().toFile().toURI().toURL();
            urls.add(rUrl);
        }
        return new URLClassLoader(urls.toArray(new URL[urls.size()]),
                CamelComponentUtils.class.getClassLoader());
    } catch (Exception ex) {
        CamelModelServiceCoreActivator.pluginLog().logError(ex);
    }
    return null;
}

From source file:org.gemoc.execution.sequential.javaengine.PlainK3ExecutionEngine.java

License:Open Source License

/**
 * Return a bundle containing 'aspectClassName'.
 * //from w  w w  .  j av  a2  s .  c  o m
 * Return null if not found.
 */
private Bundle findBundle(final IExecutionContext executionContext, String aspectClassName) {

    // first look using JavaWorkspaceScope as this is safer and will look in
    // dependencies
    IType mainIType = getITypeMainByWorkspaceScope(aspectClassName);

    Bundle bundle = null;
    String bundleName = null;
    if (mainIType != null) {
        IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) mainIType.getPackageFragment()
                .getParent();

        bundleName = packageFragmentRoot.getPath().removeLastSegments(1).lastSegment().toString();
        if (bundleName != null) {

            // First we try to look into an already loaded bundle
            bundle = Platform.getBundle(bundleName);

            // If this doesn't work, we use the provisioner to load
            // the corresponding project
            if (bundle == null) {

                String projectName = mainIType.getJavaProject().getElementName();
                IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
                if (project != null && project.exists()
                        && !project.getFullPath().equals(executionContext.getWorkspace().getProjectPath())) {
                    Provisionner p = new Provisionner();
                    IStatus status = p.provisionFromProject(project, null);
                    if (!status.isOK()) {
                        // return status;
                        throw new RuntimeException("Coudln't provision project.");
                    }
                }
                bundleName = project.getName();
                bundle = Platform.getBundle(bundleName);
            }
        }
    } else {
        // the main isn't visible directly from the workspace, try another
        // method
        bundle = _executionContext.getMelangeBundle();
    }

    return bundle;
}

From source file:org.gw4e.eclipse.facade.GraphWalkerContextManager.java

License:Open Source License

/**
 * @param file//from  ww  w .  j av  a 2s. c om
 * @return
 * @throws CoreException
 */
public static AbstractPostConversion getDefaultGraphConversion(IFile file, boolean generateOnlyInterface)
        throws CoreException {
    AbstractPostConversion converter = null;
    boolean canBeConverted = PreferenceManager.isGraphModelFile(file);
    if (canBeConverted) {
        String targetFolder = GraphWalkerContextManager.getTargetFolderForTestImplementation(file);
        IPath pkgFragmentRootPath = file.getProject().getFullPath().append(new Path(targetFolder));
        IPackageFragmentRoot implementationFragmentRoot = JDTManager.getPackageFragmentRoot(file.getProject(),
                pkgFragmentRootPath);
        String classname = file.getName().split("\\.")[0];
        classname = classname + PreferenceManager.suffixForTestImplementation(
                implementationFragmentRoot.getJavaProject().getProject().getName()) + ".java";

        ClassExtension ce = PreferenceManager.getDefaultClassExtension(file);
        IPath p = ResourceManager.getPathWithinPackageFragment(file).removeLastSegments(1);
        p = implementationFragmentRoot.getPath().append(p);
        ResourceContext context = new ResourceContext(p, classname, file, true, false, generateOnlyInterface,
                ce);
        converter = new JavaTestBasedPostConversionImpl(context);
    }
    return converter;
}

From source file:org.gw4e.eclipse.facade.JDTManager.java

License:Open Source License

public static IPackageFragment getPackageFragment(IFile file) throws JavaModelException {
    IPackageFragmentRoot root1 = JDTManager.findPackageFragmentRoot(file.getProject(), file.getFullPath());
    IPath path = file.getParent().getFullPath().makeRelativeTo(root1.getPath());
    IPackageFragment pf = root1.getPackageFragment(path.toString().replace("/", "."));
    return pf;//from w  ww  .ja  v a2 s.  co m
}

From source file:org.gw4e.eclipse.facade.ResourceManager.java

License:Open Source License

/**
 * Lets get the path of hat have been selected in the UI - the complete path
 * a path is something like "src/main/resources"
 * /*from ww  w.j av a2s  .com*/
 * @param receiver
 * @return
 */
public static String getSelectedPathInProject(Object receiver) {
    if (!ResourceManager.isPackageFragmentRoot(receiver)) {
        return null;
    }
    IPackageFragmentRoot pkg = (IPackageFragmentRoot) receiver;

    IJavaProject javaProject = pkg.getJavaProject();
    if (javaProject == null)
        return null;

    IProject project = javaProject.getProject();

    if (!GW4ENature.hasGW4ENature(project))
        return null;

    String projectName = pkg.getJavaProject().getElementName();
    int pos = projectName.length();
    String input = pkg.getPath().toString().substring(pos + 2);
    return input;
}

From source file:org.gw4e.eclipse.fwk.project.GW4EProject.java

License:Open Source License

public void assertHasSourceFolders(String[] folders) throws JavaModelException {
    IProject project = getRoot().getProject(this.projectName);
    IJavaProject jproject = JavaCore.create(project);
    IPackageFragmentRoot[] pkgs = jproject.getPackageFragmentRoots();

    for (int i = 0; i < folders.length; i++) {
        String folder = folders[i];
        boolean found = false;
        for (int j = 0; j < pkgs.length; j++) {
            IPackageFragmentRoot pkg = pkgs[j];
            IPath path = new Path("/").append(this.projectName).append(folder);
            if (pkg.getPath().toString().equalsIgnoreCase(path.toString())) {
                found = true;/*w w w  .  j  a  va2 s .  c om*/
            }
            ;
        }
        assertTrue("Expected folder: " + folder, found);
    }
}