Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * 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;
 
 
 
 
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 BytecodeScanningDetector {
 
     private static JavaClass CLONE_CLASS;
 
     static {
         try {
              = Repository.lookupClass("java/lang/Cloneable");
         } catch (ClassNotFoundException cnfe) {
              = null;
         }
     }
 
     private BugReporter bugReporter;
     private JavaClass cls;
     private String clsName;
     private OpcodeStack stack;
     private boolean throwsCNFE;

    
constructs a CU detector given the reporter to report bugs on

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

    
overrides the visitor to check for classes that implement Cloneable.

Parameters:
classContext the context object that holds the JavaClass being parsed
 
     @Override
 	public void visitClassContext(ClassContext classContext) {
         try {
              = classContext.getJavaClass();
             if (.implementationOf()) {
                  = .getClassName();
                  = new OpcodeStack();
                 super.visitClassContext(classContext);
             }
         } catch (ClassNotFoundException cnfe) {
             .reportMissingClass(cnfe);
         } finally {
              = null;
              = null;
         }
     }

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

Parameters:
obj the method being parsed
 
     @Override
     public void visitCode(Code obj) {
         try {
             Method m = getMethod();
            if (m.isPublic() && !m.isSynthetic() && "clone".equals(m.getName()) && (m.getArgumentTypes().length == 0)) {
                String returnClsName = m.getReturnType().getSignature();
                returnClsName = returnClsName.substring(1, returnClsName.length() - 1).replaceAll("/"".");
                if (!.equals(returnClsName))
                {
                    if ("java.lang.Object".equals(returnClsName)) {
                        .reportBug(new BugInstance(this..name(), )
                                    .addClass(this)
                                    .addMethod(this));
                    } else {
                        JavaClass cloneClass = Repository.lookupClass(returnClsName);
                        if (!.instanceOf(cloneClass)) {
                            .reportBug(new BugInstance(this..name(), )
                            .addClass(this)
                            .addMethod(this));
                        }
                    }
                }
                ExceptionTable et = m.getExceptionTable();
                if ((et != null) && (et.getLength() > 0)) {
                    
                     = false;
                    if (prescreen(m)) {
                        .resetForMethodEntry(this);
                        super.visitCode(obj);
                    }
                        
                    if (!) {
                        .reportBug(new BugInstance(this..name(), )
                        .addClass(this)
                        .addMethod(this));
                    }
                }
            }
        } catch (ClassNotFoundException cnfe) {
            .reportMissingClass(cnfe);
        }
    }
    
    @Override
	public void sawOpcode(int seen) {
        try {
            if (seen == ) {
                if (.getStackDepth() > 0) {
                    OpcodeStack.Item item = .getStackItem(0);
                    if ("Ljava/lang/CloneNotSupportedException;".equals(item.getSignature())) {
                         = true;  
                    }
                }
            }
        }
        finally {
            .sawOpcode(thisseen);
        }
    }
    
    
looks for methods that contain a THROW opcode

Parameters:
method the context object of the current method
Returns:
if the class throws exceptions
    private boolean prescreen(Method method) {
        BitSet bytecodeSet = getClassContext().getBytecodeSet(method);
        return (bytecodeSet != null) && bytecodeSet.get(.);
    }
New to GrepCode? Check out our FAQ X