⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multithreadedinstanceaccess.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
字号:
/* * FindBugs - Find bugs in Java programs * Copyright (C) 2005 Dave Brosius <dbrosius@users.sourceforge.net> * Copyright (C) 2005 University of Maryland *  * 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 edu.umd.cs.findbugs.detect;import edu.umd.cs.findbugs.*;import edu.umd.cs.findbugs.ba.ClassContext;import java.util.*;import org.apache.bcel.Repository;import org.apache.bcel.classfile.*;public class MultithreadedInstanceAccess extends BytecodeScanningDetector{	private static final String STRUTS_ACTION_NAME = "org.apache.struts.action.Action";	private static final String SERVLET_NAME = "javax.servlet.Servlet";	private BugReporter bugReporter;	private Set<JavaClass> mtClasses;	private String mtClassName;	private int monitorCount;	private boolean writingField;	private Set<String> alreadyReported;	public MultithreadedInstanceAccess(BugReporter bugReporter) {		this.bugReporter = bugReporter;	}	private Set<JavaClass> getMtClasses() {		if (mtClasses != null)			return mtClasses;		mtClasses = new HashSet<JavaClass>();		try {			mtClasses.add(Repository.lookupClass(STRUTS_ACTION_NAME));		} catch (ClassNotFoundException cnfe) {			//probably would be annoying to report		}				try {			mtClasses.add(Repository.lookupClass(SERVLET_NAME));		} catch (ClassNotFoundException cnfe) {			//probably would be annoying to report		}		return mtClasses;	}	@Override		 public void visitClassContext(ClassContext classContext) {		try {			JavaClass cls = classContext.getJavaClass();			String superClsName = cls.getSuperclassName();			if ("java.lang.Object".equals(superClsName))				return;			if (STRUTS_ACTION_NAME.equals(superClsName)) {				mtClassName = STRUTS_ACTION_NAME;				super.visitClassContext(classContext);			}			else if (SERVLET_NAME.equals(superClsName)) {				mtClassName = SERVLET_NAME;				super.visitClassContext(classContext);			}			else {				for (JavaClass mtClass : getMtClasses()) {					/* note: We could just call cls.instanceOf(mtClass) and it would work for both					 * classes and interfaces, but if mtClass is an interface it is more efficient					 * to call cls.implementationOf() and since we're doing this on each visit that's					 * what we'll do.					 * also note: implementationOf(mtClass) throws an IllegalArgumentException when					 * mtClass is not an interface. See bug#1428253. */					if (mtClass.isClass() ? cls.instanceOf(mtClass) : cls.implementationOf(mtClass)) {						mtClassName = mtClass.getClassName();						super.visitClassContext(classContext);						return;					}				}			}		} catch (Exception e) {			//already reported		}	}	@Override		 public void visitMethod(Method obj) {		monitorCount = 0;		alreadyReported = new HashSet<String>();		writingField = false;	}	@Override		 public void visitCode(Code obj) {		if (!getMethodName().equals("<init>") && !getMethodName().equals("init"))			super.visitCode(obj);	}	@Override		 public void sawField() {		if ((monitorCount > 0) || (!writingField))			return;		ConstantFieldref fieldRef;		Constant c = getConstantRefOperand();		if (c instanceof ConstantFieldref) {			fieldRef = (ConstantFieldref)c;			String className = fieldRef.getClass(getConstantPool()).replace('.', '/');			if (className.equals( this.getClassName())) {				ConstantPool cp = getConstantPool();				int nameAndTypeIdx = fieldRef.getNameAndTypeIndex();				ConstantNameAndType ntc = (ConstantNameAndType)cp.getConstant(nameAndTypeIdx);				int nameIdx = ntc.getNameIndex();				Field[] flds = getClassContext().getJavaClass().getFields();				for (Field fld : flds) {					if (fld.getNameIndex() == nameIdx) {						if (!fld.isStatic()) {							ConstantUtf8 nameCons = (ConstantUtf8) cp.getConstant(nameIdx);							ConstantUtf8 typeCons = (ConstantUtf8) cp.getConstant(ntc.getSignatureIndex());							if (alreadyReported.contains(nameCons.getBytes()))								return;							alreadyReported.add(nameCons.getBytes());							bugReporter.reportBug(new BugInstance(this,									STRUTS_ACTION_NAME.equals(mtClassName) ? "MTIA_SUSPECT_STRUTS_INSTANCE_FIELD" : "MTIA_SUSPECT_SERVLET_INSTANCE_FIELD",									LOW_PRIORITY)									.addField(new FieldAnnotation(getClassName(), nameCons.getBytes(), typeCons.getBytes(), false))									.addClass(this).addSourceLine(this)									);						}						break;					}				}			}		}	}	@Override		 public void sawOpcode(int seen) {		if (seen == MONITORENTER)			monitorCount++;		else if (seen == MONITOREXIT)			monitorCount--;		writingField = ((seen == PUTFIELD) || (seen == PUTFIELD_QUICK) || (seen == PUTFIELD_QUICK_W));	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -