Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * fb-contrib - Auxiliary detectors for Java programs
   * Copyright (C) 2005-2015 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;
 
 
 
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.
 
 {
 	private static final Integer OWNED = Integer.valueOf(.);
 	private static final Integer LOW = Integer.valueOf();
 	private static final Integer NORMAL = Integer.valueOf();
 	private final BugReporter bugReporter;
 	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(.));
 	}

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 :
					tosIsPriority = ;
				break;
				case :
				case :
				case :
				case :
				case : {
					int reg = RegisterUtils.getALoadReg(thisseen);
					if (getMethod().isStatic() && (reg == 0))
						tosIsPriority = ;
					else {
						tosIsPriority = .get(Integer.valueOf(reg));
						if (tosIsPriority == null)
							tosIsPriority = ;
					}
				}
				break;
				case :
				case :
				case :
				case :
				case : {
					if (.getStackDepth() > 0) {
						Integer priority = (Integer)item.getUserValue();
						.put(Integer.valueOf(RegisterUtils.getAStoreReg(thisseen)), priority);
					}
				}
				break;
					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 : {
					Type t = Type.getReturnType(sig);
					if (t.getSignature().startsWith("L"))
						tosIsPriority = // can't be sure
				}
				break;
				case : {
					if (..equals(name)) {
						tosIsPriority = ;
					}
				}
				break;
				case : {
					if (.getStackDepth() > 0) {
						Integer priority = (Integer)itm.getUserValue();
						if ((priority != null) && (!priority.equals())) {
											.addClass(this)
											.addMethod(this)
						}
					}
				}
				break;
				default:
					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