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

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

Introduction

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

Prototype

IPackageFragmentRoot getPackageFragmentRoot(IResource resource);

Source Link

Document

Returns a package fragment root for the given resource, which must either be a folder representing the top of a package hierarchy, or a ZIP archive (e.g.

Usage

From source file:org.eclipse.ant.tests.ui.debug.AbstractAntDebugTest.java

License:Open Source License

/**
 * Returns the source folder with the given name in the given project.
 * /* w  ww  . j  a v a  2  s .c o  m*/
 * @param project
 * @param name
 *            source folder name
 * @return package fragment root
 */
protected IPackageFragmentRoot getPackageFragmentRoot(IJavaProject project, String name) {
    IProject p = project.getProject();
    return project.getPackageFragmentRoot(p.getFolder(name));
}

From source file:org.eclipse.ant.tests.ui.debug.AbstractAntDebugTest.java

License:Open Source License

/**
 * Returns the compilation unit with the given name.
 * //  ww w  . ja  v  a2s.  co  m
 * @param project
 *            the project containing the CU
 * @param root
 *            the name of the source folder in the project
 * @param pkg
 *            the name of the package (empty string for default package)
 * @param name
 *            the name of the CU (ex. Something.java)
 * @return compilation unit
 */
protected ICompilationUnit getCompilationUnit(IJavaProject project, String root, String pkg, String name) {
    IProject p = project.getProject();
    IResource r = p.getFolder(root);
    return project.getPackageFragmentRoot(r).getPackageFragment(pkg).getCompilationUnit(name);
}

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//w  ww  . j a v  a 2  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();
}

From source file:org.eclipse.capra.testsuite.TestHelper.java

License:Open Source License

public static IType createJavaProjectWithASingleJavaClass(String projectName) throws CoreException {
    IProject project = getProject(projectName);

    // Create project
    IProgressMonitor progressMonitor = new NullProgressMonitor();
    project.create(progressMonitor);/*from  w ww. j  av  a  2 s  .c  om*/
    project.open(progressMonitor);

    // Add Java nature
    IProjectDescription description = project.getDescription();
    description.setNatureIds(new String[] { JavaCore.NATURE_ID });
    project.setDescription(description, null);

    // Create as Java project and set up build path etc.
    IJavaProject javaProject = JavaCore.create(project);
    IFolder binFolder = project.getFolder("bin");
    binFolder.create(false, true, null);
    javaProject.setOutputLocation(binFolder.getFullPath(), null);
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    IVMInstall vmInstall = JavaRuntime.getDefaultVMInstall();
    LibraryLocation[] locations = JavaRuntime.getLibraryLocations(vmInstall);
    for (LibraryLocation element : locations)
        entries.add(JavaCore.newLibraryEntry(element.getSystemLibraryPath(), null, null));

    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[entries.size()]), null);

    // Create a src file
    IFolder sourceFolder = project.getFolder("src");
    sourceFolder.create(false, true, null);
    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(sourceFolder);
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
    javaProject.setRawClasspath(newEntries, null);

    IPackageFragment pack = javaProject.getPackageFragmentRoot(sourceFolder)
            .createPackageFragment("org.amalthea.test", false, null);

    StringBuffer buffer = new StringBuffer();
    buffer.append("package " + pack.getElementName() + ";\n");
    buffer.append("\n");
    buffer.append("public class TestClass {}");

    ICompilationUnit icu = pack.createCompilationUnit("TestClass.java", buffer.toString(), false, null);
    return icu.getType("TestClass");
}

From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Creates and returns a compilation unit element for the given <code>.java</code>
 * file, its project being the given project. Returns <code>null</code> if unable
 * to recognize the compilation unit.//from  w  ww  . j  ava 2 s . co  m
 */
public static ICompilationUnit createCompilationUnitFrom(File file, IJavaProject project) {

    if (file == null)
        return null;

    //        if (project == null) {
    //            project = JavaCore.create(file.getProject());
    //        }
    IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, (JavaProject) project);
    if (pkg == null) {
        // not on classpath - make the root its folder, and a default package
        PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent());
        pkg = root.getPackageFragment(CharOperation.NO_STRINGS);

        if (VERBOSE) {
            System.out.println("WARNING : creating unit element outside classpath (" + Thread.currentThread()
                    + "): " + file.getAbsolutePath()); //$NON-NLS-1$//$NON-NLS-2$
        }
    }
    return pkg.getCompilationUnit(file.getName());
}

From source file:org.eclipse.che.jdt.internal.core.JavaModelManager.java

License:Open Source License

/**
 * Creates and returns a class file element for the given <code>.class</code> file,
 * its project being the given project. Returns <code>null</code> if unable
 * to recognize the class file.//from  w  w w  . j  a  v  a2  s  .co  m
 */
public static IClassFile createClassFileFrom(File file, IJavaProject project) {
    if (file == null) {
        return null;
    }
    //        if (project == null) {
    //            project = JavaCore.create(file.getProject());
    //        }
    IPackageFragment pkg = (IPackageFragment) determineIfOnClasspath(file, (JavaProject) project);
    if (pkg == null) {
        // fix for 1FVS7WE
        // not on classpath - make the root its folder, and a default package
        PackageFragmentRoot root = (PackageFragmentRoot) project.getPackageFragmentRoot(file.getParent());
        pkg = root.getPackageFragment(CharOperation.NO_STRINGS);
    }
    return pkg.getClassFile(file.getName());
}

From source file:org.eclipse.che.jdt.testplugin.JavaProjectHelper.java

License:Open Source License

/**
 * Adds a source container to a IJavaProject.
 * @param jproject The parent project//from  www  . j a  v a 2 s .  co  m
 * @param containerName The name of the new source container
 * @param inclusionFilters Inclusion filters to set
 * @param exclusionFilters Exclusion filters to set
 * @param outputLocation The location where class files are written to, <b>null</b> for project output folder
 * @return The handle to the new source container
 * @throws CoreException Creation failed
 */
public static IPackageFragmentRoot addSourceContainer(IJavaProject jproject, String containerName,
        IPath[] inclusionFilters, IPath[] exclusionFilters, String outputLocation) throws CoreException {
    IProject project = jproject.getProject();
    IContainer container = null;
    if (containerName == null || containerName.length() == 0) {
        container = project;
    } else {
        IFolder folder = project.getFolder(containerName);
        if (!folder.exists()) {
            CoreUtility.createFolder(folder, false, true, null);
        }
        container = folder;
    }
    IPackageFragmentRoot root = jproject.getPackageFragmentRoot(container);

    IPath outputPath = null;
    if (outputLocation != null) {
        IFolder folder = project.getFolder(outputLocation);
        if (!folder.exists()) {
            CoreUtility.createFolder(folder, false, true, null);
        }
        outputPath = folder.getFullPath();
    }
    IClasspathEntry cpe = JavaCore.newSourceEntry(root.getPath(), inclusionFilters, exclusionFilters,
            outputPath);
    addToClasspath(jproject, cpe);
    return root;
}

From source file:org.eclipse.che.jdt.testplugin.JavaProjectHelper.java

License:Open Source License

/**
 * Adds a library entry with source attachment to a IJavaProject.
 * @param jproject The parent project/*ww w  .  jav  a2  s  .  c o  m*/
 * @param path The path of the library to add
 * @param sourceAttachPath The source attachment path
 * @param sourceAttachRoot The source attachment root path
 * @return The handle of the created root
 * @throws JavaModelException
 */
public static IPackageFragmentRoot addLibrary(IJavaProject jproject, IPath path, IPath sourceAttachPath,
        IPath sourceAttachRoot) throws JavaModelException {
    IClasspathEntry cpe = JavaCore.newLibraryEntry(path, sourceAttachPath, sourceAttachRoot);
    addToClasspath(jproject, cpe);
    IResource workspaceResource = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
    if (workspaceResource != null) {
        return jproject.getPackageFragmentRoot(workspaceResource);
    }
    return jproject.getPackageFragmentRoot(path.toString());
}

From source file:org.eclipse.che.jdt.testplugin.JavaProjectHelper.java

License:Open Source License

/**
 * Creates and adds a class folder to the class path.
 * @param jproject The parent project/*w ww .  j ava2  s .com*/
 * @param containerName
 * @param sourceAttachPath The source attachment path
 * @param sourceAttachRoot The source attachment root path
 * @return The handle of the created root
 * @throws CoreException
 */
public static IPackageFragmentRoot addClassFolder(IJavaProject jproject, String containerName,
        IPath sourceAttachPath, IPath sourceAttachRoot) throws CoreException {
    IProject project = jproject.getProject();
    IContainer container = null;
    if (containerName == null || containerName.length() == 0) {
        container = project;
    } else {
        IFolder folder = project.getFolder(containerName);
        if (!folder.exists()) {
            CoreUtility.createFolder(folder, false, true, null);
        }
        container = folder;
    }
    IClasspathEntry cpe = JavaCore.newLibraryEntry(container.getFullPath(), sourceAttachPath, sourceAttachRoot);
    addToClasspath(jproject, cpe);
    return jproject.getPackageFragmentRoot(container);
}

From source file:org.eclipse.contribution.weaving.jdt.tests.WeavingTestCase.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);/* w  w  w. j a v  a2  s .com*/
    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;
    }
    IClasspathEntry[] oldEntries = javaProject.getRawClasspath();
    IClasspathEntry[] newEntries = new IClasspathEntry[oldEntries.length + 1];
    System.arraycopy(oldEntries, 0, newEntries, 0, oldEntries.length);
    newEntries[oldEntries.length] = JavaCore.newSourceEntry(root.getPath());
    javaProject.setRawClasspath(newEntries, null);
    return root;
}