Search in sources :

Example 1 with LineNumberTable

use of org.apache.bcel.classfile.LineNumberTable in project fb-contrib by mebigfatguy.

the class PossiblyRedundantMethodCalls method getLineNumber.

/**
 * returns the source line number for the pc, or just the pc if the line number table doesn't exist
 *
 * @param pc
 *            current pc
 * @return the line number
 */
private int getLineNumber(int pc) {
    LineNumberTable lnt = getMethod().getLineNumberTable();
    if (lnt == null) {
        return pc;
    }
    LineNumber[] lns = lnt.getLineNumberTable();
    if (lns == null) {
        return pc;
    }
    if (pc > lns[lns.length - 1].getStartPC()) {
        return lns[lns.length - 1].getLineNumber();
    }
    int lo = 0;
    int hi = lns.length - 2;
    int mid = 0;
    while (lo <= hi) {
        mid = (lo + hi) >>> 1;
        if (pc < lns[mid].getStartPC()) {
            hi = mid - 1;
        } else if (pc >= lns[mid + 1].getStartPC()) {
            lo = mid + 1;
        } else {
            break;
        }
    }
    return lns[mid].getLineNumber();
}
Also used : LineNumberTable(org.apache.bcel.classfile.LineNumberTable) LineNumber(org.apache.bcel.classfile.LineNumber)

Example 2 with LineNumberTable

use of org.apache.bcel.classfile.LineNumberTable in project jop by jop-devel.

the class SymbolicPointsTo method printResult.

public void printResult(DFATool program) {
    Map<String, String> getFields = new TreeMap<String, String>();
    for (InstructionHandle instr : usedRefs.keySet()) {
        ContextMap<CallString, BoundedSet<SymbolicAddress>> r = usedRefs.get(instr);
        Context c = r.getContext();
        MethodInfo method = c.getMethodInfo();
        if (method == null) {
            throw new AssertionError("Internal Error: No method '" + c.method() + "'");
        }
        LineNumberTable lines = method.getCode().getLineNumberTable();
        int sourceLine = lines.getSourceLine(instr.getPosition());
        for (CallString callString : r.keySet()) {
            System.out.println(c.method() + ":" + sourceLine + ":" + callString + ": " + instr);
            BoundedSet<SymbolicAddress> symAddr = r.get(callString);
            String infoStr;
            if (instr.getInstruction() instanceof GETFIELD) {
                GETFIELD gfInstr = (GETFIELD) instr.getInstruction();
                infoStr = String.format("GETFIELD %s %s %s", symAddr.toString(), gfInstr.getFieldName(c.constPool()), gfInstr.getFieldType(c.constPool()));
            } else if (instr.getInstruction() instanceof ARRAYLENGTH) {
                infoStr = String.format("ARRAYLENGTH %s", symAddr.toString());
            } else if (instr.getInstruction() instanceof ArrayInstruction) {
                ArrayInstruction aInstr = (ArrayInstruction) instr.getInstruction();
                infoStr = String.format("%s %s %s[]", aInstr.getName().toUpperCase(), symAddr.toString(), aInstr.getType(c.constPool()));
            } else {
                infoStr = String.format("%s %s", instr.getInstruction().getName().toUpperCase(), symAddr.toString());
            }
            if (infoStr != null) {
                String infoKey = String.format("%s:%04d:%s", c.method(), sourceLine, callString);
                while (getFields.containsKey(infoKey)) infoKey += "'";
                getFields.put(infoKey, infoStr);
            }
        }
    }
    for (Entry<String, String> entry : getFields.entrySet()) {
        System.out.println(entry.getKey());
        System.out.println("  " + entry.getValue());
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) ARRAYLENGTH(org.apache.bcel.generic.ARRAYLENGTH) CallString(com.jopdesign.common.code.CallString) TreeMap(java.util.TreeMap) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LineNumberTable(org.apache.bcel.classfile.LineNumberTable) CallString(com.jopdesign.common.code.CallString) BoundedSet(com.jopdesign.dfa.framework.BoundedSetFactory.BoundedSet) GETFIELD(org.apache.bcel.generic.GETFIELD) ArrayInstruction(org.apache.bcel.generic.ArrayInstruction) MethodInfo(com.jopdesign.common.MethodInfo)

Example 3 with LineNumberTable

use of org.apache.bcel.classfile.LineNumberTable in project jop by jop-devel.

the class LoopBounds method printSizeResult.

public void printSizeResult(DFATool program) {
    for (InstructionHandle instr : sizes.keySet()) {
        ContextMap<CallString, Interval[]> r = sizes.get(instr);
        Context c = r.getContext();
        LineNumberTable lines = c.getMethodInfo().getCode().getLineNumberTable();
        int sourceLine = lines.getSourceLine(instr.getPosition());
        for (CallString callString : r.keySet()) {
            Interval[] bounds = r.get(callString);
            System.out.println(c.method() + ":" + sourceLine + ":\t" + callString.toStringList() + ": ");
            System.out.println(Arrays.asList(bounds));
        }
    }
}
Also used : Context(com.jopdesign.dfa.framework.Context) InstructionHandle(org.apache.bcel.generic.InstructionHandle) LineNumberTable(org.apache.bcel.classfile.LineNumberTable) CallString(com.jopdesign.common.code.CallString)

Example 4 with LineNumberTable

use of org.apache.bcel.classfile.LineNumberTable in project fb-contrib by mebigfatguy.

the class AttributesUtils method isValidLineNumber.

/**
 * returns whether the pc is at a line number that also appears for a another byte code offset later on in the method. If this occurs we are in a jdk6
 * finally replicated block, and so don't report this. If the code has no line number table, then just report it.
 *
 * @param obj
 *            the code object to find line number attributes from
 * @param pc
 *            the pc to check
 *
 * @return whether the pc is in user code
 */
public static boolean isValidLineNumber(Code obj, int pc) {
    LineNumberTable lnt = obj.getLineNumberTable();
    if (lnt == null)
        return true;
    LineNumber[] lineNumbers = lnt.getLineNumberTable();
    if (lineNumbers == null)
        return true;
    int lo = 0;
    int hi = lineNumbers.length - 1;
    int mid = 0;
    int linePC = 0;
    while (lo <= hi) {
        mid = (lo + hi) >>> 1;
        linePC = lineNumbers[mid].getStartPC();
        if (linePC == pc)
            break;
        if (linePC < pc)
            lo = mid + 1;
        else
            hi = mid - 1;
    }
    int lineNo = lineNumbers[mid].getLineNumber();
    for (int i = 0; i < lineNumbers.length; i++) {
        if ((mid != i) && (lineNumbers[i].getLineNumber() == lineNo))
            return false;
    }
    return true;
}
Also used : LineNumberTable(org.apache.bcel.classfile.LineNumberTable) LineNumber(org.apache.bcel.classfile.LineNumber)

Aggregations

LineNumberTable (org.apache.bcel.classfile.LineNumberTable)4 CallString (com.jopdesign.common.code.CallString)2 Context (com.jopdesign.dfa.framework.Context)2 LineNumber (org.apache.bcel.classfile.LineNumber)2 InstructionHandle (org.apache.bcel.generic.InstructionHandle)2 MethodInfo (com.jopdesign.common.MethodInfo)1 BoundedSet (com.jopdesign.dfa.framework.BoundedSetFactory.BoundedSet)1 TreeMap (java.util.TreeMap)1 ARRAYLENGTH (org.apache.bcel.generic.ARRAYLENGTH)1 ArrayInstruction (org.apache.bcel.generic.ArrayInstruction)1 GETFIELD (org.apache.bcel.generic.GETFIELD)1