Example usage for org.eclipse.jdt.core IMember getCompilationUnit

List of usage examples for org.eclipse.jdt.core IMember getCompilationUnit

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IMember getCompilationUnit.

Prototype

ICompilationUnit getCompilationUnit();

Source Link

Document

Returns the compilation unit in which this member is declared, or null if this member is not declared in a compilation unit (for example, a binary type).

Usage

From source file:ch.powerunit.poweruniteclipse.tab.PowerUnitLaunchTabClass.java

License:Open Source License

public void initializeClazz(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
    String name = null;/* w w  w.ja  v a  2s .  c o  m*/
    if (javaElement instanceof IMember) {
        IMember member = (IMember) javaElement;
        if (member.isBinary()) {
            javaElement = member.getClassFile();
        } else {
            javaElement = member.getCompilationUnit();
        }
    }
    if (javaElement instanceof ICompilationUnit) {
        try {
            name = ((ICompilationUnit) javaElement).getTypes()[0].getFullyQualifiedName();
        } catch (JavaModelException e) {
            // TODO
        }
    } else if (javaElement instanceof IClassFile) {
        name = ((IClassFile) javaElement).getType().getFullyQualifiedName();
    }
    if (name == null) {
        name = EMPTY_STRING;
    }
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name);
    if (name.length() > 0) {
        int index = name.lastIndexOf('.');
        if (index > 0) {
            name = name.substring(index + 1);
        }
        name = parent.getLaunchConfigurationDialog().generateName(name);
        config.rename(name);
    }
}

From source file:com.aliyun.odps.eclipse.launch.configuration.udf.SharedUDFClassTab.java

License:Apache License

/**
 * Set the main type & name attributes on the working copy based on the IJavaElement
 *///from w  w w.  j a  va 2  s  .c  om
protected void initializeMainTypeAndName(IJavaElement javaElement, ILaunchConfigurationWorkingCopy config) {
    String name = null;
    if (javaElement instanceof IMember) {
        IMember member = (IMember) javaElement;
        if (member.isBinary()) {
            javaElement = member.getClassFile();
        } else {
            javaElement = member.getCompilationUnit();
        }
    }
    if (javaElement instanceof ICompilationUnit || javaElement instanceof IClassFile) {
        try {
            IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] { javaElement },
                    false);
            MainMethodSearchEngine engine = new MainMethodSearchEngine();
            IType[] types = engine.searchMainMethods(getLaunchConfigurationDialog(), scope, false);
            if (types != null && (types.length > 0)) {
                // Simply grab the first main type found in the searched element
                name = types[0].getFullyQualifiedName();
            }
        } catch (InterruptedException ie) {
            JDIDebugUIPlugin.log(ie);
        } catch (InvocationTargetException ite) {
            JDIDebugUIPlugin.log(ite);
        }
    }
    if (name == null) {
        name = EMPTY_STRING;
    }
    config.setAttribute(IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, name);
    if (name.length() > 0) {
        int index = name.lastIndexOf('.');
        if (index > 0) {
            name = name.substring(index + 1);
        }
        name = getLaunchConfigurationDialog().generateName(name);
        config.rename(name);
    }
}

From source file:com.codenvy.ide.ext.java.server.internal.core.search.BasicSearchEngine.java

License:Open Source License

public void searchDeclarations(IJavaElement enclosingElement, SearchRequestor requestor, SearchPattern pattern,
        IProgressMonitor monitor) throws JavaModelException {
    if (VERBOSE) {
        Util.verbose("   - java element: " + enclosingElement); //$NON-NLS-1$
    }/*  w w w.ja  va2s .c o m*/
    IJavaSearchScope scope = createJavaSearchScope(new IJavaElement[] { enclosingElement });
    IResource resource = ((JavaElement) enclosingElement).resource();
    if (enclosingElement instanceof IMember) {
        IMember member = (IMember) enclosingElement;
        ICompilationUnit cu = member.getCompilationUnit();
        if (cu != null) {
            resource = cu.getResource();
        } else if (member.isBinary()) {
            // binary member resource cannot be used as this
            // see bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=148215
            resource = null;
        }
    }
    try {
        if (resource instanceof IFile) {
            try {
                requestor.beginReporting();
                if (VERBOSE) {
                    Util.verbose("Searching for " + pattern + " in " + resource.getFullPath()); //$NON-NLS-1$//$NON-NLS-2$
                }
                SearchParticipant participant = getDefaultSearchParticipant(indexManager);
                SearchDocument[] documents = MatchLocator.addWorkingCopies(pattern,
                        new SearchDocument[] {
                                new JavaSearchDocument(enclosingElement.getPath().toString(), participant) },
                        getWorkingCopies(enclosingElement), participant);
                participant.locateMatches(documents, pattern, scope, requestor, monitor);
            } finally {
                requestor.endReporting();
            }
        } else {
            search(pattern, new SearchParticipant[] { getDefaultSearchParticipant(indexManager) }, scope,
                    requestor, monitor);
        }
    } catch (CoreException e) {
        if (e instanceof JavaModelException)
            throw (JavaModelException) e;
        throw new JavaModelException(e);
    }
}

From source file:com.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static String getJavadoc(IMember member) {
    if (member != null) {
        try {// w  w  w. j a  va  2  s .c  o m
            ICompilationUnit unit = member.getCompilationUnit();
            ISourceRange range = member.getJavadocRange();
            if (unit != null && range != null) {
                String raw = unit.getSource().substring(range.getOffset(),
                        range.getOffset() + range.getLength());
                String trimmed = raw.replaceAll("\\n\\s*\\*", EMPTY_STRING + "\n*");
                return trimmed;
            }
        } catch (JavaModelException e) {
            SystemLogger.logCatch(e.getMessage());
        }
    }
    return null;
}

From source file:com.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static void exportJavadoc(IMember member, String comments) throws JavaModelException {
    if (member != null) {
        if (comments == null || comments.length() == 0) {
            removeJavadoc(member);/*from   w  ww .  j a v  a  2  s.  c o  m*/
        } else {
            ISourceRange currentJavaDocRange = member.getJavadocRange();
            TextEdit edit = null;
            String indent = getIndent(member);
            if (currentJavaDocRange != null) {
                String javadoc = addJavadocFormatting(comments, indent);
                edit = new ReplaceEdit(currentJavaDocRange.getOffset(), currentJavaDocRange.getLength(),
                        javadoc);
            } else if (member.getSourceRange().getOffset() >= 0) {
                boolean moveToNewLine = false;
                if (indent.matches("\\s*") == false) {
                    indent = trimIndent(indent);
                    moveToNewLine = true;
                }
                String javadoc = addJavadocFormatting(comments, indent);
                String comment = javadoc + "\n" + indent;
                if (moveToNewLine) {
                    comment = "\n" + indent + comment;
                }
                edit = new InsertEdit(member.getSourceRange().getOffset(), comment);
            }
            if (edit != null) {
                ICompilationUnit unit = member.getCompilationUnit();
                if (unit != null) {
                    unit.becomeWorkingCopy(null);
                    unit.applyTextEdit(edit, null);
                    unit.commitWorkingCopy(true, null);
                }
            }
        }
    }
}

From source file:com.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static void removeJavadoc(IMember member) throws JavaModelException {
    ISourceRange currentJavaDocRange = member.getJavadocRange();
    if (currentJavaDocRange != null) {
        ICompilationUnit unit = member.getCompilationUnit();
        String source = unit.getSource();
        int offset = currentJavaDocRange.getOffset();
        int length = currentJavaDocRange.getLength();
        while (((Character) source.charAt(offset + length)).toString().matches("\\s")) {
            ++length;//from  w  w  w . j  a va  2  s  .c  o  m
        }

        unit.applyTextEdit(new DeleteEdit(offset, length), null);
        unit.commitWorkingCopy(false, null);
    }
}

From source file:com.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static String getIndent(IMember member) {
    try {/*from   w  w w.  j  av a  2s.  c o m*/
        ISourceRange range = member.getSourceRange();
        String source = member.getCompilationUnit().getSource();
        int begin = range.getOffset();
        while (begin >= 0 && source.charAt(begin) != '\n') {
            --begin;
        }
        String indent = source.substring(begin + 1, range.getOffset());
        return indent;
    } catch (JavaModelException e) {
        SystemLogger.logCatch(e.getMessage());
    }
    return null;
}

From source file:com.testify.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static String getJavadoc(IMember member) {
    if (member != null) {
        try {/*from   www  .  j a  v a2 s .  com*/
            ICompilationUnit unit = member.getCompilationUnit();
            ISourceRange range = member.getJavadocRange();
            if (unit != null && range != null) {
                String raw = unit.getSource().substring(range.getOffset(),
                        range.getOffset() + range.getLength());
                String trimmed = raw.replaceAll("\\n\\s*\\*", EMPTY_STRING + "\n*");
                return trimmed;
            }
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:com.testify.ecfeed.ui.common.JavaDocSupport.java

License:Open Source License

private static String getIndent(IMember member) {
    try {/*  www  .j a  va2 s.c o  m*/
        ISourceRange range = member.getSourceRange();
        String source = member.getCompilationUnit().getSource();
        int begin = range.getOffset();
        while (begin >= 0 && source.charAt(begin) != '\n') {
            --begin;
        }
        String indent = source.substring(begin + 1, range.getOffset());
        return indent;
    } catch (JavaModelException e) {
    }
    return null;
}

From source file:de.akra.idocit.java.ui.JavaEditorSelectionListener.java

License:Apache License

/**
 * Extracts the code snippet of this Java element {@code member} from the source code.
 * /* ww w  . java2s . c  o m*/
 * @param member
 *            [SOURCE]
 * @return [OBJECT] the code snippet.
 * @throws JavaModelException
 */
private String extractCodeSnippetOf(final IMember member) throws JavaModelException {
    final ICompilationUnit cu = member.getCompilationUnit();
    final ISourceRange memberRange = member.getSourceRange();
    final String source = cu.getSource();

    // sometimes if characters were deleted, the gotten memberRange is greater than
    // the new real range of the member in the source code. Therefore, the length must
    // be corrected.
    final int len = memberRange.getLength() > source.length() ? source.length() : memberRange.getLength();

    return source.substring(memberRange.getOffset(), memberRange.getOffset() + len);
}