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

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

Introduction

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

Prototype

IClassFile getClassFile();

Source Link

Document

Returns the class file in which this member is declared, or null if this member is not declared in a class file (for example, a source 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;/*from  w w w .  ja  v a 2 s .  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
 */// w  ww .j  av a 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.matching.MatchLocator.java

License:Open Source License

protected void reportBinaryMemberDeclaration(IResource resource, IMember binaryMember,
        Binding binaryMemberBinding, IBinaryType info, int accuracy) throws CoreException {
    ClassFile classFile = (ClassFile) binaryMember.getClassFile();
    ISourceRange range = classFile.isOpen() ? binaryMember.getNameRange() : SourceMapper.UNKNOWN_RANGE;
    if (range.getOffset() == -1) {
        BinaryType type = (BinaryType) classFile.getType();
        String sourceFileName = type.sourceFileName(info);
        if (sourceFileName != null) {
            SourceMapper mapper = classFile.getSourceMapper();
            if (mapper != null) {
                char[] contents = mapper.findSource(type, sourceFileName);
                if (contents != null)
                    range = mapper.mapSource(type, contents, info, binaryMember);
            }//w  w w  .  j  av a 2  s  . c  o m
        }
    }
    if (resource == null)
        resource = this.currentPossibleMatch.resource;
    SearchMatch match = newDeclarationMatch(binaryMember, binaryMemberBinding, accuracy, range.getOffset(),
            range.getLength(), getParticipant(), resource);
    report(match);
}

From source file:org.eclipse.che.jdt.search.SearchManager.java

License:Open Source License

private FindUsagesResponse performFindUsageSearch(IJavaElement element)
        throws JavaModelException, BadLocationException {
    JavaSearchScopeFactory factory = JavaSearchScopeFactory.getInstance();
    boolean isInsideJRE = factory.isInsideJRE(element);
    JavaSearchQuery query = new JavaSearchQuery(new ElementQuerySpecification(element,
            IJavaSearchConstants.REFERENCES, factory.createWorkspaceScope(isInsideJRE), "workspace scope"));
    NewSearchUI.runQueryInForeground(null, query);
    ISearchResult result = query.getSearchResult();
    JavaSearchResult javaResult = ((JavaSearchResult) result);
    FindUsagesResponse response = DtoFactory.newDto(FindUsagesResponse.class);
    Map<String, List<org.eclipse.che.ide.ext.java.shared.dto.search.Match>> mapMaches = new HashMap<>();
    JavaElementToDtoConverter converter = new JavaElementToDtoConverter(javaResult);
    for (Object o : javaResult.getElements()) {
        IJavaElement javaElement = (IJavaElement) o;
        IDocument document = null;//from  ww  w.  ja  v a2s.co m
        if (javaElement instanceof IMember) {
            IMember member = ((IMember) javaElement);
            if (member.isBinary()) {
                if (member.getClassFile().getSource() != null) {
                    document = new Document(member.getClassFile().getSource());
                }
            } else {
                document = getDocument(member.getCompilationUnit());
            }
        } else if (javaElement instanceof IPackageDeclaration) {
            ICompilationUnit ancestor = (ICompilationUnit) (javaElement)
                    .getAncestor(IJavaElement.COMPILATION_UNIT);
            document = getDocument(ancestor);

        }
        converter.addElementToProjectHierarchy(javaElement);

        Match[] matches = javaResult.getMatches(o);

        List<org.eclipse.che.ide.ext.java.shared.dto.search.Match> matchList = new ArrayList<>();
        for (Match match : matches) {
            org.eclipse.che.ide.ext.java.shared.dto.search.Match dtoMatch = DtoFactory
                    .newDto(org.eclipse.che.ide.ext.java.shared.dto.search.Match.class);
            if (document != null) {
                IRegion lineInformation = document.getLineInformationOfOffset(match.getOffset());

                int offsetInLine = match.getOffset() - lineInformation.getOffset();
                Region matchInLine = DtoFactory.newDto(Region.class).withOffset(offsetInLine)
                        .withLength(match.getLength());
                dtoMatch.setMatchInLine(matchInLine);
                dtoMatch.setMatchLineNumber(document.getLineOfOffset(match.getOffset()));
                dtoMatch.setMatchedLine(document.get(lineInformation.getOffset(), lineInformation.getLength()));
            }
            dtoMatch.setFileMatchRegion(DtoFactory.newDto(Region.class).withOffset(match.getOffset())
                    .withLength(match.getLength()));
            matchList.add(dtoMatch);

        }
        mapMaches.put(javaElement.getHandleIdentifier(), matchList);
    }
    List<JavaProject> projects = converter.getProjects();
    response.setProjects(projects);
    response.setMatches(mapMaches);
    response.setSearchElementLabel(JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT));
    return response;
}

From source file:org.eclipse.che.plugin.java.server.JavaNavigation.java

License:Open Source License

private void setRootPath(IMember iMember, Member member) {
    if (iMember.isBinary()) {
        member.setBinary(true);/*from  ww  w .  jav  a2  s.com*/
        member.setRootPath(iMember.getClassFile().getType().getFullyQualifiedName());
        member.setLibId(
                iMember.getClassFile().getAncestor(IPackageFragmentRoot.PACKAGE_FRAGMENT_ROOT).hashCode());
    } else {
        member.setBinary(false);
        member.setRootPath(iMember.getCompilationUnit().getPath().toOSString());
    }
}

From source file:org.eclipse.jst.j2ee.internal.common.operations.JavaModelUtil.java

License:Open Source License

public static IJavaElement getTypeContainerUnit(IMember member) {
    ICompilationUnit cu = member.getCompilationUnit();
    if (cu != null)
        return cu;
    return member.getClassFile();
}

From source file:org.eclipse.wb.internal.core.utils.jdt.core.JavadocContentAccess.java

License:Open Source License

/**
 * @return the {@link Reader} for an {@link IMember}'s Javadoc comment content from the source
 *         attachment./*w w  w . jav  a2 s. c o m*/
 */
public static Reader getContentReader(IMember member, boolean allowInherited) throws Exception {
    // check current type
    {
        IBuffer buffer = member.isBinary() ? member.getClassFile().getBuffer()
                : member.getCompilationUnit().getBuffer();
        // no source attachment found
        if (buffer == null) {
            return null;
        }
        //
        ISourceRange range = member.getSourceRange();
        int start = range.getOffset();
        int length = range.getLength();
        if (length > 0 && buffer.getChar(start) == '/') {
            // prepare scanner
            IScanner scanner;
            {
                scanner = ToolFactory.createScanner(true, false, false, false);
                scanner.setSource(buffer.getCharacters());
                scanner.resetTo(start, start + length - 1);
            }
            // find last JavaDoc comment
            {
                int docOffset = -1;
                int docEnd = -1;
                {
                    int terminal = scanner.getNextToken();
                    while (org.eclipse.jdt.internal.corext.dom.TokenScanner.isComment(terminal)) {
                        if (terminal == ITerminalSymbols.TokenNameCOMMENT_JAVADOC) {
                            docOffset = scanner.getCurrentTokenStartPosition();
                            docEnd = scanner.getCurrentTokenEndPosition() + 1;
                        }
                        terminal = scanner.getNextToken();
                    }
                }
                // if comment found, return it
                if (docOffset != -1) {
                    return new JavaDocCommentReader(buffer, docOffset, docEnd);
                }
            }
        }
    }
    // check inherited
    if (allowInherited && member.getElementType() == IJavaElement.METHOD) {
        IMethod method = (IMethod) member;
        IMethod superMethod = CodeUtils.findSuperMethod(method);
        if (superMethod != null) {
            return getContentReader(superMethod, allowInherited);
        }
    }
    // not found
    return null;
}