summaryrefslogtreecommitdiff
path: root/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/annotations/typeAnnotations.java
blob: 01f7486ea89a1e400389433fd1a2964be4589f77 (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
import java.lang.annotation.*;
import java.io.*;
import java.util.*;
import java.io.<error descr="Annotations are not allowed here">@SuppressWarnings</error> Reader;
import <error descr="Annotations are not allowed here">@SuppressWarnings</error> java.io.Writer;
import static java.lang.annotation.ElementType.*;

/*@Target({CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE})*/ @interface A { }
@Target({TYPE_USE}) @interface TA { }
@Target({TYPE_PARAMETER}) @interface TPA { }

@A @TA <error descr="'@TPA' not applicable to type">@TPA</error>
class Outer {
  private Map<@TA String, @TA List<@TA <error descr="'@A' not applicable to type use">@A</error> String>> m;

  interface I { void m(int i); }
  private I i = (@TA <error descr="'@Override' not applicable to parameter">@Override</error> final int k) -> { };

  <error descr="'void' type may not be annotated">@TA</error> <T> void m(T t) { }
  <error descr="'void' type may not be annotated">@TA</error> void test1() {
    this.<@TA <error descr="'@TPA' not applicable to type use">@TPA</error> String>m("...");
  }

  class FF<F extends @TA String> { }

  Collection<? super @TA String> cs;

  interface BI<T> { }
  class BII<T> implements @TA BI<@TA T> { }
  class BIII extends @TA BII<Object> { }

  void tm() throws @TA RuntimeException { }

  class Middle {
    class Inner {
      void test() {
        @TA Inner v1;
        @TA Middle.@TA Inner v2;
        @TA Outer.@TA Middle.@TA Inner v3;
        @TA Outer v4;
        @TA Outer.@TA Middle v5;
        @TA Outer.@TA Middle.@TA Inner v6;
        List<@TA Outer.@TA Middle.@TA Inner> l;
      }
    }
  }

  static class StaticMiddle {
    static class StaticInner {
      void test() {
        @TA StaticInner v1;
        <error descr="Static member qualifying type may not be annotated">@TA</error> StaticMiddle.@TA StaticInner v2;
        <error descr="Static member qualifying type may not be annotated">@TA</error> Outer.<error descr="Static member qualifying type may not be annotated">@TA</error> StaticMiddle.@TA StaticInner v3;
        List<@TA Outer.<error descr="Static member qualifying type may not be annotated">@TA</error> StaticMiddle.@TA StaticInner> l;
      }
    }
  }

  {
    new @TA Object();
    new @TA ArrayList<String>();

    ArrayList<String> var = new <String> @TA ArrayList();
    new @TA Outer().new @TA Middle();

    @A Map.@TA Entry e1;
    @A <error descr="Static member qualifying type may not be annotated">@TA</error> Map.@TA Entry e2;
    @A java.<error descr="Annotation not applicable to this kind of reference">@TA</error> util.Map.@TA Entry e3;
    @A List<java.<error descr="Annotation not applicable to this kind of reference">@TA</error> lang.@TA String> l1;

    Object obj = "str";
    @TA String str = (@TA String)obj;

    boolean b = str instanceof @TA String;

    <error descr="Annotations are not allowed here">@TA</error> tm();

    try (@TA Reader r = new @TA FileReader("/dev/zero"); @TA Writer w = new @TA FileWriter("/dev/null")) { }
    catch (@TA IllegalArgumentException | @TA IOException e) { }

    @A @TA <error descr="Cannot resolve symbol 'Unknown'">Unknown</error>.@TA Unknown uu;

    Class<?> c1 = <error descr="Class literal type may not be annotated">@TA</error> String.class;
    Class<?> c2 = int <error descr="Class literal type may not be annotated">@TA</error> [].class;
  }

  interface IntFunction<T> { int apply(T t); }
  interface Sorter<T> { void sort(T[] a, Comparator<? super T> c); }
  void m1(IntFunction<Date> f) { }
  void m2(IntFunction<List<String>> f) { }
  void m3(Sorter<Integer> s) { }

  void lambdas() {
    m1(@TA Date::getDay);
    m1(<error descr="Annotations are not allowed here">@TA</error> java.util.@TA Date::getDay);
    m2(List<@TA String>::size);
    m3(Arrays::<@TA Integer>sort);

    Comparator<Object> cmp = (@TA Object x, @TA Object y) -> { System.out.println("x=" + x + " y=" + y); return 0; };
  }

  void m(List<@TA ? extends Comparable<Object>> p) { }

  void arrays(String @TA ... docs) {
    @TA String @TA [] @TA [] docs1 = new @TA String @TA [2] @TA [2];
    @TA int @TA [] ints = new @TA int @TA [2];
  }

  int @TA [] mixedArrays @TA [] <error descr="Annotations are not allowed here">@TA</error> = new int[0][0];
  int @TA [] mixedArrays(int @TA [] p @TA [] <error descr="Annotations are not allowed here">@TA</error>) @TA [] <error descr="Annotations are not allowed here">@TA</error> {
    int @TA [] a @TA [] <error descr="Annotations are not allowed here">@TA</error> = (p != null ? p : mixedArrays);
    return a;
  }
  void <error descr="Annotations are not allowed here">@TA</error> misplaced() { }

  @TA Outer() { }
  <T> <error descr="Annotations are not allowed here">@TA</error> Outer(T t) { }

  class MyClass<@TA @TPA T> { }
  interface MyInterface<@TA @TPA E> { }

  static class Super {
    protected int aField;
    int getField() { return aField; }
  }
  static class This extends Super {
    void superField() {
      Outer.<error descr="Annotations are not allowed here">@TA</error> This.super.aField = 0;
      IntFunction<Super> f = Outer.<error descr="Annotations are not allowed here">@TA</error> This.super::getField;
    }
  }

  //todo[r.sh]
  /*public String toString(@TA C this) { return ""; }

  public boolean equals(@TA C this, @TA C other) { return false; }

  C(@TA C this, boolean b) { }

  class Outer {
    class Middle {
      class Inner {
        void innerMethod(@TA Outer.@TA Middle.@TA Inner this) { }
      }
    }
  }*/
}