Example usage for org.eclipse.jdt.core.compiler IScanner getLineNumber

List of usage examples for org.eclipse.jdt.core.compiler IScanner getLineNumber

Introduction

In this page you can find the example usage for org.eclipse.jdt.core.compiler IScanner getLineNumber.

Prototype

int getLineNumber(int charPosition);

Source Link

Document

Answers a 1-based line number using the lines which have been encountered so far.

Usage

From source file:de.tobject.findbugs.reporter.MarkerUtil.java

License:Open Source License

private static void completeFieldInfo(String qualifiedClassName, @Nonnull IType type, @Nonnull BugInstance bug,
        @Nonnull FieldAnnotation field) {

    IField ifield = type.getField(field.getFieldName());
    ISourceRange sourceRange = null;/*w ww.j  av a2s  .c  o m*/
    JavaModelException ex = null;
    try {
        sourceRange = ifield.getNameRange();
    } catch (JavaModelException e) {
        ex = e;
    }
    IScanner scanner = null;
    try {
        // second try...
        if (sourceRange == null) {
            sourceRange = ifield.getSourceRange();
        }
        scanner = initScanner(type, sourceRange);
    } catch (JavaModelException e) {
        String message = "Can not complete field annotation " + field + " for the field: " + ifield
                + " in class: " + qualifiedClassName + ", type " + type + ", bug " + bug;
        if (ex != null) {
            // report only first one
            e = ex;
        }
        FindbugsPlugin.getDefault().logMessage(IStatus.WARNING, message, e);
    }
    if (scanner == null || sourceRange == null) {
        return;
    }
    int lineNbr = scanner.getLineNumber(sourceRange.getOffset());
    if (lineNbr > 0) {
        String sourceFileStr = getSourceFileHint(type, qualifiedClassName);
        field.setSourceLines(
                new SourceLineAnnotation(qualifiedClassName, sourceFileStr, lineNbr, lineNbr, 0, 0));
        if (Reporter.DEBUG) {
            System.out.println("2. Fixed start line to: " + lineNbr + " on " + qualifiedClassName);
        }
    }
}

From source file:de.tobject.findbugs.reporter.MarkerUtil.java

License:Open Source License

/**
 * @return start line of given type, or {@link #DONT_KNOW_LINE} if line could not be found
 *//*from   w w w.j a v a2  s.c  om*/
private static int getLineStart(IType source) throws JavaModelException {
    ISourceRange range = source.getNameRange();
    if (range == null) {
        range = source.getSourceRange();
    }
    IScanner scanner = initScanner(source, range);
    if (scanner != null && range != null) {
        return scanner.getLineNumber(range.getOffset());
    }
    return DONT_KNOW_LINE;
}