[Fb-contrib-commit] SF.net SVN: fb-contrib: [589] trunk/fb-contrib/samples
Brought to you by:
dbrosius
|
From: <dbr...@us...> - 2006-08-08 03:19:54
|
Revision: 589 Author: dbrosius Date: 2006-08-07 20:19:37 -0700 (Mon, 07 Aug 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=589&view=rev Log Message: ----------- start working on LEST detector Added Paths: ----------- trunk/fb-contrib/samples/LEST_Sample.java trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LostExceptionStackTrace.java Added: trunk/fb-contrib/samples/LEST_Sample.java =================================================================== --- trunk/fb-contrib/samples/LEST_Sample.java (rev 0) +++ trunk/fb-contrib/samples/LEST_Sample.java 2006-08-08 03:19:37 UTC (rev 589) @@ -0,0 +1,20 @@ +import java.text.DateFormat; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class LEST_Sample +{ + public Date testLest1(String input) + { + try + { + DateFormat df = new SimpleDateFormat("YYYY"); + return df.parse(input); + } + catch (ParseException pe) + { + throw new IllegalArgumentException(pe.getMessage()); + } + } +} Added: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LostExceptionStackTrace.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LostExceptionStackTrace.java (rev 0) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/LostExceptionStackTrace.java 2006-08-08 03:19:37 UTC (rev 589) @@ -0,0 +1,114 @@ +/* + * fb-contrib - Auxilliary detectors for Java programs + * Copyright (C) 2005-2006 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.BitSet; + +import org.apache.bcel.Constants; +import org.apache.bcel.classfile.Code; +import org.apache.bcel.classfile.CodeException; +import org.apache.bcel.classfile.ExceptionTable; +import org.apache.bcel.classfile.Method; + +import edu.umd.cs.findbugs.BugReporter; +import edu.umd.cs.findbugs.BytecodeScanningDetector; +import edu.umd.cs.findbugs.OpcodeStack; +import edu.umd.cs.findbugs.ba.ClassContext; + +/** + * looks for methods that catch exceptions, and rethrow another exception without encapsulating + * the original exception within it. Doing this loses the stack history, and where the original + * problem occurred. This makes finding and fixing errors difficult. + */ +public class LostExceptionStackTrace extends BytecodeScanningDetector +{ + private BugReporter bugReporter; + private OpcodeStack stack; + + /** + * constructs a LEST detector given the reporter to report bugs on + + * @param bugReporter the sync of bug reports + */ + public LostExceptionStackTrace(BugReporter bugReporter) { + this.bugReporter = bugReporter; + } + + /** + * implements the visitor to make sure the jdk is 1.4 or better + * + * @param classContext the context object of the currently parsed class + */ + @Override + public void visitClassContext(ClassContext classContext) { + try { + int majorVersion = classContext.getJavaClass().getMajor(); + if (majorVersion >= Constants.MAJOR_1_4) { + stack = new OpcodeStack(); + super.visitClassContext(classContext); + } + } finally { + stack = null; + } + } + + /** + * looks for methods that contain a catch block and an ATHROW opcode + * + * @param code the context object of the current code block + * @param method the context object of the current method + * @return if the class throws exceptions + */ + public boolean prescreen(Code code, Method method) { + CodeException[] ce = code.getExceptionTable(); + if ((ce == null) || (ce.length == 0)) + return false; + + BitSet bytecodeSet = getClassContext().getBytecodeSet(method); + return (bytecodeSet != null) && (bytecodeSet.get(Constants.ATHROW)); + } + + /** + * implements the visitor to filter out methods that don't throw exceptions + * + * @param obj the context object of the currently parsed code block + */ + @Override + public void visitCode(Code obj) { + if (prescreen(obj, getMethod())) { + stack.resetForMethodEntry(this); + super.visitCode(obj); + } + } + + /** + * implements the visitor to find throwing alternative exceptions from a catch block, without + * forwarding along the original exception + */ + @Override + public void sawOpcode(int seen) { + try { + stack.mergeJumps(this); + + } + finally { + stack.sawOpcode(this, seen); + } + } +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |