Java Code Examples for org.apache.bcel.classfile.Method#getLineNumberTable()

The following examples show how to use org.apache.bcel.classfile.Method#getLineNumberTable() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LocalVariableAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static LocalVariableAnnotation getLocalVariableAnnotation(Method method, int local, int position1, int position2) {

        LocalVariableTable localVariableTable = method.getLocalVariableTable();
        String localName = UNKNOWN_NAME;
        if (localVariableTable != null) {
            LocalVariable lv1 = localVariableTable.getLocalVariable(local, position1);
            if (lv1 == null) {
                lv1 = localVariableTable.getLocalVariable(local, position2);
                position1 = position2;
            }
            if (lv1 != null) {
                localName = lv1.getName();
            }
        }
        LineNumberTable lineNumbers = method.getLineNumberTable();
        if (lineNumbers == null) {
            return new LocalVariableAnnotation(localName, local, position1);
        }
        int line = lineNumbers.getSourceLine(position1);
        return new LocalVariableAnnotation(localName, local, position1, line);
    }
 
Example 2
Source File: SourceLineAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Create a SourceLineAnnotation covering an entire method.
 *
 * @param javaClass
 *            JavaClass containing the method
 * @param method
 *            the method
 * @return a SourceLineAnnotation for the entire method
 */
public static SourceLineAnnotation forEntireMethod(JavaClass javaClass, @CheckForNull Method method) {
    String sourceFile = javaClass.getSourceFileName();
    if (method == null) {
        return createUnknown(javaClass.getClassName(), sourceFile);
    }
    Code code = method.getCode();
    LineNumberTable lineNumberTable = method.getLineNumberTable();
    if (code == null || lineNumberTable == null) {
        return createUnknown(javaClass.getClassName(), sourceFile);
    }

    return forEntireMethod(javaClass.getClassName(), sourceFile, lineNumberTable, code.getLength());
}
 
Example 3
Source File: SourceLineAnnotation.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Make a best-effort attempt to create a SourceLineAnnotation for the first
 * line of a method.
 *
 * @param methodDescriptor
 *            a method
 * @return SourceLineAnnotation describing the first line of the method
 *         (insofar as we can actually figure that out from the bytecode)
 */
public static SourceLineAnnotation forFirstLineOfMethod(MethodDescriptor methodDescriptor) {
    SourceLineAnnotation result = null;

    try {
        Method m = Global.getAnalysisCache().getMethodAnalysis(Method.class, methodDescriptor);
        XClass xclass = Global.getAnalysisCache().getClassAnalysis(XClass.class, methodDescriptor.getClassDescriptor());
        LineNumberTable lnt = m.getLineNumberTable();
        String sourceFile = xclass.getSource();
        if (sourceFile != null && lnt != null) {
            int firstLine = Integer.MAX_VALUE;
            int bytecode = 0;
            LineNumber[] entries = lnt.getLineNumberTable();
            for (LineNumber entry : entries) {
                if (entry.getLineNumber() < firstLine) {
                    firstLine = entry.getLineNumber();
                    bytecode = entry.getStartPC();
                }
            }
            if (firstLine < Integer.MAX_VALUE) {

                result = new SourceLineAnnotation(methodDescriptor.getClassDescriptor().toDottedClassName(), sourceFile,
                        firstLine, firstLine, bytecode, bytecode);
            }
        }
    } catch (CheckedAnalysisException e) {
        // ignore
    }

    if (result == null) {
        result = createUnknown(methodDescriptor.getClassDescriptor().toDottedClassName());
    }
    return result;
}