|
1 | 1 | package protocolsupportbuildprocessor;
|
2 | 2 |
|
3 |
| -import java.io.File; |
4 | 3 | import java.io.IOException;
|
5 | 4 | import java.nio.file.Files;
|
| 5 | +import java.nio.file.Path; |
| 6 | +import java.nio.file.Paths; |
6 | 7 | import java.nio.file.StandardOpenOption;
|
7 | 8 | import java.util.Arrays;
|
8 | 9 | import java.util.HashSet;
|
| 10 | +import java.util.LinkedHashSet; |
9 | 11 | import java.util.Set;
|
10 | 12 | import java.util.stream.Collectors;
|
11 | 13 |
|
@@ -39,15 +41,31 @@ public Set<String> getSupportedOptions() {
|
39 | 41 | @Override
|
40 | 42 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
|
41 | 43 | try {
|
42 |
| - File file = new File(processingEnv.getOptions().get(preloadFilePathOptionName)); |
43 |
| - file.getParentFile().mkdirs(); |
44 |
| - Files.write( |
45 |
| - file.toPath(), |
| 44 | + Path path = Paths.get(processingEnv.getOptions().get(preloadFilePathOptionName)); |
| 45 | + if (Files.exists(path)) { |
| 46 | + //load existing entries to list |
| 47 | + Set<String> list = new LinkedHashSet<>(Files.readAllLines(path)); |
| 48 | + //remove entries for classes that aren't annotated |
| 49 | + env.getRootElements().stream() |
| 50 | + .filter(element -> element.getAnnotation(Preload.class) == null) |
| 51 | + .map(element -> ((TypeElement) element).getQualifiedName()) |
| 52 | + .forEach(annotatedName -> list.remove(annotatedName.toString())); |
| 53 | + //add entries for classes that are annotated |
46 | 54 | env.getElementsAnnotatedWith(Preload.class).stream()
|
47 | 55 | .map(element -> ((TypeElement) element).getQualifiedName())
|
48 |
| - .collect(Collectors.toList()), |
49 |
| - StandardOpenOption.CREATE |
50 |
| - ); |
| 56 | + .forEach(annotatedName -> list.add(annotatedName.toString())); |
| 57 | + //write entries |
| 58 | + Files.write(path, list, StandardOpenOption.TRUNCATE_EXISTING); |
| 59 | + } else { |
| 60 | + Files.createDirectories(path.getParent()); |
| 61 | + Files.write( |
| 62 | + path, |
| 63 | + env.getElementsAnnotatedWith(Preload.class).stream() |
| 64 | + .map(element -> ((TypeElement) element).getQualifiedName()) |
| 65 | + .collect(Collectors.toList()), |
| 66 | + StandardOpenOption.CREATE |
| 67 | + ); |
| 68 | + } |
51 | 69 | } catch (IOException e) {
|
52 | 70 | processingEnv.getMessager().printMessage(Kind.ERROR, "Unable to write generated preload list: " + e.getMessage());
|
53 | 71 | }
|
|
0 commit comments