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

📄 saxbugcollectionhandler.java

📁 A static analysis tool to find bugs in Java programs
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
				CompoundMatcher peek = matcherStack.peek();		if (peek == null)			throw new NullPointerException("Top of stack is null");		peek.addChild(m);		if (nextMatchedIsDisabled) {			if (peek instanceof Filter) 				((Filter)peek).disable(m);			else assert false;			nextMatchedIsDisabled = false;		}	}	private void pushCompoundMatcherAsChild(CompoundMatcher m) {		addMatcher(m);		pushCompoundMatcher(m);	}	private void pushCompoundMatcher(CompoundMatcher m) {		if (m == null) 			throw new IllegalArgumentException("matcher must not be null");		matcherStack.push(m);	}	boolean nextMatchedIsDisabled;	private void parseMatcher(String qName, Attributes attributes) throws SAXException {		if (DEBUG) System.out.println(elementStack + " " + qName + " " + matcherStack);		String disabled = attributes.getValue("disabled");		nextMatchedIsDisabled = "true".equals(disabled);	    if (qName.equals("Bug")) {	    	addMatcher(new BugMatcher(attributes.getValue("code"),	    			attributes.getValue("pattern"),	    			attributes.getValue("category")));	    } else if (qName.equals("Class")) {	    	addMatcher(new ClassMatcher(attributes.getValue("name")));	    } else if (qName.equals("FirstVersion")) {	    	addMatcher(new FirstVersionMatcher(getRequiredAttribute(attributes, "value", qName), getRequiredAttribute(attributes, "relOp", qName)));	    } else if (qName.equals("LastVersion")) {	    	addMatcher(new LastVersionMatcher(getRequiredAttribute(attributes, "value", qName), getRequiredAttribute(attributes, "relOp", qName)));	    } else if (qName.equals("Designation")) {	    	addMatcher(new DesignationMatcher(getRequiredAttribute(attributes, "designation", qName)));	    } else if (qName.equals("BugCode")) {	    	addMatcher(new BugMatcher(attributes.getValue("name"),"",""));	    } else if (qName.equals("Local")) {	    	addMatcher(new LocalMatcher(attributes.getValue("name")));	    } else if (qName.equals("BugPattern")) {	    	addMatcher(new BugMatcher("",attributes.getValue("name"),""));	    } else if (qName.equals("Priority")) {	    	addMatcher(new PriorityMatcher(attributes.getValue("value")));	    } else if (qName.equals("Package")) {	    	String pName = attributes.getValue("name");	    	pName = pName.startsWith("~") ? pName : "~" + Strings.replace(pName, ".", "\\.");						addMatcher( new ClassMatcher(pName + "\\.[^.]+"));	    } else if (qName.equals("Method")) {	    	String name = attributes.getValue("name");	    	String params = attributes.getValue("params");	    	String returns = attributes.getValue("returns");	    	addMatcher(new MethodMatcher(name, params, returns));	    } else if (qName.equals("Field")) {	    	String name = attributes.getValue("name");			String type = attributes.getValue("type");			addMatcher(new FieldMatcher(name, type));	    } else if (qName.equals("Or")) {	    	CompoundMatcher matcher = new OrMatcher();	    	pushCompoundMatcherAsChild(matcher);	    } else if (qName.equals("And") || qName.equals("Match")) {	    	AndMatcher matcher = new AndMatcher();	    	pushCompoundMatcherAsChild(matcher);	    }	    nextMatchedIsDisabled = false;    }	private void parseBugInstanceContents(String qName, Attributes attributes) throws SAXException {	    // Parsing an attribute or property of a BugInstance	    BugAnnotation bugAnnotation = null;	    if (qName.equals("Class")) {	    	String className = getRequiredAttribute(attributes, "classname", qName);	    	bugAnnotation = packageMemberAnnotation = new ClassAnnotation(className);	    } else if (qName.equals("Type")) {	    	String typeDescriptor = getRequiredAttribute(attributes, "descriptor", qName);	    	bugAnnotation = new TypeAnnotation(typeDescriptor);	    } else if (qName.equals("Method") || qName.equals("Field")) {	    	String classname = getRequiredAttribute(attributes, "classname", qName);	    	String fieldOrMethodName = getRequiredAttribute(attributes, "name", qName);	    	String signature = getRequiredAttribute(attributes, "signature", qName);	    	if (qName.equals("Method")) {	    		String isStatic = attributes.getValue("isStatic");	    		if (isStatic == null) {	    			isStatic = "false"; // Hack for old data	    		}	    		bugAnnotation = packageMemberAnnotation = 	    			new MethodAnnotation(classname, fieldOrMethodName, signature, Boolean.valueOf(isStatic));	    	} else {	    		String isStatic = getRequiredAttribute(attributes, "isStatic", qName);	    		bugAnnotation = packageMemberAnnotation = 	    			new FieldAnnotation(classname, fieldOrMethodName, signature, Boolean.valueOf(isStatic));	    	}	    } else if (qName.equals("SourceLine")) {	    	SourceLineAnnotation sourceAnnotation = createSourceLineAnnotation(qName, attributes);	    	if (!sourceAnnotation.isSynthetic())	    		bugAnnotation = sourceAnnotation;	    } else if (qName.equals("Int")) {	    	try {	    		String value = getRequiredAttribute(attributes, "value", qName);	    		bugAnnotation = new IntAnnotation(Integer.parseInt(value));	    	} catch (NumberFormatException e) {	    		throw new SAXException("Bad integer value in Int");	    	}	    } else if (qName.equals("String")) {	    		String value = getRequiredAttribute(attributes, "value", qName);	    		bugAnnotation = new StringAnnotation(value);	    } else if (qName.equals("LocalVariable")) {	    	try {	    		String varName = getRequiredAttribute(attributes, "name", qName);	    		int register = Integer.parseInt(getRequiredAttribute(attributes, "register", qName));	    		int pc = Integer.parseInt(getRequiredAttribute(attributes, "pc", qName));	    		bugAnnotation = new LocalVariableAnnotation(varName, register, pc);	    	} catch (NumberFormatException e) {	    		throw new SAXException("Invalid integer value in attribute of LocalVariable element");	    	}	    } else if (qName.equals("Property")) {	    	// A BugProperty.	    	String propName = getRequiredAttribute(attributes, "name", qName);	    	String propValue = getRequiredAttribute(attributes, "value", qName);	    	bugInstance.setProperty(propName, propValue);	    } else if (qName.equals("UserAnnotation")) {	    	// ignore AnnotationText for now; will handle in endElement	    	String s = attributes.getValue("designation"); // optional	    	BugDesignation userDesignation = bugInstance.getNonnullUserDesignation();	    	if (s != null) userDesignation.setDesignationKey(s);	    	s = attributes.getValue("user"); // optional	    	if (s != null) userDesignation.setUser(s);	    	s = attributes.getValue("timestamp"); // optional	    	if (s != null) try {	    		long timestamp = Long.valueOf(s);	    		userDesignation.setTimestamp(timestamp);	    	}	    	catch (NumberFormatException nfe) {	    		// ok to contine -- just won't set a timestamp for the user designation.	    		// but is there anyplace to report this?	    	}	    } else throw new SAXException("Unknown bug annotation named " + qName);	    if (bugAnnotation != null) {	    	String role = attributes.getValue("role");	    	if (role != null)	    		bugAnnotation.setDescription(role);	    	setAnnotationRole(attributes, bugAnnotation);	    	bugInstance.add(bugAnnotation);	    }    }	private long parseLong(String s, long defaultValue) {		long value;		try {			value = (s != null) ? Long.parseLong(s) : defaultValue;		} catch (NumberFormatException e) {			value = defaultValue;		}		return value;	}	/**	 * Extract a hash value from an element.	 * 	 * @param qName      name of element containing hash value	 * @param attributes element attributes	 * @return the decoded hash value	 * @throws SAXException	 */	private byte[] extractHash(String qName, Attributes attributes) throws SAXException {		String encodedHash = getRequiredAttribute(attributes, "value", qName);		byte[] hash;		try {			//System.out.println("Extract hash " + encodedHash);			hash= ClassHash.stringToHash(encodedHash);		} catch (IllegalArgumentException e) {			throw new SAXException("Invalid class hash", e);		}		return hash;	}	private void setAnnotationRole(Attributes attributes, BugAnnotation bugAnnotation) {		String role = attributes.getValue("role");		if (role != null)			bugAnnotation.setDescription(role);	}	private SourceLineAnnotation createSourceLineAnnotation(String qName, Attributes attributes)			throws SAXException {		String classname = getRequiredAttribute(attributes, "classname", qName);		String sourceFile = attributes.getValue("sourcefile");		if (sourceFile == null)			sourceFile = SourceLineAnnotation.UNKNOWN_SOURCE_FILE;		String startLine = attributes.getValue("start"); // "start"/"end" are now optional		String endLine = attributes.getValue("end");     // (were too many "-1"s in the xml)		String startBytecode = attributes.getValue("startBytecode");		String endBytecode = attributes.getValue("endBytecode");		try {			int sl = startLine != null ? Integer.parseInt(startLine) : -1;			int el = endLine != null ? Integer.parseInt(endLine) : -1;			int sb = startBytecode != null ? Integer.parseInt(startBytecode) : -1;			int eb = endBytecode != null ? Integer.parseInt(endBytecode) : -1;			SourceLineAnnotation annotation =				new SourceLineAnnotation(classname, sourceFile, sl, el, sb, eb);			return annotation;		} catch (NumberFormatException e) {			throw new SAXException("Bad integer value in SourceLine element", e);		}	}	@Override	public void endElement(String uri, String name, String qName) throws SAXException {		// URI should always be empty.		// So, qName is the name of the element.				if (discardedElement(qName)) {			nestingOfIgnoredElements--;		} else if (nestingOfIgnoredElements > 0) {			// ignore it		} else if (elementStack.size() > 1) {			String outerElement = elementStack.get(elementStack.size() - 2);			if (qName.equals("Or") || qName.equals("And") || qName.equals("Match") || isTopLevelFilter(qName)) {				if (DEBUG) System.out.println("  ending " + elementStack + " " + qName + " " + matcherStack);								matcherStack.pop();			} else if (outerElement.equals(BUG_COLLECTION)) {				if (qName.equals("BugInstance")) {					bugCollection.add(bugInstance, false);				   // TODO: check this					if (bugInstance.getLastVersion() == -1)						bugCollection.getProjectStats().addBug(bugInstance);				}			} else if (outerElement.equals(PROJECT)) {				//System.out.println("Adding project element " + qName + ": " + textBuffer.toString());				if (qName.equals("Jar"))					project.addFile(textBuffer.toString());				else if (qName.equals("SrcDir"))					project.addSourceDir(textBuffer.toString());				else if (qName.equals("AuxClasspathEntry"))					project.addAuxClasspathEntry(textBuffer.toString());			} else if (outerElement.equals("BugInstance")) {				if (qName.equals("UserAnnotation")) {					bugInstance.setAnnotationText(textBuffer.toString());				}			} else if (outerElement.equals(BugCollection.ERRORS_ELEMENT_NAME)) {				if (qName.equals(BugCollection.ANALYSIS_ERROR_ELEMENT_NAME)) {					analysisError.setMessage(textBuffer.toString());					bugCollection.addError(analysisError);				} else if (qName.equals(BugCollection.ERROR_ELEMENT_NAME)) {					if (stackTrace.size() > 0) {						analysisError.setStackTrace(stackTrace.toArray(new String[stackTrace.size()]));					}					bugCollection.addError(analysisError);				} else if (qName.equals(BugCollection.MISSING_CLASS_ELEMENT_NAME)) {					bugCollection.addMissingClass(textBuffer.toString());				}			} else if (outerElement.equals(BugCollection.ERROR_ELEMENT_NAME)) {				if (qName.equals(BugCollection.ERROR_MESSAGE_ELEMENT_NAME)) {					analysisError.setMessage(textBuffer.toString());				} else if (qName.equals(BugCollection.ERROR_EXCEPTION_ELEMENT_NAME)) {					analysisError.setExceptionMessage(textBuffer.toString());				} else if (qName.equals(BugCollection.ERROR_STACK_TRACE_ELEMENT_NAME)) {					stackTrace.add(textBuffer.toString());				}			} else if (outerElement.equals("ClassFeatures")) {				if (qName.equals(ClassFeatureSet.ELEMENT_NAME)) {					bugCollection.setClassFeatureSet(classFeatureSet);					classFeatureSet = null;				}			}		}		elementStack.remove(elementStack.size() - 1);	}	@Override	public void characters(char[] ch, int start, int length) {		textBuffer.append(ch, start, length);	}	private static String getRequiredAttribute(Attributes attributes, String attrName, String elementName)		throws SAXException {		String value = attributes.getValue(attrName);		if (value == null)			throw new SAXException(elementName + " element missing " + attrName + " attribute");		return value;	}}// vim:ts=4

⌨️ 快捷键说明

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