Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
   * fb-contrib - Auxiliary detectors for Java programs
   * 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  org.apache.bcel.Repository;
 import  org.apache.bcel.classfile.ExceptionTable;
 import  org.apache.bcel.classfile.JavaClass;
 import  org.apache.bcel.classfile.Method;
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.Detector;
 import  edu.umd.cs.findbugs.visitclass.PreorderVisitor;

finds classes that implement clone() that do not specialize the return value, and do not swallow CloneNotFoundException. Not doing so makes the clone method not as simple to use, and should be harmless to do.
 public class CloneUsability extends PreorderVisitor implements Detector {
     private static JavaClass CLONE_CLASS;
     static {
         try {
              = Repository.lookupClass("java.lang.Cloneable");
         } catch (ClassNotFoundException cnfe) {
              = null;
     private BugReporter bugReporter;
     private String clsName;

constructs a CU detector given the reporter to report bugs on

bugReporter the sync of bug reports
     public CloneUsability(BugReporter bugReporter) {
         this. = bugReporter;

overrides the visitor to check for classes that implement Cloneable.

classContext the context object that holds the JavaClass being parsed
     public void visitClassContext(ClassContext classContext) {
         try {
             JavaClass cls = classContext.getJavaClass();
             if (cls.implementationOf()) {
                  = cls.getClassName();
         } catch (ClassNotFoundException cnfe) {

overrides the visitor to grab the method name and reset the state.

obj the method being parsed
     public void visitMethod(Method obj) {
         if (obj.isPublic() && !obj.isSynthetic() && obj.getName().equals("clone") && (obj.getArgumentTypes().length == 0)) {
             String returnClsName = obj.getReturnType().getSignature();
             returnClsName = returnClsName.substring(1, returnClsName.length() - 1).replaceAll("/"".");
             if (!.equals(returnClsName))
                 .reportBug(new BugInstance(this"CU_CLONE_USABILITY_OBJECT_RETURN", NORMAL_PRIORITY)
             ExceptionTable et = obj.getExceptionTable();
             if ((et != null) && (et.getLength() > 0)) {
                 .reportBug(new BugInstance(this"CU_CLONE_USABILITY_THROWS", NORMAL_PRIORITY)

implements the Detector with a nop
    public void report() {
New to GrepCode? Check out our FAQ X