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

📄 findunrelatedtypesingenericcontainer.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * FindBugs - Find Bugs in Java programs * Copyright (C) 2006, 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 java.util.Arrays;import java.util.BitSet;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import org.apache.bcel.Constants;import org.apache.bcel.Repository;import org.apache.bcel.classfile.Attribute;import org.apache.bcel.classfile.ConstantCP;import org.apache.bcel.classfile.ConstantMethodref;import org.apache.bcel.classfile.ConstantNameAndType;import org.apache.bcel.classfile.JavaClass;import org.apache.bcel.classfile.Method;import org.apache.bcel.classfile.Synthetic;import org.apache.bcel.generic.ArrayType;import org.apache.bcel.generic.BasicType;import org.apache.bcel.generic.ConstantPoolGen;import org.apache.bcel.generic.Instruction;import org.apache.bcel.generic.InstructionHandle;import org.apache.bcel.generic.InvokeInstruction;import org.apache.bcel.generic.MethodGen;import org.apache.bcel.generic.ObjectType;import org.apache.bcel.generic.ReferenceType;import org.apache.bcel.generic.Type;import edu.umd.cs.findbugs.BugAccumulator;import edu.umd.cs.findbugs.BugInstance;import edu.umd.cs.findbugs.BugReporter;import edu.umd.cs.findbugs.Detector;import edu.umd.cs.findbugs.SourceLineAnnotation;import edu.umd.cs.findbugs.SystemProperties;import edu.umd.cs.findbugs.TypeAnnotation;import edu.umd.cs.findbugs.annotations.CheckForNull;import edu.umd.cs.findbugs.ba.CFG;import edu.umd.cs.findbugs.ba.CFGBuilderException;import edu.umd.cs.findbugs.ba.ClassContext;import edu.umd.cs.findbugs.ba.DataflowAnalysisException;import edu.umd.cs.findbugs.ba.IncompatibleTypes;import edu.umd.cs.findbugs.ba.Location;import edu.umd.cs.findbugs.ba.MethodUnprofitableException;import edu.umd.cs.findbugs.ba.SignatureParser;import edu.umd.cs.findbugs.ba.generic.GenericObjectType;import edu.umd.cs.findbugs.ba.generic.GenericUtilities;import edu.umd.cs.findbugs.ba.generic.GenericUtilities.TypeCategory;import edu.umd.cs.findbugs.ba.type.TopType;import edu.umd.cs.findbugs.ba.type.TypeDataflow;import edu.umd.cs.findbugs.ba.type.TypeFrame;/** * @author Nat Ayewah */public class FindUnrelatedTypesInGenericContainer implements Detector {	private BugReporter bugReporter;	private static final boolean DEBUG = SystemProperties.getBoolean("gc.debug");	/** 	 * Map classname, methodname and signature to an int []. 	 * Each position in the int [] corresponds to an argument in the methodSignature.	 * For each argument i, the value at position i corresponds to the index of the 	 * corresponding type in the class type parameters. If the argument	 * has no correspondence, then the value is -1. <p>	 * 	 * Get the String key by calling getCollectionsMapKey()	 */	private Map<String, int []> collectionsMap = new HashMap<String, int[]>();	/**	 * @param triplet[0] = className. 	 * 			The name of the collection e.g. <code>java.util.List</code>	 * @param triplet[1] = methodName.	 * 			The method's name e.g. <code>contains</code>	 * @param triplet[2] = methodSignature.	 * 			The method's signature e.g. <code>(Ljava/lang/Object;)Z</code>	 * @return	 */	public static String getCollectionsMapKey(String...triplet) {		return triplet[0] + "??" + triplet[1] + "???" + triplet[2];	}	private void addToCollectionsMap(String className, String methodName, 			String methodSignature, int... argumentParameterIndex) {		collectionsMap.put(				getCollectionsMapKey(className, methodName, methodSignature), 				argumentParameterIndex);	}	private void addToCollectionsMap(String [] classNames, String methodName, 			String methodSignature, int... argumentParameterIndex) {		for (String className : classNames)			addToCollectionsMap(					className, methodName, methodSignature, 					argumentParameterIndex);	}	String [] collectionMembers = new String [] {			"java.util.Collection",			"java.util.AbstractCollection",			"java.util.List",			"java.util.AbstractList",			"java.util.ArrayList",			"java.util.LinkedList",			"java.util.Set",			"java.util.SortedSet",			"java.util.LinkedHashSet",			"java.util.HashSet",			"java.util.TreeSet"		};	String [] mapMembers = new String [] {			"java.util.Map",			"java.util.AbstractMap",			"java.util.SortedMap",			"java.util.TreeMap",			"java.util.HashMap",			"java.util.LinkedHashMap",			"java.util.concurrent.ConcurrentHashMap",			"java.util.EnumMap",			"java.util.Hashtable",			"java.util.IdentityHashMap",			"java.util.WeakHashMap"	};	String [] listMembers = new String [] {			"java.util.List",			"java.util.AbstractList",			"java.util.ArrayList",			"java.util.LinkedList"	};	public FindUnrelatedTypesInGenericContainer(BugReporter bugReporter) {		this.bugReporter = bugReporter;		String basicSignature = "(Ljava/lang/Object;)Z";		String collectionSignature = "(Ljava/util/Collection<*>;)Z";		String indexSignature = "(Ljava/lang/Object;)I";		// Collection<E>		addToCollectionsMap(collectionMembers, "contains", basicSignature, 0);		//addToCollectionsMap(collectionMembers, "equals",   basicSignature, 0);		addToCollectionsMap(collectionMembers, "remove",   basicSignature, 0);		//addToCollectionsMap(collectionMembers, "containsAll", collectionSignature, 0);		//addToCollectionsMap(collectionMembers, "removeAll",   collectionSignature, 0);		//addToCollectionsMap(collectionMembers, "retainAll",   collectionSignature, 0);		// List<E>		addToCollectionsMap(listMembers, "indexOf", indexSignature, 0);		addToCollectionsMap(listMembers, "lastIndexOf", indexSignature, 0);		// Map<K,V>		addToCollectionsMap(mapMembers, "containsKey", basicSignature, 0);		addToCollectionsMap(mapMembers, "containsValue", basicSignature, 1);		// XXX these do not work, to support these need changeable return types		addToCollectionsMap(mapMembers, "get", basicSignature, 0);		addToCollectionsMap(mapMembers, "remove", basicSignature, 0);	}	/**	 * Visit the class context	 * @see edu.umd.cs.findbugs.Detector#visitClassContext(edu.umd.cs.findbugs.ba.ClassContext)	 */	public void visitClassContext(ClassContext classContext) {		JavaClass javaClass = classContext.getJavaClass();		Method[] methodList = javaClass.getMethods();		for (Method method : methodList) {			if (method.getCode() == null)				continue;			try {				analyzeMethod(classContext, method);			} catch (MethodUnprofitableException e) {				assert true; // move along; nothing to see			} catch (CFGBuilderException e) {				String msg = "Detector " + this.getClass().getName()										+ " caught exception while analyzing " + javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature();				bugReporter.logError(msg , e);			} catch (DataflowAnalysisException e) {				String msg = "Detector " + this.getClass().getName()										+ " caught exception while analyzing " + javaClass.getClassName() + "." + method.getName() + " : " + method.getSignature();				bugReporter.logError(msg, e);			}		}	}	/**	 * Use this to screen out methods that do not contain invocations.	 */	public boolean prescreen(ClassContext classContext, Method method) {		BitSet bytecodeSet = classContext.getBytecodeSet(method);		return bytecodeSet != null && (				  bytecodeSet.get(Constants.INVOKEINTERFACE) || 				  bytecodeSet.get(Constants.INVOKEVIRTUAL)   ||				  bytecodeSet.get(Constants.INVOKESPECIAL) 	 ||				  bytecodeSet.get(Constants.INVOKESTATIC)    ||				  bytecodeSet.get(Constants.INVOKENONVIRTUAL)				  				);	}	/**	 * Methods marked with the "Synthetic" attribute do not appear 	 * in the source code	 */	private boolean isSynthetic(Method m) {		Attribute[] attrs = m.getAttributes();		for (Attribute attr : attrs) {			if (attr instanceof Synthetic)				return true;		}		return false;	}	private void analyzeMethod(ClassContext classContext, Method method)	throws CFGBuilderException, DataflowAnalysisException {		if (isSynthetic(method) || !prescreen(classContext, method))			return;		BugAccumulator accumulator = new BugAccumulator(bugReporter);		CFG cfg = classContext.getCFG(method);		TypeDataflow typeDataflow = classContext.getTypeDataflow(method);		ConstantPoolGen cpg = classContext.getConstantPoolGen();		MethodGen methodGen = classContext.getMethodGen(method);		if (methodGen == null) return;		String fullMethodName = 			methodGen.getClassName() + "." + methodGen.getName(); 		String sourceFile = classContext.getJavaClass().getSourceFileName();		if (DEBUG) {			System.out.println("Checking " + fullMethodName);		}		// Process each instruction		for (Iterator<Location> iter = cfg.locationIterator(); iter.hasNext();) {			Location location = iter.next();			InstructionHandle handle = location.getHandle();			Instruction ins = handle.getInstruction();			// Only consider invoke instructions			if (!(ins instanceof InvokeInstruction))				continue;			InvokeInstruction inv = (InvokeInstruction)ins;			// check the relevance of this instruction			String [] itriplet = getInstructionTriplet(inv, cpg);			String [] triplet = getRelevantTriplet(itriplet);			if (triplet == null)				continue;			// get the list of parameter indexes for each argument position			int [] argumentParameterIndex = 				collectionsMap.get( getCollectionsMapKey(triplet) );			TypeFrame frame = typeDataflow.getFactAtLocation(location);			if (!frame.isValid()) {

⌨️ 快捷键说明

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