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

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

Introduction

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

Prototype

IPackageFragmentRoot findPackageFragmentRoot(IPath path) throws JavaModelException;

Source Link

Document

Returns the existing package fragment root on this project's classpath whose path matches the given (absolute) path, or null if one does not exist.

Usage

From source file:org.eclipse.wb.tests.designer.ercp.wizard.NewErcpProjectWizardTest.java

License:Open Source License

/**
 * Test for creating empty eRCP project.
 *///w ww.j  a v  a 2 s . com
public void test_empty() throws Exception {
    String projectName = "com.project.my";
    try {
        // create eRCP project
        {
            NewProjectCreationOperation operation = new NewProjectCreationOperation(projectName, false);
            operation.run(new NullProgressMonitor());
        }
        // prepare created project
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);
        // check IProject
        assertEquals(projectName, project.getName());
        assertTrue(project.exists());
        assertTrue(project.getFolder("bin").exists());
        assertTrue(project.getFolder("src").exists());
        assertTrue(project.getFolder(new Path("src/" + projectName.replace('.', '/'))).exists());
        assertTrue(
                project.getFile(new Path("src/" + projectName.replace('.', '/') + "/Activator.java")).exists());
        assertTrue(project.getFolder("META-INF").exists());
        assertTrue(project.getFile(new Path("META-INF/MANIFEST.MF")).exists());
        assertTrue(project.getFile("plugin.xml").exists());
        // check IJavaProject
        {
            assertTrue(javaProject.findPackageFragmentRoot(new Path("/" + projectName + "/src")).exists());
            assertNotNull(javaProject.findType(projectName + ".Activator"));
            // check that eRCP types can be found, so MANIFEST.MF has required plugins
            assertNotNull(javaProject.findType("org.eclipse.swt.widgets.Button"));
            // in theory we should check also eRCP classes,
            // but this required "target platform" configuration, skip for now
            if (Math.sqrt(4.0) == 3.0) {
                assertNotNull(javaProject.findType("org.eclipse.ercp.swt.mobile.Command"));
                assertNotNull(javaProject.findType("org.eclipse.ercp.eworkbench.eWorkbench"));
            }
            // no sample types expected
            assertNull(javaProject.findType(projectName + ".views.MyViewPart"));
        }
    } finally {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        project.delete(true, null);
    }
}

From source file:org.eclipse.wb.tests.designer.ercp.wizard.NewEswtProjectWizardTest.java

License:Open Source License

/**
 * Test for creating empty eSWT project.
 *///from  w  w w .jav  a2  s. c  o m
public void test_empty() throws Exception {
    String projectName = "com.project.my";
    try {
        // create eRCP project
        {
            NewProjectCreationOperation operation = new NewProjectCreationOperation(projectName, ERCP_LOCATION,
                    false);
            operation.run(new NullProgressMonitor());
        }
        // prepare created project
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        IJavaProject javaProject = JavaCore.create(project);
        // check IProject
        assertEquals(projectName, project.getName());
        assertTrue(project.exists());
        assertTrue(project.getFolder("bin").exists());
        assertTrue(project.getFolder("src").exists());
        // check IJavaProject
        {
            assertTrue(javaProject.findPackageFragmentRoot(new Path("/" + projectName + "/src")).exists());
            // check that eRCP types can be found, so needed classes are added
            assertNotNull(javaProject.findType("org.eclipse.swt.widgets.Button"));
            assertNotNull(javaProject.findType("org.eclipse.ercp.swt.mobile.Command"));
            assertNotNull(javaProject.findType("org.eclipse.jface.viewers.TableViewer"));
            // no sample types expected
            assertNull(javaProject.findType("com.test.SampleApplication"));
        }
    } finally {
        IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
        project.delete(true, null);
    }
}

From source file:org.eclipse.xtext.builder.JDTAwareEclipseResourceFileSystemAccess2.java

License:Open Source License

/**
 * @since 2.6//from  w  w  w.  j  a v a  2s .  c  o  m
 */
protected void addToSourceFolders(IContainer container) throws JavaModelException {
    IJavaProject jp = JavaCore.create(container.getProject());
    if (jp.exists() && !jp.isOnClasspath(container)) {
        IPackageFragmentRoot currentSource = jp
                .findPackageFragmentRoot(jp.getPath().append(getCurrentSource()));
        if (currentSource != null) {
            IClasspathEntry currentClasspathEntry = currentSource.getRawClasspathEntry();
            if (currentClasspathEntry != null) {
                insertClasspathEntry(container, currentClasspathEntry, jp);
                return;
            }
        }
        addClasspathEntry(container, jp);
    }
}

From source file:org.eclipse.zest.dot.internal.ZestBuilder.java

License:Open Source License

private void importToGeneratedSourceFolder(final IFile dotFile) {
    try {//from w ww.  j  a  va  2s.  c  o m
        IJavaProject javaProject = JavaCore.create(dotFile.getProject());
        String sourceGenPath = "/" + javaProject.getElementName() + "/" + ZestProjectWizard.SRC_GEN;
        IPackageFragmentRoot packageRoot = javaProject.findPackageFragmentRoot(new Path(sourceGenPath));
        IPackageFragment targetPackage = packageRoot.getPackageFragment(ZestProjectWizard.PACKAGE);
        IResource targetFolder = targetPackage.getCorrespondingResource();
        new DotImport(dotFile).newGraphSubclass((IContainer) targetFolder);
        targetFolder.refreshLocal(1, null);
    } catch (CoreException e) {
        e.printStackTrace();
    }
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

private Map<String, ClassNode> internalFindGrailsElementsForProject(PerProjectTypeCache typeCache,
        IJavaProject thisProject, GrailsElementKind kind) throws JavaModelException {
    Map<String, ClassNode> grailsElementMap = new HashMap<String, ClassNode>();
    IClasspathEntry[] rawEntries = thisProject.getRawClasspath();
    for (IClasspathEntry entry : rawEntries) {
        // this may not capture services in plugins because the source folder is linked
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().lastSegment().equals(kind.getSourceFolder())) { //$NON-NLS-1$
            IPackageFragmentRoot root = thisProject.findPackageFragmentRoot(entry.getPath());
            if (root == null)
                continue; // something is wrong with this project

            // all CUs that end in Service are services
            IJavaElement[] frags = root.getChildren();
            for (IJavaElement elt : frags) {
                if (elt instanceof IPackageFragment) {
                    IPackageFragment frag = (IPackageFragment) elt;
                    ICompilationUnit[] units = frag.getCompilationUnits();
                    for (ICompilationUnit unit : units) {
                        if (unit instanceof GroovyCompilationUnit
                                && unit.getElementName().endsWith(kind.getNameSuffix())) { //$NON-NLS-1$
                            IType graileElementType = getPrimaryType(unit);
                            if (graileElementType != null) {
                                grailsElementMap.put(getBeanName(graileElementType),
                                        typeCache.getClassNode(graileElementType.getFullyQualifiedName()));
                            }/*from ww  w  .jav  a 2s  .c  om*/
                        }
                    }
                }
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // trawl through dependent grails projects
            IProject project = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (GrailsNature.isGrailsPluginProject(project)) {
                IJavaProject otherProject = JavaCore.create(project);
                grailsElementMap.putAll(internalFindGrailsElementsForProject(typeCache, otherProject, kind));
            }
        }
    }
    return grailsElementMap;
}

From source file:org.grails.ide.eclipse.editor.groovy.elements.GrailsProject.java

License:Open Source License

private IType internalFindGrailsElementTypeForProject(String primaryTypeName, PerProjectTypeCache typeCache,
        IJavaProject thisProject, GrailsElementKind kind) throws JavaModelException {
    String unitName = primaryTypeName + ".groovy";
    IClasspathEntry[] rawEntries = thisProject.getRawClasspath();
    for (IClasspathEntry entry : rawEntries) {
        // this may not capture services in plugins because the source folder is linked
        if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE
                && entry.getPath().lastSegment().equals(kind.getSourceFolder())) { //$NON-NLS-1$
            IPackageFragmentRoot root = thisProject.findPackageFragmentRoot(entry.getPath());
            if (root == null)
                continue; // something is wrong with this project

            // all CUs that end in Service are services
            IJavaElement[] frags = root.getChildren();
            for (IJavaElement elt : frags) {
                if (elt instanceof IPackageFragment) {
                    IPackageFragment frag = (IPackageFragment) elt;
                    ICompilationUnit[] units = frag.getCompilationUnits();
                    for (ICompilationUnit unit : units) {
                        if (unit instanceof GroovyCompilationUnit && unit.getElementName().equals(unitName)) { //$NON-NLS-1$
                            IType grailsElementType = unit.getType(primaryTypeName);
                            if (grailsElementType.exists()) {
                                return grailsElementType;
                            }// w ww . ja  v  a  2 s  . c o  m
                        }
                    }
                }
            }
        } else if (entry.getEntryKind() == IClasspathEntry.CPE_PROJECT) {
            // trawl through dependent grails projects
            IProject project = ResourcesPlugin.getWorkspace().getRoot()
                    .getProject(entry.getPath().lastSegment());
            if (GrailsNature.isGrailsPluginProject(project)) {
                IJavaProject otherProject = JavaCore.create(project);
                IType type = internalFindGrailsElementTypeForProject(primaryTypeName, typeCache, otherProject,
                        kind);
                if (type != null) {
                    return type;
                }
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.common.model.util.EclipseResourceUtil.java

License:Open Source License

public static IJavaElement findJavaElement(XModelObject object) {
    int type = object.getFileType();
    IResource resource = getResource(object);
    if (resource == null)
        return null;
    IProject project = resource.getProject();
    IJavaProject javaProject = getJavaProject(project);
    if (javaProject == null)
        return null;
    try {/*w  w w . j a  v a  2s.c o m*/
        if (type == XFileObject.SYSTEM)
            return javaProject.findPackageFragmentRoot(resource.getFullPath());
        Path path = null;
        if (type == XFileObject.FOLDER) {
            String p = getJavaPackageName(object);
            if (p == null)
                return null;
            path = new Path(p.replace('.', '/'));
        } else {
            String p = getJavaClassQualifiedName(object);
            if (p == null)
                return null;
            path = new Path(p.replace('.', '/') + ".java"); //$NON-NLS-1$
        }
        return javaProject.findElement(path);
    } catch (CoreException ex) {
        ModelPlugin.getPluginLog().logError(ex);
    }
    return null;
}

From source file:org.jboss.tools.seam.internal.core.scanner.lib.LibraryScanner.java

License:Open Source License

protected void processJavaClasses(XModelObject o, LoadedDeclarations ds)
        throws JavaModelException, ClassFormatException, IOException {
    IJavaProject javaProject = JavaCore.create(classPath.getProject().getProject());
    String location = o.getAttributeValue("location"); //$NON-NLS-1$
    location = XModelObjectUtil.expand(location, o.getModel(), null);

    IFile[] fs = ModelPlugin.getWorkspace().getRoot().findFilesForLocation(new Path(location));
    IPackageFragmentRoot root = null;/*ww  w  . jav  a 2  s  .c o  m*/
    if (fs != null) {
        for (int i = 0; i < fs.length && root == null; i++) {
            root = javaProject.findPackageFragmentRoot(fs[i].getFullPath());
        }
        if (root == null) {
            root = javaProject.findPackageFragmentRoot(new Path(location));
        }
        if (root != null) {
            process(root, ds);
        }
    }
}

From source file:org.jboss.tools.ws.creation.core.commands.RSServiceCreationCommand.java

License:Open Source License

private ICompilationUnit createRESTApplicationClass(String packageName, String className,
        IJavaProject project) {
    try {//from   ww w .j a v a  2 s.c  o  m
        IPath srcPath = new Path(JBossWSCreationUtils.getJavaProjectSrcLocation(project.getProject()));
        srcPath = project.getPath().append(srcPath.makeRelativeTo(project.getProject().getLocation()));
        IPackageFragmentRoot root = project.findPackageFragmentRoot(srcPath);
        if (packageName == null) {
            packageName = ""; //$NON-NLS-1$
        }
        IPackageFragment pkg = root.createPackageFragment(packageName, false, null);
        ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
        if (!packageName.equals("")) { //$NON-NLS-1$
            wrapperCls.createPackageDeclaration(packageName, null);
        }

        StringBuffer clsContent = new StringBuffer();
        clsContent.append("public class ").append(className).append(" extends Application") //$NON-NLS-1$//$NON-NLS-2$
                .append(" {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
        wrapperCls.createType(clsContent.toString(), null, true, null);

        wrapperCls.createImport("java.util.Set", null, null); //$NON-NLS-1$
        wrapperCls.createImport("java.util.HashSet", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.core.Application", null, null); //$NON-NLS-1$

        IType serviceClsType = wrapperCls.findPrimaryType();
        clsContent = new StringBuffer();
        clsContent.append("private Set<Object> singletons = new HashSet<Object>();" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createField(clsContent.toString(), null, false, null);

        clsContent = new StringBuffer();
        clsContent.append("private Set<Class<?>> empty = new HashSet<Class<?>>();" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createField(clsContent.toString(), null, false, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("public " + className + "(){" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("     singletons.add(new " + JBossWSCreationUtils //$NON-NLS-1$
                .classNameFromQualifiedName(model.getServiceClasses().get(0)) + "());" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("public Set<Class<?>> getClasses() {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("     return empty;" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        clsContent = new StringBuffer();
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Override" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("public Set<Object> getSingletons() {" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("     return singletons;" + LINE_SEPARATOR); //$NON-NLS-1$
        clsContent.append("}" + LINE_SEPARATOR); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);

        wrapperCls.save(null, true);
        return wrapperCls;
    } catch (Exception e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
        return null;
    }
}

From source file:org.jboss.tools.ws.creation.core.commands.RSServiceCreationCommand.java

License:Open Source License

private ICompilationUnit createRESTAnnotatedJavaClass(String packageName, String className,
        IJavaProject project) {
    try {//from  w w  w  .j a  v a2 s. c o  m
        IPath srcPath = new Path(JBossWSCreationUtils.getJavaProjectSrcLocation(project.getProject()));
        srcPath = project.getPath().append(srcPath.makeRelativeTo(project.getProject().getLocation()));
        IPackageFragmentRoot root = project.findPackageFragmentRoot(srcPath);
        if (packageName == null) {
            packageName = ""; //$NON-NLS-1$
        }
        IPackageFragment pkg = root.createPackageFragment(packageName, false, null);
        ICompilationUnit wrapperCls = pkg.createCompilationUnit(className + ".java", "", true, null); //$NON-NLS-1$//$NON-NLS-2$
        if (!packageName.equals("")) { //$NON-NLS-1$
            wrapperCls.createPackageDeclaration(packageName, null);
        }

        StringBuffer clsContent = new StringBuffer();
        clsContent.append("@Path(\"/" + model.getServiceName() + "\")").append(LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("public class ").append(className).append(" {" + LINE_SEPARATOR); //$NON-NLS-1$ //$NON-NLS-2$
        clsContent.append("}").append(LINE_SEPARATOR); //$NON-NLS-1$
        wrapperCls.createType(clsContent.toString(), null, true, null);

        wrapperCls.createImport("javax.ws.rs.Produces", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.GET", null, null); //$NON-NLS-1$
        wrapperCls.createImport("javax.ws.rs.Path", null, null); //$NON-NLS-1$

        IType serviceClsType = wrapperCls.findPrimaryType();
        clsContent = new StringBuffer();
        clsContent.append("@GET()"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("@Produces(\"text/plain\")"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("public String sayHello() {"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("    return \"Hello World!\";"); //$NON-NLS-1$
        clsContent.append(LINE_SEPARATOR);
        clsContent.append("}"); //$NON-NLS-1$
        serviceClsType.createMethod(clsContent.toString(), null, true, null);
        wrapperCls.save(null, true);
        return wrapperCls;
    } catch (Exception e) {
        JBossWSCreationCorePlugin.getDefault().logError(e);
        return null;
    }
}