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

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

Introduction

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

Prototype

IResource getResource();

Source Link

Document

Returns the innermost resource enclosing this element.

Usage

From source file:org.wso2.developerstudio.eclipse.utils.jdt.JavaUtils.java

License:Open Source License

public static IPath[] getJavaSourceDirectories(IProject project) throws CoreException {
    List<IPath> paths = new ArrayList<IPath>();
    try {// w  w w. j  ava 2 s  .co m
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject != null) {
            IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
            for (IPackageFragmentRoot fragmentRoot : packageFragmentRoots) {
                if (fragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    paths.add(fragmentRoot.getResource().getFullPath());
                }
            }
        } else {
            IFolder folder = project.getFolder("src").getFolder("main").getFolder("java");
            addJavaSourceFolder(folder, javaProject);
            paths.add(folder.getLocation());
        }
    } catch (JavaModelException e) {
    }
    return paths.toArray(new IPath[] {});
}

From source file:potes.cucumberjvm.eclipseplugin.launch.CucumberTestLaunchDelegate.java

License:Apache License

@Override
public String getVMArguments(ILaunchConfiguration configuration) throws CoreException {
    IJavaProject javaProject = getJavaProject(configuration);
    Set<String> packages = new HashSet<String>(
            Activator.getLanguage().getCucumberDefinitionPackages(javaProject));

    StringBuilder pathsBuilder = new StringBuilder();
    List<String> paths = configuration.getAttribute(Activator.LAUNCH_FEATURE_PATH, Collections.EMPTY_LIST);
    for (String path : paths) {
        IPath fullPath = javaProject.getProject().getFile(path).getParent().getFullPath();
        IPackageFragment packageFragment = javaProject.findPackageFragment(fullPath);
        if (packageFragment != null) {
            packages.add(packageFragment.getElementName());
        } else {/*from ww w .j a va 2  s  .  c  om*/
            for (IPackageFragmentRoot root : javaProject.getPackageFragmentRoots()) {
                if (root.getResource().getFullPath().isPrefixOf(fullPath)) {
                    packages.add(fullPath.makeRelativeTo(root.getResource().getFullPath()).toString()
                            .replace('/', '.'));
                    break;
                }
            }
        }
        pathsBuilder.append(" ").append(path);
    }

    normalisePackages(packages);

    StringBuilder builder = new StringBuilder("-ea -Dcucumber.options=\"--strict");
    for (String pkg : packages) {
        builder.append(" --glue ").append(pkg.replace('.', '/'));
    }

    builder.append(pathsBuilder).append("\"");
    String args = builder.toString();
    Activator.getDefault().getLog().log(new Status(Status.INFO, Activator.PLUGIN_ID, "VM Args: " + args));
    return args;
}

From source file:qwickie.hyperlink.WicketHyperlink.java

License:Apache License

private void createJavaFile(final IResource resource) {
    Assert.isNotNull(resource);// www.j  a v  a 2 s .  c o  m

    final OpenNewClassWizardAction action = new OpenNewClassWizardAction();
    final NewClassWizardPage ncwp = new NewClassWizardPage();

    ncwp.setTypeName(resource.getName().replaceAll("\\.html", ""), true);

    final IJavaProject javaProject = JavaCore.create(resource.getProject());
    IPackageFragmentRoot root = null;
    try {
        final IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < roots.length; i++) {
            if (roots[i].getKind() == IPackageFragmentRoot.K_SOURCE) {
                root = roots[i];
                break;
            }
        }

        ncwp.setPackageFragmentRoot(root, true);

    } catch (final JavaModelException e) {
    }
    final String os = root.getParent().getPath().toPortableString();
    final String fp = root.getResource().getFullPath().toPortableString().replaceFirst(os, "").substring(1);
    final String ps = resource.getProjectRelativePath().toPortableString().replaceFirst(fp, "");
    String pn = ps.replaceFirst(resource.getName(), "").substring(1).replaceAll("/", ".");
    pn = pn.substring(0, pn.length() - 1);

    ncwp.setPackageFragment(root.getPackageFragment(pn), true);

    ncwp.setSuperClass("org.apache.wicket.markup.html.WebPage", openJavaFile());

    action.setConfiguredWizardPage(ncwp);
    action.setOpenEditorOnFinish(true);
    action.run();
}

From source file:x10dt.search.core.pdb.X10FactGenerator.java

License:Open Source License

private void processEntries(final CompilerOptionsBuilder cmpOptBuilder, final IWorkspaceRoot wsRoot,
        final IClasspathEntry[] entries, final IJavaProject javaProject, final IResource contextResource,
        final boolean isInRuntime) throws JavaModelException, AnalysisException {
    for (final IClasspathEntry pathEntry : entries) {
        switch (pathEntry.getEntryKind()) {
        case IClasspathEntry.CPE_SOURCE:
            if (pathEntry.getPath().isRoot()) {
                cmpOptBuilder.addToSourcePath(pathEntry.getPath().toFile());
            } else {
                cmpOptBuilder.addToSourcePath(wsRoot.getLocation().append(pathEntry.getPath()).toFile());
            }/*w  w w .j  a  v a 2s  .  c  om*/
            if (pathEntry.getPath().segmentCount() > 1) {
                processSourceFolder(cmpOptBuilder, wsRoot.getFolder(pathEntry.getPath()), contextResource);
            }
            break;

        case IClasspathEntry.CPE_LIBRARY:
            try {
                final IPackageFragmentRoot pkgRoot = javaProject.findPackageFragmentRoot(pathEntry.getPath());
                if ((pkgRoot != null) && pkgRoot.exists()) {
                    final File localFile;
                    if (pkgRoot.isExternal()) {
                        localFile = pathEntry.getPath().toFile();
                    } else {
                        localFile = pkgRoot.getResource().getLocation().toFile();
                    }
                    cmpOptBuilder.addToClassPath(localFile.getAbsolutePath());
                    if (isInRuntime) {
                        cmpOptBuilder.addToSourcePath(localFile);
                    }
                    final ZipFile zipFile;
                    if (JAR_EXT.equals(pathEntry.getPath().getFileExtension())) {
                        zipFile = new JarFile(localFile);
                    } else {
                        zipFile = new ZipFile(localFile);
                    }
                    processLibrary(cmpOptBuilder, zipFile, localFile, contextResource, isInRuntime);
                }
            } catch (IOException except) {
                throw new AnalysisException(NLS.bind(Messages.XFG_JarReadingError, pathEntry.getPath()),
                        except);
            }
            break;

        case IClasspathEntry.CPE_CONTAINER:
            final IClasspathContainer cpContainer = JavaCore.getClasspathContainer(pathEntry.getPath(),
                    javaProject);
            processEntries(cmpOptBuilder, wsRoot, cpContainer.getClasspathEntries(), javaProject,
                    contextResource, true);
            break;

        case IClasspathEntry.CPE_PROJECT:
            final IResource projectResource = ResourcesPlugin.getWorkspace().getRoot()
                    .findMember(pathEntry.getPath());
            if ((projectResource != null) && projectResource.isAccessible()) {
                final IJavaProject newJavaProject = JavaCore.create((IProject) projectResource);
                processEntries(cmpOptBuilder, wsRoot, newJavaProject.getRawClasspath(), newJavaProject,
                        contextResource, false);
            }
            break;

        case IClasspathEntry.CPE_VARIABLE:
            processEntries(cmpOptBuilder, wsRoot,
                    new IClasspathEntry[] { JavaCore.getResolvedClasspathEntry(pathEntry) }, javaProject,
                    contextResource, false);
            break;
        }
    }
}