Search in sources :

Example 1 with ToString

use of com.mebigfatguy.fbcontrib.utils.ToString in project fb-contrib by mebigfatguy.

the class InefficientStringBuffering method sawInvokeVirtual.

private ISBUserValue sawInvokeVirtual() {
    ISBUserValue userValue = null;
    String calledClass = getClassConstantOperand();
    if (SignatureUtils.isPlainStringConvertableClass(calledClass)) {
        String methodName = getNameConstantOperand();
        if ("append".equals(methodName)) {
            OpcodeStack.Item itm = getStringBufferItemAt(1);
            if (itm != null) {
                userValue = (ISBUserValue) itm.getUserValue();
            }
            if (stack.getStackDepth() > 0) {
                itm = stack.getStackItem(0);
                ISBUserValue uv = (ISBUserValue) itm.getUserValue();
                if (uv != null) {
                    switch(uv.getAppendType()) {
                        case NESTED:
                            bugReporter.reportBug(new BugInstance(this, BugType.ISB_INEFFICIENT_STRING_BUFFERING.name(), Values.TOSTRING.equals(getMethodName()) ? LOW_PRIORITY : NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                            break;
                        case TOSTRING:
                            if (stack.getStackDepth() > 1) {
                                itm = stack.getStackItem(1);
                                if (itm != null) {
                                    uv = (ISBUserValue) itm.getUserValue();
                                    if ((uv != null) && uv.hasResolvedString()) {
                                        bugReporter.reportBug(new BugInstance(this, BugType.ISB_TOSTRING_APPENDING.name(), NORMAL_PRIORITY).addClass(this).addMethod(this).addSourceLine(this));
                                    }
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            if (getSigConstantOperand().startsWith(SignatureBuilder.PARAM_STRING)) {
                if (userValue == null) {
                    userValue = new ISBUserValue(AppendType.CLEAR, true);
                } else {
                    userValue = new ISBUserValue(userValue.getAppendType(), true);
                }
            }
        } else if (Values.TOSTRING.equals(methodName)) {
            OpcodeStack.Item itm = getStringBufferItemAt(0);
            if (itm != null) {
                userValue = (ISBUserValue) itm.getUserValue();
            }
        }
    } else if (Values.TOSTRING.equals(getNameConstantOperand()) && SignatureBuilder.SIG_VOID_TO_STRING.equals(getSigConstantOperand()) && // calls to this.toString() are okay, some people like to be explicit
    (stack.getStackDepth() > 0) && (stack.getStackItem(0).getRegisterNumber() != 0)) {
        userValue = new ISBUserValue(AppendType.TOSTRING);
    }
    return userValue;
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) BugInstance(edu.umd.cs.findbugs.BugInstance) ToString(com.mebigfatguy.fbcontrib.utils.ToString) ConstantString(org.apache.bcel.classfile.ConstantString)

Example 2 with ToString

use of com.mebigfatguy.fbcontrib.utils.ToString in project fb-contrib by mebigfatguy.

the class CustomBuiltXML method sawOpcode.

/**
 * overrides the visitor to find String concatenations including xml strings
 *
 * @param seen
 *            the opcode that is being visited
 */
@Override
public void sawOpcode(int seen) {
    String strCon = null;
    try {
        stack.precomputation(this);
        if (seen == INVOKESPECIAL) {
            String clsName = getClassConstantOperand();
            if (SignatureUtils.isPlainStringConvertableClass(clsName)) {
                String methodName = getNameConstantOperand();
                String methodSig = getSigConstantOperand();
                if (Values.CONSTRUCTOR.equals(methodName) && XML_SIG_BUILDER.withReturnType(clsName).toString().equals(methodSig) && (stack.getStackDepth() > 0)) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    strCon = (String) itm.getConstant();
                }
            }
        } else if (seen == INVOKEVIRTUAL) {
            String clsName = getClassConstantOperand();
            if (SignatureUtils.isPlainStringConvertableClass(clsName)) {
                String methodName = getNameConstantOperand();
                String methodSig = getSigConstantOperand();
                if ("append".equals(methodName) && XML_SIG_BUILDER.withReturnType(clsName).toString().equals(methodSig) && (stack.getStackDepth() > 0)) {
                    OpcodeStack.Item itm = stack.getStackItem(0);
                    strCon = (String) itm.getConstant();
                }
            }
        }
        if (strCon != null) {
            strCon = strCon.trim();
            if (strCon.length() == 0) {
                return;
            }
            for (XMLPattern pattern : xmlPatterns) {
                Matcher m = pattern.getPattern().matcher(strCon);
                if (m.matches()) {
                    xmlItemCount++;
                    if (pattern.isConfident()) {
                        xmlConfidentCount++;
                    }
                    if ((firstPC < 0) && (xmlConfidentCount > 0)) {
                        firstPC = getPC();
                    }
                    break;
                }
            }
        }
    } finally {
        stack.sawOpcode(this, seen);
    }
}
Also used : OpcodeStack(edu.umd.cs.findbugs.OpcodeStack) Matcher(java.util.regex.Matcher) ToString(com.mebigfatguy.fbcontrib.utils.ToString)

Aggregations

ToString (com.mebigfatguy.fbcontrib.utils.ToString)2 OpcodeStack (edu.umd.cs.findbugs.OpcodeStack)2 BugInstance (edu.umd.cs.findbugs.BugInstance)1 Matcher (java.util.regex.Matcher)1 ConstantString (org.apache.bcel.classfile.ConstantString)1