Skip to content

Commit 978437d

Browse files
authored
Update JctCompiler.java with examples in docs
Signed-off-by: ascopes <[email protected]>
1 parent 8ffd708 commit 978437d

File tree

1 file changed

+140
-15
lines changed
  • java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers

1 file changed

+140
-15
lines changed

java-compiler-testing/src/main/java/io/github/ascopes/jct/compilers/JctCompiler.java

Lines changed: 140 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,64 @@ default JctCompilation compile(Workspace workspace, String firstClassName,
195195
JctCompilation compile(Workspace workspace, Collection<String> classNames);
196196

197197
/**
198-
* Apply a given configurer to this compiler that can throw a checked exception.
198+
* Apply a given configurer to this compiler.
199+
*
200+
* <p>Configurers can be lambdas, method references, or objects.
201+
*
202+
* <pre><code>
203+
* // Using an object configurer
204+
* var werrorConfigurer = new JctCompilerConfigurer&lt;RuntimeException&gt;() {
205+
* {@literal @Override}
206+
* public void configure(JctCompiler compiler) {
207+
* compiler.failOnWarnings(true);
208+
* }
209+
* };
210+
* compiler.configure(werrorConfigurer);
211+
*
212+
* // Using a lambda configurer
213+
* compiler.configure(c -&gt; c.verbose(true));
214+
* </code></pre>
215+
*
216+
* <p>Configurers take a type parameter that corresponds to an exception type. This
217+
* is the exception type that can be thrown by the configurer, or {@link RuntimeException}
218+
* if no checked exception is thrown. This mechanism allows configurers to propagate
219+
* checked exceptions to their caller where needed.
220+
*
221+
* <pre><code>
222+
* class FileFlagConfigurer implements JctCompilerConfigurer&lt;IOException&gt; {
223+
* private final Path path;
224+
*
225+
* public FileFlagConfigurer(String... path) {
226+
* this(Path.of(path));
227+
* }
228+
*
229+
* public FileFlagConfigurer(Path path) {
230+
* this.path = path;
231+
* }
232+
*
233+
* {@literal @Override}
234+
* public void configure(JctCompiler compiler) throws IOException {
235+
* var flags = Files.lines(path)
236+
* .map(String::trim)
237+
* .filter(not(String::isBlank))
238+
* .toList();
239+
* compiler.addCompilerOptions(flags);
240+
* }
241+
* }
242+
*
243+
* {@literal @Test}
244+
* void testSomething() throws IOException {
245+
* ...
246+
* compiler.configure(new FileFlagConfigurer("src", "test", "resources", "flags.txt"));
247+
* ...
248+
* }
249+
* </code></pre>
199250
*
200251
* @param <E> any exception that may be thrown.
201252
* @param configurer the configurer to invoke.
202253
* @return this compiler object for further call chaining.
203-
* @throws E any exception that may be thrown by the configurer.
254+
* @throws E any exception that may be thrown by the configurer. If no checked exception
255+
* is thrown, then this should be treated as {@link RuntimeException}.
204256
*/
205257
<E extends Exception> JctCompiler configure(JctCompilerConfigurer<E> configurer) throws E;
206258

@@ -214,6 +266,10 @@ default JctCompilation compile(Workspace workspace, String firstClassName,
214266
/**
215267
* Set the friendly name of this compiler.
216268
*
269+
* <p>This will be used by the
270+
* {@link io.github.ascopes.jct.junit.JavacCompilerTest JUnit5 support}
271+
* to name unit test cases.
272+
*
217273
* @param name the name to set.
218274
* @return this compiler object for further call chaining.
219275
*/
@@ -262,8 +318,9 @@ default JctCompiler addAnnotationProcessorOptions(
262318
/**
263319
* Add annotation processors to invoke.
264320
*
265-
* <p>This bypasses the discovery process of annotation processors provided in the annotation
266-
* processor path.
321+
* <p><strong>Warning:</strong> This bypasses the discovery process of annotation processors
322+
* provided in the annotation processor path and annotation processor module paths, as well as
323+
* any other locations such as class paths and module paths.
267324
*
268325
* @param annotationProcessors the processors to invoke.
269326
* @return this compiler object for further call chaining.
@@ -273,8 +330,9 @@ default JctCompiler addAnnotationProcessorOptions(
273330
/**
274331
* Add annotation processors to invoke.
275332
*
276-
* <p>This bypasses the discovery process of annotation processors provided in the annotation
277-
* processor path.
333+
* <p><strong>Warning:</strong> This bypasses the discovery process of annotation processors
334+
* provided in the annotation processor path and annotation processor module paths, as well as
335+
* any other locations such as class paths and module paths.
278336
*
279337
* @param annotationProcessor the first processor to invoke.
280338
* @param annotationProcessors additional processors to invoke.
@@ -354,11 +412,14 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
354412
boolean isPreviewFeatures();
355413

356414
/**
357-
* Set whether to enable preview features or not.
415+
* Set whether to enable compiler preview features or not.
358416
*
359417
* <p>Unless otherwise changed or specified, implementations should default to
360418
* {@link #DEFAULT_PREVIEW_FEATURES}.
361419
*
420+
* <p>Generally, this feature should be avoided if testing across multiple versions
421+
* of Java, as preview features are oftenvnot finalised and may change without warning.
422+
*
362423
* @param enabled {@code true} to enable preview features, or {@code false} to disable them.
363424
* @return this compiler object for further call chaining.
364425
*/
@@ -421,6 +482,8 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
421482
/**
422483
* Set whether to enable treating warnings as errors or not.
423484
*
485+
* <p>Some compilers may call this flag something different, such as "{@code -Werror}".
486+
*
424487
* <p>This is ignored if {@link #showWarnings(boolean)} is disabled.
425488
*
426489
* <p>Unless otherwise changed or specified, implementations should default to
@@ -445,6 +508,10 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
445508
/**
446509
* Set the compilation mode to use for this compiler.
447510
*
511+
* <p>This allows you to override whether sources are compiled or annotation-processed
512+
* without running the full compilation process. Tuning this may provide faster test
513+
* cases in some situations.
514+
*
448515
* <p>Unless otherwise changed or specified, implementations should default to
449516
* {@link #DEFAULT_COMPILATION_MODE}.
450517
*
@@ -456,7 +523,12 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
456523
/**
457524
* Get the default release to use if no release or target version is specified.
458525
*
459-
* <p>This can <strong>not</strong> be configured.
526+
* <p>This can <strong>not</strong> be configured generally, as it is defined by
527+
* the internal compiler implementation.
528+
*
529+
* <p>Generally, this value will be an integer within a string. The value is
530+
* represented as a string to allow supporting compilers which may use non-integer
531+
* version numbers.
460532
*
461533
* @return the default release version to use.
462534
*/
@@ -465,6 +537,10 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
465537
/**
466538
* Get the effective release to use for the actual compilation.
467539
*
540+
* <p>Generally, this value will be an integer within a string. The value is
541+
* represented as a string to allow supporting compilers which may use non-integer
542+
* version numbers.
543+
*
468544
* <p>This may be determined from the {@link #getSource() source},
469545
* {@link #getTarget() target}, {@link #getRelease() release}, and
470546
* {@link #getDefaultRelease() default release.}
@@ -477,6 +553,10 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
477553
* Get the current release version that is set, or {@code null} if left to the compiler to decide.
478554
* default.
479555
*
556+
* <p>Generally, this value will be an integer within a string. The value is
557+
* represented as a string to allow supporting compilers which may use non-integer
558+
* version numbers.
559+
*
480560
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
481561
* default.
482562
*
@@ -489,6 +569,10 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
489569
*
490570
* <p>This will clear any source and target version that is set.
491571
*
572+
* <p>Generally, this value will be an integer within a string. The value is
573+
* represented as a string to allow supporting compilers which may use non-integer
574+
* version numbers.
575+
*
492576
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
493577
* default.
494578
*
@@ -502,6 +586,10 @@ default JctCompiler addCompilerOptions(String compilerOption, String... compiler
502586
*
503587
* <p>This will clear any source and target version that is set.
504588
*
589+
* <p>Generally, this value will be an integer within a string. The value is
590+
* represented as a string to allow supporting compilers which may use non-integer
591+
* version numbers.
592+
*
505593
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
506594
* default.
507595
*
@@ -521,6 +609,10 @@ default JctCompiler release(int release) {
521609
*
522610
* <p>This will clear any source and target version that is set.
523611
*
612+
* <p>Generally, this value will be an integer within a string. The value is
613+
* represented as a string to allow supporting compilers which may use non-integer
614+
* version numbers.
615+
*
524616
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
525617
* default.
526618
*
@@ -533,13 +625,16 @@ default JctCompiler release(SourceVersion release) {
533625

534626
/**
535627
* Request that the compiler uses a language version that corresponds to the
536-
* runtime language version in use on the JVM running tests.
628+
* runtime language version in use on the current JVM.
537629
*
538630
* <p>For example, running this on JRE 19 would set the release to "19".
539631
*
540632
* <p>This calls {@link #release(int) internally}.
541633
*
542634
* @return this compiler object for further call chaining.
635+
* @since 1.1.0
636+
* @throws UnsupportedOperationException if the current JVM version does not
637+
* correspond to a supported Jave release version in the compiler.
543638
*/
544639
@API(since = "1.1.0", status = Status.STABLE)
545640
default JctCompiler useRuntimeRelease() {
@@ -565,6 +660,11 @@ default JctCompiler useRuntimeRelease() {
565660
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
566661
* default.
567662
*
663+
* <p>Source and target versions have mostly been replaced with the release version
664+
* mechanism which controls both flags and can ensure other behaviours are consistent.
665+
* This feature is still provided in case you have a specific use case that is not covered
666+
* by this functionality.
667+
*
568668
* @param source the version to set.
569669
* @return this compiler object for further call chaining.
570670
*/
@@ -578,6 +678,11 @@ default JctCompiler useRuntimeRelease() {
578678
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
579679
* default.
580680
*
681+
* <p>Source and target versions have mostly been replaced with the release version
682+
* mechanism which controls both flags and can ensure other behaviours are consistent.
683+
* This feature is still provided in case you have a specific use case that is not covered
684+
* by this functionality.
685+
*
581686
* @param source the version to set.
582687
* @return this compiler object for further call chaining.
583688
*/
@@ -597,6 +702,11 @@ default JctCompiler source(int source) {
597702
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
598703
* default.
599704
*
705+
* <p>Source and target versions have mostly been replaced with the release version
706+
* mechanism which controls both flags and can ensure other behaviours are consistent.
707+
* This feature is still provided in case you have a specific use case that is not covered
708+
* by this functionality.
709+
*
600710
* @param source the version to set.
601711
* @return this compiler object for further call chaining.
602712
*/
@@ -622,6 +732,11 @@ default JctCompiler source(SourceVersion source) {
622732
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
623733
* default.
624734
*
735+
* <p>Source and target versions have mostly been replaced with the release version
736+
* mechanism which controls both flags and can ensure other behaviours are consistent.
737+
* This feature is still provided in case you have a specific use case that is not covered
738+
* by this functionality.
739+
*
625740
* @param target the version to set.
626741
* @return this compiler object for further call chaining.
627742
*/
@@ -635,6 +750,11 @@ default JctCompiler source(SourceVersion source) {
635750
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
636751
* default.
637752
*
753+
* <p>Source and target versions have mostly been replaced with the release version
754+
* mechanism which controls both flags and can ensure other behaviours are consistent.
755+
* This feature is still provided in case you have a specific use case that is not covered
756+
* by this functionality.
757+
*
638758
* @param target the version to set.
639759
* @return this compiler object for further call chaining.
640760
*/
@@ -654,6 +774,11 @@ default JctCompiler target(int target) {
654774
* <p>Unless explicitly defined, the default setting is expected to be a sane compiler-specific
655775
* default.
656776
*
777+
* <p>Source and target versions have mostly been replaced with the release version
778+
* mechanism which controls both flags and can ensure other behaviours are consistent.
779+
* This feature is still provided in case you have a specific use case that is not covered
780+
* by this functionality.
781+
*
657782
* @param target the version to set.
658783
* @return this compiler object for further call chaining.
659784
*/
@@ -694,7 +819,7 @@ default JctCompiler target(SourceVersion target) {
694819
JctCompiler fixJvmModulePathMismatch(boolean fixJvmModulePathMismatch);
695820

696821
/**
697-
* Get whether the class path is inherited from the caller JVM or not.
822+
* Get whether the class path is inherited from the active JVM or not.
698823
*
699824
* <p>Unless otherwise changed or specified, implementations should default to
700825
* {@link #DEFAULT_INHERIT_CLASS_PATH}.
@@ -704,7 +829,7 @@ default JctCompiler target(SourceVersion target) {
704829
boolean isInheritClassPath();
705830

706831
/**
707-
* Set whether the class path is inherited from the caller JVM or not.
832+
* Set whether the class path is inherited from the active JVM or not.
708833
*
709834
* <p>Unless otherwise changed or specified, implementations should default to
710835
* {@link #DEFAULT_INHERIT_CLASS_PATH}.
@@ -715,7 +840,7 @@ default JctCompiler target(SourceVersion target) {
715840
JctCompiler inheritClassPath(boolean inheritClassPath);
716841

717842
/**
718-
* Get whether the module path is inherited from the caller JVM or not.
843+
* Get whether the module path is inherited from the active JVM or not.
719844
*
720845
* <p>Unless otherwise changed or specified, implementations should default to
721846
* {@link #DEFAULT_INHERIT_MODULE_PATH}.
@@ -725,7 +850,7 @@ default JctCompiler target(SourceVersion target) {
725850
boolean isInheritModulePath();
726851

727852
/**
728-
* Set whether the module path is inherited from the caller JVM or not.
853+
* Set whether the module path is inherited from the active JVM or not.
729854
*
730855
* <p>Unless otherwise changed or specified, implementations should default to
731856
* {@link #DEFAULT_INHERIT_MODULE_PATH}.
@@ -736,7 +861,7 @@ default JctCompiler target(SourceVersion target) {
736861
JctCompiler inheritModulePath(boolean inheritModulePath);
737862

738863
/**
739-
* Get whether the system module path is inherited from the caller JVM or not.
864+
* Get whether the system module path is inherited from the active JVM or not.
740865
*
741866
* <p>Unless otherwise changed or specified, implementations should default to
742867
* {@link #DEFAULT_INHERIT_SYSTEM_MODULE_PATH}.
@@ -746,7 +871,7 @@ default JctCompiler target(SourceVersion target) {
746871
boolean isInheritSystemModulePath();
747872

748873
/**
749-
* Set whether the system module path is inherited from the caller JVM or not.
874+
* Set whether the system module path is inherited from the active JVM or not.
750875
*
751876
* <p>Unless otherwise changed or specified, implementations should default to
752877
* {@link #DEFAULT_INHERIT_SYSTEM_MODULE_PATH}.

0 commit comments

Comments
 (0)