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

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

Introduction

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

Prototype

boolean isOnClasspath(IResource resource);

Source Link

Document

Returns whether the given resource is on the classpath of this project, that is, referenced from a classpath entry and not explicitly excluded using an exclusion pattern.

Usage

From source file:com.amazonaws.eclipse.lambda.project.wizard.util.FunctionProjectUtil.java

License:Open Source License

private static void addTestDirectoryToClasspath(IProject project) {

    try {//from   w w  w .  ja  va 2  s .c  om
        IJavaProject javaProj = JavaCore.create(project);
        IFolder tstFolder = project.getFolder("tst");

        IPackageFragmentRoot tstRoot = javaProj.getPackageFragmentRoot(tstFolder);

        if (javaProj.isOnClasspath(tstRoot))
            return;

        IClasspathEntry[] originalCp = javaProj.getRawClasspath();
        IClasspathEntry[] augmentedCp = new IClasspathEntry[originalCp.length + 1];
        System.arraycopy(originalCp, 0, augmentedCp, 0, originalCp.length);

        augmentedCp[originalCp.length] = JavaCore.newSourceEntry(tstRoot.getPath());
        javaProj.setRawClasspath(augmentedCp, null);

    } catch (Exception e) {
        LambdaPlugin.getDefault().warn("Failed to add tst directory to the classpath", e);
    }
}

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;
    }//ww w  .j a v a  2  s.  c  om
    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.builders.GwtBuilder.java

License:Open Source License

/**
 * Rebuilds GWT project if any of the *.gwt.xml file was modified. We need this to force out
 * {@link MyCompilationParticipant} to validate again source, because *.gwt.xml may be
 * included/removed some other GWT module and some types become accessible/inaccessible in
 * "source" classpath.//  ww w.j a v  a 2  s.  com
 */
private void checkModuleFileModification() throws CoreException {
    // optimization for tests
    if (!MyCompilationParticipant.ENABLED) {
        return;
    }
    //
    // prepare projects with changed *.gwt.xml files
    final Set<IProject> projectsToBuild = Sets.newHashSet();
    IResourceDeltaVisitor visitor = new IResourceDeltaVisitor() {
        public boolean visit(IResourceDelta delta) throws CoreException {
            IResource resource = delta.getResource();
            switch (resource.getType()) {
            case IResource.PROJECT:
                // process only GWT projects
                IProject project = (IProject) resource;
                return Utils.isGWTProject(project);
            case IResource.FOLDER: {
                // process only resources in "src" folders
                IJavaProject javaProject = JavaCore.create(getProject());
                return javaProject.isOnClasspath(resource);
            }
            case IResource.FILE:
                if (Utils.getExactModule(resource) != null) {
                    projectsToBuild.add(resource.getProject());
                }
            }
            return true;
        }
    };
    m_delta.accept(visitor);
    // run "Clean" job for found projects
    if (!projectsToBuild.isEmpty()) {
        WorkspaceJob cleanJob = new WorkspaceJob("GWT project rebuild") {
            @Override
            public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
                for (IProject project : projectsToBuild) {
                    project.build(IncrementalProjectBuilder.CLEAN_BUILD, new NullProgressMonitor());
                }
                return Status.OK_STATUS;
            }
        };
        cleanJob.setRule(ResourcesPlugin.getWorkspace().getRuleFactory().buildRule());
        cleanJob.setUser(true);
        cleanJob.schedule();
    }
}

From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java

License:Open Source License

private String getSourceAnnotationValue(IType clientBundle) throws JavaModelException {
    IJavaProject javaProject = clientBundle.getJavaProject();
    assert (javaProject.isOnClasspath(file));

    IPackageFragment resourcePckg = javaProject.findPackageFragment(file.getParent().getFullPath());

    // If the resource is not in the same package as our ClientBundle, we need
    // an @Source with the full classpath-relative path to the resource.
    if (!clientBundle.getPackageFragment().equals(resourcePckg)) {
        return ResourceUtils.getClasspathRelativePath(resourcePckg, file.getName()).toString();
    }//from  ww  w  . j a  va2  s.c  om

    // If the resource has a different name than the method, we need an @Source,
    // although in this case we don't need the full path.
    String fileNameWithoutExt = ResourceUtils.filenameWithoutExtension(file);
    if (!ResourceUtils.areFilenamesEqual(fileNameWithoutExt, methodName)) {
        return file.getName();
    }

    // If resource doesn't have one of the default extensions, we need @Source.
    IType resourceType = JavaModelSearch.findType(javaProject, resourceTypeName);
    if (!hasDefaultExtension(file, resourceType)) {
        return file.getName();
    }

    // If the resource is in ClientBundle package and its name (without file
    // extension) matches the method name, no need for @Source
    return null;
}

From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResource.java

License:Open Source License

private IStatus validateFile(IJavaProject javaProject) {
    if (file == null || !file.exists()) {
        return errorStatus("The file ''{0}'' does not exist", file.getName());
    }/*from  w  ww .j a va  2  s  . c  om*/

    if (!javaProject.isOnClasspath(file)) {
        return errorStatus("The file ''{0}'' is not on the project''s classpath", file.getName());
    }

    return StatusUtilities.OK_STATUS;
}

From source file:com.google.gwt.eclipse.core.clientbundle.ClientBundleResourceChangeListener.java

License:Open Source License

private static boolean isPossibleClientBundleResourceDelta(IResourceDelta delta) {
    IResource resource = delta.getResource();

    if (resource.isDerived()) {
        return false;
    }//from  w w  w  .j ava2s .  c  o  m

    if (resource.getType() != IResource.FILE) {
        return false;
    }

    // We only care about additions/deletions. If the contents of a
    // resource file change, it doesn't affect our validation.
    if (delta.getKind() != IResourceDelta.ADDED && delta.getKind() != IResourceDelta.REMOVED) {
        return false;
    }

    // Ignore Java source changes, since we're only interested in resource files
    if ("java".equals(resource.getFileExtension())) {
        return false;
    }

    if (!GWTNature.isGWTProject(resource.getProject())) {
        return false;
    }

    IJavaProject javaProject = JavaCore.create(resource.getProject());
    if (!javaProject.isOnClasspath(resource)) {
        return false;
    }

    // All checks passed; it looks like a change we care about
    return true;
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerContentProvider.java

License:Open Source License

private boolean isOnClassPath(ICompilationUnit element) {
    IJavaProject project = element.getJavaProject();
    if (project == null || !project.exists())
        return false;
    return project.isOnClasspath(element);
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerPart.java

License:Open Source License

private Object convertElement(Object original) {
    if (original instanceof IJavaElement) {
        if (original instanceof ICompilationUnit) {
            ICompilationUnit cu = (ICompilationUnit) original;
            IJavaProject javaProject = cu.getJavaProject();
            if (javaProject != null && javaProject.exists() && !javaProject.isOnClasspath(cu)) {
                // could be a working copy of a .java file that is not on classpath
                IResource resource = cu.getResource();
                if (resource != null)
                    return resource;
            }//from   w w  w.  j a va  2 s.co  m

        }
        return original;

    } else if (original instanceof IResource) {
        IJavaElement je = JavaCore.create((IResource) original);
        if (je != null && je.exists()) {
            IJavaProject javaProject = je.getJavaProject();
            if (javaProject != null && javaProject.exists()) {
                if (javaProject.equals(je) || javaProject.isOnClasspath(je)) {
                    return je;
                } else {
                    // a working copy of a .java file that is not on classpath
                    return original;
                }
            }
        }
    } else if (original instanceof IAdaptable) {
        IAdaptable adaptable = (IAdaptable) original;
        IJavaElement je = (IJavaElement) adaptable.getAdapter(IJavaElement.class);
        if (je != null && je.exists())
            return je;

        IResource r = (IResource) adaptable.getAdapter(IResource.class);
        if (r != null) {
            je = JavaCore.create(r);
            if (je != null && je.exists())
                return je;
            else
                return r;
        }
    }
    return original;
}

From source file:com.redhat.ceylon.eclipse.code.explorer.PackageExplorerPart.java

License:Open Source License

private boolean isOnClassPath(IFile file) {
    IJavaProject jproject = JavaCore.create(file.getProject());
    return jproject.isOnClasspath(file);
}

From source file:com.redhat.ceylon.eclipse.core.classpath.CeylonProjectModulesContainer.java

License:Apache License

public static boolean isProjectModule(IJavaProject javaProject, Module module) throws JavaModelException {
    boolean isSource = false;
    for (IPackageFragmentRoot s : javaProject.getPackageFragmentRoots()) {
        if (s.exists() && javaProject.isOnClasspath(s) && s.getKind() == IPackageFragmentRoot.K_SOURCE
                && s.getPackageFragment(module.getNameAsString()).exists()) {
            isSource = true;//from   ww w .j a  va 2  s. com
            break;
        }
    }
    return isSource;
}