org.apache.bcel.classfile.LocalVariableTable Java Examples

The following examples show how to use org.apache.bcel.classfile.LocalVariableTable. 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: PLSETestCase.java    From commons-bcel with Apache License 2.0 6 votes vote down vote up
/**
 * BCEL-295:
 */
public void testB295() throws Exception
{
    final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass2");
    final ClassGen cg = new ClassGen(clazz);
    final ConstantPoolGen pool = cg.getConstantPool();
    final Method m = cg.getMethodAt(1);  // 'main'
    final LocalVariableTable lvt = m.getLocalVariableTable();
    final LocalVariable lv = lvt.getLocalVariable(2, 4);  // 'i'
    //System.out.println(lv);
    final MethodGen mg = new MethodGen(m, cg.getClassName(), pool);
    final LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool());
    final LocalVariable new_lv = new_lvt.getLocalVariable(2, 4);  // 'i'
    //System.out.println(new_lv);
    assertEquals("live range length", lv.getLength(), new_lv.getLength());
}
 
Example #2
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 #3
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 #4
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 #5
Source File: VisitorSet.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor
 */
public void visitLocalVariableTable(LocalVariableTable aTable)
{
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aTable.accept(v);
    }
}
 
Example #6
Source File: PLSETestCase.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * BCEL-79:
 */
public void testB79() throws ClassNotFoundException
{
    final JavaClass clazz = getTestClass(PACKAGE_BASE_NAME+".data.PLSETestClass");
    final ClassGen gen = new ClassGen(clazz);
    final ConstantPoolGen pool = gen.getConstantPool();
    final Method m = gen.getMethodAt(2);
    final LocalVariableTable lvt = m.getLocalVariableTable();
    //System.out.println(lvt);
    //System.out.println(lvt.getTableLength());
    final MethodGen mg = new MethodGen(m, gen.getClassName(), pool);
    final LocalVariableTable new_lvt = mg.getLocalVariableTable(mg.getConstantPool());
    //System.out.println(new_lvt);
    assertEquals("number of locals", lvt.getTableLength(), new_lvt.getTableLength());
}
 
Example #7
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;
            }
        }
    }
}
 
Example #8
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 5 votes vote down vote up
/**
 * @return `LocalVariableTable' attribute of all the local variables of this method.
 */
public LocalVariableTable getLocalVariableTable( final ConstantPoolGen cp ) {
    final LocalVariableGen[] lg = getLocalVariables();
    final int size = lg.length;
    final LocalVariable[] lv = new LocalVariable[size];
    for (int i = 0; i < size; i++) {
        lv[i] = lg[i].getLocalVariable(cp);
    }
    return new LocalVariableTable(cp.addUtf8("LocalVariableTable"), 2 + lv.length * 10, lv, cp
            .getConstantPool());
}
 
Example #9
Source File: VisitorSet.java    From contribution with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * @see org.apache.bcel.classfile.Visitor
 */
public void visitLocalVariableTable(LocalVariableTable aTable)
{
    for (Iterator iter = mVisitors.iterator(); iter.hasNext();) {
        IDeepVisitor visitor = (IDeepVisitor) iter.next();
        Visitor v = visitor.getClassFileVisitor();
        aTable.accept(v);
    }
}
 
Example #10
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 #11
Source File: OpcodeStack.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void pushByLocalObjectLoad(DismantleBytecode dbc, int register) {
    Method m = dbc.getMethod();
    LocalVariableTable lvt = m.getLocalVariableTable();
    if (lvt != null) {
        LocalVariable lv = LVTHelper.getLocalVariableAtPC(lvt, register, dbc.getPC());
        if (lv != null) {
            String signature = lv.getSignature();
            pushByLocalLoad(signature, register);
            return;
        }
    }
    pushByLocalLoad("Ljava/lang/Object;", register);
}
 
Example #12
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 #13
Source File: TrainLongInstantfParams.java    From spotbugs with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
public void visit(Code obj) {

    if (!getMethod().isPublic() && !getMethod().isProtected()) {
        return;
    }
    SignatureParser p = new SignatureParser(getMethodSig());
    LocalVariableTable t = obj.getLocalVariableTable();

    if (t == null) {
        return;
    }
    ParameterProperty property = new ParameterProperty();

    int index = getMethod().isStatic() ? 0 : 1;
    int parameterNumber = 0;
    for (Iterator<String> i = p.parameterSignatureIterator(); i.hasNext();) {
        String s = i.next();
        LocalVariable localVariable = t.getLocalVariable(index, 0);
        if (localVariable != null) {
            String name = localVariable.getName();
            if ("J".equals(s) && (name.toLowerCase().indexOf("instant") >= 0 || name.startsWith("date"))) {

                // System.out.println(getFullyQualifiedMethodName() + " " + s + " " + index + " " + name);
                property.setParamWithProperty(parameterNumber, true);
            }
        }
        if ("J".equals(s) || "D".equals(s)) {
            index += 2;
        } else {
            index += 1;
        }
        parameterNumber++;
    }
    if (!property.isEmpty()) {
        // System.out.println(getFullyQualifiedMethodName() + " " + property);
        database.setProperty(getMethodDescriptor(), property);
    }
}
 
Example #14
Source File: ValueRangeAnalysisFactory.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public Context(ConstantPool cp, LocalVariableTable lvTable, Map<Integer, Value> types, ValueNumberDataflow vnaDataflow) {
    this.cp = cp;
    this.lvTable = lvTable;
    this.types = types;
    this.vnaDataflow = vnaDataflow;
}
 
Example #15
Source File: MethodGen.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
/**
 * Instantiate from existing method.
 *
 * @param method method
 * @param className class name containing this method
 * @param cp constant pool
 */
public MethodGen(final Method method, final String className, final ConstantPoolGen cp) {
    this(method.getAccessFlags(), Type.getReturnType(method.getSignature()),
        Type.getArgumentTypes(method.getSignature()), null /* may be overridden anyway */
        , method.getName(), className,
        ((method.getAccessFlags() & (Const.ACC_ABSTRACT | Const.ACC_NATIVE)) == 0)
            ? new InstructionList(getByteCodes(method))
            : null,
        cp);
    final Attribute[] attributes = method.getAttributes();
    for (final Attribute attribute : attributes) {
        Attribute a = attribute;
        if (a instanceof Code) {
            final Code c = (Code) a;
            setMaxStack(c.getMaxStack());
            setMaxLocals(c.getMaxLocals());
            final CodeException[] ces = c.getExceptionTable();
            if (ces != null) {
                for (final CodeException ce : ces) {
                    final int type = ce.getCatchType();
                    ObjectType c_type = null;
                    if (type > 0) {
                        final String cen = method.getConstantPool().getConstantString(type, Const.CONSTANT_Class);
                        c_type = ObjectType.getInstance(cen);
                    }
                    final int end_pc = ce.getEndPC();
                    final int length = getByteCodes(method).length;
                    InstructionHandle end;
                    if (length == end_pc) { // May happen, because end_pc is exclusive
                        end = il.getEnd();
                    } else {
                        end = il.findHandle(end_pc);
                        end = end.getPrev(); // Make it inclusive
                    }
                    addExceptionHandler(il.findHandle(ce.getStartPC()), end, il.findHandle(ce.getHandlerPC()),
                        c_type);
                }
            }
            final Attribute[] c_attributes = c.getAttributes();
            for (final Attribute c_attribute : c_attributes) {
                a = c_attribute;
                if (a instanceof LineNumberTable) {
                    final LineNumber[] ln = ((LineNumberTable) a).getLineNumberTable();
                    for (final LineNumber l : ln) {
                        final InstructionHandle ih = il.findHandle(l.getStartPC());
                        if (ih != null) {
                            addLineNumber(ih, l.getLineNumber());
                        }
                    }
                } else if (a instanceof LocalVariableTable) {
                    updateLocalVariableTable((LocalVariableTable) a);
                } else if (a instanceof LocalVariableTypeTable) {
                    this.localVariableTypeTable = (LocalVariableTypeTable) a.copy(cp.getConstantPool());
                } else {
                    addCodeAttribute(a);
                }
            }
        } else if (a instanceof ExceptionTable) {
            final String[] names = ((ExceptionTable) a).getExceptionNames();
            for (final String name2 : names) {
                addException(name2);
            }
        } else if (a instanceof Annotations) {
            final Annotations runtimeAnnotations = (Annotations) a;
            final AnnotationEntry[] aes = runtimeAnnotations.getAnnotationEntries();
            for (final AnnotationEntry element : aes) {
                addAnnotationEntry(new AnnotationEntryGen(element, cp, false));
            }
        } else {
            addAttribute(a);
        }
    }
}
 
Example #16
Source File: ConfusionBetweenInheritedAndOuterMethod.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void sawOpcode(int seen) {
    if (iteratorBug != null) {
        if (isRegisterStore()) {
            LocalVariableTable lvt = getMethod().getLocalVariableTable();
            if (lvt != null) {
                LocalVariable localVariable = lvt.getLocalVariable(getRegisterOperand(), getNextPC());
                if (localVariable == null || localVariable.getName().endsWith("$")) {
                    // iterator() result is stored to the synthetic variable which has no name in LVT or name is suffixed with '$'
                    // Looks like it's for-each cycle like for(Object obj : this)
                    // Do not report such case
                    iteratorBug = null;
                }
            }
        }
        if (iteratorBug != null) {
            bugAccumulator.accumulateBug(iteratorBug, this);
        }
        iteratorBug = null;
    }
    if (seen != Const.INVOKEVIRTUAL) {
        return;
    }
    if (!getClassName().equals(getClassConstantOperand())) {
        return;
    }
    XMethod invokedMethod = XFactory.createXMethod(getDottedClassConstantOperand(), getNameConstantOperand(),
            getSigConstantOperand(), false);
    if (invokedMethod.isResolved() && invokedMethod.getClassName().equals(getDottedClassConstantOperand())
            || invokedMethod.isSynthetic()) {
        return;
    }
    if (getStack().getStackItem(getNumberArguments(getSigConstantOperand())).getRegisterNumber() != 0) {
        // called not for this object
        return;
    }
    // method is inherited
    String possibleTargetClass = getDottedClassName();
    String superClassName = getDottedSuperclassName();
    while (true) {
        int i = possibleTargetClass.lastIndexOf('$');
        if (i <= 0) {
            break;
        }
        possibleTargetClass = possibleTargetClass.substring(0, i);
        if (possibleTargetClass.equals(superClassName)) {
            break;
        }
        XMethod alternativeMethod = XFactory.createXMethod(possibleTargetClass, getNameConstantOperand(),
                getSigConstantOperand(), false);
        if (alternativeMethod.isResolved() && alternativeMethod.getClassName().equals(possibleTargetClass)) {
            String targetPackage = invokedMethod.getPackageName();
            String alternativePackage = alternativeMethod.getPackageName();
            int priority = HIGH_PRIORITY;
            if (targetPackage.equals(alternativePackage)) {
                priority++;
            }
            if (targetPackage.startsWith("javax.swing") || targetPackage.startsWith("java.awt")) {
                priority += 2;
            }
            if (invokedMethod.getName().equals(getMethodName())) {
                priority++;
            }

            BugInstance bug = new BugInstance(this, "IA_AMBIGUOUS_INVOCATION_OF_INHERITED_OR_OUTER_METHOD", priority)
                    .addClassAndMethod(this).addMethod(invokedMethod).describe("METHOD_INHERITED")
                    .addMethod(alternativeMethod).describe("METHOD_ALTERNATIVE_TARGET");
            if (invokedMethod.getName().equals("iterator") && invokedMethod.getSignature().equals("()Ljava/util/Iterator;")
                    && Subtypes2.instanceOf(getDottedClassName(), "java.lang.Iterable")) {
                iteratorBug = bug;
            } else {
                bugAccumulator.accumulateBug(bug, this);
            }
            break;
        }
    }
}
 
Example #17
Source File: Pass2Verifier.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLocalVariableTable(final LocalVariableTable obj) {//vmspec2 4.7.9
    //In JustIce,this check is partially delayed to Pass 3a.
    //The other part can be found in the visitCode(Code) method.
}
 
Example #18
Source File: StringRepresentation.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLocalVariableTable(final LocalVariableTable obj) {
    tostring = "<LocalVariableTable: " + toString(obj) + ">";
}
 
Example #19
Source File: CounterVisitor.java    From commons-bcel with Apache License 2.0 4 votes vote down vote up
@Override
public void visitLocalVariableTable(final LocalVariableTable obj)
{
    localVariableTableCount++;
}
 
Example #20
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public void visitLocalVariableTable(LocalVariableTable obj) {
    visit(obj);
}
 
Example #21
Source File: BetterVisitor.java    From spotbugs with GNU Lesser General Public License v2.1 4 votes vote down vote up
public void visit(LocalVariableTable obj) {
    visit((Attribute) obj);
}
 
Example #22
Source File: UnreadVariableCheck.java    From cacheonix-core with GNU Lesser General Public License v2.1 2 votes vote down vote up
public void visitLocalVariableTable(LocalVariableTable aTable)
{

  
}
 
Example #23
Source File: UnreadVariableCheck.java    From contribution with GNU Lesser General Public License v2.1 2 votes vote down vote up
public void visitLocalVariableTable(LocalVariableTable aTable)
{

  
}