Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /* NullnessAnalysis
   * Copyright (C) 2006 Eric Bodden
   * Copyright (C) 2007 Julian Tibble
   *
   * 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 soot.jimple.toolkits.annotation.nullcheck;
 
 import java.util.List;
 
 import soot.Immediate;
 import soot.Local;
 import soot.Unit;
 import soot.Value;
An intraprocedural nullness analysis that computes for each location and each value in a method if the value is (before or after that location) definetely null, definetely non-null or neither. This class replaces BranchedRefVarsAnalysis which is known to have bugs.

Author(s):
Eric Bodden
Julian Tibble
 
 public class NullnessAnalysis  extends ForwardBranchedFlowAnalysis
 {
The analysis info is a simple mapping of type soot.Value to any of the constants BOTTOM, NON_NULL, NULL or TOP. This class returns BOTTOM by default.

Author(s):
Julian Tibble
 
 	protected class AnalysisInfo extends java.util.BitSet
 	{
 		public AnalysisInfo() {
 			super();
 		}
 
 		public AnalysisInfo(AnalysisInfo other) {
 			super();
 			or(other);
 		}
 
 		public int get(Value key)
 		{
 				return ;
 
 			int index = .get(key);
 			int result = get(index) ? 2 : 0;
 			result += get(index + 1) ? 1 : 0;
 
 			return result;
 		}
 		
 		public void put(Value keyint val)
 		{
			int index;
				index = ;
				 += 2;
				.put(keyindex);
else {
				index = .get(key);
			}
			set(index, (val & 2) == 2);
			set(index + 1, (val & 1) == 1);
		}
	}
	protected final static int BOTTOM = 0;
	protected final static int NULL = 1;
	protected final static int NON_NULL = 2;
	protected final static int TOP = 3;
	protected final HashMap<Value,IntegervalueToIndex = new HashMap<Value,Integer>();
	protected int used = 0;

Creates a new analysis for the given graph/

Parameters:
graph any unit graph
	public NullnessAnalysis(UnitGraph graph) {
		super(graph);
	}

	@SuppressWarnings("unchecked")
	protected void flowThrough(Object flowinUnit uList fallOutList branchOuts) {
		AnalysisInfo in = (AnalysisInfoflowin;
		AnalysisInfo out = new AnalysisInfo(in);
		AnalysisInfo outBranch = new AnalysisInfo(in);
		Stmt s = (Stmt)u;
		//in case of an if statement, we neet to compute the branch-flow;
		//e.g. for a statement "if(x!=null) goto s" we have x==null for the fallOut and
		//x!=null for the branchOut
		//or for an instanceof expression
		if(s instanceof JIfStmt) {
			JIfStmt ifStmt = (JIfStmts;
			handleIfStmt(ifStmtinoutoutBranch);
		}
		//in case of a monitor statement, we know that if it succeeds, we have a non-null value
		else if(s instanceof MonitorStmt) {
			MonitorStmt monitorStmt = (MonitorStmts;
			out.put(monitorStmt.getOp(), );
		}
		// if we have an array ref, set the base to non-null
			ArrayRef arrayRef = s.getArrayRef();
			handleArrayRef(arrayRef,out);
		}
		// for field refs, set the receiver object to non-null, if there is one
			FieldRef fieldRef = s.getFieldRef();
			handleFieldRef(fieldRefout);
		}
		// for invoke expr, set the receiver object to non-null, if there is one
			InvokeExpr invokeExpr = s.getInvokeExpr();
			handleInvokeExpr(invokeExprout);
		}
		//if we have a definition (assignment) statement to a ref-like type, handle it,
		//i.e. assign it TOP, except in the following special cases:
		// x=null,               assign NULL
		// x=@this or x= new...  assign NON_NULL
		// x=y,                  copy the info for y (for locals x,y)
		if(s instanceof DefinitionStmt) {
			DefinitionStmt defStmt = (DefinitionStmts;
			if(defStmt.getLeftOp().getType() instanceof RefLikeType) {
			}
		}
		// now copy the computed info to all successors
		forIterator it = fallOut.iterator(); it.hasNext(); ) {
			copyoutit.next() );
		}
		forIterator it = branchOuts.iterator(); it.hasNext(); ) {
			copyoutBranchit.next() );
		}
	}

This can be overwritten by sublasses to mark a certain value as constantly non-null.

Parameters:
v any value
Returns:
true if it is known that this value (e.g. a method return value) is never null
	protected boolean isAlwaysNonNull(Value v) {
		return false;
	}
	private void handleIfStmt(JIfStmt ifStmtAnalysisInfo inAnalysisInfo outAnalysisInfo outBranch) {
		Value condition = ifStmt.getCondition();
		if(condition instanceof JInstanceOfExpr) {
			//a instanceof X ; if this succeeds, a is not null
			JInstanceOfExpr expr = (JInstanceOfExprcondition;
			handleInstanceOfExpression(exprinoutoutBranch);
else if(condition instanceof JEqExpr || condition instanceof JNeExpr) {
			//a==b or a!=b
			AbstractBinopExpr eqExpr = (AbstractBinopExprcondition;
			handleEqualityOrNonEqualityCheck(eqExprinoutoutBranch);
	}
			AnalysisInfo outAnalysisInfo outBranch) {
		Value left = eqExpr.getOp1();
		Value right = eqExpr.getOp2();
		Value val=null;
		if(left==NullConstant.v()) {
			if(right!=NullConstant.v()) {
				val = right;
			}
else if(right==NullConstant.v()) {
			if(left!=NullConstant.v()) {
				val = left;
			}
		}
		//if we compare a local with null then process further...
		if(val!=null && val instanceof Local) {
			if(eqExpr instanceof JEqExpr)
				//a==null
				handleEquality(valoutoutBranch);
			else if(eqExpr instanceof JNeExpr)
				//a!=null
				handleNonEquality(valoutoutBranch);
			else
				throw new IllegalStateException("unexpected condition: "+eqExpr.getClass());
		}
	}
	private void handleNonEquality(Value valAnalysisInfo out,
			AnalysisInfo outBranch) {
		out.put(val);
		outBranch.put(val);
	}
	private void handleEquality(Value valAnalysisInfo out,
			AnalysisInfo outBranch) {
		out.put(val);
		outBranch.put(val);
	}
			AnalysisInfo inAnalysisInfo outAnalysisInfo outBranch) {
		Value op = expr.getOp();
		//if instanceof succeeds, we have a non-null value
		outBranch.put(op,);
	}
	private void handleArrayRef(ArrayRef arrayRefAnalysisInfo out) {
		Value array = arrayRef.getBase();
		//here we know that the array must point to an object
		out.put(array);
	}
	private void handleFieldRef(FieldRef fieldRef,
			AnalysisInfo out) {
		if(fieldRef instanceof InstanceFieldRef) {
			InstanceFieldRef instanceFieldRef = (InstanceFieldReffieldRef;
			//here we know that the receiver must point to an object
			Value base = instanceFieldRef.getBase();
			out.put(base,);
		}
	}
	private void handleInvokeExpr(InvokeExpr invokeExpr,AnalysisInfo out) {
		if(invokeExpr instanceof InstanceInvokeExpr) {
			InstanceInvokeExpr instanceInvokeExpr = (InstanceInvokeExprinvokeExpr;
			//here we know that the receiver must point to an object
			Value base = instanceInvokeExpr.getBase();
			out.put(base,);
		}
	}
	private void handleRefTypeAssignment(DefinitionStmt assignStmtAnalysisInfo out) {
		Value left = assignStmt.getLeftOp();
		Value right = assignStmt.getRightOp();
		//unbox casted value
		if(right instanceof JCastExpr) {
			JCastExpr castExpr = (JCastExprright;
			right = castExpr.getOp();
		}
		//if we have a definition (assignment) statement to a ref-like type, handle it,
		if ( isAlwaysNonNull(right)
		|| right instanceof NewExpr || right instanceof NewArrayExpr
		|| right instanceof NewMultiArrayExpr || right instanceof ThisRef
		|| right instanceof StringConstant || right instanceof ClassConstant) {
			//if we assign new... or @this, the result is non-null
			out.put(left,);
else if(right==NullConstant.v()) {
			//if we assign null, well, it's null
			out.put(left);
else if(left instanceof Local && right instanceof Local) {
			out.put(leftout.get(right));
else {
			out.put(left);
		}
	}

	protected void copy(Object sourceObject dest) {
		AnalysisInfo s = (AnalysisInfosource;
		AnalysisInfo d = (AnalysisInfodest;
		d.clear();
		d.or(s);
	}

	protected Object entryInitialFlow() {
		return new AnalysisInfo();
	}

	protected void merge(Object in1Object in2Object out) {
		AnalysisInfo outflow = (AnalysisInfoout;
		outflow.clear();
		outflow.or((AnalysisInfoin1);
		outflow.or((AnalysisInfoin2);
	}

	protected Object newInitialFlow() {
		return new AnalysisInfo();
	}

Returns true if the analysis could determine that i is always null before the statement s.

Parameters:
s a statement of the respective body
i a local or constant of that body
Returns:
true if i is always null right before this statement
	public boolean isAlwaysNullBefore(Unit sImmediate i) {
		return ai.get(i)==;
	}

Returns true if the analysis could determine that i is always non-null before the statement s.

Parameters:
s a statement of the respective body
i a local of that body
Returns:
true if i is always non-null right before this statement
	public boolean isAlwaysNonNullBefore(Unit sImmediate i) {
		return ai.get(i)==;
	}
New to GrepCode? Check out our FAQ X