Skip to content

Commit f5106ed

Browse files
committed
Support updating existing preload list
1 parent 1c29941 commit f5106ed

File tree

1 file changed

+26
-8
lines changed

1 file changed

+26
-8
lines changed

Diff for: src/protocolsupportbuildprocessor/AnnotationProcessor.java

+26-8
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package protocolsupportbuildprocessor;
22

3-
import java.io.File;
43
import java.io.IOException;
54
import java.nio.file.Files;
5+
import java.nio.file.Path;
6+
import java.nio.file.Paths;
67
import java.nio.file.StandardOpenOption;
78
import java.util.Arrays;
89
import java.util.HashSet;
10+
import java.util.LinkedHashSet;
911
import java.util.Set;
1012
import java.util.stream.Collectors;
1113

@@ -39,15 +41,31 @@ public Set<String> getSupportedOptions() {
3941
@Override
4042
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment env) {
4143
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
4654
env.getElementsAnnotatedWith(Preload.class).stream()
4755
.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+
}
5169
} catch (IOException e) {
5270
processingEnv.getMessager().printMessage(Kind.ERROR, "Unable to write generated preload list: " + e.getMessage());
5371
}

0 commit comments

Comments
 (0)