| 1 19 20 package edu.umd.cs.findbugs.detect; 21 22 23 import edu.umd.cs.findbugs.*; 24 import org.apache.bcel.classfile.Method; 25 26 32 public class IncompatMask extends BytecodeScanningDetector implements StatelessDetector { 33 int state; 34 long arg0, arg1; 35 int bitop; 36 37 private BugReporter bugReporter; 38 39 public IncompatMask(BugReporter bugReporter) { 40 this.state = 0; 41 this.bugReporter = bugReporter; 42 } 43 44 45 46 @Override  47 public void visit(Method obj) { 48 super.visit(obj); 49 this.state = 0; 50 } 51 52 private void checkState(int expectedState) { 53 if (state == expectedState) 54 state++; 55 else 56 state = 0; 57 } 58 59 private void noteVal(long val) { 60 if (state == 0) 61 arg0 = val; 62 else if (state == 2) 63 arg1 = val; 64 else 65 state = -1; 66 state++; 67 } 68 69 @Override  70 public void sawInt(int val) { 71 noteVal(val); 72 } 73 74 @Override  75 public void sawLong(long val) { 76 noteVal(val); 77 } 78 79 @Override  80 public void sawOpcode(int seen) { 81 83 switch (seen) { 84 case ICONST_M1: 85 noteVal(-1); 86 return; 87 case ICONST_0: 88 noteVal(0); 89 return; 90 case ICONST_1: 91 noteVal(1); 92 return; 93 case ICONST_2: 94 noteVal(2); 95 return; 96 case ICONST_3: 97 noteVal(3); 98 return; 99 case ICONST_4: 100 noteVal(4); 101 return; 102 case ICONST_5: 103 noteVal(5); 104 return; 105 case LCONST_0: 106 noteVal(0); 107 return; 108 case LCONST_1: 109 noteVal(1); 110 return; 111 112 case BIPUSH: 113 return; 114 case LDC2_W: 115 return; 116 117 case SIPUSH: 118 return; 119 case LDC: 120 return; 121 122 case IAND: 123 case LAND: 124 bitop = IAND; 125 checkState(1); 126 return; 127 case IOR: 128 case LOR: 129 bitop = IOR; 130 checkState(1); 131 return; 132 133 case LCMP: 134 return; 135 136 case IFEQ: 137 case IFNE: 138 139 if (state == 2) { 140 arg1 = 0; 141 state = 3; 142 } 143 144 145 case IF_ICMPEQ: 146 case IF_ICMPNE: 147 checkState(3); 148 if (state != 4) 149 return; 150 break; 151 152 case GOTO: 153 state = -1; 154 return; 155 156 default: 157 state = 0; 158 return; 159 } 160 161 162 163 long dif; 164 String t; 165 166 if (bitop == IOR) { 167 dif = arg0 & ~arg1; 168 t = "BIT_IOR"; 169 } else if (arg0 != 0 || arg1 != 0) { 170 dif = arg1 & ~arg0; 171 t = "BIT_AND"; 172 } else { 173 dif = 1; 174 t = "BIT_AND_ZZ"; 175 } 176 177 if (dif != 0) { 178 bugReporter.reportBug(new BugInstance(this, t, NORMAL_PRIORITY) 180 .addClassAndMethod(this) 181 .addSourceLine(this)); 182 } 183 state = 0; 184 } 185 } 186 187 | Popular Tags |