Example usage for org.eclipse.jdt.core IJavaElement getJavaProject

List of usage examples for org.eclipse.jdt.core IJavaElement getJavaProject

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IJavaElement getJavaProject.

Prototype

IJavaProject getJavaProject();

Source Link

Document

Returns the Java project this element is contained in, or null if this element is not contained in any Java project (for instance, the IJavaModel is not contained in any Java project).

Usage

From source file:org.eclipse.buildship.ui.launch.JavaElementResolver.java

License:Open Source License

private Optional<IMethod> resolveMethod(IJavaElement javaElement) {
    // exclude methods which have no parent projects
    if (javaElement.getJavaProject() == null || javaElement.getJavaProject().getProject() == null) {
        return Optional.absent();
    }//from   ww  w.ja  v a2  s . c o  m

    if (javaElement instanceof IMethod) {
        IMethod method = (IMethod) javaElement;
        return method.getDeclaringType() != null ? Optional.of(method) : Optional.<IMethod>absent();
    } else {
        return Optional.absent();
    }
}

From source file:org.eclipse.buildship.ui.launch.JavaElementResolver.java

License:Open Source License

private Optional<IType> resolveType(IJavaElement javaElement) {
    // exclude elements with no parent projects
    if (javaElement.getJavaProject() == null || javaElement.getJavaProject().getProject() == null) {
        return Optional.absent();
    }/* ww  w .  ja va  2  s  .c o  m*/

    IType result = null;
    switch (javaElement.getElementType()) {
    case IJavaElement.TYPE:
        result = (IType) javaElement;
        break;
    case IJavaElement.FIELD:
        result = ((IField) javaElement).getDeclaringType();
        break;
    case IJavaElement.CLASS_FILE:
    case IJavaElement.COMPILATION_UNIT:
        result = ((ITypeRoot) javaElement).findPrimaryType();
        break;
    }

    return Optional.fromNullable(result);
}

From source file:org.eclipse.buildship.ui.launch.JavaElementResolver.java

License:Open Source License

/**
 * Iterates through the java elements and returns the first non-null container project.
 *
 * @return the container project or absent value if none
 *///from   w ww.ja v  a  2  s .c  o  m
public Optional<IProject> findFirstContainerProject() {
    for (IJavaElement javaElement : findJavaElements()) {
        IJavaProject javaProject = javaElement.getJavaProject();
        if (javaProject != null) {
            IProject project = javaProject.getProject();
            if (project != null) {
                return Optional.of(project);
            }
        }
    }
    return Optional.absent();
}

From source file:org.eclipse.buildship.ui.launch.TestLaunchShortcutValidator.java

License:Open Source License

private static boolean validateJavaElements(List<? extends IJavaElement> elements) {
    // at least one java element is present
    if (elements.isEmpty()) {
        return false;
    }//from   w w w. j ava 2 s  .c o  m

    // all elements have associated projects
    for (IJavaElement element : elements) {
        if (element.getJavaProject() == null || element.getJavaProject().getProject() == null) {
            return false;
        }
    }

    // all elements belong to the same project
    IProject project = elements.get(0).getJavaProject().getProject();
    for (IJavaElement element : elements) {
        if (!element.getJavaProject().getProject().equals(project)) {
            return false;
        }
    }

    // the container project has the Gradle nature
    if (!project.isAccessible() || !GradleProjectNature.INSTANCE.isPresentOn(project)) {
        return false;
    }

    // otherwise the collection is valid
    return true;
}

From source file:org.eclipse.che.jdt.javadoc.JavadocContentAccess2.java

License:Open Source License

private static CompilationUnit createAST(IJavaElement element, String cuSource) {
    Assert.isNotNull(element);// w w  w  .j  a v a2s. c o  m
    CheASTParser parser = CheASTParser.newParser(AST.JLS8);

    IJavaProject javaProject = element.getJavaProject();
    parser.setProject(javaProject);
    Map<String, String> options = javaProject.getOptions(true);
    options.put(JavaCore.COMPILER_DOC_COMMENT_SUPPORT, JavaCore.ENABLED); // workaround for https://bugs.eclipse.org/bugs/show_bug.cgi?id=212207
    parser.setCompilerOptions(options);

    parser.setSource(cuSource.toCharArray());
    return (CompilationUnit) parser.createAST(null);
}

From source file:org.eclipse.contribution.jdt.preferences.WeavableProjectListener.java

License:Open Source License

public boolean isInWeavableProject(IJavaElement element) {
    if (element != null) {
        IJavaProject jProject = element.getJavaProject();
        if (jProject != null) {
            IProject project = jProject.getProject();
            return isWeavableProject(project);
        }//from   w  w  w .  j  av  a2 s .  com
    }
    return false;
}

From source file:org.eclipse.contribution.visualiser.jdtImpl.JDTContentProvider.java

License:Open Source License

/** 
 * Keeps the currentResource and currentProject information up to date
 * in this class, as this method is called whenever a user changes
 * their selection in the workspace.// w  w w . j  a v  a2  s . c  o  m
 */
public void selectionChanged(IWorkbenchPart iwp, ISelection is) {
    if (!(ProviderManager.getContentProvider().equals(this))) {
        return;
    }

    boolean updateRequired = false;

    try {
        if (is instanceof IStructuredSelection) {
            IStructuredSelection structuredSelection = (IStructuredSelection) is;
            Object o = structuredSelection.getFirstElement();

            if (o != null) {
                if (o instanceof IResource) {
                    currentlySelectedResource = (IResource) o;
                } else if (o instanceof IJavaElement) {
                    IJavaElement je = (IJavaElement) o;
                    currentlySelectedJE = je;
                    if (je.getUnderlyingResource() != null) {
                        if (!je.getUnderlyingResource().equals(currentlySelectedResource))
                            updateRequired = true;
                        currentlySelectedResource = je.getUnderlyingResource();
                        // Might be null!
                    }
                    if (je.getJavaProject() != null) {
                        setCurrentProject(je.getJavaProject());
                    }
                }
            }
        } else if (is instanceof ITextSelection) {
        }
        if (updateRequired) {
            VisualiserPlugin.refresh();
        }
    } catch (JavaModelException jme) {
        VisualiserPlugin.logException(jme);
    }
}

From source file:org.eclipse.contribution.visualiser.jdtImpl.JDTContentProvider.java

License:Open Source License

/**
 * Attempts to find some data to display by looking for selections in each
 * of the packages view, projects view, and package explorer (in turn). The
 * first selection found is used to refresh the visualiser. It is called by
 * the visualiser if it receives a paint request, but hasn't been given any
 * data. Selections are generally preserved by Eclipse across sessions, so
 * this should result in the visualisation from a previous session being
 * restored. This method should not be used when the provider is
 * initialised, because that may occur before the other views have been
 * created./*from ww w . j  a v a2 s . co  m*/
 *  
 */
public void lookForData() {
    IWorkbenchWindow iww = VisualiserPlugin.getActiveWorkbenchWindow();
    if (iww != null) {
        IWorkbenchPage iwp = iww.getActivePage();
        if (iwp != null) {
            String[] views = new String[] { JavaUI.ID_PACKAGES_VIEW, JavaUI.ID_PROJECTS_VIEW,
                    JavaUI.ID_PACKAGES };
            for (int i = 0; i < views.length; i++) {
                IViewPart ivp = iwp.findView(views[i]);
                if (ivp != null) {
                    ISelectionProvider isp = ivp.getViewSite().getSelectionProvider();
                    if (isp != null) {
                        ISelection is = isp.getSelection();
                        if (is instanceof IStructuredSelection) {
                            IStructuredSelection ss = (IStructuredSelection) is;
                            Object o = ss.getFirstElement();
                            if ((o != null) && (o instanceof IJavaElement)) {
                                IJavaElement je = (IJavaElement) o;
                                currentlySelectedJE = je;
                                if (je.getJavaProject() != null) {
                                    setCurrentProject(je.getJavaProject());
                                }
                                VisualiserPlugin.refresh();
                                return;
                            }
                        }
                    }
                }
            }
        }
    }
}

From source file:org.eclipse.contribution.weaving.jdt.tests.refactoring.MockRefactoringProvider.java

License:Open Source License

public boolean belongsToInterestingCompilationUnit(IJavaElement elt) {
    try {//from w w w  .  j av a2  s . c om
        return elt.getJavaProject().getProject().hasNature(MockNature.ID_NATURE);
    } catch (CoreException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.eclipse.emf.cdo.releng.apireports.ApiReportsActor.java

License:Open Source License

public static ApiScope walkStructureSelection(List<Object> projects, IProgressMonitor monitor) {
    ApiScope scope = new ApiScope();
    IApiBaseline workspaceBaseline = ApiBaselineManager.getManager().getWorkspaceBaseline();
    if (workspaceBaseline == null) {
        return scope;
    }//from   w w w  . j a va2 s  .co m

    Collections.sort(projects, new Comparator() {
        public int compare(Object o1, Object o2) {
            if (o1 instanceof IJavaElement && o2 instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) o1;
                IJavaElement element2 = (IJavaElement) o2;
                return element.getElementType() - element2.getElementType();
            }

            return 0;
        }
    });

    for (Object project : projects) {
        if (project instanceof IJavaElement) {
            IJavaElement element = (IJavaElement) project;
            IJavaProject javaProject = element.getJavaProject();
            try {
                switch (element.getElementType()) {
                case IJavaElement.COMPILATION_UNIT: {
                    ICompilationUnit compilationUnit = (ICompilationUnit) element;
                    IApiComponent apiComponent = workspaceBaseline
                            .getApiComponent(javaProject.getElementName());
                    if (apiComponent != null) {
                        addElementFor(compilationUnit, apiComponent, scope);
                    }

                    break;
                }
                case IJavaElement.PACKAGE_FRAGMENT: {
                    IPackageFragment fragment = (IPackageFragment) element;
                    IApiComponent apiComponent = workspaceBaseline
                            .getApiComponent(javaProject.getElementName());
                    IPackageFragmentRoot packageFragmentRoot = (IPackageFragmentRoot) fragment
                            .getAncestor(IJavaElement.PACKAGE_FRAGMENT_ROOT);
                    boolean isArchive = false;
                    if (packageFragmentRoot != null) {
                        isArchive = packageFragmentRoot.isArchive();
                    }

                    if (apiComponent != null) {
                        addElementFor(fragment, isArchive, apiComponent, scope);
                    }

                    break;
                }
                case IJavaElement.PACKAGE_FRAGMENT_ROOT: {
                    IPackageFragmentRoot fragmentRoot = (IPackageFragmentRoot) element;
                    IApiComponent apiComponent = workspaceBaseline
                            .getApiComponent(javaProject.getElementName());
                    if (apiComponent != null) {
                        addElementFor(fragmentRoot, apiComponent, scope);
                    }

                    break;
                }
                case IJavaElement.JAVA_PROJECT:
                    IApiComponent apiComponent = workspaceBaseline
                            .getApiComponent(javaProject.getElementName());
                    if (apiComponent != null) {
                        scope.addElement(apiComponent);
                    }

                    break;
                }
            } catch (JavaModelException e) {
                ApiPlugin.log(e);
            } catch (CoreException e) {
                ApiPlugin.log(e);
            }
        }
    }

    return scope;
}