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

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

Introduction

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

Prototype

int METHOD

To view the source code for org.eclipse.jdt.core IJavaElement METHOD.

Click Source Link

Document

Constant representing a method or constructor.

Usage

From source file:org.grails.ide.eclipse.search.action.ControllerActionSearch.java

License:Open Source License

public ControllerActionSearch(QuerySpecification specification) throws JavaModelException {
    if (specification instanceof ElementQuerySpecification) {
        ElementQuerySpecification spec = (ElementQuerySpecification) specification;
        if (SearchUtil.wantsReferences(spec)) {
            IJavaSearchScope scope = spec.getScope();
            IJavaElement el = spec.getElement();
            if (el.getElementType() == IJavaElement.METHOD || el.getElementType() == IJavaElement.FIELD) {
                IMember targetAction = (IMember) el;
                IType targetType = (IType) el.getParent();
                String targetActionName = targetAction.getElementName();
                IJavaProject project = targetAction.getJavaProject();
                if (project != null) {
                    GrailsProject grailsProject = GrailsWorkspaceCore.get().create(project);
                    if (grailsProject != null) {
                        init(grailsProject, targetType.getElementName(), targetActionName, scope);
                    }//from  w ww  .j  a v  a 2 s . c o  m
                }
            }
        }
    }
}

From source file:org.grails.ide.eclipse.test.util.AbstractGrailsJUnitIntegrationsTest.java

License:Open Source License

/**
 * COPIED from JUnitLaunchShortcut... create a JUnit lauch config just like the one the JUnit UI would.
 *///from w ww.j av  a2  s .c  o m
public static ILaunchConfigurationWorkingCopy createLaunchConfiguration(IJavaElement element)
        throws CoreException {
    final String testName;
    final String mainTypeQualifiedName;
    final String containerHandleId;

    switch (element.getElementType()) {
    case IJavaElement.JAVA_PROJECT:
    case IJavaElement.PACKAGE_FRAGMENT_ROOT:
    case IJavaElement.PACKAGE_FRAGMENT: {
        String name = element.getElementName();
        containerHandleId = element.getHandleIdentifier();
        mainTypeQualifiedName = "";
        testName = name.substring(name.lastIndexOf(IPath.SEPARATOR) + 1);
    }
        break;
    case IJavaElement.TYPE: {
        containerHandleId = "";
        mainTypeQualifiedName = ((IType) element).getFullyQualifiedName('.'); // don't replace, fix for binary inner types
        testName = element.getElementName();
    }
        break;
    case IJavaElement.METHOD: {
        IMethod method = (IMethod) element;
        containerHandleId = "";
        mainTypeQualifiedName = method.getDeclaringType().getFullyQualifiedName('.');
        testName = method.getDeclaringType().getElementName() + '.' + method.getElementName();
    }
        break;
    default:
        throw new IllegalArgumentException(
                "Invalid element type to create a launch configuration: " + element.getClass().getName()); //$NON-NLS-1$
    }

    String testKindId = TestKindRegistry.getContainerTestKindId(element);

    ILaunchConfigurationType configType = getLaunchManager()
            .getLaunchConfigurationType(JUnitLaunchConfigurationConstants.ID_JUNIT_APPLICATION);
    ILaunchConfigurationWorkingCopy wc = configType.newInstance(null,
            getLaunchManager().generateLaunchConfigurationName(testName));

    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, mainTypeQualifiedName);
    wc.setAttribute(IJavaLaunchConfigurationConstants.ATTR_PROJECT_NAME,
            element.getJavaProject().getElementName());
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_KEEPRUNNING, false);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_CONTAINER, containerHandleId);
    wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_RUNNER_KIND, testKindId);
    JUnitMigrationDelegate.mapResources(wc);
    //AssertionVMArg.setArgDefault(wc);
    if (element instanceof IMethod) {
        wc.setAttribute(JUnitLaunchConfigurationConstants.ATTR_TEST_METHOD_NAME, element.getElementName()); // only set for methods
    }
    return wc;
}

From source file:org.incha.core.jswingripples.MethodOverrideTester.java

License:Open Source License

/**
 * Evaluates if a member in the focus' element hierarchy is visible from
 * elements in a package.//w  ww .j av  a2 s . c o m
 * @param member The member to test the visibility for
 * @param pack The package of the focus element focus
 * @return returns <code>true</code> if the member is visible from the package
 * @throws JavaModelException thrown when the member can not be accessed
 */
public static boolean isVisibleInHierarchy(final IMember member, final IPackageFragment pack)
        throws JavaModelException {
    final int type = member.getElementType();
    if (type == IJavaElement.INITIALIZER
            || (type == IJavaElement.METHOD && member.getElementName().startsWith("<"))) { //$NON-NLS-1$
        return false;
    }

    final int otherflags = member.getFlags();

    final IType declaringType = member.getDeclaringType();
    if (Flags.isPublic(otherflags) || Flags.isProtected(otherflags)
            || (declaringType != null && declaringType.isInterface())) {
        return true;
    } else if (Flags.isPrivate(otherflags)) {
        return false;
    }

    final IPackageFragment otherpack = (IPackageFragment) member.getAncestor(IJavaElement.PACKAGE_FRAGMENT);
    return (pack != null && pack.equals(otherpack));
}

From source file:org.jboss.tools.arquillian.ui.internal.commands.ArquillianPropertyTester.java

License:Open Source License

private boolean canLaunchAsArquillianJUnitTest(IJavaElement element) {
    try {/*from w  w  w .  j  a  va  2 s.  co m*/
        switch (element.getElementType()) {
        case IJavaElement.JAVA_PROJECT:
        case IJavaElement.PACKAGE_FRAGMENT_ROOT:
            return true; // can run, let test runner detect if there are tests
        case IJavaElement.PACKAGE_FRAGMENT:
            return ((IPackageFragment) element).hasChildren();
        case IJavaElement.COMPILATION_UNIT:
        case IJavaElement.CLASS_FILE:
        case IJavaElement.TYPE:
        case IJavaElement.METHOD:
            return ArquillianSearchEngine.isArquillianJUnitTest(element, true, true, false);
        default:
            return false;
        }
    } catch (JavaModelException e) {
        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 ww w  .  ja v a 2 s. c  om*/
        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.common.validation.java.JavaDirtyRegionProcessor.java

License:Open Source License

private boolean isJavaElementValidationRequired() {
    ICompilationUnit unit = fReporter.getCompilationUnit();
    if (unit == null)
        return false;

    boolean result = false;
    boolean atLeastOneElementIsProcessed = false;

    int position = fStartRegionToProcess;
    try {/* w w w .j a  v  a 2s  .c o m*/
        unit = unit.getWorkingCopy(null);
        IJavaElement element = null;
        while (position >= 0 && (element = unit.getElementAt(position--)) == null)
            ;

        if (position < 0)
            position = 0;

        ITypedRegion[] partitions = computePartitioning(position, fEndPartitionsToProcess - position);

        ITypedRegion startPartition = findPartitionByOffset(partitions, position);
        ITypedRegion endPartition = (startPartition != null && fEndRegionToProcess >= startPartition.getOffset()
                && fEndRegionToProcess < startPartition.getOffset() + startPartition.getLength())
                        ? startPartition
                        : findPartitionByOffset(partitions, fEndRegionToProcess);

        if (startPartition != null && startPartition.equals(endPartition)
                && !isProcessingRequiredForPartition(startPartition)) {
            return false;
        }

        while (position <= fEndRegionToProcess) {
            ITypedRegion partition = findPartitionByOffset(partitions, position);
            if (!isProcessingRequiredForPartition(partition)) {
                position = partition.getOffset() + partition.getLength();
                continue;
            }

            element = unit.getElementAt(position);
            if (element == null) {
                position++;
                continue;
            }

            atLeastOneElementIsProcessed = true;
            boolean doSkipThisElement = false;
            if (element instanceof IMember && element.getElementType() == IJavaElement.METHOD) {
                ISourceRange range = ((IMember) element).getSourceRange();
                if (position >= range.getOffset()) {
                    try {
                        String text = fDocument.get(range.getOffset(), position - range.getOffset() + 1);
                        if (text.indexOf('{') != -1 && !text.endsWith("}")) { //$NON-NLS-1$
                            doSkipThisElement = true;
                            position = range.getOffset() + range.getLength();
                        }
                    } catch (BadLocationException e) {
                        // Ignore it and do not skip validation
                    }
                    position++;
                }
            } else {
                IJavaElement parent = element.getParent();
                while (parent != null && parent.getElementType() != IJavaElement.COMPILATION_UNIT) {
                    if (parent.getElementType() == IJavaElement.METHOD) {
                        doSkipThisElement = true;
                        break;
                    }
                    parent = parent.getParent();
                }
                position++;
            }

            if (!doSkipThisElement)
                return true;
        }
    } catch (JavaModelException e) {
        LogHelper.logError(CommonValidationPlugin.getDefault(), e);
    } finally {
        try {
            unit.discardWorkingCopy();
        } catch (JavaModelException e) {
            LogHelper.logError(CommonValidationPlugin.getDefault(), e);
        }
    }

    return atLeastOneElementIsProcessed ? result : true;
}

From source file:org.jboss.tools.seam.ui.search.SeamSearchEngine.java

License:Open Source License

/**
 * Checks if the given element is IMethod
 * /*w  w  w  . j a  v  a 2 s  .com*/
 * @param element
 * @return
 */
public static boolean isMethod(IJavaElement element) {
    if (element == null)
        return false;

    return (element.getElementType() == IJavaElement.METHOD);
}

From source file:org.jboss.tools.vscode.java.internal.handlers.CodeLensHandler.java

License:Open Source License

private void collectChildren(ICompilationUnit unit, IJavaElement[] elements, ArrayList<CodeLens> lenses)
        throws JavaModelException {
    for (IJavaElement element : elements) {
        if (element.getElementType() == IJavaElement.TYPE) {
            collectChildren(unit, ((IType) element).getChildren(), lenses);
        } else if (element.getElementType() != IJavaElement.METHOD) {
            continue;
        }/*from w  w  w.  j a  v a  2s  .  c  o  m*/

        CodeLens lens = new CodeLens();
        ISourceRange r = ((ISourceReference) element).getNameRange();
        final org.jboss.tools.langs.Range range = JDTUtils.toRange(unit, r.getOffset(), r.getLength());
        lens.setRange(range);
        lens.setData(Arrays.asList(JDTUtils.getFileURI(unit), range.getStart()));
        lenses.add(lens);
    }
}

From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentSymbolHandler.java

License:Open Source License

private void collectChildren(ITypeRoot unit, IJavaElement[] elements, ArrayList<SymbolInformation> symbols)
        throws JavaModelException {
    for (IJavaElement element : elements) {
        if (element.getElementType() == IJavaElement.TYPE) {
            collectChildren(unit, ((IType) element).getChildren(), symbols);
        }//from   ww w . j  a va2s  .co  m
        if (element.getElementType() != IJavaElement.FIELD && element.getElementType() != IJavaElement.METHOD) {
            continue;
        }

        SymbolInformation si = new SymbolInformation();
        si.setName(element.getElementName());
        si.setKind(new Double(mapKind(element)));
        if (element.getParent() != null)
            si.setContainerName(element.getParent().getElementName());
        si.setLocation(JDTUtils.toLocation(element));
        symbols.add(si);
    }
}

From source file:org.jboss.tools.vscode.java.internal.handlers.DocumentSymbolHandler.java

License:Open Source License

public static int mapKind(IJavaElement element) {
    //      /**//from w w  w .  j av a 2  s.  co  m
    //      * A symbol kind.
    //      */
    //      export enum SymbolKind {
    //        File = 1,
    //        Module = 2,
    //        Namespace = 3,
    //        Package = 4,
    //        Class = 5,
    //        Method = 6,
    //        Property = 7,
    //        Field = 8,
    //        Constructor = 9,
    //        Enum = 10,
    //        Interface = 11,
    //        Function = 12,
    //        Variable = 13,
    //        Constant = 14,
    //        String = 15,
    //        Number = 16,
    //        Boolean = 17,
    //        Array = 18,
    //      }
    switch (element.getElementType()) {
    case IJavaElement.ANNOTATION:
        return 7; // TODO: find a better mapping 
    case IJavaElement.CLASS_FILE:
    case IJavaElement.COMPILATION_UNIT:
        return 1;
    case IJavaElement.FIELD:
        return 8;
    case IJavaElement.IMPORT_CONTAINER:
    case IJavaElement.IMPORT_DECLARATION:
        return 2;
    case IJavaElement.INITIALIZER:
        return 9;
    case IJavaElement.LOCAL_VARIABLE:
    case IJavaElement.TYPE_PARAMETER:
        return 13;
    case IJavaElement.METHOD:
        return 12;
    case IJavaElement.PACKAGE_DECLARATION:
        return 3;
    case IJavaElement.TYPE:
        try {
            return (((IType) element).isInterface() ? 11 : 5);
        } catch (JavaModelException e) {
            return 5; //fallback 
        }
    }
    return 15;
}