-
Notifications
You must be signed in to change notification settings - Fork 11
Adding Documentation Annotations #137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1356a01
fixing tests
janheise efb2ea7
adding Documentation annotations
janheise 14fbfbf
update to junit5 to accomodate new tests
janheise 8b6154d
going to Java21
janheise 0428545
fixing pom and main class
janheise ecdcf9b
junit updated to 6.0.0
todvora 6664b02
code cleanup
todvora f99c7b1
removed not needed java 21 reference
todvora File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
src/main/java/com/github/joschi/jadconfig/documentation/ConfigurationBeansSPI.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import java.util.Collection; | ||
| import java.util.List; | ||
| import java.util.ServiceLoader; | ||
|
|
||
| public class ConfigurationBeansSPI { | ||
| public static List<Object> loadConfigurationBeans() { | ||
| return ServiceLoader.load(DocumentedBeansService.class).stream() | ||
| .map(ServiceLoader.Provider::get) | ||
| .map(DocumentedBeansService::getDocumentedConfigurationBeans) | ||
| .flatMap(Collection::stream) | ||
| .distinct() | ||
| .toList(); | ||
| } | ||
| } |
171 changes: 171 additions & 0 deletions
171
src/main/java/com/github/joschi/jadconfig/documentation/ConfigurationDocsGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import com.github.joschi.jadconfig.Parameter; | ||
| import com.github.joschi.jadconfig.ReflectionUtils; | ||
| import com.github.joschi.jadconfig.documentation.printers.ConfigFileDocsPrinter; | ||
| import com.github.joschi.jadconfig.documentation.printers.ConfigurationSection; | ||
| import com.github.joschi.jadconfig.documentation.printers.CsvDocsPrinter; | ||
| import com.github.joschi.jadconfig.documentation.printers.DocsPrinter; | ||
| import jakarta.annotation.Nonnull; | ||
| import org.apache.commons.lang3.ClassUtils; | ||
|
|
||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.lang.reflect.Field; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
| import java.util.function.Supplier; | ||
| import java.util.stream.Collector; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class ConfigurationDocsGenerator { | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| final ConfigurationDocsGenerator generator = new ConfigurationDocsGenerator(); | ||
| generator.generateDocumentation(parseDocumentationFormat(args), ConfigurationBeansSPI::loadConfigurationBeans); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private static DocumentationFormat parseDocumentationFormat(String[] args) { | ||
| if (args.length != 2) { | ||
| throw new IllegalArgumentException("This command needs two arguments - a format and file path. For example" + "csv ${project.build.directory}/configuration-documentation.csv"); | ||
| } | ||
| final String format = args[0].toLowerCase(Locale.ROOT).trim(); | ||
| final String file = args[1]; | ||
| return new DocumentationFormat(format, file); | ||
| } | ||
|
|
||
| public void generateDocumentation(DocumentationFormat format, Supplier<List<Object>> configurationBeans) throws IOException { | ||
| final List<ConfigurationSection> sections = detectConfigurationSections(configurationBeans); | ||
| try (final DocsPrinter writer = createWriter(format)) { | ||
| writer.write(sections); | ||
| } | ||
| } | ||
|
|
||
| private DocsPrinter createWriter(DocumentationFormat format) throws IOException { | ||
| final FileWriter fileWriter = new FileWriter(format.outputFile(), StandardCharsets.UTF_8); | ||
| // TODO: if the format list expands, introduce DI and factories for printers | ||
| return switch (format.format()) { | ||
| case "csv" -> new CsvDocsPrinter(fileWriter); | ||
| case "conf" -> new ConfigFileDocsPrinter(fileWriter); | ||
| default -> throw new IllegalArgumentException("Unsupported format " + format.format()); | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Collects all configuration options from all available configuration beans. | ||
| */ | ||
| private List<ConfigurationSection> detectConfigurationSections(Supplier<List<Object>> configurationBeans) { | ||
| return configurationBeans.get() | ||
| .stream() | ||
| .map(ConfigurationDocsGenerator::beanToConfigSections) | ||
| .sorted(Comparator.comparing(ConfigurationSection::hasPriority).reversed()) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static ConfigurationSection beanToConfigSections(Object configurationBean) { | ||
|
|
||
| String sectionHeading = null; | ||
| String sectionDescription = null; | ||
| if (configurationBean.getClass().isAnnotationPresent(DocumentationSection.class)) { | ||
| final DocumentationSection documentationSection = configurationBean.getClass().getAnnotation(DocumentationSection.class); | ||
| sectionHeading = documentationSection.heading(); | ||
| sectionDescription = documentationSection.description(); | ||
| } | ||
|
|
||
| final List<ConfigurationEntryWithSection> entries = Arrays.stream(configurationBean.getClass().getDeclaredFields()) | ||
| .filter(f -> f.isAnnotationPresent(Parameter.class)) | ||
| .filter(ConfigurationDocsGenerator::isPublicFacing) | ||
| .map(f -> toConfigurationEntry(f, configurationBean)) | ||
| .toList(); | ||
|
|
||
| final List<ConfigurationEntry> entriesWithoutSection = getEntriesWithoutSection(entries); | ||
| final List<ConfigurationSection> sortedSections = sectionsFromEntries(entries); | ||
|
|
||
| return new ConfigurationSection(sectionHeading, sectionDescription, sortedSections, entriesWithoutSection); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private static List<ConfigurationSection> sectionsFromEntries(List<ConfigurationEntryWithSection> entries) { | ||
| return entries.stream() | ||
| .filter(ConfigurationEntryWithSection::hasSection) | ||
| .collect(groupBySection()) | ||
| .values() | ||
| .stream() | ||
| .sorted(sortByPriority()) | ||
| .toList(); | ||
| } | ||
|
|
||
| private static Comparator<ConfigurationSection> sortByPriority() { | ||
| return Comparator.comparing(ConfigurationSection::hasPriority, Comparator.reverseOrder()); | ||
| } | ||
|
|
||
| private static Collector<ConfigurationEntryWithSection, ?, Map<String, ConfigurationSection>> groupBySection() { | ||
| return Collectors.groupingBy(ConfigurationEntryWithSection::sectionHeading, Collectors.collectingAndThen(Collectors.toList(), ConfigurationDocsGenerator::toConfigurationSection)); | ||
| } | ||
|
|
||
| private static ConfigurationSection toConfigurationSection(List<ConfigurationEntryWithSection> list) { | ||
| final ConfigurationEntryWithSection section = list.stream().findFirst().orElseThrow(() -> new IllegalArgumentException("No configuration section found but expected!")); | ||
| final List<ConfigurationEntry> entries = list.stream().map(ConfigurationEntryWithSection::entry).collect(Collectors.toList()); | ||
| return new ConfigurationSection(section.sectionHeading(), section.sectionDescription(), Collections.emptyList(), entries); | ||
| } | ||
|
|
||
| @Nonnull | ||
| private static List<ConfigurationEntry> getEntriesWithoutSection(List<ConfigurationEntryWithSection> entries) { | ||
| return entries.stream() | ||
| .filter(e -> !e.hasSection()) | ||
| .map(ConfigurationEntryWithSection::entry) | ||
| .sorted(Comparator.comparing(ConfigurationEntry::hasPriority, Comparator.reverseOrder())) | ||
| .collect(Collectors.toList()); | ||
| } | ||
|
|
||
| /** | ||
| * There are some configuration options not intended for general usage, mainly just for system packages configuration. | ||
| * | ||
| * @see Documentation#visible() | ||
| */ | ||
| private static boolean isPublicFacing(Field f) { | ||
| return !f.isAnnotationPresent(Documentation.class) || f.getAnnotation(Documentation.class).visible(); | ||
| } | ||
|
|
||
| private static ConfigurationEntryWithSection toConfigurationEntry(Field f, Object instance) { | ||
| final String documentation = Optional.ofNullable(f.getAnnotation(Documentation.class)).map(Documentation::value).orElse(null); | ||
| final Parameter parameter = f.getAnnotation(Parameter.class); | ||
| final String propertyName = parameter.value(); | ||
| final Object defaultValue = getDefaultValue(f, instance); | ||
| final String type = getType(f); | ||
| final boolean required = parameter.required(); | ||
|
|
||
| final DocumentationSection documentationSection = f.getAnnotation(DocumentationSection.class); | ||
| String sectionHeading = null; | ||
| String sectionDescription = null; | ||
| if (documentationSection != null) { | ||
| sectionHeading = documentationSection.heading(); | ||
| sectionDescription = documentationSection.description(); | ||
| } | ||
| final ConfigurationEntry entry = new ConfigurationEntry(instance.getClass(), f.getName(), type, propertyName, defaultValue, required, documentation); | ||
| return new ConfigurationEntryWithSection(entry, sectionHeading, sectionDescription); | ||
| } | ||
|
|
||
| private static Object getDefaultValue(Field f, Object instance) { | ||
| try { | ||
| return ReflectionUtils.getFieldValue(instance, f); | ||
| } catch (IllegalAccessException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
|
|
||
| private static String getType(Field f) { | ||
| if (f.getType().isPrimitive()) { // unify primitive types and wrappers, e.g. int -> Integer | ||
| return ClassUtils.primitiveToWrapper(f.getType()).getSimpleName(); | ||
| } else { | ||
| return f.getType().getSimpleName(); | ||
| } | ||
| } | ||
| } |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/github/joschi/jadconfig/documentation/ConfigurationEntry.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import jakarta.annotation.Nullable; | ||
|
|
||
| /** | ||
| * @param configurationBean Class that defines this configuration entry | ||
| * @param fieldName java field name | ||
| * @param type Java type name, e.g. String or Integer. | ||
| * @param configName configuration property name, as written in the config file | ||
| * @param defaultValue default value declared in the java field, null if not defined | ||
| * @param required if the configuration property is mandatory (needs default or entry in the config file) | ||
| * @param documentation textual documentation of this configuration propery | ||
| */ | ||
| public record ConfigurationEntry( | ||
| Class<?> configurationBean, | ||
| String fieldName, | ||
| String type, | ||
| String configName, | ||
| @Nullable Object defaultValue, | ||
| boolean required, | ||
| String documentation | ||
| ) { | ||
|
|
||
| public boolean hasPriority() { | ||
| return required && defaultValue == null; | ||
| } | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/github/joschi/jadconfig/documentation/ConfigurationEntryWithSection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| public record ConfigurationEntryWithSection(ConfigurationEntry entry, String sectionHeading, String sectionDescription) { | ||
| public boolean hasSection() { | ||
| return sectionHeading != null && !sectionHeading.isBlank(); | ||
| } | ||
| } |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/github/joschi/jadconfig/documentation/Documentation.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| /** | ||
| * This annotation is used to document configuration files. It allows autogeneration of config documentation. | ||
| */ | ||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target(ElementType.FIELD) | ||
| public @interface Documentation { | ||
| /** | ||
| * We don't want to expose some configuration fields to users. They are internal, required for system packages functionality | ||
| * or deprecated. Set to false if you want to hide this field from documentation. | ||
| */ | ||
| boolean visible() default true; | ||
|
|
||
| /** | ||
| * Description of this configuration property. | ||
| */ | ||
| String value() default ""; | ||
| } |
4 changes: 4 additions & 0 deletions
4
src/main/java/com/github/joschi/jadconfig/documentation/DocumentationFormat.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| public record DocumentationFormat(String format, String outputFile) { | ||
| } |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/github/joschi/jadconfig/documentation/DocumentationSection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import java.lang.annotation.Documented; | ||
| import java.lang.annotation.ElementType; | ||
| import java.lang.annotation.Retention; | ||
| import java.lang.annotation.RetentionPolicy; | ||
| import java.lang.annotation.Target; | ||
|
|
||
| @Documented | ||
| @Retention(RetentionPolicy.RUNTIME) | ||
| @Target({ElementType.TYPE, ElementType.FIELD}) | ||
| public @interface DocumentationSection { | ||
| String heading(); | ||
| String description(); | ||
| } |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/github/joschi/jadconfig/documentation/DocumentedBeansService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package com.github.joschi.jadconfig.documentation; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface DocumentedBeansService { | ||
| List<Object> getDocumentedConfigurationBeans(); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.