• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 Square, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.squareup.javapoet;
17 
18 import java.io.IOException;
19 import java.io.OutputStream;
20 import java.nio.file.FileSystem;
21 import java.nio.file.Files;
22 import java.nio.file.Path;
23 import java.nio.file.spi.FileSystemProvider;
24 import java.util.Arrays;
25 import java.util.LinkedHashMap;
26 import java.util.Map;
27 import java.util.Set;
28 import javax.annotation.processing.Filer;
29 import javax.lang.model.element.Element;
30 import javax.tools.FileObject;
31 import javax.tools.JavaFileManager;
32 import javax.tools.JavaFileObject;
33 import javax.tools.SimpleJavaFileObject;
34 
35 final class TestFiler implements Filer {
36   class Source extends SimpleJavaFileObject {
37     private final Path path;
Source(Path path)38     protected Source(Path path) {
39       super(path.toUri(), Kind.SOURCE);
40       this.path = path;
41     }
openOutputStream()42     @Override public OutputStream openOutputStream() throws IOException {
43       Path parent = path.getParent();
44       if (!Files.exists(parent)) fileSystemProvider.createDirectory(parent);
45       return fileSystemProvider.newOutputStream(path);
46     }
47   }
48 
49   private final String separator;
50   private final Path fileSystemRoot;
51   private final FileSystemProvider fileSystemProvider;
52   private final Map<Path, Set<Element>> originatingElementsMap;
53 
TestFiler(FileSystem fileSystem, Path fsRoot)54   public TestFiler(FileSystem fileSystem, Path fsRoot) {
55     separator = fileSystem.getSeparator();
56     fileSystemRoot = fsRoot;
57     fileSystemProvider = fileSystem.provider();
58     originatingElementsMap = new LinkedHashMap<>();
59   }
60 
getOriginatingElements(Path path)61   public Set<Element> getOriginatingElements(Path path) {
62     return originatingElementsMap.get(path);
63   }
64 
createSourceFile( CharSequence name, Element... originatingElements)65   @Override public JavaFileObject createSourceFile(
66       CharSequence name, Element... originatingElements) throws IOException {
67     String relative = name.toString().replace(".", separator) + ".java"; // Assumes well-formed.
68     Path path = fileSystemRoot.resolve(relative);
69     originatingElementsMap.put(path, Util.immutableSet(Arrays.asList(originatingElements)));
70     return new Source(path);
71   }
72 
createClassFile(CharSequence name, Element... originatingElements)73   @Override public JavaFileObject createClassFile(CharSequence name, Element... originatingElements)
74       throws IOException {
75     throw new UnsupportedOperationException("Not implemented.");
76   }
77 
createResource(JavaFileManager.Location location, CharSequence pkg, CharSequence relativeName, Element... originatingElements)78   @Override public FileObject createResource(JavaFileManager.Location location, CharSequence pkg,
79       CharSequence relativeName, Element... originatingElements) throws IOException {
80     throw new UnsupportedOperationException("Not implemented.");
81   }
82 
getResource(JavaFileManager.Location location, CharSequence pkg, CharSequence relativeName)83   @Override public FileObject getResource(JavaFileManager.Location location, CharSequence pkg,
84       CharSequence relativeName) throws IOException {
85     throw new UnsupportedOperationException("Not implemented.");
86   }
87 }
88