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.gw4e.eclipse.test.facade.ResourceManagerTest.java

License:Open Source License

@Test
public void testGetPackageFragment() throws Exception {
    IJavaProject project = ProjectHelper.getOrCreateSimpleGW4EProject(PROJECT_NAME, true, false);
    ResourceManager.createFile(project.getProject().getName(), "src/test/resources", "mypkg/subpkg",
            "test.java", "");
    IPackageFragment pf = ResourceManager.getPackageFragment(project.getProject(),
            new Path("src/test/resources/mypkg/subpkg"));
    assertNotNull(pf);//from   w ww.j  a  v a  2s  .  c o  m

    IPackageFragment frag = (IPackageFragment) project.findElement(new Path("mypkg/subpkg"));
    IPackageFragmentRoot pfr = ResourceManager.getPackageFragmentRoot(project.getProject(), frag);
    pf = ResourceManager.getPackageFragment(project.getProject(), pfr.getPath().removeFirstSegments(1));
    assertNotNull(pf);
}

From source file:org.gw4e.eclipse.test.fwk.GenerationFactory.java

License:Open Source License

public static TestResourceGeneration get(IFile file) throws CoreException, FileNotFoundException {
    String targetFolder = GraphWalkerContextManager.getTargetFolderForTestImplementation(file);
    IPath pkgFragmentRootPath = file.getProject().getFullPath().append(new Path(targetFolder));
    IPackageFragmentRoot implementationFragmentRoot = JDTManager.getPackageFragmentRoot(file.getProject(),
            pkgFragmentRootPath);/*from w  w w  .j av  a 2 s  .c o  m*/
    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, false, ce);

    TestResourceGeneration trg = new TestResourceGeneration(context);
    return trg;
}

From source file:org.gw4e.eclipse.test.fwk.GenerationFactory.java

License:Open Source License

public static ResourceContext getResourceContext(IFile file) throws CoreException, FileNotFoundException {
    String targetFolder = GraphWalkerContextManager.getTargetFolderForTestImplementation(file);
    IPath pkgFragmentRootPath = file.getProject().getFullPath().append(new Path(targetFolder));
    IPackageFragmentRoot implementationFragmentRoot = JDTManager.getPackageFragmentRoot(file.getProject(),
            pkgFragmentRootPath);//w  ww.j av a 2  s  . c  om
    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, false, ce);

    return context;
}

From source file:org.hibernate.eclipse.console.test.project.SimpleTestProject.java

License:Open Source License

private IPackageFragmentRoot buildSourceFolder(IProject project, IJavaProject javaProject)
        throws CoreException {
    IFolder folder = project.getFolder(SRC_FOLDER);
    folder.create(false, true, null);/*from   w w w.  ja v  a 2 s. co  m*/
    IPackageFragmentRoot root = javaProject.getPackageFragmentRoot(folder);
    IClasspathEntry[] newEntries = { JavaCore.newSourceEntry(root.getPath()) };
    javaProject.setRawClasspath(newEntries, null);
    return root;
}

From source file:org.hibernate.eclipse.console.test.project.TestProject.java

License:Open Source License

public void generateClassPath(List<IPath> libs, IPackageFragmentRoot sourceFolder) throws JavaModelException {
    List<IClasspathEntry> entries = new ArrayList<IClasspathEntry>();
    //entries.addAll(Arrays.asList(javaProject.getRawClasspath()));
    for (IPath lib_path : libs) {
        entries.add(JavaCore.newLibraryEntry(lib_path, null, null));
    }/* w w w.j  av a2 s.co m*/
    if (sourceFolder != null) {
        entries.add(JavaCore.newSourceEntry(sourceFolder.getPath()));
    }
    entries.add(JavaCore.newContainerEntry(JRE_CONTAINER));
    javaProject.setRawClasspath(entries.toArray(new IClasspathEntry[0]), null);
}

From source file:org.hibernate.eclipse.console.test.project.xpl.JavaProjectHelper.java

License:Open Source License

/**
 * Creates and adds a class folder to the class path and imports all files
 * contained in the given ZIP file.// w ww  .  j  a  v  a  2 s  .  c  o m
 * @param jproject The parent project
 * @param containerName 
 * @param sourceAttachPath The source attachment path
 * @param sourceAttachRoot The source attachment root path
 * @param zipFile 
 * @return The handle of the created root
 * @throws IOException 
 * @throws CoreException 
 * @throws InvocationTargetException 
 */
public static IPackageFragmentRoot addClassFolderWithImport(IJavaProject jproject, String containerName,
        IPath sourceAttachPath, IPath sourceAttachRoot, File zipFile)
        throws IOException, CoreException, InvocationTargetException {
    ZipFile file = new ZipFile(zipFile);
    try {
        IPackageFragmentRoot root = addClassFolder(jproject, containerName, sourceAttachPath, sourceAttachRoot);
        importFilesFromZip(file, root.getPath(), null);
        return root;
    } finally {
        file.close();
    }
}

From source file:org.j2eespider.util.ClassSearchUtil.java

License:Open Source License

/**
 * Return path of JavaSources defined in .classpath
 * @return/*from   w  ww  .j  a v  a  2  s  .  c  o  m*/
 * @throws JavaModelException
 */
public static List<String> getJavaSources() throws JavaModelException {
    List<String> javaSources = new ArrayList<String>();

    IJavaProject javaProject = JavaCore.create(ConfigurationEditor.activeProject);
    IPackageFragmentRoot[] roots = javaProject.getAllPackageFragmentRoots();
    if (roots != null && roots.length > 0) {
        for (IPackageFragmentRoot root : roots) {
            if (root.getPath().segment(0).equals(javaProject.getElementName())) {
                javaSources.add(root.getPath().removeFirstSegments(1).makeRelative().toString());
            }
        }
    }

    return javaSources;
}

From source file:org.jboss.ide.eclipse.as.ui.mbeans.wizards.pages.NewSessionBeanWizardPage.java

License:Open Source License

protected IPackageFragment getPackageFragment(String packageName) throws CoreException {
    // This was copied over from NewTypeWizardPage.packageChanged, and made more generic so we could use it for multiple package inputs
    StatusInfo status = new StatusInfo();

    if (packageName.length() > 0) {
        IStatus val = JavaConventions.validatePackageName(packageName);
        if (val.getSeverity() == IStatus.ERROR) {
            status.setError(NewWizardMessages.bind(NewWizardMessages.NewTypeWizardPage_error_InvalidPackageName,
                    val.getMessage()));
            throw new CoreException(status);
        } else if (val.getSeverity() == IStatus.WARNING) {
            status.setWarning(NewWizardMessages.bind(
                    NewWizardMessages.NewTypeWizardPage_warning_DiscouragedPackageName, val.getMessage()));
            // continue
        }/*  ww  w .  j  a v  a  2  s .c  o m*/
    } else {
        status.setWarning(NewWizardMessages.NewTypeWizardPage_warning_DefaultPackageDiscouraged);
    }

    IPackageFragmentRoot root = getPackageFragmentRoot();
    if (root != null) {
        if (root.getJavaProject().exists() && packageName.length() > 0) {
            try {
                IPath rootPath = root.getPath();
                IPath outputPath = root.getJavaProject().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(packageName.replace('.', '/'));
                    if (outputPath.isPrefixOf(packagePath)) {
                        status.setError(NewWizardMessages.NewTypeWizardPage_error_ClashOutputLocation);
                        throw new CoreException(status);
                    }
                }
            } catch (JavaModelException e) {
                JavaPlugin.log(e);
                // let pass         
            }
        }

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

    throw new CoreException(status);
}

From source file:org.jboss.tools.arquillian.core.internal.util.ArquillianSearchEngine.java

License:Open Source License

private static IFile getFile(IJavaProject javaProject, String fileName) throws JavaModelException {
    IClasspathEntry[] rawClasspath = javaProject.getRawClasspath();
    for (IClasspathEntry entry : rawClasspath) {
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
            IPackageFragmentRoot[] roots = javaProject.findPackageFragmentRoots(entry);
            if (roots == null) {
                continue;
            }//from  ww w .j av  a2 s.  c o  m
            for (IPackageFragmentRoot root : roots) {
                IPath path = root.getPath();
                path = path.append(fileName);
                IWorkspaceRoot wsRoot = ResourcesPlugin.getWorkspace().getRoot();
                IFile file = wsRoot.getFile(path);
                if (file != null && file.exists()) {
                    return file;
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.arquillian.ui.internal.detectors.ArquillianResourceHyperlinkDetector.java

License:Open Source License

private IFile getFile(String resource, ITypeRoot javaElement) {
    IJavaProject project = javaElement.getJavaProject();
    try {/*from w  ww.  j  a  v a 2 s. com*/
        IPackageFragmentRoot[] roots = project.getAllPackageFragmentRoots();
        for (IPackageFragmentRoot root : roots) {
            if (root.getKind() == IPackageFragmentRoot.K_SOURCE) {
                IPath path = root.getPath();
                path = path.append(resource);
                path = path.removeFirstSegments(1);
                IFile file = project.getProject().getFile(path);
                if (file != null && file.exists()) {
                    return file;
                }
            }
        }
    } catch (JavaModelException e) {
        ArquillianUIActivator.log(e);
    }
    return null;
}