Search in sources :

Example 16 with LocalVariableTable

use of org.apache.bcel.classfile.LocalVariableTable in project spotbugs by spotbugs.

the class OpcodeStack method pushByLocalObjectLoad.

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);
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) Method(org.apache.bcel.classfile.Method) XMethod(edu.umd.cs.findbugs.ba.XMethod) ConstantString(org.apache.bcel.classfile.ConstantString)

Example 17 with LocalVariableTable

use of org.apache.bcel.classfile.LocalVariableTable in project spotbugs by spotbugs.

the class LocalVariableAnnotation method getLocalVariableAnnotation.

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);
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) LocalVariable(org.apache.bcel.classfile.LocalVariable) LineNumberTable(org.apache.bcel.classfile.LineNumberTable)

Example 18 with LocalVariableTable

use of org.apache.bcel.classfile.LocalVariableTable in project opengrok by oracle.

the class JavaClassAnalyzer method getContent.

// TODO this class needs to be thread safe to avoid bug 13364, which was fixed by just updating bcel to 5.2
private void getContent(Writer out, Writer fout, JavaClass c, List<String> defs, List<String> refs, List<String> full) throws IOException {
    String t;
    ConstantPool cp = c.getConstantPool();
    int[] v = new int[cp.getLength() + 1];
    out.write(linkPath(t = c.getSourceFileName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    out.write(PACKAGE);
    fout.write(PACKAGE);
    out.write(linkDef(t = c.getPackageName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    String aflg;
    out.write(aflg = Utility.accessToString(c.getAccessFlags(), true));
    if (aflg != null) {
        out.write(SPACE);
        fout.write(aflg);
        fout.write(SPACE);
    }
    v[c.getClassNameIndex()] = 1;
    out.write(tagDef(t = c.getClassName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EXTENDS);
    fout.write(EXTENDS);
    v[c.getSuperclassNameIndex()] = 1;
    out.write(linkDef(t = c.getSuperclassName()));
    refs.add(t);
    fout.write(t);
    for (int i : c.getInterfaceIndices()) {
        v[i] = 1;
    }
    String[] ins = c.getInterfaceNames();
    if (ins != null && ins.length > 0) {
        out.write(IMPLEMENTS);
        fout.write(IMPLEMENTS);
        for (String in : ins) {
            out.write(linkDef(t = in));
            refs.add(t);
            fout.write(t);
            out.write(SPACE);
            fout.write(SPACE);
        }
    }
    out.write(LCBREOL);
    fout.write(LCBREOL);
    for (Attribute a : c.getAttributes()) {
        if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
            for (Attribute ca : ((Code) a).getAttributes()) {
                if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                    for (LocalVariable l : ((LocalVariableTable) ca).getLocalVariableTable()) {
                        printLocal(out, fout, l, v, defs, refs);
                    }
                }
            }
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_BOOTSTRAP_METHODS) {
        // TODO fill in bootstrap methods, fix the else if
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_SOURCE_FILE) {
            v[a.getNameIndex()] = 1;
            break;
        }
    }
    String aflgs;
    String fldsig;
    String tdef;
    for (org.apache.bcel.classfile.Field fld : c.getFields()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(fld.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            fout.write(SPACE);
            out.write(SPACE);
        }
        fldsig = Utility.typeSignatureToString(fld.getSignature(), true);
        out.write(fldsig);
        fout.write(fldsig);
        out.write(SPACE);
        fout.write(SPACE);
        tdef = tagDef(t = fld.getName());
        out.write(tdef);
        fout.write(tdef);
        defs.add(t);
        refs.add(t);
        out.write(EOL);
        fout.write(EOL);
    // TODO show Attributes
    }
    String sig;
    String msig;
    String ltdef;
    for (org.apache.bcel.classfile.Method m : c.getMethods()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(m.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            out.write(SPACE);
            fout.write(SPACE);
        }
        sig = m.getSignature();
        msig = Utility.methodSignatureReturnType(sig, false);
        out.write(msig);
        fout.write(msig);
        out.write(SPACE);
        fout.write(SPACE);
        ltdef = tagDef(t = m.getName());
        out.write(ltdef);
        fout.write(ltdef);
        defs.add(t);
        refs.add(t);
        out.write(LBRA);
        fout.write(LBRA);
        String[] args = Utility.methodSignatureArgumentTypes(sig, false);
        for (int i = 0; i < args.length; i++) {
            t = args[i];
            out.write(t);
            fout.write(t);
            int spi = t.indexOf(SPACE);
            if (spi > 0) {
                refs.add(t.substring(0, spi));
                defs.add(t.substring(spi + 1));
            }
            if (i < args.length - 1) {
                out.write(COMMA);
                fout.write(COMMA);
            }
        }
        out.write(RBRA);
        fout.write(RBRA);
        ArrayList<LocalVariable[]> locals = new ArrayList<>();
        for (Attribute a : m.getAttributes()) {
            if (a.getTag() == org.apache.bcel.Const.ATTR_EXCEPTIONS) {
                for (int i : ((ExceptionTable) a).getExceptionIndexTable()) {
                    v[i] = 1;
                }
                String[] exs = ((ExceptionTable) a).getExceptionNames();
                if (exs != null && exs.length > 0) {
                    out.write(THROWS);
                    fout.write(THROWS);
                    for (String ex : exs) {
                        out.write(linkDef(ex));
                        fout.write(ex);
                        refs.add(ex);
                        out.write(SPACE);
                        fout.write(SPACE);
                    }
                }
            } else if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
                for (Attribute ca : ((Code) a).getAttributes()) {
                    if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                        locals.add(((LocalVariableTable) ca).getLocalVariableTable());
                    }
                }
            }
        }
        out.write(EOL);
        fout.write(EOL);
        if (!locals.isEmpty()) {
            for (LocalVariable[] ls : locals) {
                for (LocalVariable l : ls) {
                    printLocal(out, fout, l, v, defs, refs);
                }
            }
        }
    }
    out.write(RCBREOL);
    fout.write(RCBREOL);
    for (int i = 0; i < v.length - 1; i++) {
        if (v[i] != 1) {
            Constant constant = cp.getConstant(i);
            if (constant != null) {
                full.add(constantToString(constant, cp, v));
            }
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) Attribute(org.apache.bcel.classfile.Attribute) Constant(org.apache.bcel.classfile.Constant) LocalVariable(org.apache.bcel.classfile.LocalVariable) ArrayList(java.util.ArrayList) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ConstantString(org.apache.bcel.classfile.ConstantString) Code(org.apache.bcel.classfile.Code) ConstantPool(org.apache.bcel.classfile.ConstantPool)

Example 19 with LocalVariableTable

use of org.apache.bcel.classfile.LocalVariableTable in project OpenGrok by OpenGrok.

the class JavaClassAnalyzer method getContent.

// TODO this class needs to be thread safe to avoid bug 13364, which was fixed by just updating bcel to 5.2
private void getContent(Writer out, Writer fout, JavaClass c, List<String> defs, List<String> refs, List<String> full) throws IOException {
    String t;
    ConstantPool cp = c.getConstantPool();
    int[] v = new int[cp.getLength() + 1];
    out.write(linkPath(t = c.getSourceFileName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    out.write(PACKAGE);
    fout.write(PACKAGE);
    out.write(linkDef(t = c.getPackageName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EOL);
    fout.write(EOL);
    String aflg;
    out.write(aflg = Utility.accessToString(c.getAccessFlags(), true));
    if (aflg != null) {
        out.write(SPACE);
        fout.write(aflg);
        fout.write(SPACE);
    }
    v[c.getClassNameIndex()] = 1;
    out.write(tagDef(t = c.getClassName()));
    defs.add(t);
    refs.add(t);
    fout.write(t);
    out.write(EXTENDS);
    fout.write(EXTENDS);
    v[c.getSuperclassNameIndex()] = 1;
    out.write(linkDef(t = c.getSuperclassName()));
    refs.add(t);
    fout.write(t);
    for (int i : c.getInterfaceIndices()) {
        v[i] = 1;
    }
    String[] ins = c.getInterfaceNames();
    if (ins != null && ins.length > 0) {
        out.write(IMPLEMENTS);
        fout.write(IMPLEMENTS);
        for (String in : ins) {
            out.write(linkDef(t = in));
            refs.add(t);
            fout.write(t);
            out.write(SPACE);
            fout.write(SPACE);
        }
    }
    out.write(LCBREOL);
    fout.write(LCBREOL);
    for (Attribute a : c.getAttributes()) {
        if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
            for (Attribute ca : ((Code) a).getAttributes()) {
                if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                    for (LocalVariable l : ((LocalVariableTable) ca).getLocalVariableTable()) {
                        printLocal(out, fout, l, v, defs, refs);
                    }
                }
            }
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_BOOTSTRAP_METHODS) {
        // TODO fill in bootstrap methods, fix the else if
        } else if (a.getTag() == org.apache.bcel.Const.ATTR_SOURCE_FILE) {
            v[a.getNameIndex()] = 1;
            break;
        }
    }
    String aflgs;
    String fldsig;
    String tdef;
    for (org.apache.bcel.classfile.Field fld : c.getFields()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(fld.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            fout.write(SPACE);
            out.write(SPACE);
        }
        fldsig = Utility.typeSignatureToString(fld.getSignature(), true);
        out.write(fldsig);
        fout.write(fldsig);
        out.write(SPACE);
        fout.write(SPACE);
        tdef = tagDef(t = fld.getName());
        out.write(tdef);
        fout.write(tdef);
        defs.add(t);
        refs.add(t);
        out.write(EOL);
        fout.write(EOL);
    // TODO show Attributes
    }
    String sig;
    String msig;
    String ltdef;
    for (org.apache.bcel.classfile.Method m : c.getMethods()) {
        out.write(TAB);
        fout.write(TAB);
        aflgs = Utility.accessToString(m.getAccessFlags());
        if (aflgs != null && aflgs.length() > 0) {
            out.write(aflgs);
            fout.write(aflgs);
            out.write(SPACE);
            fout.write(SPACE);
        }
        sig = m.getSignature();
        msig = Utility.methodSignatureReturnType(sig, false);
        out.write(msig);
        fout.write(msig);
        out.write(SPACE);
        fout.write(SPACE);
        ltdef = tagDef(t = m.getName());
        out.write(ltdef);
        fout.write(ltdef);
        defs.add(t);
        refs.add(t);
        out.write(LBRA);
        fout.write(LBRA);
        String[] args = Utility.methodSignatureArgumentTypes(sig, false);
        for (int i = 0; i < args.length; i++) {
            t = args[i];
            out.write(t);
            fout.write(t);
            int spi = t.indexOf(SPACE);
            if (spi > 0) {
                refs.add(t.substring(0, spi));
                defs.add(t.substring(spi + 1));
            }
            if (i < args.length - 1) {
                out.write(COMMA);
                fout.write(COMMA);
            }
        }
        out.write(RBRA);
        fout.write(RBRA);
        ArrayList<LocalVariable[]> locals = new ArrayList<>();
        for (Attribute a : m.getAttributes()) {
            if (a.getTag() == org.apache.bcel.Const.ATTR_EXCEPTIONS) {
                for (int i : ((ExceptionTable) a).getExceptionIndexTable()) {
                    v[i] = 1;
                }
                String[] exs = ((ExceptionTable) a).getExceptionNames();
                if (exs != null && exs.length > 0) {
                    out.write(THROWS);
                    fout.write(THROWS);
                    for (String ex : exs) {
                        out.write(linkDef(ex));
                        fout.write(ex);
                        refs.add(ex);
                        out.write(SPACE);
                        fout.write(SPACE);
                    }
                }
            } else if (a.getTag() == org.apache.bcel.Const.ATTR_CODE) {
                for (Attribute ca : ((Code) a).getAttributes()) {
                    if (ca.getTag() == org.apache.bcel.Const.ATTR_LOCAL_VARIABLE_TABLE) {
                        locals.add(((LocalVariableTable) ca).getLocalVariableTable());
                    }
                }
            }
        }
        out.write(EOL);
        fout.write(EOL);
        if (!locals.isEmpty()) {
            for (LocalVariable[] ls : locals) {
                for (LocalVariable l : ls) {
                    printLocal(out, fout, l, v, defs, refs);
                }
            }
        }
    }
    out.write(RCBREOL);
    fout.write(RCBREOL);
    for (int i = 0; i < v.length - 1; i++) {
        if (v[i] != 1) {
            Constant constant = cp.getConstant(i);
            if (constant != null) {
                full.add(constantToString(constant, cp, v));
            }
        }
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) Attribute(org.apache.bcel.classfile.Attribute) Constant(org.apache.bcel.classfile.Constant) LocalVariable(org.apache.bcel.classfile.LocalVariable) ArrayList(java.util.ArrayList) ExceptionTable(org.apache.bcel.classfile.ExceptionTable) ConstantString(org.apache.bcel.classfile.ConstantString) Code(org.apache.bcel.classfile.Code) ConstantPool(org.apache.bcel.classfile.ConstantPool)

Example 20 with LocalVariableTable

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

the class UnusedParameter method visitCode.

/**
 * implements the visitor to clear the parm set, and check for potential methods
 *
 * @param obj
 *            the context object of the currently parsed code block
 */
@Override
public void visitCode(Code obj) {
    unusedParms.clear();
    regToParm.clear();
    stack.resetForMethodEntry(this);
    Method m = getMethod();
    String methodName = m.getName();
    if (IGNORE_METHODS.contains(methodName)) {
        return;
    }
    int accessFlags = m.getAccessFlags();
    if (((accessFlags & (Constants.ACC_STATIC | Constants.ACC_PRIVATE)) == 0) || ((accessFlags & Constants.ACC_SYNTHETIC) != 0)) {
        return;
    }
    List<String> parmTypes = SignatureUtils.getParameterSignatures(m.getSignature());
    if (parmTypes.isEmpty()) {
        return;
    }
    int firstReg = 0;
    if ((accessFlags & Constants.ACC_STATIC) == 0) {
        ++firstReg;
    }
    int reg = firstReg;
    for (int i = 0; i < parmTypes.size(); ++i) {
        unusedParms.set(reg);
        regToParm.put(Integer.valueOf(reg), Integer.valueOf(i + 1));
        String parmSig = parmTypes.get(i);
        reg += SignatureUtils.getSignatureSize(parmSig);
    }
    try {
        super.visitCode(obj);
        if (!unusedParms.isEmpty()) {
            LocalVariableTable lvt = m.getLocalVariableTable();
            reg = unusedParms.nextSetBit(firstReg);
            while (reg >= 0) {
                LocalVariable lv = (lvt == null) ? null : lvt.getLocalVariable(reg, 0);
                if (lv != null) {
                    String parmName = lv.getName();
                    bugReporter.reportBug(new BugInstance(this, BugType.UP_UNUSED_PARAMETER.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addString("Parameter " + regToParm.get(Integer.valueOf(reg)) + ": " + parmName));
                }
                reg = unusedParms.nextSetBit(reg + 1);
            }
        }
    } catch (StopOpcodeParsingException e) {
    // no unusedParms left
    }
}
Also used : LocalVariableTable(org.apache.bcel.classfile.LocalVariableTable) StopOpcodeParsingException(com.mebigfatguy.fbcontrib.utils.StopOpcodeParsingException) LocalVariable(org.apache.bcel.classfile.LocalVariable) BugInstance(edu.umd.cs.findbugs.BugInstance) Method(org.apache.bcel.classfile.Method)

Aggregations

LocalVariableTable (org.apache.bcel.classfile.LocalVariableTable)32 LocalVariable (org.apache.bcel.classfile.LocalVariable)25 BugInstance (edu.umd.cs.findbugs.BugInstance)7 Method (org.apache.bcel.classfile.Method)7 Attribute (org.apache.bcel.classfile.Attribute)6 Code (org.apache.bcel.classfile.Code)5 ExceptionTable (org.apache.bcel.classfile.ExceptionTable)5 ArrayList (java.util.ArrayList)4 CodeException (org.apache.bcel.classfile.CodeException)4 ConstantString (org.apache.bcel.classfile.ConstantString)4 LineNumberTable (org.apache.bcel.classfile.LineNumberTable)4 Type (org.apache.bcel.generic.Type)4 XMethod (edu.umd.cs.findbugs.ba.XMethod)3 Constant (org.apache.bcel.classfile.Constant)3 ConstantPool (org.apache.bcel.classfile.ConstantPool)3 JavaClass (org.apache.bcel.classfile.JavaClass)3 ConstantPoolGen (org.apache.bcel.generic.ConstantPoolGen)3 ToString (com.mebigfatguy.fbcontrib.utils.ToString)2 SignatureParser (edu.umd.cs.findbugs.ba.SignatureParser)2 BitSet (java.util.BitSet)2