|
94 | 94 | import com.sun.tools.javac.comp.CompileStates.CompileState; |
95 | 95 | import com.sun.tools.javac.file.JavacFileManager; |
96 | 96 | import com.sun.tools.javac.main.Option; |
| 97 | +import com.sun.tools.javac.main.Option.OptionKind; |
97 | 98 | import com.sun.tools.javac.parser.JavadocTokenizer; |
98 | 99 | import com.sun.tools.javac.parser.Scanner; |
99 | 100 | import com.sun.tools.javac.parser.ScannerFactory; |
@@ -756,8 +757,13 @@ public Void visitClass(ClassTree node, Void p) { |
756 | 757 | fileObjects.add(fileObject); |
757 | 758 | } |
758 | 759 |
|
759 | | - |
760 | | - Iterable<String> options = configureAPIfNecessary(fileManager) ? null : Arrays.asList("-proc:none"); |
| 760 | + // some options needs to be passed to getTask() to be properly handled |
| 761 | + // (just having them set in Options is sometimes not enough). So we |
| 762 | + // turn them back into CLI arguments to pass them. |
| 763 | + List<String> options = new ArrayList<>(toCLIOptions(javacOptions)); |
| 764 | + if (!configureAPTIfNecessary(fileManager)) { |
| 765 | + options.add("-proc:none"); |
| 766 | + } |
761 | 767 | JavacTask task = ((JavacTool)compiler).getTask(null, fileManager, null /* already added to context */, options, List.of() /* already set */, fileObjects, context); |
762 | 768 | { |
763 | 769 | // don't know yet a better way to ensure those necessary flags get configured |
@@ -1169,7 +1175,7 @@ private static Function<String, IBinding> javacAdditionalBindingCreator(Map<Stri |
1169 | 1175 | }; |
1170 | 1176 | } |
1171 | 1177 |
|
1172 | | - private boolean configureAPIfNecessary(JavacFileManager fileManager) { |
| 1178 | + private boolean configureAPTIfNecessary(JavacFileManager fileManager) { |
1173 | 1179 | Iterable<? extends File> apPaths = fileManager.getLocation(StandardLocation.ANNOTATION_PROCESSOR_PATH); |
1174 | 1180 | if (apPaths != null) { |
1175 | 1181 | return true; |
@@ -1213,4 +1219,20 @@ private boolean configureAPIfNecessary(JavacFileManager fileManager) { |
1213 | 1219 | return false; |
1214 | 1220 | } |
1215 | 1221 |
|
| 1222 | + private static List<String> toCLIOptions(Options opts) { |
| 1223 | + return opts.keySet().stream().map(Option::lookup) |
| 1224 | + .filter(Objects::nonNull) |
| 1225 | + .filter(opt -> opt.getKind() != OptionKind.HIDDEN) |
| 1226 | + .map(opt -> |
| 1227 | + switch (opt.getArgKind()) { |
| 1228 | + case NONE -> Stream.of(opt.primaryName); |
| 1229 | + case REQUIRED -> opt.primaryName.endsWith("=") || opt.primaryName.endsWith(":") ? Stream.of(opt.primaryName + opts.get(opt)) : Stream.of(opt.primaryName, opts.get(opt)); |
| 1230 | + case ADJACENT -> { |
| 1231 | + var value = opts.get(opt); |
| 1232 | + yield value == null || value.isEmpty() ? Arrays.stream(new String[0]) : |
| 1233 | + Stream.of(opt.primaryName + opts.get(opt)); |
| 1234 | + } |
| 1235 | + }).flatMap(Function.identity()) |
| 1236 | + .toList(); |
| 1237 | + } |
1216 | 1238 | } |
0 commit comments