summaryrefslogtreecommitdiff
path: root/java/java-tests/testData/psi/parser-full/annotationParsing/annotation/TypeAnnotations.java
blob: a74d4d6bbfad5aa8555d3e97dd715cf25e23a04a (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
import java.util.@NotAllowed Date;      // illegal
import @IllegalSyntax java.util.Date;   // illegal

class SpecSamples {

  //
  // 1. A type annotation appears before the type’s simple name ...
  //

  Map<@NonNull String, @NonEmpty List<@Readonly Document>> files;

  { o.<@NonNull String>m("..."); }

  class Folder<F extends @Existing File> { }

  Collection<? super @Existing File> files;

  class UnmodifiableList<T> implements @Readonly List<@Readonly T> { }

  void monitorTemperature() throws @Critical TemperatureException { }

  {
    new @Interned MyObject();
    new @NonEmpty @Readonly List<String>(myNonEmptyStringSet);
    myVar.new @Tainted NestedClass();

    new <String> @Interned MyObject();

    Map.@NonNull Entry ee;

    myString = (@NonNull String) myObject;
    myString = (@NonNull) myObject; // error: type omitted

    isNonNull = myString instanceof @NonNull String;
    isNonNull = myString instanceof @NonNull; // error: type omitted

    m(@Vernal Date::getDay);
    m(@IllegalAnnotation java.util.@Vernal Date::getDay);
    m(List<@English String>::size);
    m(Arrays::<@NonNegative Integer>sort);

    m((@TA Object x, @TB Object y) -> { System.out.println("x=" + x + " y=" + y); });

    try { m(); }
    catch (@A1 NullPointerException | @A2 IllegalArgumentException e) { }

    try (@A Reader r = new @B FileReader("/dev/zero"); @A Writer w = new @B FileWriter("/dev/null")) { }
  }

  //
  // 2. An annotation on a wildcard type argument appears before the wildcard ...
  //

  void m(MyClass<@Immutable ? extends Comparable<MyClass>> p) { }

  //
  // 3. The annotation on a given array level prefixes the brackets that introduce that level ...
  //

  void m(Document @Readonly ... docs) {
    @Readonly Document [][] docs1 = new @Readonly Document [2][12]; // array of arrays of read-only documents
    Document @Readonly [][] docs2 = new Document @Readonly [2][12]; // read-only array of arrays of documents
    Document[] @Readonly [] docs3 = new Document[2] @Readonly [12]; // array of read-only arrays of documents

    @NonNegative int @NonEmpty [] ints = new @NonNegative int @MinSize(2) [2];
  }

  int m() @Slowpoke [] @Slowbro [] { return null; }

  //
  // 4. A type annotation is permitted in front of a constructor declaration ...
  //

  @Immutable SpecSamples() { }

  //
  // todo [r.sh] 5. It is permitted to explicitly declare the method receiver as the first formal parameter ...
  //

  /*public String toString(@Readonly MyClass this) {  }

  public boolean equals(@Readonly MyClass this, @Readonly Object other) {  }

  SpecSamples(@Receiver SpecSamples this, boolean b) { }

  class Outer {
    class Middle {
      class Inner {
        void innerMethod(@A Outer. @B Middle. @C Inner this) { }
      }
    }
  }

  void replace(@Readonly Object other, @Mutable MyClass this) {  } // illegal */

  //
  // 6. It is permitted to write an annotation on a type parameter declaration ...
  //

  class MyClass<@Immutable T> { }

  interface WonderfulList<@Reified E> { }

}