aboutsummaryrefslogtreecommitdiff
path: root/javatests/com/google/turbine/processing/TurbineElementsHidesTest.java
blob: 69418d5112848a49c1c6eba340b45dfd1eea409f (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
/*
 * Copyright 2019 Google Inc. All Rights Reserved.
 *
 * 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.
 */

package com.google.turbine.processing;

import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static java.util.Arrays.stream;
import static org.junit.Assert.fail;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ObjectArrays;
import com.google.common.truth.Expect;
import com.google.turbine.binder.Binder;
import com.google.turbine.binder.ClassPathBinder;
import com.google.turbine.binder.bound.TypeBoundClass;
import com.google.turbine.binder.env.CompoundEnv;
import com.google.turbine.binder.env.Env;
import com.google.turbine.binder.env.SimpleEnv;
import com.google.turbine.binder.sym.ClassSymbol;
import com.google.turbine.diag.SourceFile;
import com.google.turbine.lower.IntegrationTestSupport;
import com.google.turbine.lower.IntegrationTestSupport.TestInput;
import com.google.turbine.parse.Parser;
import com.google.turbine.processing.TurbineElement.TurbineTypeElement;
import com.google.turbine.testing.TestClassPaths;
import com.google.turbine.tree.Tree.CompUnit;
import com.sun.source.util.JavacTask;
import com.sun.source.util.TaskEvent;
import com.sun.source.util.TaskListener;
import java.io.IOException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.ElementScanner8;
import javax.lang.model.util.Elements;
import javax.tools.DiagnosticCollector;
import javax.tools.JavaFileObject;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class TurbineElementsHidesTest {

  @Rule public final Expect expect = Expect.create();

  @Parameters
  public static Iterable<TestInput[]> parameters() {
    // An array of test inputs. Each element is an array of lines of sources to compile.
    String[][] inputs = {
      {
        "=== A.java ===", //
        "abstract class A {",
        "  int f;",
        "  static int f() { return 1; }",
        "  static int f(int x) { return 1; }",
        "}",
        "=== B.java ===",
        "abstract class B extends A {",
        "  int f;",
        "  int g;",
        "  static int f() { return 1; }",
        "  static int f(int x) { return 1; }",
        "  static int g() { return 1; }",
        "  static int g(int x) { return 1; }",
        "}",
        "=== C.java ===",
        "abstract class C extends B {",
        "  int f;",
        "  int g;",
        "  int h;",
        "  static int f() { return 1; }",
        "  static int g() { return 1; }",
        "  static int h() { return 1; }",
        "  static int f(int x) { return 1; }",
        "  static int g(int x) { return 1; }",
        "  static int h(int x) { return 1; }",
        "}",
      },
      {
        "=== A.java ===",
        "class A {",
        "  class I {",
        "  }",
        "}",
        "=== B.java ===",
        "class B extends A {",
        "  class I extends A.I {",
        "  }",
        "}",
        "=== C.java ===",
        "class C extends B {",
        "  class I extends B.I {",
        "  }",
        "}",
      },
      {
        "=== A.java ===",
        "class A {",
        "  class I {",
        "  }",
        "}",
        "=== B.java ===",
        "class B extends A {",
        "  interface I {}",
        "}",
        "=== C.java ===",
        "class C extends B {",
        "  @interface I {}",
        "}",
      },
      {
        // the containing class or interface of Intf.foo is an interface
        "=== Outer.java ===",
        "class Outer {",
        "  static class Inner {",
        "    static void foo() {}",
        "    static class Innerer extends Inner {",
        "      interface Intf {",
        "        static void foo() {}",
        "      }",
        "    }",
        "  }",
        "}",
      },
      {
        // test two top-level classes with the same name
        "=== one/A.java ===",
        "package one;",
        "public class A {",
        "}",
        "=== two/A.java ===",
        "package two;",
        "public class A {",
        "}",
      },
    };
    // https://bugs.openjdk.java.net/browse/JDK-8275746
    if (IntegrationTestSupport.getMajor() >= 11) {
      inputs =
          ObjectArrays.concat(
              inputs,
              new String[][] {
                {
                  // interfaces
                  "=== A.java ===",
                  "interface A {",
                  "  static void f() {}",
                  "  int x = 42;",
                  "}",
                  "=== B.java ===",
                  "interface B extends A {",
                  "  static void f() {}",
                  "  int x = 42;",
                  "}",
                }
              },
              String[].class);
    }
    return stream(inputs)
        .map(input -> TestInput.parse(Joiner.on('\n').join(input)))
        .map(x -> new TestInput[] {x})
        .collect(toImmutableList());
  }

  private final TestInput input;

  public TurbineElementsHidesTest(TestInput input) {
    this.input = input;
  }

  // Compile the test inputs with javac and turbine, and assert that 'hides' returns the same
  // results under each implementation.
  @Test
  public void test() throws Exception {
    HidesTester javac = runJavac();
    HidesTester turbine = runTurbine();
    assertThat(javac.keys()).containsExactlyElementsIn(turbine.keys());
    for (String k1 : javac.keys()) {
      for (String k2 : javac.keys()) {
        expect
            .withMessage("hides(%s, %s)", k1, k2)
            .that(javac.test(k1, k2))
            .isEqualTo(turbine.test(k1, k2));
      }
    }
  }

  static class HidesTester {
    // The elements for a particular annotation processing implementation
    final Elements elements;
    // A collection of Elements to use as test inputs, keyed by unique strings that can be used to
    // compare them across processing implementations
    final ImmutableMap<String, Element> inputs;

    HidesTester(Elements elements, ImmutableMap<String, Element> inputs) {
      this.elements = elements;
      this.inputs = inputs;
    }

    boolean test(String k1, String k2) {
      return elements.hides(inputs.get(k1), inputs.get(k2));
    }

    public ImmutableSet<String> keys() {
      return inputs.keySet();
    }
  }

  /** Compiles the test input with turbine. */
  private HidesTester runTurbine() throws IOException {
    ImmutableList<CompUnit> units =
        input.sources.entrySet().stream()
            .map(e -> new SourceFile(e.getKey(), e.getValue()))
            .map(Parser::parse)
            .collect(toImmutableList());
    Binder.BindingResult bound =
        Binder.bind(
            units,
            ClassPathBinder.bindClasspath(ImmutableList.of()),
            TestClassPaths.TURBINE_BOOTCLASSPATH,
            Optional.empty());
    Env<ClassSymbol, TypeBoundClass> env =
        CompoundEnv.<ClassSymbol, TypeBoundClass>of(bound.classPathEnv())
            .append(new SimpleEnv<>(bound.units()));
    ModelFactory factory = new ModelFactory(env, ClassLoader.getSystemClassLoader(), bound.tli());
    TurbineTypes turbineTypes = new TurbineTypes(factory);
    TurbineElements elements = new TurbineElements(factory, turbineTypes);
    ImmutableList<TurbineTypeElement> typeElements =
        bound.units().keySet().stream().map(factory::typeElement).collect(toImmutableList());
    return new HidesTester(elements, collectElements(typeElements));
  }

  /** Compiles the test input with turbine. */
  private HidesTester runJavac() throws Exception {
    DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
    JavacTask javacTask =
        IntegrationTestSupport.runJavacAnalysis(
            input.sources, ImmutableList.of(), ImmutableList.of(), diagnostics);
    List<TypeElement> typeElements = new ArrayList<>();
    javacTask.addTaskListener(
        new TaskListener() {
          @Override
          public void started(TaskEvent e) {
            if (e.getKind().equals(TaskEvent.Kind.ANALYZE)) {
              typeElements.add(e.getTypeElement());
            }
          }
        });
    Elements elements = javacTask.getElements();
    if (!javacTask.call()) {
      fail(Joiner.on("\n").join(diagnostics.getDiagnostics()));
    }
    return new HidesTester(elements, collectElements(typeElements));
  }

  /** Scans a test compilation for elements to use as test inputs. */
  private ImmutableMap<String, Element> collectElements(List<? extends TypeElement> typeElements) {
    Map<String, Element> elements = new HashMap<>();
    for (TypeElement typeElement : typeElements) {
      elements.put(key(typeElement), typeElement);
      new ElementScanner8<Void, Void>() {
        @Override
        public Void scan(Element e, Void unused) {
          Element p = elements.put(key(e), e);
          if (p != null && !e.equals(p) && !p.getKind().equals(ElementKind.CONSTRUCTOR)) {
            throw new AssertionError(key(e) + " " + p + " " + e);
          }
          return super.scan(e, unused);
        }
      }.visit(typeElement);
    }
    return ImmutableMap.copyOf(elements);
  }

  /** A unique string representation of an element. */
  private static String key(Element e) {
    ArrayDeque<Name> names = new ArrayDeque<>();
    Element curr = e;
    do {
      if (curr.getSimpleName().length() > 0) {
        names.addFirst(curr.getSimpleName());
      }
      curr = curr.getEnclosingElement();
    } while (curr != null);
    String key = e.getKind() + ":" + Joiner.on('.').join(names);
    if (e.getKind().equals(ElementKind.METHOD)) {
      key += ":" + e.asType();
    }
    return key;
  }
}