Example usage for org.eclipse.jdt.core ICompilationUnit findElements

List of usage examples for org.eclipse.jdt.core ICompilationUnit findElements

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ICompilationUnit findElements.

Prototype

@Override
IJavaElement[] findElements(IJavaElement element);

Source Link

Document

Finds the elements in this compilation unit that correspond to the given element.

Usage

From source file:org.eclim.plugin.jdt.command.impl.ImplCommand.java

License:Open Source License

private void insertMethods(ICompilationUnit src, IType type, CommandLine commandLine) throws Exception {
    String methodsOption = commandLine.getValue(Options.METHOD_OPTION);
    HashSet<String> chosen = null;
    if (methodsOption != null) {
        chosen = new HashSet<String>();
        String[] sigs = new Gson().fromJson(methodsOption, String[].class);
        for (String sig : sigs) {
            chosen.add(sig.replace(" ", ""));
        }//from  w  ww  . j  ava  2s .  co  m
    }

    int pos = -1;
    int len = src.getBuffer().getLength();
    final IJavaElement sibling;
    if (commandLine.hasOption(Options.OFFSET_OPTION)) {
        sibling = getSibling(type, getOffset(commandLine));
    } else {
        sibling = getSibling(type);
    }
    if (sibling != null) {
        pos = getOffset(sibling);
    }

    IWorkspaceRunnable op = getImplOperation(src, type, chosen, sibling, pos, commandLine);
    if (op != null) {
        String lineDelim = src.findRecommendedLineSeparator();
        IImportDeclaration[] imports = src.getImports();
        int importsEnd = -1;
        if (imports.length > 0) {
            ISourceRange last = imports[imports.length - 1].getSourceRange();
            importsEnd = last.getOffset() + last.getLength() + lineDelim.length();
        }

        op.run(null);

        // an op.getResultingEdit() would be nice here, but we'll make do w/ what
        // we got and caculate our own edit offset/length combo so we can format
        // the new code.
        int offset = pos != -1 ? pos : (len - 1 - lineDelim.length());
        int newLen = src.getBuffer().getLength();
        int length = newLen - len - 1;

        // a more accurate length estimate can be found by locating the
        //  sibling at its new position and taking the difference.
        //  this prevents the formatting from screwing up the sibling's formatting
        final IJavaElement[] newSiblings = sibling == null ? null : src.findElements(sibling);
        if (newSiblings != null && newSiblings.length == 1) {
            // not sure what it would mean if there were more than one...
            length = getOffset(newSiblings[0]) - offset;
        }

        // the change in length may include newly added imports, so handle that as
        // best we can
        int importLenChange = 0;
        imports = src.getImports();
        if (importsEnd != -1) {
            ISourceRange last = imports[imports.length - 1].getSourceRange();
            importLenChange = last.getOffset() + last.getLength() + lineDelim.length() - importsEnd;
        } else if (imports.length > 0) {
            ISourceRange first = imports[0].getSourceRange();
            ISourceRange last = imports[imports.length - 1].getSourceRange();
            importLenChange = last.getOffset() + last.getLength() + (lineDelim.length() * 2)
                    - first.getOffset();
        }

        offset += importLenChange;
        length -= importLenChange;

        JavaUtils.format(src, CodeFormatter.K_COMPILATION_UNIT, offset, length);
    }
}

From source file:org.eclipse.che.jdt.util.JavaModelUtil.java

License:Open Source License

/**
 * Returns the element of the given compilation unit which is "equal" to the
 * given element. Note that the given element usually has a parent different
 * from the given compilation unit.// ww w. j a v  a 2 s  .c o  m
 *
 * @param cu the cu to search in
 * @param element the element to look for
 * @return an element of the given cu "equal" to the given element
 */
public static IJavaElement findInCompilationUnit(ICompilationUnit cu, IJavaElement element) {
    IJavaElement[] elements = cu.findElements(element);
    if (elements != null && elements.length > 0) {
        return elements[0];
    }
    return null;
}

From source file:org.eclipse.jst.jsf.common.ui.internal.utils.JavaModelUtil.java

License:Open Source License

/**
 * Finds a a member in a compilation unit. Typical usage is to find the
 * corresponding member in a working copy.
 * //from w w  w.  j  a v  a  2s  . com
 * @param cu
 *            the compilation unit (eg. working copy) to search in
 * @param member
 *            the member (eg. from the original)
 * @return the member found, or null if not existing
 */
public static IMember findMemberInCompilationUnit(ICompilationUnit cu, IMember member) {
    IJavaElement[] elements = cu.findElements(member);
    if (elements != null && elements.length > 0) {
        return (IMember) elements[0];
    }
    return null;
}

From source file:org.jboss.tools.common.refactoring.MarkerResolutionUtils.java

License:Open Source License

@SuppressWarnings("unchecked")
public static <T extends IJavaElement> T findWorkingCopy(ICompilationUnit compilationUnit, T element)
        throws JavaModelException {
    if (element instanceof IAnnotation) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IAnnotatable) {
            for (IAnnotation a : ((IAnnotatable) parent).getAnnotations()) {
                if (a.getElementName().equals(element.getElementName()))
                    return (T) a;
            }/*from   ww w  .ja v a2 s  .co  m*/
        }
    } else if (element instanceof ILocalVariable && ((ILocalVariable) element).isParameter()) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IMethod) {
            for (ILocalVariable parameter : ((IMethod) parent).getParameters()) {
                if (parameter.getElementName().equals(element.getElementName())
                        && parameter.getTypeSignature().equals(((ILocalVariable) element).getTypeSignature()))
                    return (T) parameter;
            }
        }
    } else {
        IJavaElement[] elements = compilationUnit.findElements(element);
        if (elements != null) {
            for (IJavaElement e : elements) {
                if (e.getHandleIdentifier().equals(element.getHandleIdentifier()))
                    return (T) e;
            }
        }
    }
    return null;
}

From source file:org.jboss.tools.common.ui.marker.AddSuppressWarningsMarkerResolution.java

License:Open Source License

@SuppressWarnings("unchecked")
private static <T extends IJavaElement> T findWorkingCopy(ICompilationUnit compilationUnit, T element)
        throws JavaModelException {
    if (element instanceof IAnnotation) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IAnnotatable) {
            for (IAnnotation a : ((IAnnotatable) parent).getAnnotations()) {
                if (a.getElementName().equals(element.getElementName()))
                    return (T) a;
            }/*  www . j a va2 s . c o  m*/
        }
    } else if (element instanceof ILocalVariable && ((ILocalVariable) element).isParameter()) {
        IJavaElement parent = findWorkingCopy(compilationUnit, element.getParent());
        if (parent instanceof IMethod) {
            for (ILocalVariable parameter : ((IMethod) parent).getParameters()) {
                if (parameter.getElementName().equals(element.getElementName())
                        && parameter.getTypeSignature().equals(((ILocalVariable) element).getTypeSignature()))
                    return (T) parameter;
            }
        }
    } else {
        IJavaElement[] elements = compilationUnit.findElements(element);
        if (elements != null) {
            for (IJavaElement e : elements) {
                if (e.getClass().equals(element.getClass()))
                    return (T) e;
            }
        }
    }
    return null;
}