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.Map;
 
 import  org.apache.bcel.Constants;
 import  org.apache.bcel.classfile.Code;
 import  org.apache.bcel.classfile.Method;
 import  org.apache.bcel.generic.Type;
 
 
looks for methods that synchronize on variables that are not owned by the current class. Doing this causes confusion when two classes use the same variable for their own synchronization purposes. For cleanest separation of interests, only synchronize on private fields of the class. Note that 'this' is not owned by the current class and synchronization on 'this' should be avoided as well.
 
 public class NonOwnedSynchronization extends BytecodeScanningDetector
 {
 	private static final Integer OWNED = Integer.valueOf(.);
 	private static final Integer LOW = Integer.valueOf(LOW_PRIORITY);
 	private static final Integer NORMAL = Integer.valueOf(NORMAL_PRIORITY);
 	private OpcodeStack stack;
constructs a NOS detector given the reporter to report bugs on

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

    
implements the visitor to set and clear the stack and priorities
 
     @Override
     public void visitClassContext(ClassContext classContext) {
         try {
              = new OpcodeStack();
              = new HashMap<IntegerInteger>();
             super.visitClassContext(classContext);
         } finally {
              = null;
              = null;
         }
     }

looks for methods that contain a MONITORENTER opcode

Parameters:
method the context object of the current method
Returns:
if the class uses synchronization
 
 	public boolean prescreen(Method method) {
 		BitSet bytecodeSet = getClassContext().getBytecodeSet(method);
 		return bytecodeSet != null && (bytecodeSet.get(Constants.MONITORENTER));
 	}

implements the visitor to reset the opcode stack

Parameters:
obj the context object of the currently parsed code block
 
 	public void visitCode(Code obj) {
 		Method method = getMethod();
 		if (prescreen(method)) {
			int[] parmRegs = RegisterUtils.getParameterRegisters(method);
			for (int reg : parmRegs)
			if (!method.isStatic())
			super.visitCode(obj);
		}
	}

implements the visitor to look for synchronization on non-owned objects

Parameters:
seen the opcode of the currently parsed instruction
	public void sawOpcode(int seen) {
		Integer tosIsPriority = null;
		try {
	        .precomputation(this);
			switch (seen) {
				case GETFIELD:
					tosIsPriority = ;
				break;
				case ALOAD:
				case ALOAD_0:
				case ALOAD_1:
				case ALOAD_2:
				case ALOAD_3: {
					int reg = RegisterUtils.getALoadReg(thisseen);
					if (getMethod().isStatic() && (reg == 0))
						tosIsPriority = ;
					else {
						tosIsPriority = .get(Integer.valueOf(reg));
						if (tosIsPriority == null)
							tosIsPriority = ;
					}
				}
				break;
				case ASTORE:
				case ASTORE_0:
				case ASTORE_1:
				case ASTORE_2:
				case ASTORE_3: {
					if (.getStackDepth() > 0) {
						Integer priority = (Integer)item.getUserValue();
						.put(Integer.valueOf(RegisterUtils.getAStoreReg(thisseen)), priority);
					}
				}
				break;
				case INVOKEVIRTUAL:
				case INVOKEINTERFACE: {
					Type t = Type.getReturnType(sig);
					if (t.getSignature().startsWith("L")) {
						int parmCnt = Type.getArgumentTypes(sig).length;
						if (.getStackDepth() > parmCnt) {
							Integer priority = (Integer)itm.getUserValue();
							if ((priority != null) && .equals(priority)) {
								tosIsPriority = ;
else {
								int reg = itm.getRegisterNumber();
								if (reg > 0)
									tosIsPriority = .get(Integer.valueOf(reg));
								else
									tosIsPriority = ;
							}
						}
					}
				}
				break;
				case INVOKESTATIC: {
					Type t = Type.getReturnType(sig);
					if (t.getSignature().startsWith("L"))
						tosIsPriority = // can't be sure
				}
				break;
				case INVOKESPECIAL: {
					if ("<init>".equals(name)) {
						tosIsPriority = ;
					}
				}
				break;
				case MONITORENTER: {
					if (.getStackDepth() > 0) {
						Integer priority = (Integer)itm.getUserValue();
						if ((priority != null) && (!priority.equals())) {
							.reportBug(new BugInstance(this"NOS_NON_OWNED_SYNCHRONIZATION"priority.intValue())
											.addClass(this)
											.addMethod(this)
											.addSourceLine(this));
						}
					}
				}
				break;
			}
finally {
			TernaryPatcher.pre(seen);
			.sawOpcode(thisseen);
			TernaryPatcher.post(seen);
			if (tosIsPriority != null) {
				if (.getStackDepth() > 0) {
					itm.setUserValue(tosIsPriority);
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X