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

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

Introduction

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

Prototype

int getKind() throws JavaModelException;

Source Link

Document

Returns this package fragment root's kind encoded as an integer.

Usage

From source file:com.google.gdt.eclipse.appengine.rpc.util.JavaUtils.java

License:Open Source License

public static IStatus validateContainer(String packageRootText) {
    StatusInfo status = new StatusInfo();

    String str = packageRootText;
    if (str.length() == 0) {
        status.setError("Source folder name is empty."); //$NON-NLS-1$
        return status;
    }/*from   w w w .j a  v  a  2s .co m*/
    IPath path = new Path(str);
    IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
    IResource res = workspaceRoot.findMember(path);
    if (res != null) {
        int resType = res.getType();
        if (resType == IResource.PROJECT || resType == IResource.FOLDER) {
            IProject proj = res.getProject();
            if (!proj.isOpen()) {
                status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_ProjectClosed,
                        BasicElementLabels.getPathLabel(proj.getFullPath(), false)));
                return status;
            }
            IJavaProject jproject = JavaCore.create(proj);
            IPackageFragmentRoot root = jproject.getPackageFragmentRoot(res);
            if (res.exists()) {
                if (isVirtual(res)) {
                    status.setError("Source folder cannot be a virtual folder."); //$NON-NLS-1$
                    return status;
                }
                try {
                    if (!proj.hasNature(JavaCore.NATURE_ID)) {
                        if (resType == IResource.PROJECT) {
                            status.setError(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject);
                        } else {
                            status.setWarning(
                                    NewWizardMessages.NewContainerWizardPage_warning_NotInAJavaProject);
                        }
                        return status;
                    }
                    if (root.isArchive()) {
                        status.setError(Messages.format(
                                NewWizardMessages.NewContainerWizardPage_error_ContainerIsBinary,
                                BasicElementLabels.getPathLabel(path, false)));
                        return status;
                    }
                    if (root.getKind() == IPackageFragmentRoot.K_BINARY) {
                        status.setWarning(Messages.format(
                                NewWizardMessages.NewContainerWizardPage_warning_inside_classfolder,
                                BasicElementLabels.getPathLabel(path, false)));
                    } else if (!jproject.isOnClasspath(root)) {
                        status.setWarning(
                                Messages.format(NewWizardMessages.NewContainerWizardPage_warning_NotOnClassPath,
                                        BasicElementLabels.getPathLabel(path, false)));
                    }
                } catch (JavaModelException e) {
                    status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotOnClassPath);
                } catch (CoreException e) {
                    status.setWarning(NewWizardMessages.NewContainerWizardPage_warning_NotAJavaProject);
                }
            }
            return status;
        } else {
            status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_NotAFolder,
                    BasicElementLabels.getPathLabel(path, false)));
            return status;
        }
    } else {
        status.setError(Messages.format(NewWizardMessages.NewContainerWizardPage_error_ContainerDoesNotExist,
                BasicElementLabels.getPathLabel(path, false)));
        return status;
    }
}

From source file:com.google.gdt.eclipse.designer.actions.deploy.DeployModuleAction.java

License:Open Source License

/**
 * Returns ANT code for creating jar's for given project itself, coping its jar's from classpath
 * and calls itself for required projects.
 *//*from ww  w. j  av a2s.  com*/
private static String prepareJars(IProject project, String targetModulePath, boolean addRuntimeJars)
        throws Exception {
    String script = "";
    //
    IJavaProject javaProject = JavaCore.create(project);
    IPackageFragmentRoot[] roots = javaProject.getPackageFragmentRoots();
    // add <jar> task for creating jar from project source and output folders
    {
        List<String> sourceLocations = Lists.newArrayList();
        List<String> binaryLocations = Lists.newArrayList();
        for (IPackageFragmentRoot packageFragmentRoot : roots) {
            if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                // add source location
                sourceLocations.add(packageFragmentRoot.getResource().getLocation().toPortableString());
                // add output location
                {
                    // prepare output location
                    IPath location;
                    {
                        IClasspathEntry cpEntry = packageFragmentRoot.getRawClasspathEntry();
                        location = cpEntry.getOutputLocation();
                        if (location == null) {
                            location = javaProject.getOutputLocation();
                        }
                    }
                    // add absolute location
                    {
                        // remove first segment (project)
                        location = location.removeFirstSegments(1);
                        // prepare absolute location
                        IPath absoluteLocation = project.getLocation().append(location);
                        binaryLocations.add(absoluteLocation.toPortableString());
                    }
                }
            }
        }
        //
        script += "\t\t<!--=== " + project.getName() + " ===-->\n";
        script += "\t\t<jar destfile='" + targetModulePath + "/WEB-INF/lib/" + project.getName() + ".jar'>\n";
        script += prepareFileSets(sourceLocations, "**");
        script += prepareFileSets(binaryLocations, "**/*.class");
        script += "\t\t</jar>\n";
    }
    // add <copy> task for coping required runtime jar's
    if (addRuntimeJars) {
        String jars = "";
        IRuntimeClasspathEntry[] classpathEntries = JavaRuntime.computeUnresolvedRuntimeClasspath(javaProject);
        for (int entryIndex = 0; entryIndex < classpathEntries.length; entryIndex++) {
            IRuntimeClasspathEntry entry = classpathEntries[entryIndex];
            IRuntimeClasspathEntry[] resolvedEntries = JavaRuntime.resolveRuntimeClasspathEntry(entry,
                    javaProject);
            for (int resolvedEntryIndex = 0; resolvedEntryIndex < resolvedEntries.length; resolvedEntryIndex++) {
                IRuntimeClasspathEntry resolvedEntry = resolvedEntries[resolvedEntryIndex];
                if (resolvedEntry.getClasspathProperty() == IRuntimeClasspathEntry.USER_CLASSES
                        && resolvedEntry.getType() == IRuntimeClasspathEntry.ARCHIVE) {
                    String location = resolvedEntry.getLocation();
                    // exclude gwt-user.jar, it is in classpath, but it has Servlet class, so can not be in application
                    if (location.endsWith("gwt-user.jar")) {
                        continue;
                    }
                    // add jar file in fileset
                    jars += "\t\t\t<fileset file=\"" + location + "\"/>\n";
                }
            }
        }
        //
        if (jars.length() != 0) {
            script += "\t\t<copy todir='" + targetModulePath + "/WEB-INF/lib'>\n";
            script += jars;
            script += "\t\t</copy>\n";
        }
    }
    // add required projects
    {
        IProject[] referencedProjects = project.getReferencedProjects();
        for (int i = 0; i < referencedProjects.length; i++) {
            IProject referencedProject = referencedProjects[i];
            script += prepareJars(referencedProject, targetModulePath, false);
        }
    }
    //
    return script;
}

From source file:com.google.gdt.eclipse.designer.launch.AbstractGwtLaunchConfigurationDelegate.java

License:Open Source License

/**
 * GWT requires source folders in classpath (because it has its own compiler), so we should add
 * all source folders for given project and its required projects.
 *///from w w w .  j a v  a 2 s . co m
public static void addSourceFolders(Set<IProject> visitedProjects, List<String> entries, IProject project)
        throws CoreException {
    // HACK: see above
    if (project.getName().endsWith("-design")) {
        return;
    }
    // check for recursion
    if (visitedProjects.contains(project)) {
        return;
    }
    visitedProjects.add(project);
    //
    IJavaProject javaProject = JavaCore.create(project);
    // add source folders for given project
    {
        IPackageFragmentRoot[] packageFragmentRoots = javaProject.getPackageFragmentRoots();
        for (int i = 0; i < packageFragmentRoots.length; i++) {
            IPackageFragmentRoot packageFragmentRoot = packageFragmentRoots[i];
            if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                entries.add(packageFragmentRoot.getResource().getLocation().toPortableString());
            }
        }
    }
    // process required projects
    {
        IProject[] referencedProjects = project.getReferencedProjects();
        for (int i = 0; i < referencedProjects.length; i++) {
            IProject referencedProject = referencedProjects[i];
            addSourceFolders(visitedProjects, entries, referencedProject);
        }
    }
}

From source file:com.google.gdt.eclipse.designer.util.DefaultModuleProvider.java

License:Open Source License

/**
 * @return the module files to which belongs given {@link IResource}, may be <code>null</code> if
 *         no module found. Climbs up by {@link IPackageFragment}'s hierarchy, good for (Case
 *         33265), i.e. Maven-like source folder structure.
 *///w  w w. ja v a2  s . c  om
private static List<IFile> getModuleFiles_java(IResource resource) throws Exception {
    IProject project = resource.getProject();
    IJavaProject javaProject = JavaCore.create(project);
    // prepare package name
    String packageName;
    {
        // prepare folder
        IFolder folder;
        if (resource instanceof IFolder) {
            folder = (IFolder) resource;
        } else if (resource.getParent() instanceof IFolder) {
            folder = (IFolder) resource.getParent();
        } else {
            return ImmutableList.of();
        }
        // prepare package fragment
        IJavaElement javaElement = JavaCore.create(folder);
        if (javaElement instanceof IPackageFragment) {
            IPackageFragment pkgFragment = (IPackageFragment) javaElement;
            packageName = pkgFragment.getElementName();
        } else {
            return ImmutableList.of();
        }
    }
    // prepare source folders
    List<IPackageFragmentRoot> sourceFolders = Lists.newArrayList();
    for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
            sourceFolders.add(packageFragmentRoot);
        }
    }
    // search in this package and packages above
    for (; packageName.length() != 0; packageName = CodeUtils.getPackage(packageName)) {
        for (IPackageFragmentRoot packageFragmentRoot : sourceFolders) {
            IPackageFragment packageFragment = packageFragmentRoot.getPackageFragment(packageName);
            if (packageFragment.exists()) {
                IResource underlyingResource = packageFragment.getUnderlyingResource();
                if (underlyingResource instanceof IFolder) {
                    IFolder folder = (IFolder) underlyingResource;
                    List<IFile> moduleFiles = getModuleFiles(folder, false);
                    if (!moduleFiles.isEmpty()) {
                        return moduleFiles;
                    }
                }
            }
        }
    }
    // no modules
    return ImmutableList.of();
}

From source file:com.google.gdt.eclipse.designer.util.DefaultModuleProvider.java

License:Open Source License

/**
 * @return the module {@link IFile} with given id, may be <code>null</code>.
 *//*from ww w  .  j a v  a2 s  . c o  m*/
private static IFile getModuleFile(IJavaProject javaProject, String moduleId) throws Exception {
    String moduleFileName = moduleId.replace('.', '/') + ".gwt.xml";
    for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        // check only in source folders
        if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
            continue;
        }
        // check IFolder of source folder
        IContainer sourceFolder = (IContainer) packageFragmentRoot.getUnderlyingResource();
        IFile moduleFile = sourceFolder.getFile(new Path(moduleFileName));
        if (moduleFile.exists()) {
            return moduleFile;
        }
    }
    // not found
    return null;
}

From source file:com.google.gdt.eclipse.designer.util.DefaultModuleProvider.java

License:Open Source License

/**
 * @return all module files in given {@link IJavaProject}.
 *///from  w  w w . j a va  2 s .c  o m
private static List<IFile> getModuleFiles(IJavaProject javaProject) throws Exception {
    List<IFile> moduleFiles = Lists.newArrayList();
    for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
        // check only in source folders
        if (packageFragmentRoot.getKind() != IPackageFragmentRoot.K_SOURCE) {
            continue;
        }
        // check packages
        for (IJavaElement rootChild : packageFragmentRoot.getChildren()) {
            // check only packages
            if (rootChild instanceof IPackageFragment) {
                IPackageFragment pkg = (IPackageFragment) rootChild;
                for (Object object : pkg.getNonJavaResources()) {
                    // check only files
                    if (object instanceof IFile) {
                        IFile file = (IFile) object;
                        // check that file is GWT module file
                        if (isModuleFile(file)) {
                            moduleFiles.add(file);
                        }
                    }
                }
            }
        }
    }
    return moduleFiles;
}

From source file:com.google.gdt.eclipse.designer.util.resources.DefaultResourcesProvider.java

License:Open Source License

/**
 * Appends source {@link IPackageFragmentRoot}'s, because {@link DefaultResourcesProvider} should
 * provide access not only to the binary resources (i.e. just resources in classpath), but also to
 * the source resources./*from w  ww.j av a  2 s .  c om*/
 */
private static void addSourceFolders(Set<IProject> visitedProjects, List<String> entries, IProject project)
        throws Exception {
    // may be not exists
    if (!project.exists()) {
        return;
    }
    // may be already visited
    if (visitedProjects.contains(project)) {
        return;
    }
    visitedProjects.add(project);
    // add source folders for IJavaProject
    {
        IJavaProject javaProject = JavaCore.create(project);
        if (javaProject.exists()) {
            for (IPackageFragmentRoot packageFragmentRoot : javaProject.getPackageFragmentRoots()) {
                if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_SOURCE) {
                    entries.add(packageFragmentRoot.getResource().getLocation().toPortableString());
                }
            }
        }
    }
    // process required projects
    for (IProject referencedProject : project.getReferencedProjects()) {
        addSourceFolders(visitedProjects, entries, referencedProject);
    }
}

From source file:com.google.gdt.eclipse.maven.sdk.GWTMavenRuntime.java

License:Open Source License

/**
 * Find the classpath for get-dev.jar which is used to run super dev mode.
 *
 * @return IClasspathEntry for the path to gwt-dev.jar
 * @throws JavaModelException//from w  w w . j av  a  2 s  .  c  om
 */
private IClasspathEntry findGwtCodeServerClasspathEntry() throws JavaModelException {
    IType type = javaProject
            .findType(GwtLaunchConfigurationProcessorUtilities.SUPERDEVMODE_CODESERVER_MAIN_TYPE);
    if (type == null) {
        return null;
    }

    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
        return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
    }

    return null;
}

From source file:com.google.gdt.eclipse.maven.sdk.GWTMavenRuntime.java

License:Open Source License

private IClasspathEntry findGwtUserClasspathEntry() throws JavaModelException {
    /*//ww  w  .  ja  v  a  2  s.c om
     * Note that the type that we're looking for to determine if we're part of the gwt-user library
     * is different than the one that is used by the superclass. This is because the class that the
     * superclass is querying for, "com.google.gwt.core.client.GWT", also exists in the gwt-servlet
     * library, and for some reason, this sometimes ends up on the build path for Maven projects.
     *
     * TODO: See why Maven is putting gwt-servlet on the build path.
     *
     * TODO: Change the class query in the superclass to "com.google.gwt.junit.client.GWTTestCase"
     */
    IType type = javaProject.findType("com.google.gwt.junit.client.GWTTestCase");

    if (type == null) {
        return null;
    }

    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
        // TODO: If the Maven javadoc and source libs for gwt-dev.jar are
        // available, attach them here.
        return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
    }

    return null;
}

From source file:com.google.gdt.eclipse.maven.sdk.GWTMavenRuntime.java

License:Open Source License

private IClasspathEntry findJavaXValidationClasspathEntry() throws JavaModelException {
    IType type = javaProject.findType("javax.validation.Constraint");

    if (type == null) {
        return null;
    }/*from  www.j  a  v a  2s. com*/

    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) type
            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
    if (packageFragmentRoot.getKind() == IPackageFragmentRoot.K_BINARY) {
        return JavaCore.newLibraryEntry(packageFragmentRoot.getPath(), null, null);
    }

    return null;
}