Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2012 Bhaskar Maddala
   *
   * 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  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.LocalVariable;
 import  org.apache.bcel.classfile.LocalVariableTable;
 import  org.apache.bcel.generic.ArrayType;
 import  org.apache.bcel.generic.ObjectType;
 import  org.apache.bcel.generic.Type;
 
 
 import  edu.umd.cs.findbugs.BugInstance;
 import  edu.umd.cs.findbugs.BugReporter;
 import  edu.umd.cs.findbugs.BytecodeScanningDetector;
 import  edu.umd.cs.findbugs.OpcodeStack;

Finds contravariant array assignments. Since arrays are mutable data structures, their use must be restricted to covariant or invariant usage
 class A {}
 class B extends A {}

 B[] b = new B[2];
 A[] a = b;
 a[0] = new A(); // results in ArrayStoreException (Runtime)
 
Contravariant array assignments are reported as low or normal priority bugs. In cases where the detector can determine an ArrayStoreException the bug is reported with high priority.
 
 public class ContraVariantArrayAssignment extends BytecodeScanningDetector {
 	private final BugReporter bugReporter;
 	private final OpcodeStack stack;

    
constructs a CVAA detector given the reporter to report bugs on.

Parameters:
bugReporter the sync of bug reports
 
 	public ContraVariantArrayAssignment(final BugReporter bugReporter) {
 		this. = bugReporter;
 		 = new OpcodeStack();
 	}

implements the visitor to pass through constructors and static initializers to the byte code scanning code. These methods are not reported, but are used to build SourceLineAnnotations for fields, if accessed.

Parameters:
obj the context object of the currently parsed code attribute
 
 	public void visitCode(Code obj) {
 		.resetForMethodEntry(this);
 		LocalVariableTable lvt = getMethod().getLocalVariableTable();
 		if(lvt != null) {
 			super.visitCode(obj);
 		}
 	}
 
 	public void sawOpcode(int seen) {
 		try{
 			switch(seen){
 			case ASTORE:
 			case ASTORE_0:
 			case ASTORE_1:
 			case ASTORE_2:
 			case ASTORE_3:
 				if(.getStackDepth() > 0) {
 					LocalVariable lv = getMethod().getLocalVariableTable()
 					   .getLocalVariable(RegisterUtils.getAStoreReg(thisseen), getNextPC());
 					if(lv != null) {
 	                    OpcodeStack.Item item = .getStackItem(0);
 	                    String sourceSignature = item.getSignature();
 						String targetSignature = lv.getSignature();
 						checkSignatures(sourceSignaturetargetSignature);
 					}
				}
				break;
			case PUTFIELD:
			case PUTSTATIC:
				if(.getStackDepth() > 0) {
					OpcodeStack.Item item = .getStackItem(0);
					String sourceSignature = item.getSignature();
					String targetSignature = getSigConstantOperand();
					checkSignatures(sourceSignaturetargetSignature);
				}
				break;
			case AASTORE:
/*
				OpcodeStack.Item arrayref = stack.getStackItem(2);
				OpcodeStack.Item value = stack.getStackItem(0);
				if(!value.isNull()) {
					String sourceSignature = value.getSignature();
					String targetSignature = arrayref.getSignature();
					if (!"Ljava/lang/Object;".equals(targetSignature)) {
						try{
							if(Type.getType(sourceSignature) instanceof ObjectType ) {
								ObjectType sourceType = (ObjectType) Type.getType(sourceSignature);
								ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
								if(!sourceType.equals(targetType) && !sourceType.subclassOf(targetType)){
									bugReporter.reportBug(new BugInstance(this, "CVAA_CONTRAVARIANT_ARRAY_ASSIGNMENT", HIGH_PRIORITY)
								    .addClass(this)
								    .addMethod(this)
								    .addSourceLine(this));
								}
							}
						} catch (ClassNotFoundException cnfe) {
							bugReporter.reportMissingClass(cnfe);
						}
					}
				}
*/
				break;
			}
			super.sawOpcode(seen);
		}
		finally{
			.sawOpcode(thisseen);
		}
	}
	private boolean isArrayType(String signature){
	    return Type.getType(signatureinstanceof ArrayType;
	}
	private boolean isObjectType(String signature){
		return ((ArrayType)Type.getType(signature)).getBasicType() instanceof ObjectType;
	}
	private void checkSignatures(String sourceSignatureString targetSignature) {
		try{
			if ("Ljava/lang/Object;".equals(targetSignature)) {
				return;
			}
			if(isArrayType(sourceSignature) && isArrayType(targetSignature)) {
				if(isObjectType(sourceSignature) && isObjectType(targetSignature)) {
					ObjectType sourceType = (ObjectType) ((ArrayType) Type.getType(sourceSignature)).getBasicType();
					ObjectType targetType = (ObjectType) ((ArrayType) Type.getType(targetSignature)).getBasicType();
					if(!targetType.equals(sourceType) && !targetType.subclassOf(sourceType)) {
						.reportBug(new BugInstance(this"CVAA_CONTRAVARIANT_ELEMENT_ASSIGNMENT", NORMAL_PRIORITY)
						    .addClass(this)
						    .addMethod(this)
						    .addSourceLine(this));
					}
				}
			}
		}
		catch(ClassNotFoundException cnfe) {
			.reportMissingClass(cnfe);
		}
	}
New to GrepCode? Check out our FAQ X