Calin Juravle | 77520bc | 2015-01-12 18:45:46 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | public class Main { |
| 18 | |
| 19 | private volatile Object objectField; |
| 20 | private volatile int intField; |
| 21 | private volatile float floatField; |
| 22 | private volatile long longField; |
| 23 | private volatile double doubleField; |
| 24 | private volatile byte byteField; |
| 25 | private volatile boolean booleanField; |
| 26 | private volatile char charField; |
| 27 | private volatile short shortField; |
| 28 | |
| 29 | public static void $opt$setObjectField(Main m) { |
| 30 | m.objectField = null; |
| 31 | } |
| 32 | |
| 33 | public static void $opt$setIntField(Main m) { |
| 34 | m.intField = 0; |
| 35 | } |
| 36 | |
| 37 | public static void $opt$setFloatField(Main m) { |
| 38 | m.floatField = 0; |
| 39 | } |
| 40 | |
| 41 | public static void $opt$setLongField(Main m) { |
| 42 | m.longField = 0; |
| 43 | } |
| 44 | |
| 45 | public static void $opt$setDoubleField(Main m) { |
| 46 | m.doubleField = 0; |
| 47 | } |
| 48 | |
| 49 | public static void $opt$setByteField(Main m) { |
| 50 | m.byteField = 0; |
| 51 | } |
| 52 | |
| 53 | public static void $opt$setBooleanField(Main m) { |
| 54 | m.booleanField = false; |
| 55 | } |
| 56 | |
| 57 | public static void $opt$setCharField(Main m) { |
| 58 | m.charField = 0; |
| 59 | } |
| 60 | |
| 61 | public static void $opt$setShortField(Main m) { |
| 62 | m.shortField = 0; |
| 63 | } |
| 64 | |
| 65 | public static Object $opt$getObjectField(Main m) { |
| 66 | return m.objectField; |
| 67 | } |
| 68 | |
| 69 | public static int $opt$getIntField(Main m) { |
| 70 | return m.intField; |
| 71 | } |
| 72 | |
| 73 | public static float $opt$getFloatField(Main m) { |
| 74 | return m.floatField; |
| 75 | } |
| 76 | |
| 77 | public static long $opt$getLongField(Main m) { |
| 78 | return m.longField; |
| 79 | } |
| 80 | |
| 81 | public static double $opt$getDoubleField(Main m) { |
| 82 | return m.doubleField; |
| 83 | } |
| 84 | |
| 85 | public static byte $opt$getByteField(Main m) { |
| 86 | return m.byteField; |
| 87 | } |
| 88 | |
| 89 | public static boolean $opt$getBooleanField(Main m) { |
| 90 | return m.booleanField; |
| 91 | } |
| 92 | |
| 93 | public static char $opt$getCharField(Main m) { |
| 94 | return m.charField; |
| 95 | } |
| 96 | |
| 97 | public static short $opt$getShortField(Main m) { |
| 98 | return m.shortField; |
| 99 | } |
| 100 | |
| 101 | public static void main(String[] args) { |
| 102 | int methodLine = 30; |
| 103 | int thisLine = 103; |
| 104 | try { |
| 105 | $opt$setObjectField(null); |
| 106 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 107 | } catch (NullPointerException npe) { |
| 108 | check(npe, thisLine += 2, methodLine, "$opt$setObjectField"); |
| 109 | } |
| 110 | try { |
| 111 | $opt$setIntField(null); |
| 112 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 113 | } catch (NullPointerException npe) { |
| 114 | check(npe, thisLine += 6, methodLine += 4, "$opt$setIntField"); |
| 115 | } |
| 116 | try { |
| 117 | $opt$setFloatField(null); |
| 118 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 119 | } catch (NullPointerException npe) { |
| 120 | check(npe, thisLine += 6, methodLine += 4, "$opt$setFloatField"); |
| 121 | } |
| 122 | try { |
| 123 | $opt$setLongField(null); |
| 124 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 125 | } catch (NullPointerException npe) { |
| 126 | check(npe, thisLine += 6, methodLine += 4, "$opt$setLongField"); |
| 127 | } |
| 128 | try { |
| 129 | $opt$setDoubleField(null); |
| 130 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 131 | } catch (NullPointerException npe) { |
| 132 | check(npe, thisLine += 6, methodLine += 4, "$opt$setDoubleField"); |
| 133 | } |
| 134 | try { |
| 135 | $opt$setByteField(null); |
| 136 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 137 | } catch (NullPointerException npe) { |
| 138 | check(npe, thisLine += 6, methodLine += 4, "$opt$setByteField"); |
| 139 | } |
| 140 | try { |
| 141 | $opt$setBooleanField(null); |
| 142 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 143 | } catch (NullPointerException npe) { |
| 144 | check(npe, thisLine += 6, methodLine += 4, "$opt$setBooleanField"); |
| 145 | } |
| 146 | try { |
| 147 | $opt$setCharField(null); |
| 148 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 149 | } catch (NullPointerException npe) { |
| 150 | check(npe, thisLine += 6, methodLine += 4, "$opt$setCharField"); |
| 151 | } |
| 152 | try { |
| 153 | $opt$setShortField(null); |
| 154 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 155 | } catch (NullPointerException npe) { |
| 156 | check(npe, thisLine += 6, methodLine += 4, "$opt$setShortField"); |
| 157 | } |
| 158 | try { |
| 159 | $opt$getObjectField(null); |
| 160 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 161 | } catch (NullPointerException npe) { |
| 162 | check(npe, thisLine += 6, methodLine += 4, "$opt$getObjectField"); |
| 163 | } |
| 164 | try { |
| 165 | $opt$getIntField(null); |
| 166 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 167 | } catch (NullPointerException npe) { |
| 168 | check(npe, thisLine += 6, methodLine += 4, "$opt$getIntField"); |
| 169 | } |
| 170 | try { |
| 171 | $opt$getFloatField(null); |
| 172 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 173 | } catch (NullPointerException npe) { |
| 174 | check(npe, thisLine += 6, methodLine += 4, "$opt$getFloatField"); |
| 175 | } |
| 176 | try { |
| 177 | $opt$getLongField(null); |
| 178 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 179 | } catch (NullPointerException npe) { |
| 180 | check(npe, thisLine += 6, methodLine += 4, "$opt$getLongField"); |
| 181 | } |
| 182 | try { |
| 183 | $opt$getDoubleField(null); |
| 184 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 185 | } catch (NullPointerException npe) { |
| 186 | check(npe, thisLine += 6, methodLine += 4, "$opt$getDoubleField"); |
| 187 | } |
| 188 | try { |
| 189 | $opt$getByteField(null); |
| 190 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 191 | } catch (NullPointerException npe) { |
| 192 | check(npe, thisLine += 6, methodLine += 4, "$opt$getByteField"); |
| 193 | } |
| 194 | try { |
| 195 | $opt$getBooleanField(null); |
| 196 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 197 | } catch (NullPointerException npe) { |
| 198 | check(npe, thisLine += 6, methodLine += 4, "$opt$getBooleanField"); |
| 199 | } |
| 200 | try { |
| 201 | $opt$getCharField(null); |
| 202 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 203 | } catch (NullPointerException npe) { |
| 204 | check(npe, thisLine += 6, methodLine += 4, "$opt$getCharField"); |
| 205 | } |
| 206 | try { |
| 207 | $opt$getShortField(null); |
| 208 | throw new RuntimeException("Failed to throw NullPointerException."); |
| 209 | } catch (NullPointerException npe) { |
| 210 | check(npe, thisLine += 6, methodLine += 4, "$opt$getShortField"); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | static void check(NullPointerException npe, int mainLine, int medthodLine, String methodName) { |
| 215 | System.out.println(methodName); |
| 216 | StackTraceElement[] trace = npe.getStackTrace(); |
| 217 | checkElement(trace[0], "Main", methodName, "Main.java", medthodLine); |
| 218 | checkElement(trace[1], "Main", "main", "Main.java", mainLine); |
| 219 | } |
| 220 | |
| 221 | static void checkElement(StackTraceElement element, |
| 222 | String declaringClass, String methodName, |
| 223 | String fileName, int lineNumber) { |
| 224 | assertEquals(declaringClass, element.getClassName()); |
| 225 | assertEquals(methodName, element.getMethodName()); |
| 226 | assertEquals(fileName, element.getFileName()); |
| 227 | assertEquals(lineNumber, element.getLineNumber()); |
| 228 | } |
| 229 | |
| 230 | static void assertEquals(Object expected, Object actual) { |
| 231 | if (!expected.equals(actual)) { |
| 232 | String msg = "Expected \"" + expected + "\" but got \"" + actual + "\""; |
| 233 | throw new AssertionError(msg); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | static void assertEquals(int expected, int actual) { |
| 238 | if (expected != actual) { |
| 239 | throw new AssertionError("Expected " + expected + " got " + actual); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | } |