forked from antlr/grammars-v4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAllInOne7.java
683 lines (572 loc) · 17.1 KB
/
AllInOne7.java
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Source: https://en.wikipedia.org/wiki/Java_syntax
// Source: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/index.html
/* This is a multi-line comment.
It may occupy more than one line. */
// This is an end-of-line comment
/**
* This is a documentation comment.
*
* @author John Doe
*/
package myapplication.mylibrary;
import java.util.Random; // Single type declaration
import java.util.*;
import java.*;
import static java.lang.System.out; //'out' is a static field in java.lang.System
import static screen.ColorName.*;
public enum ColorName {
RED, BLUE, GREEN
};
// See http://docs.oracle.com/javase/7/docs/technotes/guides/language/underscores-literals.html
public class LexerTest {
static void main(String[] args) {
long creditCardNumber = 1234_5678_9012_3456L;
long socialSecurityNumber = 999_99_9999L;
float pi = 3.14_15F;
long hexBytes = 0xFF_EC_DE_5E;
long hexWords = 0xCAFE_BABE;
long maxLong = 0x7fff_ffff_ffff_ffffL;
byte nybbles = 0b0010_0101;
long bytes = 0b11010010_01101001_10010100_10010010;
long lastReceivedMessageId = 0L;
double hexDouble1 = 0x1.0p0;
double hexDouble2 = 0x1.956ad0aae33a4p117;
int octal = 01234567;
long hexUpper = 0x1234567890ABCDEFL;
long hexLower = 0x1234567890abcedfl;
int x1 = _52; // This is an identifier, not a numeric literal
int x2 = 5_2; // OK (decimal literal)
int x4 = 5_______2; // OK (decimal literal)
int x7 = 0x5_2; // OK (hexadecimal literal)
int x9 = 0_52; // OK (octal literal)
int x10 = 05_2; // OK (octal literal)
int x, y, result;
// Arithmetic operators
result = x + y;
result = x - y;
result = x * y;
result = y / x;
result = x % 3;
// Unary operators
result = +x;
result = -y;
result = ++x;
result = --y;
boolean ok = false;
boolean not_ok = !ok;
// assignments yield a value
(result = System.class).getName();
// Prefix & postfix
++x;
x++;
--y;
y--;
LexerTest.prePost++;
LexerTest.prePost--;
myapplication.mylibrary.LexerTest.prePost++;
myapplication.mylibrary.LexerTest.prePost--;
this.prePost++;
this.prePost--;
super.prePost++;
super.prePost--;
someMethod()[0]++;
someMethod()[0]--;
++LexerTest.prePost;
--LexerTest.prePost;
++myapplication.mylibrary.LexerTest.prePost;
--myapplication.mylibrary.LexerTest.prePost;
++this.prePost;
--this.prePost;
++super.prePost;
--super.prePost;
++someMethod()[0];
--someMethod()[0];
// Relational operators
result = x == y;
result = x != y;
result = x > y;
result = x >= y;
result = x < y;
result = x <= y;
// Conditional operators
if ((x > 8) && (y > 8)) {
}
if ((x > 10) || (y > 10)) {
}
result = (x > 10) ? x : y;
// Bitwise and Bit shift operators
result = ~x;
result = x << 1;
result = x >> 2;
result = x >>> 3;
result = x & 4;
result = x ^ 5;
result = x | 6;
// Assignment operators
result = x;
result += x;
result -= x;
result *= x;
result /= x;
result %= x;
result &= x;
result ^= x;
result |= x;
result <<= x;
result >>= x;
result >>>= x;
}
public static void methodCalls() {
new Object().getClass().hashCode();
new String[] { "test" }[0].getLength();
String[] strings;
(strings = new String[] {"test"})[0].charAt(0);
strings[0].length();
Foo foo = new Foo().new Bar();
foo.hashCode();
Foo.class.hashCode();
new HashMap<Object, String>(5).get(null);
}
}
public class ImportsTest {
public static void main(String[] args) {
/* The following line is equivalent to
* java.util.Random random = new java.util.Random();
* It would've been incorrect without the import declaration */
Random random = new Random();
}
}
public class HelloWorld {
public static void main(String[] args) {
/* The following line is equivalent to:
System.out.println("Hello World!");
and would have been incorrect without the import declaration. */
out.println("Hello World!");
// Conditional statements -------------------
if (i == 3) doSomething();
if (i == 2) {
doSomething();
} else {
doSomethingElse();
}
if (i == 3) {
doSomething();
} else if (i == 2) {
doSomethingElse();
} else {
doSomethingDifferent();
}
int a = 1;
int b = 2;
int minVal = (a < b) ? a : b;
// switch
switch (ch) {
case 'A':
doSomething(); // Triggered if ch == 'A'
break;
case 'B':
case 'C':
doSomethingElse(); // Triggered if ch == 'B' or ch == 'C'
break;
default:
doSomethingDifferent(); // Triggered in any other case
break;
}
// Iteration statements -------------------
while (i < 10) {
doSomething();
}
do {
doSomething();
} while (i < 10);
for (int i = 0; i < 10; i++) {
doSomething();
}
// A more complex loop using two variables
for (int i = 0, j = 9; i < 10; i++, j -= 3) {
doSomething();
}
for (;;) {
doSomething();
}
for (int i : intArray) {
doSomething(i);
}
// Jump statements -------------------
// Label
start:
someMethod();
// break
for (int i = 0; i < 10; i++) {
while (true) {
break;
}
// Will break to this point
}
outer:
for (int i = 0; i < 10; i++) {
while (true) {
break outer;
}
}
// Will break to this point
// continue
int ch;
while (ch = getChar()) {
if (ch == ' ') {
continue; // Skips the rest of the while-loop
}
// Rest of the while-loop, will not be reached if ch == ' '
doSomething();
}
outer:
for (String str : stringsArr) {
char[] strChars = str.toCharArray();
for (char ch : strChars) {
if (ch == ' ') {
/* Continues the outer cycle and the next
string is retrieved from stringsArr */
continue outer;
}
doSomething(ch);
}
}
// return
// If streamClosed is true, execution is stopped
if (streamClosed) {
return;
}
readFromStream();
int result = a + b;
return result;
// Exception handling statements -------------------
// try-catch-finally
try {
// Statements that may throw exceptions
methodThrowingExceptions();
} catch (Exception ex) {
// Exception caught and handled here
reportException(ex);
} finally {
// Statements always executed after the try/catch blocks
freeResources();
}
try {
methodThrowingExceptions();
} catch (IOException | IllegalArgumentException ex) {
//Both IOException and IllegalArgumentException will be caught and handled here
reportException(ex);
}
// try-with-resources statement
try (FileOutputStream fos = new FileOutputStream("filename");
XMLEncoder xEnc = new XMLEncoder(fos))
{
xEnc.writeObject(object);
} catch (IOException ex) {
Logger.getLogger(Serializer.class.getName()).log(Level.SEVERE, null, ex);
}
// throw
if (obj == null) {
// Throws exception of NullPointerException type
throw new NullPointerException();
}
// Will not be called, if object is null
doSomethingWithObject(obj);
// Thread concurrency control -------------------
/* Acquires lock on someObject. It must be of a reference type and must be non-null */
synchronized (someObject) {
// Synchronized statements
}
// assert statement
// If n equals 0, AssertionError is thrown
assert n != 0;
/* If n equals 0, AssertionError will be thrown
with the message after the colon */
assert n != 0 : "n was equal to zero";
// Reference types -------------------
// Arrays
int[] numbers = new int[5];
numbers[0] = 2;
int x = numbers[0];
// Initializers -------------------
// Long syntax
int[] numbers = new int[] {20, 1, 42, 15, 34};
// Short syntax
int[] numbers2 = {20, 1, 42, 15, 34};
// Multi-dimensional arrays
int[][] numbers = new int[3][3];
numbers[1][2] = 2;
int[][] numbers2 = {{2, 3, 2}, {1, 2, 6}, {2, 4, 5}};
int[][] numbers = new int[2][]; //Initialization of the first dimension only
numbers[0] = new int[3];
numbers[1] = new int[2];
// Prefix & postfix
numbers[0][0]++;
numbers[0][0]--;
++numbers[0][0];
--numbers[0][0];
foo()[0]++;
foo()[0]--;
++foo()[0];
--foo()[0];
}
}
// Classes -------------------
// Top-level class
class Foo {
// Class members
}
// Inner class
class Foo { // Top-level class
class Bar { // Inner class
}
static void inner_class_constructor() {
// https://docs.oracle.com/javase/specs/jls/se9/html/jls-15.html#jls-15.9
Foo foo = new Foo();
Foo.Bar fooBar1 = foo.new Bar();
Foo.Bar fooBar2 = new Foo().new Bar();
}
}
// Nested class
class Foo { // Top-level class
static class Bar { // Nested class
}
}
// Local class
class Foo {
void bar() {
@WeakOuter
class Foobar {// Local class within a method
}
}
}
// Anonymous class
class Foo {
void bar() {
new Object() {// Creation of a new anonymous class extending Object
};
}
}
// Access modifiers
public class Foo {
int go() {
return 0;
}
private class Bar {
}
}
// Constructors and initializers
class Foo {
String str;
Foo() { // Constructor with no arguments
// Initialization
}
Foo(String str) { // Constructor with one argument
this.str = str;
}
}
class Foo {
static {
// Initialization
}
}
class Foo {
{
// Initialization
}
}
// Methods -------------------
class Foo {
public Foo() {
System.out.println(Foo.class.getName() + ": constructor runtime");
}
public Foo(int a, int b) {
System.out.println(Foo.class.getName() + ": overloaded constructor " + this());
}
int bar(int a, int b) {
return (a*2) + b;
}
/* Overloaded method with the same name but different set of arguments */
int bar(int a) {
return a*2;
}
void openStream() throws IOException, myException { // Indicates that IOException may be thrown
}
// Varargs
void printReport(String header, int... numbers) { //numbers represents varargs
System.out.println(header);
for (int num : numbers) {
System.out.println(num);
}
}
}
// Overriding methods
class Operation {
public int doSomething() {
return 0;
}
}
class NewOperation extends Operation {
@Override
public int doSomething() {
return 1;
}
}
// Abstract classes
public class AbstractClass {
private static final String hello;
static {
System.out.println(AbstractClass.class.getName() + ": static block runtime");
hello = "hello from " + AbstractClass.class.getName();
}
{
System.out.println(AbstractClass.class.getName() + ": instance block runtime");
}
public AbstractClass() {
System.out.println(AbstractClass.class.getName() + ": constructor runtime");
}
public static void hello() {
System.out.println(hello);
}
}
public class CustomClass extends AbstractClass {
static {
System.out.println(CustomClass.class.getName() + ": static block runtime");
}
{
System.out.println(CustomClass.class.getName() + ": instance block runtime");
}
public CustomClass() {
super();
System.out.println(CustomClass.class.getName() + ": constructor runtime");
}
public static void main(String[] args) {
CustomClass nc = new CustomClass();
hello();
AbstractClass.hello();//also valid
}
}
// Enumerations -------------------
enum Season {
WINTER, SPRING, SUMMER, AUTUMN
}
public enum Season {
WINTER("Cold"), SPRING("Warmer"), SUMMER("Hot"), AUTUMN("Cooler");
Season(String description) {
this.description = description;
}
private final String description;
public String getDescription() {
return description;
}
}
public enum Season {
WINTER {
String getDescription() {return "cold";}
},
SPRING {
String getDescription() {return "warmer";}
},
SUMMER {
String getDescription() {return "hot";}
},
FALL {
String getDescription() {return "cooler";}
};
}
// Interfaces -------------------
interface ActionListener {
int ACTION_ADD = 0;
int ACTION_REMOVE = 1;
void actionSelected(int action);
}
interface RequestListener {
int requestReceived();
}
class ActionHandler implements ActionListener, RequestListener {
public void actionSelected(int action) {
}
public int requestReceived() {
}
}
class Dummy {
public void dummy() {
//Calling method defined by interface
RequestListener listener = new ActionHandler(); /*ActionHandler can be
represented as RequestListener...*/
listener.requestReceived(); /*...and thus is known to implement
requestReceived() method*/
}
}
class Dummy {
public void dummy() {
interface AnotherInterface extends Runnable { // local interface
void work();
}
}
}
// Annotations -------------------
@interface BlockingOperations {
}
@interface BlockingOperations {
boolean fileSystemOperations();
boolean networkOperations() default false;
}
class Dummy {
@BlockingOperations(/*mandatory*/ fileSystemOperations = true,
/*optional*/ networkOperations = true)
void openOutputStream() { //Annotated method
}
@Unused // Shorthand for @Unused()
void travelToJupiter() {
}
}
// Generics -------------------
// Generic classes
/* This class has two type variables, T and V. T must be
a subtype of ArrayList and implement Formattable interface */
public class Mapper<T extends ArrayList & Formattable, V> {
public void add(T array, V item) {
// array has add method because it is an ArrayList subclass
array.add(item);
/* Mapper is created with CustomList as T and Integer as V.
CustomList must be a subclass of ArrayList and implement Formattable */
Mapper<CustomList, Integer> mapper = new Mapper<CustomList, Integer>();
Mapper<CustomList, ?> mapper;
mapper = new Mapper<CustomList, Boolean>();
mapper = new Mapper<CustomList, Integer>();
}
}
// Generic methods and constructors -------------------
class Mapper {
// The class itself is not generic, the constructor is
<T, V> Mapper(T array, V item) {
}
/* This method will accept only arrays of the same type as
the searched item type or its subtype*/
static <T, V extends T> boolean contains(T item, V[] arr) {
for (T currentItem : arr) {
if (item.equals(currentItem)) {
return true;
}
}
return false;
}
}
interface Expandable<T extends Number> {
void addItem(T item);
}
// This class is parameterized
class Array<T extends Number> implements Expandable<T> {
void addItem(T item) {
}
}
// And this is not and uses an explicit type instead
class IntegerArray implements Expandable<Integer> {
void addItem(Integer item) {
}
}
// Annotation type definition
public @interface Bean {
public static final String ASDF = "ASDF";
}