Java Code Examples for org.apache.bcel.classfile.LocalVariableTable#getLocalVariableTable()

The following examples show how to use org.apache.bcel.classfile.LocalVariableTable#getLocalVariableTable() . 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: LVTHelper.java    From spotbugs with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * returns the local variable at an index int the scope of PC
 *
 * @param lvt
 *            the local variable table
 * @param index
 *            the variable index
 * @param pc
 *            the PC where the variable is used
 */
public static LocalVariable getLocalVariableAtPC(@Nonnull LocalVariableTable lvt, int index, int pc) {
    int length = lvt.getTableLength();
    LocalVariable[] lvs = lvt.getLocalVariableTable();

    for (int i = 0; i < length; i++) {
        if (lvs[i].getIndex() == index) {
            int startPC = lvs[i].getStartPC();
            if ((pc >= startPC) && (pc < (startPC + lvs[i].getLength()))) {
                return lvs[i];
            }
        }
    }

    return null;
}
 
Example 2
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
private void updateLocalVariableTable(final LocalVariableTable a) {
    final LocalVariable[] lv = a.getLocalVariableTable();
    removeLocalVariables();
    for (final LocalVariable l : lv) {
        InstructionHandle start = il.findHandle(l.getStartPC());
        final InstructionHandle end = il.findHandle(l.getStartPC() + l.getLength());
        // Repair malformed handles
        if (null == start) {
            start = il.getStart();
        }
        // end == null => live to end of method
        // Since we are recreating the LocalVaraible, we must
        // propagate the orig_index to new copy.
        addLocalVariable(l.getName(), Type.getType(l.getSignature()), l
                .getIndex(), start, end, l.getOrigIndex());
    }
}
 
Example 3
Source File: PreorderVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visitLocalVariableTable(LocalVariableTable obj) {
    super.visitLocalVariableTable(obj);
    LocalVariable[] local_variable_table = obj.getLocalVariableTable();
    for (LocalVariable aLocal_variable_table : local_variable_table) {
        aLocal_variable_table.accept(this);
    }
}
 
Example 4
Source File: FindMaskedFields.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(LocalVariableTable obj) {
    if (ENABLE_LOCALS) {
        if (staticMethod) {
            return;
        }

        LocalVariable[] vars = obj.getLocalVariableTable();
        // System.out.println("Num params = " + numParms);
        for (LocalVariable var : vars) {
            if (var.getIndex() < numParms) {
                continue;
            }
            String varName = var.getName();
            if ("serialVersionUID".equals(varName)) {
                continue;
            }
            Field f = classFields.get(varName);
            // System.out.println("Checking " + varName);
            // System.out.println(" got " + f);
            // TODO: we could distinguish between obscuring a field in the
            // same class
            // vs. obscuring a field in a superclass. Not sure how important
            // that is.
            if (f != null) {
                FieldAnnotation fa = FieldAnnotation.fromBCELField(getDottedClassName(), f);
                if (true || var.getStartPC() > 0) {
                    bugReporter.reportBug(new BugInstance(this, "MF_METHOD_MASKS_FIELD", LOW_PRIORITY)
                            .addClassAndMethod(this).addField(fa).addSourceLine(this, var.getStartPC() - 1));
                }
            }
        }
    }
    super.visit(obj);
}
 
Example 5
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
private void adjustLocalVariableTypeTable(final LocalVariableTable lvt) {
    final LocalVariable[] lv = lvt.getLocalVariableTable();
    final LocalVariable[] lvg = localVariableTypeTable.getLocalVariableTypeTable();

    for (final LocalVariable element : lvg) {
        for (final LocalVariable l : lv) {
            if (element.getName().equals(l.getName()) && element.getIndex() == l.getOrigIndex()) {
                element.setLength(l.getLength());
                element.setStartPC(l.getStartPC());
                element.setIndex(l.getIndex());
                break;
            }
        }
    }
}