Example usage for org.eclipse.jdt.core ITypeRoot getCorrespondingResource

List of usage examples for org.eclipse.jdt.core ITypeRoot getCorrespondingResource

Introduction

In this page you can find the example usage for org.eclipse.jdt.core ITypeRoot getCorrespondingResource.

Prototype

IResource getCorrespondingResource() throws JavaModelException;

Source Link

Document

Returns the resource that corresponds directly to this element, or null if there is no resource that corresponds to this element.

Usage

From source file:org.eclipse.modisco.java.discoverer.internal.io.java.JDTVisitor.java

License:Open Source License

@Override
public void endVisit(final org.eclipse.jdt.core.dom.CompilationUnit node) {
    CompilationUnit element = (CompilationUnit) this.binding.get(node);

    // if any type of this cu already exists in the model, we don't
    // visit this cu
    if (this.isAlreadyVisited) {
        return;//from ww  w  . j a  v a 2 s .c o  m
    }

    this.jdtModel.getCompilationUnits().add(element);
    ITypeRoot rootType = node.getTypeRoot();

    if (rootType != null) {
        if (rootType instanceof IClassFile) {
            // type comes from a .class file
            ClassFile classFile = this.factory.createClassFile();
            classFile.setName(rootType.getElementName());
            classFile.setAttachedSource(element);
            classFile.setOriginalFilePath(this.currentFilePath);

            Archive ar = LibraryReader.getArchive((IClassFile) rootType, this.factory, this.jdtModel);
            if (ar == null) {
                this.jdtModel.getClassFiles().add(classFile);
            } else {
                ar.getClassFiles().add(classFile);
            }
        } else if (rootType instanceof ICompilationUnit) {
            // type comes a .java file
            IPath absolutePath = null;
            try {
                absolutePath = rootType.getCorrespondingResource().getLocation();
            } catch (JavaModelException e) {
                absolutePath = ResourcesPlugin.getWorkspace().getRoot().getRawLocation()
                        .append(rootType.getPath());
            }
            element.setOriginalFilePath(absolutePath.toOSString());
        } else {
            element.setOriginalFilePath(""); //$NON-NLS-1$
        }
        element.setName(rootType.getElementName());
    } else {
        element.setProxy(true);
    }

    Package packageDeclaration = (Package) this.binding.get(node.getPackage());
    element.setPackage(packageDeclaration);

    for (Object importNode : node.imports()) {
        ImportDeclaration importDeclaration = (ImportDeclaration) this.binding.get(importNode);
        element.getImports().add(importDeclaration);
    }

    for (Object typeNode : node.types()) {
        AbstractTypeDeclaration typeDeclaration = (AbstractTypeDeclaration) this.binding.get(typeNode);
        element.getTypes().add(typeDeclaration);
    }

    try {
        // accessing BlockComment and LineComment
        // (https://bugs.eclipse.org/bugs/show_bug.cgi?format=multiple&id=84528)
        List<?> comments = node.getCommentList();
        for (Object name : comments) {
            org.eclipse.jdt.core.dom.Comment aComment = (org.eclipse.jdt.core.dom.Comment) name;

            Comment commentElement = null;
            if (aComment.isLineComment()) {
                commentElement = this.factory.createLineComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isBlockComment()) {
                commentElement = this.factory.createBlockComment();
                initializeNode(commentElement, aComment);
                String content = CommentsManager.extractCommentContent(aComment, this.javaContent);
                commentElement.setContent(content);
                this.binding.put(aComment, commentElement);
            } else if (aComment.isDocComment()) {
                // one javadoc node (and its tag elements) should have been
                // already visited

                commentElement = (Javadoc) this.binding.get(aComment);
                if (commentElement == null) { // happen if more than one javadoc
                    // for a node
                    commentElement = this.factory.createJavadoc();
                    initializeNode(commentElement, aComment);
                }
                commentElement.setContent(aComment.toString());
            }

            getCommentsBinding().put(aComment, commentElement);
            // initialisation of element CompilationUnit
            element.getCommentList().add(commentElement);
        }

        CommentsManager.resolveCommentPositions(this);
    } catch (StringIndexOutOfBoundsException e) {
        // IGNORE hub, sam, markus
    }
}

From source file:org.jboss.tools.common.jdt.debug.ui.actions.RemoteLaunchAction.java

License:Open Source License

private IResource getLaunchableResource(IEditorPart editorpart) {
    ITypeRoot element = JavaUI.getEditorInputTypeRoot(editorpart.getEditorInput());
    if (element != null) {
        try {/*from  w  ww. j  av a2 s. c om*/
            return element.getCorrespondingResource();
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:org.jboss.tools.common.jdt.debug.ui.launching.RemoteJavaApplicationLaunchShortcut.java

License:Open Source License

public IResource getLaunchableResource(IEditorPart editor) {
    ITypeRoot element = JavaUI.getEditorInputTypeRoot(editor.getEditorInput());
    if (element != null) {
        try {//from ww w .ja  va2s .  c om
            return element.getCorrespondingResource();
        } catch (JavaModelException e) {
        }
    }
    return null;
}

From source file:org.jboss.tools.jst.jsp.jspeditor.info.JavaStringELInfoHover.java

License:Open Source License

public String getHoverInfoDepracated(ITextViewer textViewer, IRegion region) {
    // find a region of __java_string, if we're in it - use it
    IDocument document = textViewer == null ? null : textViewer.getDocument();
    if (document == null)
        return null;

    int rangeStart = -1;
    int rangeLength = 0;
    IToken rangeToken = null;//from  ww  w  .  j  a v a2  s. c o m
    FastJavaPartitionScanner scanner = new FastJavaPartitionScanner();
    scanner.setRange(document, 0, document.getLength());
    while (true) {
        IToken token = scanner.nextToken();
        if (token == null || token.isEOF())
            break;
        int start = scanner.getTokenOffset();
        int length = scanner.getTokenLength();
        int end = start + length;
        if (start <= region.getOffset() && end >= region.getOffset()) {
            rangeStart = start;
            rangeLength = length;
            rangeToken = token;
            break;
        }
        if (start > region.getOffset())
            break;
    }

    if (rangeToken == null || rangeStart == -1 || rangeLength <= 0
            || !IJavaPartitions.JAVA_STRING.equals(rangeToken.getData()))
        return null;

    // OK. We've found JAVA_STRING token  
    // Check that the position is in the EL 
    if (!checkStartPosition(document, region.getOffset()))
        return null;

    // Calculate and prepare KB-query parameters
    String text = null;
    try {
        text = document.get(rangeStart, rangeLength);
    } catch (BadLocationException e) {
        JspEditorPlugin.getPluginLog().logError(e);
    }
    int inValueOffset = region.getOffset() - rangeStart;

    ELParser p = ELParserUtil.getJbossFactory().createParser();
    ELModel model = p.parse(text);

    ELInvocationExpression ie = ELUtil.findExpression(model, inValueOffset);// ELExpression
    if (ie == null)
        return null;

    String query = "#{" + ie.getText(); //$NON-NLS-1$

    KbQuery kbQuery = Utils.createKbQuery(Type.ATTRIBUTE_VALUE, region.getOffset() + region.getLength(), query,
            query, "", "", null, null, false); //$NON-NLS-1$ //$NON-NLS-2$

    ITypeRoot input = getEditorInputJavaElement();
    if (input == null)
        return null;

    IFile file = null;

    try {
        IResource resource = input.getCorrespondingResource();
        if (resource instanceof IFile)
            file = (IFile) resource;
    } catch (JavaModelException e) {
        // Ignore. It is probably because of Java element's resource is not found 
    }

    if (file == null) {
        return null;
    }

    ELContext context = PageContextFactory.createPageContext(file, JavaCore.JAVA_SOURCE_CONTENT_TYPE);

    TextProposal[] proposals = PageProcessor.getInstance().getProposals(kbQuery, context);
    if (proposals == null)
        return null;

    for (TextProposal proposal : proposals) {
        String label = proposal == null ? null : proposal.getLabel();
        label = (label == null || label.indexOf(':') == -1) ? label
                : label.substring(0, label.indexOf(':')).trim();
        if (label != null && query.endsWith(label) && proposal != null && proposal.getContextInfo() != null
                && proposal.getContextInfo().trim().length() > 0) {
            return proposal.getContextInfo();
        }
    }

    return null;
}