aboutsummaryrefslogtreecommitdiff
path: root/java/com/google/turbine/processing/TurbineFiler.java
blob: 8c522bafe09f3fb82f36ea194c1c6207662ab13f (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
/*
 * 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.base.Preconditions.checkArgument;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;

import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.google.turbine.diag.SourceFile;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.StringReader;
import java.io.Writer;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.processing.Filer;
import javax.annotation.processing.FilerException;
import javax.lang.model.element.Element;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.NestingKind;
import javax.tools.FileObject;
import javax.tools.JavaFileManager.Location;
import javax.tools.JavaFileObject;
import javax.tools.JavaFileObject.Kind;
import javax.tools.StandardLocation;

/** Turbine's implementation of {@link Filer}. */
public class TurbineFiler implements Filer {

  /**
   * Existing paths of file objects that cannot be regenerated, including the original compilation
   * inputs and source or class files generated during any annotation processing round.
   */
  private final Set<String> seen;

  /**
   * File objects generated during the current processing round. Each entry has a unique path, which
   * is enforced by {@link #seen}.
   */
  private final List<TurbineJavaFileObject> files = new ArrayList<>();

  /** Loads resources from the classpath. */
  private final Function<String, Supplier<byte[]>> classPath;

  /** The {@link ClassLoader} for the annotation processor path, for loading resources. */
  private final ClassLoader loader;

  private final Map<String, SourceFile> generatedSources = new LinkedHashMap<>();
  private final Map<String, byte[]> generatedClasses = new LinkedHashMap<>();

  /** Generated source file objects from all rounds. */
  public ImmutableMap<String, SourceFile> generatedSources() {
    return ImmutableMap.copyOf(generatedSources);
  }

  /** Generated class file objects from all rounds. */
  public ImmutableMap<String, byte[]> generatedClasses() {
    return ImmutableMap.copyOf(generatedClasses);
  }

  public TurbineFiler(
      Set<String> seen, Function<String, Supplier<byte[]>> classPath, ClassLoader loader) {
    this.seen = seen;
    this.classPath = classPath;
    this.loader = loader;
  }

  /**
   * Called when the current annotation processing round is complete, and returns the sources
   * generated in that round.
   */
  public Collection<SourceFile> finishRound() {
    Map<String, SourceFile> roundSources = new LinkedHashMap<>();
    for (TurbineJavaFileObject e : files) {
      String path = e.getName();
      switch (e.getKind()) {
        case SOURCE:
          roundSources.put(path, new SourceFile(path, e.contents()));
          break;
        case CLASS:
          generatedClasses.put(path, e.bytes());
          break;
        case OTHER:
          switch (e.location()) {
            case CLASS_OUTPUT:
              generatedClasses.put(path, e.bytes());
              break;
            case SOURCE_OUTPUT:
              this.generatedSources.put(path, new SourceFile(path, e.contents()));
              break;
            default:
              throw new AssertionError(e.location());
          }
          break;
        case HTML:
          throw new UnsupportedOperationException(String.valueOf(e.getKind()));
      }
    }
    files.clear();
    this.generatedSources.putAll(roundSources);
    return roundSources.values();
  }

  @Override
  public JavaFileObject createSourceFile(CharSequence n, Element... originatingElements)
      throws IOException {
    String name = n.toString();
    checkArgument(!name.contains("/"), "invalid type name: %s", name);
    return create(StandardLocation.SOURCE_OUTPUT, Kind.SOURCE, name.replace('.', '/') + ".java");
  }

  @Override
  public JavaFileObject createClassFile(CharSequence n, Element... originatingElements)
      throws IOException {
    String name = n.toString();
    checkArgument(!name.contains("/"), "invalid type name: %s", name);
    return create(StandardLocation.CLASS_OUTPUT, Kind.CLASS, name.replace('.', '/') + ".class");
  }

  @Override
  public FileObject createResource(
      Location location, CharSequence p, CharSequence r, Element... originatingElements)
      throws IOException {
    checkArgument(location instanceof StandardLocation, "%s", location);
    String pkg = p.toString();
    String relativeName = r.toString();
    checkArgument(!pkg.contains("/"), "invalid package: %s", pkg);
    String path = packageRelativePath(pkg, relativeName);
    return create((StandardLocation) location, Kind.OTHER, path);
  }

  private JavaFileObject create(StandardLocation location, Kind kind, String path)
      throws FilerException {
    checkArgument(location.isOutputLocation());
    if (!seen.add(path)) {
      throw new FilerException("already created " + path);
    }
    TurbineJavaFileObject result = new TurbineJavaFileObject(location, kind, path);
    files.add(result);
    return result;
  }

  @Override
  public FileObject getResource(Location location, CharSequence p, CharSequence r)
      throws IOException {
    String pkg = p.toString();
    String relativeName = r.toString();
    checkArgument(!pkg.contains("/"), "invalid package: %s", pkg);
    checkArgument(location instanceof StandardLocation, "unsupported location %s", location);
    StandardLocation standardLocation = (StandardLocation) location;
    String path = packageRelativePath(pkg, relativeName);
    switch (standardLocation) {
      case CLASS_OUTPUT:
        byte[] generated = generatedClasses.get(path);
        if (generated == null) {
          throw new FileNotFoundException(path);
        }
        return new BytesFileObject(path, Suppliers.ofInstance(generated));
      case SOURCE_OUTPUT:
        SourceFile source = generatedSources.get(path);
        if (source == null) {
          throw new FileNotFoundException(path);
        }
        return new SourceFileObject(path, source.source());
      case ANNOTATION_PROCESSOR_PATH:
        if (loader.getResource(path) == null) {
          throw new FileNotFoundException(path);
        }
        return new ResourceFileObject(loader, path);
      case CLASS_PATH:
        Supplier<byte[]> bytes = classPath.apply(path);
        if (bytes == null) {
          throw new FileNotFoundException(path);
        }
        return new BytesFileObject(path, bytes);
      default:
        throw new IllegalArgumentException(standardLocation.getName());
    }
  }

  private static String packageRelativePath(String pkg, String relativeName) {
    if (pkg.isEmpty()) {
      return relativeName;
    }
    return pkg.replace('.', '/') + '/' + relativeName;
  }

  private abstract static class ReadOnlyFileObject implements FileObject {

    protected final String path;

    public ReadOnlyFileObject(String path) {
      this.path = path;
    }

    @Override
    public final String getName() {
      return path;
    }

    @Override
    public URI toUri() {
      return URI.create("file:///" + path);
    }

    @Override
    public final OutputStream openOutputStream() {
      throw new IllegalStateException();
    }

    @Override
    public final Writer openWriter() {
      throw new IllegalStateException();
    }

    @Override
    public final long getLastModified() {
      return 0;
    }

    @Override
    public final boolean delete() {
      throw new IllegalStateException();
    }
  }

  private abstract static class WriteOnlyFileObject implements FileObject {

    @Override
    public final InputStream openInputStream() {
      throw new IllegalStateException();
    }

    @Override
    public final Reader openReader(boolean ignoreEncodingErrors) {
      throw new IllegalStateException();
    }

    @Override
    public final CharSequence getCharContent(boolean ignoreEncodingErrors) {
      throw new IllegalStateException();
    }
  }

  private static class TurbineJavaFileObject extends WriteOnlyFileObject implements JavaFileObject {

    private final StandardLocation location;
    private final Kind kind;
    private final CharSequence name;
    private final ByteArrayOutputStream baos = new ByteArrayOutputStream();

    public TurbineJavaFileObject(StandardLocation location, Kind kind, CharSequence name) {
      this.location = location;
      this.kind = kind;
      this.name = name;
    }

    @Override
    public Kind getKind() {
      return kind;
    }

    @Override
    public boolean isNameCompatible(String simpleName, Kind kind) {
      throw new UnsupportedOperationException();
    }

    @Override
    public NestingKind getNestingKind() {
      throw new UnsupportedOperationException();
    }

    @Override
    public Modifier getAccessLevel() {
      throw new UnsupportedOperationException();
    }

    @Override
    public URI toUri() {
      return URI.create("file:///" + name + kind.extension);
    }

    @Override
    public String getName() {
      return name.toString();
    }

    @Override
    public OutputStream openOutputStream() {
      return baos;
    }

    @Override
    public Writer openWriter() {
      return new OutputStreamWriter(openOutputStream(), UTF_8);
    }

    @Override
    public long getLastModified() {
      return 0;
    }

    @Override
    public boolean delete() {
      throw new IllegalStateException();
    }

    public byte[] bytes() {
      return baos.toByteArray();
    }

    public String contents() {
      return new String(baos.toByteArray(), UTF_8);
    }

    public StandardLocation location() {
      return location;
    }
  }

  private static class ResourceFileObject extends ReadOnlyFileObject {

    private final ClassLoader loader;

    public ResourceFileObject(ClassLoader loader, String path) {
      super(path);
      this.loader = loader;
    }

    @Override
    public URI toUri() {
      try {
        return requireNonNull(loader.getResource(path)).toURI();
      } catch (URISyntaxException e) {
        throw new AssertionError(e);
      }
    }

    @Override
    public InputStream openInputStream() {
      return loader.getResourceAsStream(path);
    }

    @Override
    public Reader openReader(boolean ignoreEncodingErrors) throws IOException {
      return new InputStreamReader(openInputStream(), UTF_8);
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
      return new String(openInputStream().readAllBytes(), UTF_8);
    }
  }

  private static class BytesFileObject extends ReadOnlyFileObject {

    private final Supplier<byte[]> bytes;

    public BytesFileObject(String path, Supplier<byte[]> bytes) {
      super(path);
      this.bytes = bytes;
    }

    @Override
    public InputStream openInputStream() {
      return new ByteArrayInputStream(bytes.get());
    }

    @Override
    public Reader openReader(boolean ignoreEncodingErrors) {
      return new InputStreamReader(openInputStream(), UTF_8);
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
      return new String(bytes.get(), UTF_8);
    }
  }

  private static class SourceFileObject extends ReadOnlyFileObject {

    private final String source;

    public SourceFileObject(String path, String source) {
      super(path);
      this.source = source;
    }

    @Override
    public InputStream openInputStream() {
      return new ByteArrayInputStream(source.getBytes(UTF_8));
    }

    @Override
    public Reader openReader(boolean ignoreEncodingErrors) {
      return new StringReader(source);
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
      return source;
    }
  }
}