summaryrefslogtreecommitdiffstats
path: root/test/201-built-in-exception-detail-messages/src/Main.java
blob: f8da6446cf2451c8e62038b058f10fd541b66c23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class Main {
  public static void main(String[] args) throws Exception {
    arrayAccess();
    arrayStore();
    classCast();
    classNotFound();
    negativeArraySize();
    nullPointers();
    reflection();
    stringIndex();
  }

  private static void assertEquals(String expected, String actual) {
    if (expected == null && actual == null) {
      return;
    }
    if (expected != null && expected.equals(actual)) {
      return;
    }
    throw new AssertionError("not equal\n" +
                                 "expected: " + expected + "\n" +
                                 "actual:   " + actual);
  }

  private static void fail() {
    throw new AssertionError();
  }

  private static void arrayAccess() throws Exception {
    byte[] bs = new byte[1];
    double[] ds = new double[1];
    Object[] os = new Object[1];

    // aput
    try {
      bs[2] = 0;
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }

    // aget
    try {
      byte b = bs[2];
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }

    // aput-wide
    try {
      ds[2] = 0.0;
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }

    // aget-wide
    try {
      double d = ds[2];
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }

    // aput-object
    try {
      os[2] = null;
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }

    // aget-object
    try {
      Object o = os[2];
      fail();
    } catch (ArrayIndexOutOfBoundsException ex) {
      assertEquals("length=1; index=2", ex.getMessage());
    }
  }

  private static void arrayStore() throws Exception {
    try {
      Object[] array = new String[10];
      Object o = new Exception();
      array[0] = o;
      fail();
    } catch (ArrayStoreException ex) {
      assertEquals("java.lang.Exception cannot be stored in an array of type java.lang.String[]",
                   ex.getMessage());
    }

    try {
      Object[] array = new C[10][];
      Object o = new Integer(5);
      array[0] = o;
      fail();
    } catch (ArrayStoreException ex) {
      assertEquals("java.lang.Integer cannot be stored in an array of type Main$C[][]",
                   ex.getMessage());
    }

    try {
      Object[] array = new Float[10][];
      Object o = new C[4];
      array[0] = o;
      fail();
    } catch (ArrayStoreException ex) {
      assertEquals("Main$C[] cannot be stored in an array of type java.lang.Float[][]",
                   ex.getMessage());
    }

    try {
      String[] src = new String[] { null, null, null, null, "hello", "goodbye" };
      Integer[] dst = new Integer[10];
      System.arraycopy(src, 1, dst, 0, 5);
    } catch (ArrayStoreException ex) {
      assertEquals("source[4] of type java.lang.String cannot be stored in destination array of type java.lang.Integer[]",
                   ex.getMessage());
    }

    try {
      String[] src = new String[1];
      int[] dst = new int[1];
      System.arraycopy(src, 0, dst, 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("Incompatible types: src=java.lang.String[], dst=int[]", ex.getMessage());
    }

    try {
      float[] src = new float[1];
      Runnable[] dst = new Runnable[1];
      System.arraycopy(src, 0, dst, 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("Incompatible types: src=float[], dst=java.lang.Runnable[]", ex.getMessage());
    }

    try {
      boolean[] src = new boolean[1];
      double[][] dst = new double[1][];
      System.arraycopy(src, 0, dst, 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("Incompatible types: src=boolean[], dst=double[][]", ex.getMessage());
    }

    try {
      String src = "hello";
      Object[] dst = new Object[1];
      System.arraycopy(src, 0, dst, 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("source of type java.lang.String is not an array", ex.getMessage());
    }

    try {
      Object[] src = new Object[1];
      Integer dst = new Integer(5);
      System.arraycopy(src, 0, dst, 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("destination of type java.lang.Integer is not an array", ex.getMessage());
    }

    // This test demonstrates that the exception message complains
    // about the source in cases where neither source nor
    // destination is an array.
    try {
      System.arraycopy(new C(), 0, "hello", 0, 1);
    } catch (ArrayStoreException ex) {
      assertEquals("source of type Main$C is not an array", ex.getMessage());
    }
  }

  private static void classCast() throws Exception {
    // Reference types.
    try {
      Object o = new Exception();
      String s = (String) o;
      fail();
    } catch (ClassCastException ex) {
      assertEquals("java.lang.Exception cannot be cast to java.lang.String", ex.getMessage());
    }

    // Arrays of reference types.
    try {
      Object o = (C) makeArray(String.class);
      fail();
    } catch (ClassCastException ex) {
      assertEquals("java.lang.String[] cannot be cast to Main$C", ex.getMessage());
    }

    // Arrays of primitives.
    try {
      Object o = (C) makeArray(float.class);
      fail();
    } catch (ClassCastException ex) {
      assertEquals("float[] cannot be cast to Main$C", ex.getMessage());
    }

    // Multi-dimensional arrays of primitives.
    try {
      Object o = (C) makeArray(char[].class);
      fail();
    } catch (ClassCastException ex) {
      assertEquals("char[][] cannot be cast to Main$C", ex.getMessage());
    }

    // Multi-dimensional arrays of references.
    try {
      Object o = (Object[][][]) makeInteger();
      fail();
    } catch (ClassCastException ex) {
      assertEquals("java.lang.Integer cannot be cast to java.lang.Object[][][]", ex.getMessage());
    }
  }

  static class C { }

  /**
   * Helper for testCastOperator and testCastOperatorWithArrays. It's important that the
   * return type is Object, since otherwise the compiler will just reject the code.
   */
  private static Object makeInteger() {
    return new Integer(5);
  }

  /**
   * Helper for testCastOperatorWithArrays. It's important that
   * the return type is Object.
   */
  private static Object makeArray(Class c) {
    return Array.newInstance(c, 1);
  }

  private static void classNotFound() throws Exception {
    try {
      // There is no such thing as an array of void.
      Class.forName("[V");
      fail();
    } catch (ClassNotFoundException ex) {
      assertEquals("Invalid name: [V", ex.getMessage());
    }

    try {
      // This class name is valid, but doesn't exist.
      Class.forName("package.Class");
      fail();
    } catch (ClassNotFoundException ex) {
      assertEquals("package.Class", ex.getMessage());
    }

    try {
      // This array class name is valid, but the type doesn't exist.
      Class.forName("[[Lpackage.Class;");
      fail();
    } catch (ClassNotFoundException ex) {
      assertEquals("[[Lpackage.Class;", ex.getMessage());
    }
  }

  private static void negativeArraySize() throws Exception {
    try {
      int[] is = new int[-123];
      fail();
    } catch (NegativeArraySizeException ex) {
      assertEquals("-123", ex.getMessage());
    }
  }

  private static void nullPointers() throws Exception {
    // Invoke method.
    try {
      Object o = null;
      o.hashCode();
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to invoke virtual method 'int java.lang.Object.hashCode()' on a null object reference", ex.getMessage());
    }

    // Read field.
    try {
      A a = null;
      int i = a.i;
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to read from field 'int A.i' on a null object reference", ex.getMessage());
    }

    // Write field.
    try {
      A a = null;
      a.i = 1;
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to write to field 'int A.i' on a null object reference", ex.getMessage());
    }

    // Read array.
    try {
      int[] is = null;
      int i = is[0];
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to read from null array", ex.getMessage());
    }

    // Write array.
    try {
      int[] is = null;
      is[0] = 1;
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to write to null array", ex.getMessage());
    }

    // Invoke method.
    try {
      int[] is = null;
      int i = is.length;
      fail();
    } catch (NullPointerException ex) {
      assertEquals("Attempt to get length of null array", ex.getMessage());
    }
  }

  private static void reflection() throws Exception {
    // Can't assign Integer to a String field.
    try {
      Field field = A.class.getField("b");
      field.set(new A(), 5);
      fail();
    } catch (IllegalArgumentException expected) {
      assertEquals("field A.b has type java.lang.String, got java.lang.Integer", expected.getMessage());
    }

    // Can't unbox null to a primitive.
    try {
      Field field = A.class.getField("i");
      field.set(new A(), null);
      fail();
    } catch (IllegalArgumentException expected) {
      assertEquals("field A.i has type int, got null", expected.getMessage());
    }

    // Can't unbox String to a primitive.
    try {
      Field field = A.class.getField("i");
      field.set(new A(), "hello, world!");
      fail();
    } catch (IllegalArgumentException expected) {
      assertEquals("field A.i has type int, got java.lang.String", expected.getMessage());
    }

    // Can't pass an Integer as a String.
    try {
      Method m = A.class.getMethod("m", int.class, String.class);
      m.invoke(new A(), 2, 2);
      fail();
    } catch (IllegalArgumentException expected) {
      assertEquals("method A.m argument 2 has type java.lang.String, got java.lang.Integer", expected.getMessage());
    }

    // Can't pass null as an int.
    try {
      Method m = A.class.getMethod("m", int.class, String.class);
      m.invoke(new A(), null, "");
      fail();
    } catch (IllegalArgumentException expected) {
      assertEquals("method A.m argument 1 has type int, got null", expected.getMessage());
    }

    try {
      Method m = String.class.getMethod("charAt", int.class);
      m.invoke("hello"); // Wrong number of arguments.
      fail();
    } catch (IllegalArgumentException iae) {
      assertEquals("Wrong number of arguments; expected 1, got 0", iae.getMessage());
    }
    try {
      Method m = String.class.getMethod("charAt", int.class);
      m.invoke("hello", "world"); // Wrong type.
      fail();
    } catch (IllegalArgumentException iae) {
      assertEquals("method java.lang.String.charAt argument 1 has type int, got java.lang.String", iae.getMessage());
    }
    try {
      Method m = String.class.getMethod("charAt", int.class);
      m.invoke("hello", (Object) null); // Null for a primitive argument.
      fail();
    } catch (IllegalArgumentException iae) {
      assertEquals("method java.lang.String.charAt argument 1 has type int, got null", iae.getMessage());
    }
    try {
      Method m = String.class.getMethod("charAt", int.class);
      m.invoke(new Integer(5)); // Wrong type for 'this'.
      fail();
    } catch (IllegalArgumentException iae) {
      assertEquals("Expected receiver of type java.lang.String, but got java.lang.Integer", iae.getMessage());
    }
    try {
      Method m = String.class.getMethod("charAt", int.class);
      m.invoke(null); // Null for 'this'.
      fail();
    } catch (NullPointerException npe) {
      assertEquals("null receiver", npe.getMessage());
    }
  }

  private static void stringIndex() throws Exception {
    // charAt too small.
    try {
      "hello".charAt(-1);
      fail();
    } catch (StringIndexOutOfBoundsException ex) {
      assertEquals("length=5; index=-1", ex.getMessage());
    }

    // charAt too big.
    try {
      "hello".charAt(7);
      fail();
    } catch (StringIndexOutOfBoundsException ex) {
      assertEquals("length=5; index=7", ex.getMessage());
    }

    // substring too big.
    try {
      "hello there".substring(9,14);
      fail();
    } catch (StringIndexOutOfBoundsException ex) {
      assertEquals("length=11; regionStart=9; regionLength=5", ex.getMessage());
    }
  }
}

class A {
  public String b;
  public int i;
  public void m(int i, String s) {}
}