title: Writing a detector to search for uses of "System.out.println" using Findbugs
body: <p>I am trying to write a bug detector to find instances of the method call "System.out.println" using Findbugs. </p>
  
  <p>I understand that "System.out.println" in bytecode is compiled to a call to GETSTATIC, <a href="https://www.vmth.ucdavis.edu/incoming/Jasmin/ref--19.html">which pushes "System.out" onto the stack</a>. A call to INVOKEVIRTUAL pops "System.out" off the stack and calls the method. </p>
  
  <p>I have prepared some code (found below) which finds the correct GETSTATIC and INVOKEVIRTUAL calls, but have been unable to link the two together. I suspect I may need to use OpcodeStack in some way, but am having trouble in understanding how I can use it. Any help would be appreciated. </p>
  
  <pre><code>    @Override 
      public void sawOpcode(int seen) { 
              // if opcode is getstatic 
              if (seen == GETSTATIC) { 
                      String clsName = getClassConstantOperand(); 
                      if ("java/lang/System".equals(clsName)) { 
                              String fldName = getNameConstantOperand(); 
                              if ("out".equals(fldName)) { 
                                      System.out.println("SYSTEM.OUT here"); 
                              } 
                      } 
              } 
  
              // if opcode is invokevirtual 
              if (seen == INVOKEVIRTUAL) { 
                      String cls = getDottedClassConstantOperand(); 
                      if ("java.io.PrintStream".equals(cls)) { 
                              String methodName = getNameConstantOperand(); 
                              if ("println".equals(methodName)) { 
                                      bugReporter.reportBug(new BugInstance("SYSTEM_OUT_PRINTLN", 
                                                      NORMAL_PRIORITY).addClassAndMethod(this) 
                                                      .addSourceLine(this)); 
                              } 
                      } 
              } 
  
      }
  </code></pre>