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
  * 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.Set;
 
 import  org.apache.bcel.classfile.Constant;
 import  org.apache.bcel.classfile.ConstantClass;
 import  org.apache.bcel.classfile.ConstantPool;
 import  org.apache.bcel.classfile.JavaClass;
 
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.Detector;
 import  edu.umd.cs.findbugs.ba.ClassContext;

looks for classes that use objects from com.sun.xxx packages. As these are internal to sun and subject to change, this should not be done.
 
 public class IncorrectInternalClassUse implements Detector
 {
     private final BugReporter bugReporter;
     private static final Set<StringinternalPackages = new HashSet<String>();
     private static final Set<StringexternalPackages = new HashSet<String>();
     static {
         .add("com/sun/");
         .add("org/apache/xerces/");
         .add("org/apache/xalan/");
         .add("com/sun/jersey");
         .add("com/sun/xml/xsom");
         .add("org/apache/xerces/xni/");
         .add("org/apache/xerces/xs/");
         .add("org/apache/xalan/extensions");
     }

    
constructs a IICU detector given the reporter to report bugs on

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

    
implements the visitor to look for classes that reference com.sun.xxx, or org.apache.xerces.xxx classes by looking for class constants in the constant pool

Parameters:
context the context object of the currently parsed class
 
     public void visitClassContext(ClassContext context) {
         JavaClass cls = context.getJavaClass();
         if (!isInternal(cls.getClassName())) {
             ConstantPool pool = cls.getConstantPool();
             int numItems = pool.getLength();
             for (int i = 0; i < numItemsi++) {
                 Constant c = pool.getConstant(i);
                 if (c instanceof ConstantClass) {
                     String clsName = ((ConstantClass) c).getBytes(pool);
                     if (isInternal(clsName)) {
                         .reportBug(new BugInstance(this"IICU_INCORRECT_INTERNAL_CLASS_USE", NORMAL_PRIORITY)
                         .addClass(cls)
                         .addString(clsName));
                     }
                 }
             }
         }
     }

    
implementation stub for Detector interface
 
     public void report() {
     }

    
determines if the class in question is an internal class by looking at package prefixes

Parameters:
clsName the name of the class to check
Returns:
whether the class is internal
 
     private boolean isInternal(String clsName) {
         boolean internal = false;
        for (String internalPackage : ) {
            if (clsName.startsWith(internalPackage)) {
                internal = true;
                break;
            }
        }
        if (internal) {
            for (String externalPackage : ) {
                if (clsName.startsWith(externalPackage)) {
                    internal = false;
                    break;
                }
            }
        }
        return internal;
    }
New to GrepCode? Check out our FAQ X