Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2014 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.Code;
 
 
looks for if/else blocks where a series of them use instanceof on the same variable to determine what do to. If these classes are related by inheritance, this often is better handled through calling a single overridden method.
 
 public class InheritanceTypeChecking extends BytecodeScanningDetector 
 {
constructs a ITC detector given the reporter to report bugs on

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

implements the visitor to allocate and clear the ifStatements set

Parameters:
classContext the context object of the currently parsed class
 
 	public void visitClassContext(ClassContext classContext) {
 		try {
 			super.visitClassContext(classContext);
 		} finally {
 			 = null;
 		}
 	}

implements the visitor to clear the ifStatements set

Parameters:
obj the context object of the currently parsed code block
 
 	public void visitCode(Code obj) {
 		super.visitCode(obj);
 	}

implements the visitor to find if/else code that checks types using instanceof, and these types are related by inheritance.

Parameters:
seen the opcode of the currently parsed instruction
 
 	public void sawOpcode(int seen) {
 		boolean processed = false;
 		while (isi.hasNext()) {
 			IfStatement.Action action = isi.next().processOpcode(thisseen);
 			if (action == ..)
 				isi.remove();
 			else if (action == ..)
 				processed = true;
 		}
 		
 		if (!processed) {
 			if ((seen == ALOAD)
 			||  ((seen >= ALOAD_0) && (seen <= ALOAD_3))) {
				IfStatement is = new IfStatement(thisseen);
			}
		}
	}
	private static class IfStatement {
		enum Action {NO_ACTION, REMOVE_ACTION, PROCESSED_ACTION}
		enum State {SEEN_ALOAD, SEEN_INSTANCEOF, SEEN_IFEQ}
		private State state;
		private int reg;
		private int firstPC;
		private int branchTarget;
		private int matchCount;
		public IfStatement(BytecodeScanningDetector bsdint seen) {
			 = RegisterUtils.getALoadReg(bsdseen);
			 = bsd.getPC();
		}
		public IfStatement.Action processOpcode(BytecodeScanningDetector bsdBugReporter bugReporterint seen) {
			switch () {
					if (seen == INSTANCEOF) {
						if ( == null)
					}
				break;
					if (seen == IFEQ) {
					}
				break;
				case :
					if (bsd.getPC() == ) {
						if ((seen == ALOAD)
						||  ((seen >= ALOAD_0) && (seen <= ALOAD_3))) {
							if ( == RegisterUtils.getALoadReg(bsdseen)) {
							}
						}
						if ( > 1) {
							String clsName = bsd.getClassName();
							int priority = NORMAL_PRIORITY;
							for (String type : ) {
								if (!SignatureUtils.similarPackages(clsNametype, 2)) {
									priority = LOW_PRIORITY;
									break;
								}
							}
							bugReporter.reportBug(new BugInstance(bsd"ITC_INHERITANCE_TYPE_CHECKING"priority)
									   .addClass(bsd)
									   .addMethod(bsd)
									   .addSourceLine(bsd));
						}
					}
			}
		}
	}
New to GrepCode? Check out our FAQ X