Example usage for org.eclipse.jdt.core IType getJavadocRange

List of usage examples for org.eclipse.jdt.core IType getJavadocRange

Introduction

In this page you can find the example usage for org.eclipse.jdt.core IType getJavadocRange.

Prototype

ISourceRange getJavadocRange() throws JavaModelException;

Source Link

Document

Returns the Javadoc range if this element is from source or if this element is a binary element with an attached source, null otherwise.

Usage

From source file:com.android.ide.eclipse.adt.internal.editors.CompletionProposal.java

License:Open Source License

@Override
public String getAdditionalProposalInfo() {
    if (mAdditionalProposalInfo == null) {
        if (mChoice instanceof ElementDescriptor) {
            String tooltip = ((ElementDescriptor) mChoice).getTooltip();
            mAdditionalProposalInfo = DescriptorsUtils.formatTooltip(tooltip);
        } else if (mChoice instanceof TextAttributeDescriptor) {
            mAdditionalProposalInfo = ((TextAttributeDescriptor) mChoice).getTooltip();
        } else if (mChoice instanceof String) {
            // Try to produce it lazily for strings like @android
            String value = (String) mChoice;
            Matcher matcher = ATTRIBUTE_PATTERN.matcher(value);
            if (matcher.matches()) {
                String attrName = matcher.group(1);
                AndroidTargetData data = mAssist.getEditor().getTargetData();
                if (data != null) {
                    IDescriptorProvider descriptorProvider = data
                            .getDescriptorProvider(mAssist.getRootDescriptorId());
                    if (descriptorProvider != null) {
                        ElementDescriptor[] rootElementDescriptors = descriptorProvider
                                .getRootElementDescriptors();
                        for (ElementDescriptor elementDesc : rootElementDescriptors) {
                            for (AttributeDescriptor desc : elementDesc.getAttributes()) {
                                String name = desc.getXmlLocalName();
                                if (attrName.equals(name)) {
                                    IAttributeInfo attributeInfo = desc.getAttributeInfo();
                                    if (attributeInfo != null) {
                                        return attributeInfo.getJavaDoc();
                                    }/*from w w w . j  ava  2s .c o m*/
                                }
                            }
                        }
                    }
                }

            }
        } else if (mChoice instanceof IType) {
            IType type = (IType) mChoice;
            try {
                ISourceRange javadocRange = type.getJavadocRange();
                if (javadocRange != null && javadocRange.getLength() > 0) {
                    ISourceRange sourceRange = type.getSourceRange();
                    if (sourceRange != null) {
                        String source = type.getSource();
                        int start = javadocRange.getOffset() - sourceRange.getOffset();
                        int length = javadocRange.getLength();
                        String doc = source.substring(start, start + length);
                        return doc;
                    }
                }
                return type.getAttachedJavadoc(new NullProgressMonitor());
            } catch (JavaModelException e) {
                AdtPlugin.log(e, null);
            }
        }
    }

    return mAdditionalProposalInfo;
}

From source file:eu.modelwriter.marker.internal.MarkerFactory.java

License:Open Source License

/**
 * Creates a Marker from TreeSelection//  ww  w .ja  v  a  2s.co  m
 */
@SuppressWarnings({ "resource" })
public static IMarker createMarker(final IResource res, final ITreeSelection selection) {
    if (selection == null) {
        final MessageDialog dialog = new MessageDialog(MarkerActivator.getShell(), "Mark Information", null,
                "Please perform a valid selection", MessageDialog.WARNING, new String[] { "OK" }, 0);
        dialog.open();
        return null;
    } else if (MarkerFactory.findMarkerByTreeSelection(selection, res) != null) {
        final MessageDialog dialog = new MessageDialog(MarkerActivator.getShell(), "Mark Information", null,
                "In these area, there is already a marker", MessageDialog.WARNING, new String[] { "OK" }, 0);
        dialog.open();
        return null;
    }

    // Fetch IResource
    final IWorkbench workbench = PlatformUI.getWorkbench();
    final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
    final IWorkbenchPage page = window.getActivePage();
    final IEditorPart editor = page.getActiveEditor();
    final IFileEditorInput input = (IFileEditorInput) editor.getEditorInput();
    final IFile file = input.getFile();

    IMarker marker = null;
    if (MarkerActivator.getEditor() instanceof EcoreEditor
            && selection.getFirstElement() instanceof ENamedElement
            && ((ENamedElement) selection.getFirstElement()).getName() != null
            && !((ENamedElement) selection.getFirstElement()).getName().isEmpty()) {

        marker = MarkerFactory.createEcoreMarker(selection, file, res, editor);

    } else if (editor instanceof EcoreEditor && selection.getFirstElement() != null) {
        marker = MarkerFactory.createInstanceMarker(selection, file, res, editor);
    } else if (editor instanceof CompilationUnitEditor) {
        final Object element = selection.getFirstElement();

        String content = null;
        try {
            content = new Scanner(file.getContents()).useDelimiter("\\Z").next();
        } catch (final CoreException e1) {
            e1.printStackTrace();
        }
        int startOffset = 0;
        int length = 0;

        if (element instanceof IField) {
            final IField field = (IField) element;
            try {
                final ISourceRange nameRange = field.getNameRange();
                final int nameStartOffset = nameRange.getOffset();
                final int nameEndOffset = nameStartOffset + nameRange.getLength();

                final ISourceRange sourceRange = field.getSourceRange();
                startOffset = field.getJavadocRange().getOffset() + field.getJavadocRange().getLength() + 1;
                if (nameEndOffset + 1 == sourceRange.getOffset() + sourceRange.getLength()) {
                    length = sourceRange.getOffset() + sourceRange.getLength() - startOffset;
                } else {
                    final int indexOfAssignment = content.indexOf("=", nameEndOffset);
                    length = indexOfAssignment - startOffset;
                }

            } catch (final JavaModelException e) {
                e.printStackTrace();
            }
        } else if (element instanceof IInitializer) {
            final IInitializer initializer = (IInitializer) element;
            try {
                final ISourceRange sourceRange = initializer.getSourceRange();
                startOffset = initializer.getJavadocRange().getOffset()
                        + initializer.getJavadocRange().getLength() + 1;
                length = sourceRange.getLength();
            } catch (final JavaModelException e) {
                e.printStackTrace();
            }
        } else if (element instanceof IMethod) {
            final IMethod method = (IMethod) element;
            try {
                final ISourceRange nameRange = method.getNameRange();
                final int nameStartOffset = nameRange.getOffset();
                final int nameEndOffset = nameStartOffset + nameRange.getLength();

                final ISourceRange sourceRange = method.getSourceRange();
                startOffset = method.getJavadocRange().getOffset() + method.getJavadocRange().getLength() + 1;
                if (content.toCharArray()[sourceRange.getOffset() + sourceRange.getLength() - 1] == '}') {
                    final int indexOfParanthesis = content.indexOf("{", nameEndOffset);
                    length = indexOfParanthesis - startOffset;
                } else if (content.toCharArray()[sourceRange.getOffset() + sourceRange.getLength()
                        - 1] == ';') {
                    length = sourceRange.getLength();
                }

            } catch (final JavaModelException e) {
                e.printStackTrace();
            }
        } else if (element instanceof IType) {
            final IType type = (IType) element;
            try {
                final ISourceRange nameRange = type.getNameRange();
                final int nameStartOffset = nameRange.getOffset();
                final int nameEndOffset = nameStartOffset + nameRange.getLength();

                final int indexOfParanthesis = content.indexOf("{", nameEndOffset);

                startOffset = type.getJavadocRange().getOffset() + type.getJavadocRange().getLength() + 1;
                length = indexOfParanthesis - startOffset;
            } catch (final JavaModelException e) {
                e.printStackTrace();
            }
        }

        final IDocument document = new Document(content);
        final TextSelection textSelection = new TextSelection(document, startOffset, length);
        return MarkerFactory.createMarker(file, textSelection);

    } else {
        final MessageDialog dialog = new MessageDialog(MarkerActivator.getShell(), "Mark Information", null,
                "Please perform a valid selection", MessageDialog.WARNING, new String[] { "OK" }, 0);
        dialog.open();
    }
    return marker;
}

From source file:org.eclim.plugin.jdt.command.junit.JUnitFindTestCommand.java

License:Open Source License

@Override
public Object execute(CommandLine commandLine) throws Exception {
    String projectName = commandLine.getValue(Options.PROJECT_OPTION);
    String file = commandLine.getValue(Options.FILE_OPTION);
    int offset = getOffset(commandLine);

    IProject project = ProjectUtils.getProject(projectName);
    IJavaProject javaProject = JavaUtils.getJavaProject(project);
    JUnit4TestFinder finder = new JUnit4TestFinder();

    ICompilationUnit src = JavaUtils.getCompilationUnit(javaProject, file);
    ICompilationUnit result = null;/*from w  w  w. ja  va  2s .c  om*/
    if (finder.isTest(src.getTypes()[0])) {
        result = JUnitUtils.findClass(javaProject, src.getTypes()[0]);
        if (result == null) {
            return Services.getMessage("junit.testing.class.not.found");
        }
    } else {
        result = JUnitUtils.findTest(javaProject, src.getTypes()[0]);
        if (result == null) {
            return Services.getMessage("junit.testing.test.not.found");
        }
    }

    IType resultType = result.getTypes()[0];
    String name = resultType.getElementName();
    ISourceReference ref = resultType;
    ISourceRange docRange = resultType.getJavadocRange();

    IJavaElement element = src.getElementAt(offset);
    if (element != null && element.getElementType() == IJavaElement.METHOD) {
        IMethod method = null;
        if (finder.isTest(src.getTypes()[0])) {
            method = JUnitUtils.findClassMethod(result, (IMethod) element);
        } else {
            method = JUnitUtils.findTestMethod(result, (IMethod) element);
        }
        if (method != null) {
            name = method.getElementName();
            ref = method;
            docRange = method.getJavadocRange();
        }
    }

    String lineDelim = result.findRecommendedLineSeparator();
    int docLength = docRange != null ? docRange.getLength() + lineDelim.length() : 0;
    return Position.fromOffset(result.getResource().getLocation().toOSString(), name,
            ref.getSourceRange().getOffset() + docLength, 0);
}

From source file:org.eclipse.acceleo.internal.ide.ui.editors.template.utils.JavaServicesUtils.java

License:Open Source License

/**
 * Indicates if the compilation unit can be used.
 * // w  w  w. ja  va 2s.  c om
 * @param iCompilationUnit
 *            The compilation unit
 * @return <code>true</code> if the compilation unit can be used, <code>false</code> otherwise.
 */
public static boolean isAcceleoJavaServicesClass(ICompilationUnit iCompilationUnit) {
    try {
        IType[] types = iCompilationUnit.getTypes();
        for (IType iType : types) {
            ISourceRange javadocRange = iType.getJavadocRange();
            if (javadocRange != null) {
                String javadoc = iCompilationUnit.getOpenable().getBuffer().getText(javadocRange.getOffset(),
                        javadocRange.getLength());
                if (javadoc != null && javadoc.contains(IAcceleoConstants.JAVADOC_TAG_NS_URI)) {
                    return true;
                }
            }
        }
    } catch (JavaModelException e) {
        // do not log
    }
    return false;
}

From source file:org.eclipse.acceleo.internal.ide.ui.editors.template.utils.JavaServicesUtils.java

License:Open Source License

/**
 * Generates an Acceleo module for a given compilation unit.
 * //from w  w w .j  a v a  2  s  .c  o  m
 * @param iCompilationUnit
 *            The compilation unit
 * @param monitor
 *            The progress monitor
 */
public static void generateAcceleoServicesModule(ICompilationUnit iCompilationUnit, IProgressMonitor monitor) {
    monitor.subTask(AcceleoUIMessages.getString("JavaServiceUtils.GenerateAcceleoModuleWrapper")); //$NON-NLS-1$
    List<String> nsURIs = new ArrayList<String>();
    try {
        IType[] types = iCompilationUnit.getTypes();
        for (IType iType : types) {
            ISourceRange javadocRange = iType.getJavadocRange();
            if (javadocRange != null) {
                String javadoc = iCompilationUnit.getOpenable().getBuffer().getText(javadocRange.getOffset(),
                        javadocRange.getLength());
                Scanner scanner = new Scanner(javadoc);
                while (scanner.hasNextLine()) {
                    String line = scanner.nextLine();
                    if (javadoc != null) {
                        int index = line.indexOf(IAcceleoConstants.JAVADOC_TAG_NS_URI);
                        if (index != -1) {
                            String trimmed = line
                                    .substring(index + IAcceleoConstants.JAVADOC_TAG_NS_URI.length()).trim();
                            nsURIs.add(trimmed);
                        }
                    }
                }
                scanner.close();
            }
        }

        // Write the file
        IResource resource = iCompilationUnit.getResource();
        if (resource instanceof IFile) {
            IFile javaFile = (IFile) resource;
            String moduleName = javaFile.getName();
            if (moduleName.endsWith(".java")) { //$NON-NLS-1$
                moduleName = moduleName.substring(0, moduleName.length() - ".java".length()); //$NON-NLS-1$
            }
            String fileName = moduleName;
            moduleName = moduleName + '.' + IAcceleoConstants.MTL_FILE_EXTENSION;
            IContainer parent = javaFile.getParent();
            IFile file = parent
                    .getFile(new Path(moduleName.substring(0, 1).toLowerCase() + moduleName.substring(1)));

            String content = JavaServicesUtils.getFileContent(iCompilationUnit, fileName, nsURIs);

            InputStream source = new ByteArrayInputStream(content.getBytes());
            if (file.exists()) {
                file.setContents(source, true, false, monitor);
            } else {
                file.create(source, true, monitor);
            }
        }
    } catch (JavaModelException e) {
        // do not log
    } catch (CoreException e) {
        AcceleoUIActivator.log(e, true);
    }
}

From source file:org.eclipse.andmore.internal.editors.CompletionProposal.java

License:Open Source License

@Override
public String getAdditionalProposalInfo() {
    if (mAdditionalProposalInfo == null) {
        if (mChoice instanceof ElementDescriptor) {
            String tooltip = ((ElementDescriptor) mChoice).getTooltip();
            mAdditionalProposalInfo = DescriptorsUtils.formatTooltip(tooltip);
        } else if (mChoice instanceof TextAttributeDescriptor) {
            mAdditionalProposalInfo = ((TextAttributeDescriptor) mChoice).getTooltip();
        } else if (mChoice instanceof String) {
            // Try to produce it lazily for strings like @android
            String value = (String) mChoice;
            Matcher matcher = ATTRIBUTE_PATTERN.matcher(value);
            if (matcher.matches()) {
                String attrName = matcher.group(1);
                AndroidTargetData data = mAssist.getEditor().getTargetData();
                if (data != null) {
                    IDescriptorProvider descriptorProvider = data
                            .getDescriptorProvider(mAssist.getRootDescriptorId());
                    if (descriptorProvider != null) {
                        ElementDescriptor[] rootElementDescriptors = descriptorProvider
                                .getRootElementDescriptors();
                        for (ElementDescriptor elementDesc : rootElementDescriptors) {
                            for (AttributeDescriptor desc : elementDesc.getAttributes()) {
                                String name = desc.getXmlLocalName();
                                if (attrName.equals(name)) {
                                    IAttributeInfo attributeInfo = desc.getAttributeInfo();
                                    if (attributeInfo != null) {
                                        return attributeInfo.getJavaDoc();
                                    }//from  ww w . j  av  a2s  .  c o m
                                }
                            }
                        }
                    }
                }

            }
        } else if (mChoice instanceof IType) {
            IType type = (IType) mChoice;
            try {
                ISourceRange javadocRange = type.getJavadocRange();
                if (javadocRange != null && javadocRange.getLength() > 0) {
                    ISourceRange sourceRange = type.getSourceRange();
                    if (sourceRange != null) {
                        String source = type.getSource();
                        int start = javadocRange.getOffset() - sourceRange.getOffset();
                        int length = javadocRange.getLength();
                        String doc = source.substring(start, start + length);
                        return doc;
                    }
                }
                return type.getAttachedJavadoc(new NullProgressMonitor());
            } catch (JavaModelException e) {
                AndmoreAndroidPlugin.log(e, null);
            }
        }
    }

    return mAdditionalProposalInfo;
}

From source file:org.eclipseguru.gwt.core.internal.codegen.AsyncServiceCodeGenerator.java

License:Open Source License

/**
 * Indicates if the async service stup with the specified type name is
 * allowed to be generated in the specified compilation unit.
 * <p>//from   www.  j  a va 2 s  .co m
 * The generation of the async service interface is only allowed if the
 * parent compilation unit or the target type do not exist yet. If the
 * target type already exists it has to contain the
 * <code>&#64;generated</code> tag in its JavaDoc comment.
 * </p>
 * 
 * @param parentCU
 * @param asyncServiceTypeName
 * @return <code>true</code> if the async generation is allowed,
 *         <code>false</code> otherwise
 * @throws CoreException
 */
public static boolean isAllowedToGenerateAsyncServiceType(final ICompilationUnit parentCU,
        final String asyncServiceTypeName) throws CoreException {
    if (!parentCU.exists()) {
        return true;
    }

    final IType asyncServiceType = parentCU.getType(asyncServiceTypeName);
    if (!asyncServiceType.exists()) {
        return true;
    }

    final ISourceRange javadocRange = asyncServiceType.getJavadocRange();
    if (null == javadocRange) {
        return false;
    }

    final String text = parentCU.getBuffer().getText(javadocRange.getOffset(), javadocRange.getLength());
    return text.contains("@generated");
}

From source file:org.limy.eclipse.qalab.outline.umlimage.UmlImageSourceSupport.java

License:Open Source License

/**
 * c?[`bv???B/*from   w w w .  j av a2  s  . co  m*/
 * @param type JDTNXCX^X
 * @return 
 */
public static String createTooltipText(IType type) {
    try {
        ISourceRange javadocRange = type.getJavadocRange();
        if (javadocRange != null) {
            String lines = type.getSource().substring(0, type.getJavadocRange().getLength());
            StringBuilder buff = new StringBuilder();
            for (Iterator<String> it = new LimyLineIterator(lines); it.hasNext();) {
                String line = it.next();
                Matcher matcher = PATTERN_JAVADOC.matcher(line);
                if (matcher.matches()) {
                    buff.append(matcher.group(1)).append('\n');
                }
            }
            return buff.toString();
        }
    } catch (JavaModelException e) {
        // do nothing
    }
    return null;
}