Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2015 Bhaskar Maddala
   * Copyright (C) 2005-2015 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
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * 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 java.util.Set;
 
 
 
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();
 
 
 
     private static final Set<StringTOSTRINGBUILDER_CTOR_SIGS = new HashSet<String>();
     static {
         .add("(Ljava/lang/Object;)V");
         .add("(Ljava/lang/Object;Lorg/apache/commons/lang/builder/ToStringStyle;)V");
         .add("(Ljava/lang/Object;Lorg/apache/commons/lang3/builder/ToStringStyle;)V");
     }
     
     private final BugReporter bugReporter;
     private final Stack<PairstackTracker = new Stack<Pair>();
     private final Map<IntegerBooleanregisterTracker = new HashMap<IntegerBoolean>(10);

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

Parameters:
bugReporter the sync of bug reports
 
     public CommonsStringBuilderToString(final BugReporter bugReporter) {
         this. = bugReporter;
     }
 
     @Override
     public void visit(Code obj) {
         .clear();
         .clear();
         super.visit(obj);
     }
 
     @Override
     public boolean shouldVisitCode(Code obj) {
         LocalVariableTable lvt = getMethod().getLocalVariableTable();
         return lvt != null;
     }
 
     @Override
     public void sawOpcode(int seen) {
         switch (seen) {
         case :
         case :
         case :
         case :
         case :
             LocalVariable lv = getMethod().getLocalVariableTable()
                     .getLocalVariable(RegisterUtils.getALoadReg(thisseen),
                             getNextPC());
             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(),
                                appendInvoked.booleanValue()));
                    }
                }
            }
            break;
        case :
        case :
        case :
        case :
        case :
            Item si = .getStackItem(0);
            String signature = si.getSignature();
            if (isToStringBuilder(signature)) {
                int storeReg = getRegisterOperand();
                Pair p = .pop();
                .put(
                        Integer.valueOf(storeReg),
                        p.register == -1 ? . : 
                                .get(Integer.valueOf(p.register)));
            }
            break;
        case :
            si = .getStackItem(0);
            signature = si.getSignature();
            if (isToStringBuilder(signature)) {
                if (!.isEmpty()) {
                    Pair p = .pop();
                    .put(Integer.valueOf(p.register),
                            Boolean.valueOf(p.appendInvoked));
                }
            }
            break;
        case :
        case :
            String loadClassName = getClassConstantOperand();
            String calledMethodName = getNameConstantOperand();
            if ("org/apache/commons/lang3/builder/ToStringBuilder"
                    .equals(loadClassName)
                    || "org/apache/commons/lang/builder/ToStringBuilder"
                            .equals(loadClassName)) {
                String calledMethodSig = getSigConstantOperand();
                if (..equals(calledMethodName)
                        && .contains(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,
                                "CSBTS_COMMONS_STRING_BUILDER_TOSTRING",
                                ).addClass(this).addMethod(this)
                                .addSourceLine(this));
                    }
                }
            }
            break;
            
        default:
          	break;
        }
    }
    private static boolean isToStringBuilder(String signature) {
        return "Lorg/apache/commons/lang3/builder/ToStringBuilder;"
                .equals(signature)
                || "Lorg/apache/commons/lang/builder/ToStringBuilder;"
                        .equals(signature);
    }
    static final class Pair {
        public final int register;
        public final boolean appendInvoked;
        Pair(int registerboolean appendInvoked) {
            this. = register;
            this. = appendInvoked;
        }
        
        @Override
        public String toString() {
        	return ToString.build(this);
        }
    }
New to GrepCode? Check out our FAQ X