[Fb-contrib-commit] SF.net SVN: fb-contrib: [511] trunk/fb-contrib/samples
Brought to you by:
dbrosius
From: <dbr...@us...> - 2006-05-01 01:17:38
|
Revision: 511 Author: dbrosius Date: 2006-04-30 18:17:28 -0700 (Sun, 30 Apr 2006) ViewCVS: http://svn.sourceforge.net/fb-contrib/?rev=511&view=rev Log Message: ----------- reimplement using OpcodeStack Modified Paths: -------------- trunk/fb-contrib/samples/UEC_Sample.java trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java Modified: trunk/fb-contrib/samples/UEC_Sample.java =================================================================== --- trunk/fb-contrib/samples/UEC_Sample.java 2006-04-29 23:38:06 UTC (rev 510) +++ trunk/fb-contrib/samples/UEC_Sample.java 2006-05-01 01:17:28 UTC (rev 511) @@ -8,12 +8,23 @@ public class UEC_Sample { - enum Suite { Spades, Hearts, Clubs, Diamonds }; + public enum Suite { Spades, Hearts, Clubs, Diamonds }; private Set<Suite> wildSuites = new HashSet<Suite>(); private EnumSet<Suite> eWildSuites = EnumSet.noneOf(Suite.class); - public Map<Suite, Integer> deal() { + public UEC_Sample() + { + wildSuites.add(Suite.Spades); + } + + public UEC_Sample(Suite s) + { + wildSuites.add(s); + } + + public Map<Suite, Integer> deal() + { Map<Suite, Integer> hand = new HashMap<Suite, Integer>(); hand.put(Suite.Spades, new Integer(10)); hand.put(Suite.Hearts, new Integer(9)); @@ -21,7 +32,8 @@ return hand; } - public EnumMap<Suite, Integer> eDeal() { + public EnumMap<Suite, Integer> eDeal() + { EnumMap<Suite, Integer> hand = new EnumMap(Suite.class); hand.put(Suite.Spades, new Integer(10)); hand.put(Suite.Hearts, new Integer(9)); Modified: trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java =================================================================== --- trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-04-29 23:38:06 UTC (rev 510) +++ trunk/fb-contrib/src/com/mebigfatguy/fbcontrib/detect/UseEnumCollections.java 2006-05-01 01:17:28 UTC (rev 511) @@ -7,6 +7,7 @@ import edu.umd.cs.findbugs.BugInstance; 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; /** @@ -14,13 +15,9 @@ * more efficient to use EnumSet or EnumMap. It is a jdk1.5 only detector. */ public class UseEnumCollections extends BytecodeScanningDetector -{ - private static final int SEEN_NOTHING = 0; - private static final int SEEN_BIPUSH = 1; - private static final int SEEN_NEWINT = 2; - +{ private BugReporter bugReporter; - private int state; + private OpcodeStack stack; private boolean methodReported; /** * constructs a UEC detector given the reporter to report bugs on @@ -31,7 +28,7 @@ } /** - * implements the visitor to check that the class is greater or equal than 1.5 + * implements the visitor to check that the class is greater or equal than 1.5, and set and clear the stack * * @param classContext the context object for the currently parsed class */ @@ -39,10 +36,11 @@ try { JavaClass cls = classContext.getJavaClass(); if (cls.getMajor() >= Constants.MAJOR_1_5) { + stack = new OpcodeStack(); super.visitClassContext(classContext); } } finally { - + stack = null; } } @@ -52,57 +50,54 @@ * @param obj the context object for the currently parsed method */ public void visitMethod(Method obj) { - state = SEEN_NOTHING; + stack.resetForMethodEntry(this); methodReported = false; super.visitMethod(obj); } public void sawOpcode(int seen) { - if (methodReported) - return; - - switch (state) { - case SEEN_NOTHING: - if (seen == BIPUSH) - state = SEEN_BIPUSH; - break; + try { - case SEEN_BIPUSH: - if ((seen == INVOKESPECIAL) - && ("java/lang/Integer".equals(getClassConstantOperand()) - && ("<init>".equals(getNameConstantOperand()) - && ("(I)V".equals(getSigConstantOperand()))))) - state = SEEN_NEWINT; - else - state = SEEN_NOTHING; - break; - - case SEEN_NEWINT: + stack.mergeJumps(this); + if (methodReported) + return; + + if (seen == INVOKEINTERFACE) { boolean bug = false; - if (seen == INVOKEINTERFACE) { - String clsName = getClassConstantOperand(); - String methodName = getNameConstantOperand(); - String signature = getSigConstantOperand(); - if (("java/util/Map".equals(clsName)) - && ("put".equals(methodName)) - && ("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(signature))) { - bug = true; - } else if (("java/util/Set".equals(clsName)) - && ("add".equals(methodName)) - && ("(Ljava/lang/Object;)Ljava/lang/Object;".equals(signature))) { - bug = true; - } - - if (bug) { - bugReporter.reportBug(new BugInstance(this, "UEC_USE_ENUM_COLLECTIONS", NORMAL_PRIORITY) - .addClass(this) - .addMethod(this) - .addSourceLine(this)); - methodReported = true; - } + String clsName = getClassConstantOperand(); + String methodName = getNameConstantOperand(); + String signature = getSigConstantOperand(); + if (("java/util/Map".equals(clsName)) + && ("put".equals(methodName)) + && ("(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lang/Object;".equals(signature))) { + bug = isEnum(1); + } else if (("java/util/Set".equals(clsName)) + && ("add".equals(methodName)) + && ("(Ljava/lang/Object;)Z".equals(signature))) { + bug = isEnum(0); } - state = SEEN_NOTHING; - break; + + if (bug) { + bugReporter.reportBug(new BugInstance(this, "UEC_USE_ENUM_COLLECTIONS", NORMAL_PRIORITY) + .addClass(this) + .addMethod(this) + .addSourceLine(this)); + methodReported = true; + } + } + } catch (ClassNotFoundException cnfe) { + bugReporter.reportMissingClass(cnfe); + } finally { + stack.sawOpcode(this, seen); } } + + private boolean isEnum(int stackPos) throws ClassNotFoundException { + if (stack.getStackDepth() > stackPos) { + OpcodeStack.Item item = stack.getStackItem(stackPos); + if (item.getSignature().charAt(0) == 'L') + return (item.getJavaClass().isEnum()); + } + return false; + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |