Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2013 Bhaskar Maddala
   * Copyright (C) 2005-2013 Dave Brosius
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
   * version 2.1 of the License, or (at your option) any later version.
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 package com.mebigfatguy.fbcontrib.detect;
 import java.util.Map;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.LocalVariable;
 import  org.apache.bcel.classfile.LocalVariableTable;
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.OpcodeStack.Item;
 import  edu.umd.cs.findbugs.bcel.OpcodeStackDetector;

Find usage of ToStringBuilder from Apache commons, where the code invokes toString() on the constructed object without invoking append(). Usage without invoking append is equivalent of using the Object.toString() method
 new ToStringBuilder(this).toString();
 public class CommonsStringBuilderToString extends OpcodeStackDetector {
     private final BugReporter bugReporter;
     private Stack<PairstackTracker = new Stack<Pair>();
     private Map<IntegerBooleanregisterTracker = new HashMap<IntegerBoolean>();

constructs a CSBTS detector given the reporter to report bugs on.

bugReporter the sync of bug reports
     public CommonsStringBuilderToString(final BugReporter bugReporter) {
         this. = bugReporter;
     public void visit(Code obj) {
     public boolean shouldVisitCode(Code obj) {
         LocalVariableTable lvt = getMethod().getLocalVariableTable();
         return lvt != null;
     public void sawOpcode(int seen) {
         switch (seen) {
         case ALOAD:
         case ALOAD_0:
         case ALOAD_1:
         case ALOAD_2:
         case ALOAD_3:
             LocalVariable lv = getMethod().getLocalVariableTable()
             if (lv != null) {
                 String signature = lv.getSignature();
                 if (isToStringBuilder(signature)) {
                     Integer loadReg = Integer.valueOf(getRegisterOperand());
                     Boolean appendInvoked = .get(loadReg);
                     if (appendInvoked != null) {
                         .add(new Pair(loadReg.intValue(),
        case ASTORE:
        case ASTORE_0:
        case ASTORE_1:
        case ASTORE_2:
        case ASTORE_3:
            Item si = stack.getStackItem(0);
            String signature = si.getSignature();
            if (isToStringBuilder(signature)) {
                int storeReg = getRegisterOperand();
                Pair p = .pop();
                        p.register == -1 ? . : 
        case POP:
            si = stack.getStackItem(0);
            signature = si.getSignature();
            if (isToStringBuilder(signature)) {
                if (!.isEmpty()) {
                    Pair p = .pop();
        case INVOKESPECIAL:
        case INVOKEVIRTUAL:
            String loadClassName = getClassConstantOperand();
            String calledMethodName = getNameConstantOperand();
            if ("org/apache/commons/lang3/builder/ToStringBuilder"
                    || "org/apache/commons/lang/builder/ToStringBuilder"
                            .equals(loadClassName)) {
                String calledMethodSig = getSigConstantOperand();
                if ("<init>".equals(calledMethodName)
                        && "(Ljava/lang/Object;)V".equals(calledMethodSig)) {
                    .add(new Pair(-1, false));
                } else if ("append".equals(calledMethodName)) {
                    Pair p = .pop();
                    .add(new Pair(p.registertrue));
                } else if ("toString".equals(calledMethodName)
                        && "()Ljava/lang/String;".equals(calledMethodSig)) {
                    Pair p = .pop();
                    if (p.appendInvoked == false) {
                        .reportBug(new BugInstance(this,
    private boolean isToStringBuilder(String signature) {
        return "Lorg/apache/commons/lang3/builder/ToStringBuilder;"
                || "Lorg/apache/commons/lang/builder/ToStringBuilder;"
    static final class Pair {
        public final int register;
        public final boolean appendInvoked;
        Pair(int registerboolean appendInvoked) {
            this. = register;
            this. = appendInvoked;
New to GrepCode? Check out our FAQ X