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.grails.ide.eclipse.test.util.AbstractGrailsJUnitIntegrationsTest.java

License:Open Source License

public static TestRunSession runAsJUnit(IJavaElement element) throws CoreException, Exception, DebugException {
    StsTestUtil.assertNoErrors(element.getJavaProject().getProject()); //This is to avoid trying to run a project with errors...
    // Trying to do so would hang the test when debug UI pops up a dialog.
    ILaunchConfigurationWorkingCopy launchConf = createLaunchConfiguration(element);
    final MockTestRunListener listener = new MockTestRunListener();
    JUnitCore.addTestRunListener(listener);
    try {/* w  w  w  . j a  v  a 2 s.c om*/
        final ILaunch launch = launchConf.launch(ILaunchManager.RUN_MODE, new NullProgressMonitor(), true);

        new ACondition() {
            @Override
            public boolean test() throws Exception {
                return listener.session != null && launch.isTerminated();
            }
        }.waitFor(80000);
        assertEquals(0, launch.getProcesses()[0].getExitValue());

    } finally {
        JUnitCore.removeTestRunListener(listener);
    }
    return (TestRunSession) listener.session;
}

From source file:org.gw4e.eclipse.facade.JDTManager.java

License:Open Source License

/**
 * @param elt//from  w  w w  .  jav  a  2  s .  c om
 * @return
 * @throws JavaModelException
 */
public static String getFullyQualifiedName(IJavaElement elt) throws JavaModelException {
    IPackageFragmentRoot[] roots = elt.getJavaProject().getPackageFragmentRoots();
    for (int i = 0; i < roots.length; i++) {
        if (roots[i].getPath().isPrefixOf(elt.getPath())) {
            IPath p = elt.getPath().makeRelativeTo(roots[i].getPath());
            return p.toString();
        }
    }
    return elt.getElementName();
}

From source file:org.iti.ast.ParserUtils.java

License:Open Source License

public static IProject getSelectedProject() {
    IEditorPart editor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
    IJavaElement element = JavaUI.getEditorInputJavaElement(editor.getEditorInput());

    return (IProject) element.getJavaProject().getResource();
}

From source file:org.jboss.mapper.eclipse.internal.util.JavaUtil.java

License:Open Source License

/**
 * @param context/* w  w  w.  jav  a2  s.  c  o m*/
 * @return the Java source and runtime compliance levels for the project
 *         containing the supplied Java element
 */
public static String[] getSourceComplianceLevels(IJavaElement context) {
    if (context != null) {
        IJavaProject javaProject = context.getJavaProject();
        if (javaProject != null) {
            return new String[] { javaProject.getOption(JavaCore.COMPILER_SOURCE, true),
                    javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true) };
        }
    }
    return new String[] { JavaCore.getOption(JavaCore.COMPILER_SOURCE),
            JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE) };
}

From source file:org.jboss.tools.arquillian.core.internal.dependencies.DependencyVisitor.java

License:Open Source License

@Override
public boolean visit(SimpleName node) {
    ITypeBinding binding = node.resolveTypeBinding();
    if (binding == null || binding.isPrimitive() || !binding.isFromSource()) {
        return true;
    }/* w ww  .java2  s .  c o m*/
    IJavaElement javaElement = binding.getJavaElement();
    if (javaElement == null || javaElement.getJavaProject() == null
            || !javaElement.getJavaProject().equals(javaProject)) {
        return true;
    }
    for (final String name : binding.getQualifiedName().split("[<>,\\s\\[\\]]+")) { //$NON-NLS-1$
        if (!name.equals(exclude) && !excludeSet.contains(name)) {
            ASTNode parent = node.getParent();
            int charStart;
            int charEnd;
            if (parent != null && parent.getNodeType() == ASTNode.QUALIFIED_NAME) {
                charStart = parent.getStartPosition();
                charEnd = charStart + parent.getLength();
            } else {
                charStart = node.getStartPosition();
                charEnd = charStart + node.getLength();
            }
            int lineNumber = cu.getLineNumber(charStart);
            DependencyType type = new DependencyType(name);
            for (DependencyType t : types) {
                if (type.equals(t)) {
                    type = t;
                    break;
                }
            }
            TypeLocation location = new TypeLocation(charStart, charEnd, lineNumber);
            type.getLocations().add(location);
            types.add(type);
        }

    }
    return true;
}

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

License:Open Source License

/**
 * Returns whether a Java element is an Arquillian JUnit test
 * /*w ww.j  a v  a2s.  com*/
 * @param element the Java element
 * @param checkDeployment check if there is a deployment method
 * @param checkTest check if there is a test method
 * @param checkAbstract check an abstract class
 * @return true if a Java element is an Arquillian JUnit test
 */
public static boolean isArquillianJUnitTest(IJavaElement element, boolean checkDeployment, boolean checkTest,
        boolean checkAbstract) {
    if (element == null || element.getJavaProject() == null
            || !ArquillianUtility.isArquillianProject(element.getJavaProject().getProject())) {
        return false;
    }
    try {
        IType testType = null;
        if (element instanceof ICompilationUnit) {
            testType = (((ICompilationUnit) element)).findPrimaryType();
        } else if (element instanceof IClassFile) {
            testType = (((IClassFile) element)).getType();
        } else if (element instanceof IType) {
            testType = (IType) element;
        } else if (element instanceof IMember) {
            testType = ((IMember) element).getDeclaringType();
        }
        if (testType != null && testType.exists()) {
            return isArquillianJUnitTest(testType, checkDeployment, checkTest, checkAbstract);
        }
    } catch (CoreException e) {
        // ignore, return false
    }
    return false;
}

From source file:org.jboss.tools.arquillian.ui.internal.launcher.ArquillianLaunchShortcut.java

License:Open Source License

private void launch(Object[] elements, String mode) {
    try {//from   www.  j  a  va2 s. c  o  m
        IJavaElement elementToLaunch = null;

        if (elements.length == 1) {
            Object selected = elements[0];
            if (!(selected instanceof IJavaElement) && selected instanceof IAdaptable) {
                selected = ((IAdaptable) selected).getAdapter(IJavaElement.class);
            }
            if (selected instanceof IJavaElement) {
                IJavaElement element = (IJavaElement) selected;
                switch (element.getElementType()) {
                case IJavaElement.JAVA_PROJECT:
                case IJavaElement.PACKAGE_FRAGMENT_ROOT:
                case IJavaElement.PACKAGE_FRAGMENT:
                    IJavaProject javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.TYPE:
                    IType type = (IType) element;
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true)) {
                        elementToLaunch = type;
                    }
                case IJavaElement.METHOD:
                    javaProject = element.getJavaProject();
                    if (ArquillianSearchEngine.hasArquillianType(javaProject)) {
                        elementToLaunch = element;
                    }
                    break;
                case IJavaElement.CLASS_FILE:
                    type = ((IClassFile) element).getType();
                    if (ArquillianSearchEngine.isArquillianJUnitTest(type, true, true, false)) {
                        elementToLaunch = type;
                    }
                    break;
                case IJavaElement.COMPILATION_UNIT:
                    elementToLaunch = findTypeToLaunch((ICompilationUnit) element, mode);
                    break;
                }
            }
        }
        if (elementToLaunch == null) {
            showNoTestsFoundDialog();
            return;
        }
        performLaunch(elementToLaunch, mode);
    } catch (InterruptedException e) {
        // OK, silently move on
    } catch (CoreException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    } catch (InvocationTargetException e) {
        ExceptionHandler.handle(e, getShell(), ARQUILLIAN_J_UNIT_LAUNCH,
                LAUNCHING_OF_ARQILLIAN_J_UNIT_TESTS_FAILED);
    }
}

From source file:org.jboss.tools.arquillian.ui.internal.wizards.NewArquillianJUnitTestCaseDeploymentPage.java

License:Open Source License

private void createButtons(Group group, final TableViewer viewer, final List<?> elements,
        final boolean isType) {
    GridData gd;/*from w w w . j  a v a 2 s. c  om*/
    Composite buttonContainer = new Composite(group, SWT.NONE);
    gd = new GridData(GridData.FILL_VERTICAL);
    buttonContainer.setLayoutData(gd);
    GridLayout buttonLayout = new GridLayout();
    buttonLayout.marginWidth = 0;
    buttonLayout.marginHeight = 0;
    buttonContainer.setLayout(buttonLayout);

    Button addButton = new Button(buttonContainer, SWT.PUSH);
    addButton.setText("Add...");
    gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
    addButton.setLayoutData(gd);
    LayoutUtil.setButtonDimensionHint(addButton);

    Button removeButton;
    Button removeAllButton;

    removeButton = new Button(buttonContainer, SWT.PUSH);
    removeButton.setText("Remove");
    gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
    removeButton.setLayoutData(gd);
    LayoutUtil.setButtonDimensionHint(removeButton);

    removeAllButton = new Button(buttonContainer, SWT.PUSH);
    removeAllButton.setText("Remove All");
    gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
    removeAllButton.setLayoutData(gd);

    if (isType) {
        removeTypeButton = removeButton;
        removeTypeAllButton = removeAllButton;
    } else {
        removeResourceButton = removeButton;
        removeResourceAllButton = removeAllButton;
    }

    removeAllButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            elements.clear();
            if (viewer == typesViewer) {
                viewer.setInput(elements.toArray(new IType[0]));
            }
            if (viewer == resourcesViewer) {
                viewer.setInput(elements.toArray(new ProjectResource[0]));
            }
            viewer.refresh();
            updateButtons(viewer, elements, isType);
        }
    });
    LayoutUtil.setButtonDimensionHint(removeAllButton);

    if (isType) {
        addDependentClassesButton = new Button(buttonContainer, SWT.PUSH);
        addDependentClassesButton.setText("Add Dependent Classes");
        gd = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
        addDependentClassesButton.setLayoutData(gd);
        addDependentClassesButton.addSelectionListener(new SelectionAdapter() {
            @Override
            public void widgetSelected(SelectionEvent e) {
                final IJavaElement element = getTestJavaElement();
                if ((element instanceof ICompilationUnit) && getType((ICompilationUnit) element) != null) {
                    Set<DependencyType> dependentTypes = DependencyCache
                            .getDependentTypes((ICompilationUnit) element);
                    for (DependencyType dependentType : dependentTypes) {
                        try {
                            IType t = element.getJavaProject().findType(dependentType.getName());
                            if (!types.contains(t)) {
                                types.add(t);
                            }
                        } catch (JavaModelException e1) {
                            ArquillianUIActivator.log(e1);
                        }
                    }
                    refreshViewer(viewer, elements, isType);
                }
            }
        });
        LayoutUtil.setButtonDimensionHint(addDependentClassesButton);

    }
    viewer.addSelectionChangedListener(new ISelectionChangedListener() {

        @Override
        public void selectionChanged(SelectionChangedEvent event) {
            updateButtons(viewer, elements, isType);
        }
    });
    updateButtons(viewer, elements, isType);
    removeButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            ISelection selection = viewer.getSelection();
            if (selection instanceof IStructuredSelection) {
                IStructuredSelection sel = (IStructuredSelection) selection;
                Object[] selectedElements = sel.toArray();
                for (Object element : selectedElements) {
                    elements.remove(element);
                }
            }
            if (viewer == typesViewer) {
                viewer.setInput(elements.toArray(new IType[0]));
            }
            if (viewer == resourcesViewer) {
                viewer.setInput(elements.toArray(new ProjectResource[0]));
            }
            viewer.refresh();
            updateButtons(viewer, elements, isType);
        }
    });
    addButton.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            if (viewer == typesViewer) {
                ArquillianTypesSelectionDialog dialog = new ArquillianTypesSelectionDialog(getShell(),
                        getJavaProject(), types);
                dialog.create();
                if (dialog.open() == Window.OK) {
                    Object[] results = dialog.getResult();
                    if (results != null) {
                        for (Object result : results) {
                            if (result instanceof IType) {
                                types.add((IType) result);
                            }
                        }
                    }
                    refreshViewer(viewer, elements, isType);
                }
            }

            if (viewer == resourcesViewer) {
                List<IPath> res = new ArrayList<IPath>();
                for (ProjectResource resource : resources) {
                    res.add(resource.getPath());
                }
                ArquillianResourcesSelectionDialog dialog = new ArquillianResourcesSelectionDialog(getShell(),
                        getJavaProject(), res);
                dialog.create();
                if (dialog.open() == Window.OK) {
                    Object[] results = dialog.getResult();
                    if (results != null) {
                        for (Object result : results) {
                            if (result instanceof IPath) {
                                resources.add(new ProjectResource((IPath) result, false));
                            }
                        }
                    }
                    refreshViewer(viewer, resources, isType);
                }
            }
        }
    });

}

From source file:org.jboss.tools.arquillian.ui.internal.wizards.NewArquillianJUnitTestCaseDeploymentPage.java

License:Open Source License

private static String[] getSourceComplianceLevels(IJavaElement context) {
    if (context != null) {
        IJavaProject javaProject = context.getJavaProject();
        if (javaProject != null) {
            return new String[] { javaProject.getOption(JavaCore.COMPILER_SOURCE, true),
                    javaProject.getOption(JavaCore.COMPILER_COMPLIANCE, true) };
        }/*from   w w  w .  java  2s .  com*/
    }
    return new String[] { JavaCore.getOption(JavaCore.COMPILER_SOURCE),
            JavaCore.getOption(JavaCore.COMPILER_COMPLIANCE) };
}

From source file:org.jboss.tools.batch.ui.participants.BatchArtifactSearchParticipant.java

License:Open Source License

@Override
public void search(ISearchRequestor requestor, QuerySpecification querySpecification, IProgressMonitor monitor)
        throws CoreException {
    if (querySpecification instanceof ElementQuerySpecification) {
        if (!isSearchForReferences(querySpecification.getLimitTo())) {
            return;
        }//ww  w. j a  v a2 s  . c o m

        ElementQuerySpecification qs = (ElementQuerySpecification) querySpecification;
        IJavaElement element = qs.getElement();
        IProject project = element.getJavaProject().getProject();

        BatchProject batchProject = (BatchProject) BatchProjectFactory.getBatchProjectWithProgress(project);

        if (batchProject == null) {
            return;
        }

        if (containsInSearchScope(querySpecification, project.getFullPath())) {
            searchInProject(requestor, querySpecification, batchProject, monitor, element);
        }

        BatchProject[] projects = batchProject.getAllDependentProjects(true);
        for (BatchProject p : projects) {
            if (containsInSearchScope(querySpecification, p.getProject().getFullPath())) {
                searchInProject(requestor, querySpecification, p, monitor, element);
            }
        }
    }
}