Skip to content

Commit

Permalink
Config doc - Avoid processing methods if not @ConfigMapping
Browse files Browse the repository at this point in the history
Fixes quarkusio#35594

(cherry picked from commit 4696977)
  • Loading branch information
gsmet committed Sep 4, 2023
1 parent 5411c18 commit 5b84809
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ final public class Constants {
public static final String ANNOTATION_CONFIG_WITH_UNNAMED_KEY = "io.smallrye.config.WithUnnamedKey";

public static final Set<String> SUPPORTED_ANNOTATIONS_TYPES = Set.of(ANNOTATION_BUILD_STEP, ANNOTATION_CONFIG_GROUP,
ANNOTATION_CONFIG_ROOT, ANNOTATION_RECORDER);
ANNOTATION_CONFIG_ROOT, ANNOTATION_RECORDER, ANNOTATION_CONFIG_MAPPING);

public static final Map<String, String> ALIASED_TYPES = Map.of(
OptionalLong.class.getName(), Long.class.getName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ public void doProcess(Set<? extends TypeElement> annotations, RoundEnvironment r
trackAnnotationUsed(Constants.ANNOTATION_RECORDER);
processRecorder(roundEnv, annotation);
break;
case Constants.ANNOTATION_CONFIG_MAPPING:
trackAnnotationUsed(Constants.ANNOTATION_CONFIG_MAPPING);
break;
}
}
}
Expand Down Expand Up @@ -257,7 +260,7 @@ public FileVisitResult postVisitDirectory(final Path dir, final IOException exc)
try {
if (generateDocs) {
final Set<ConfigDocGeneratedOutput> outputs = configDocItemScanner
.scanExtensionsConfigurationItems(javaDocProperties);
.scanExtensionsConfigurationItems(javaDocProperties, isAnnotationUsed(ANNOTATION_CONFIG_MAPPING));
for (ConfigDocGeneratedOutput output : outputs) {
DocGeneratorUtil.sort(output.getConfigDocItems()); // sort before writing
configDocWriter.writeAllExtensionConfigDocumentation(output);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,18 @@ class ConfigDocItemFinder {
private final Map<String, TypeElement> configGroupQualifiedNameToTypeElementMap;
private final FsMap allConfigurationGroups;
private final FsMap allConfigurationRoots;
private final boolean configMapping;

public ConfigDocItemFinder(Set<ConfigRootInfo> configRoots,
Map<String, TypeElement> configGroupQualifiedNameToTypeElementMap,
Properties javaDocProperties, FsMap allConfigurationGroups, FsMap allConfigurationRoots) {
Properties javaDocProperties, FsMap allConfigurationGroups, FsMap allConfigurationRoots,
boolean configMapping) {
this.configRoots = configRoots;
this.configGroupQualifiedNameToTypeElementMap = configGroupQualifiedNameToTypeElementMap;
this.javaDocProperties = javaDocProperties;
this.allConfigurationGroups = allConfigurationGroups;
this.allConfigurationRoots = allConfigurationRoots;
this.configMapping = configMapping;
}

/**
Expand All @@ -97,7 +100,7 @@ ScannedConfigDocsItemHolder findInMemoryConfigurationItems() throws IOException
ConfigPhase buildTime = ConfigPhase.BUILD_TIME;
final List<ConfigDocItem> configDocItems = recursivelyFindConfigItems(entry.getValue(), EMPTY, EMPTY, buildTime,
false, 1,
false);
false, configMapping);
allConfigurationGroups.put(entry.getKey(), OBJECT_MAPPER.writeValueAsString(configDocItems));
}

Expand All @@ -107,7 +110,7 @@ ScannedConfigDocsItemHolder findInMemoryConfigurationItems() throws IOException
String rootName = configRootInfo.getName();
ConfigPhase configPhase = configRootInfo.getConfigPhase();
final List<ConfigDocItem> configDocItems = recursivelyFindConfigItems(element, rootName, rootName, configPhase,
false, sectionLevel, true);
false, sectionLevel, true, configMapping);
holder.addConfigRootItems(configRootInfo, configDocItems);
allConfigurationRoots.put(configRootInfo.getClazz().toString(), OBJECT_MAPPER.writeValueAsString(configDocItems));
}
Expand All @@ -119,7 +122,8 @@ ScannedConfigDocsItemHolder findInMemoryConfigurationItems() throws IOException
* Recursively find config item found in a config root or config group given as {@link Element}
*/
private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String rootName, String parentName,
ConfigPhase configPhase, boolean withinAMap, int sectionLevel, boolean generateSeparateConfigGroupDocsFiles)
ConfigPhase configPhase, boolean withinAMap, int sectionLevel, boolean generateSeparateConfigGroupDocsFiles,
boolean configMapping)
throws JsonProcessingException {
List<ConfigDocItem> configDocItems = new ArrayList<>();
TypeElement asTypeElement = (TypeElement) element;
Expand All @@ -138,7 +142,8 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
if (rawConfigItems == null) { // element not yet scanned
Element superElement = ((DeclaredType) superType).asElement();
superTypeConfigItems = recursivelyFindConfigItems(superElement, rootName, parentName,
configPhase, withinAMap, sectionLevel, generateSeparateConfigGroupDocsFiles);
configPhase, withinAMap, sectionLevel, generateSeparateConfigGroupDocsFiles,
configMapping);
} else {
superTypeConfigItems = OBJECT_MAPPER.readValue(rawConfigItems, LIST_OF_CONFIG_ITEMS_TYPE_REF);
}
Expand All @@ -148,7 +153,7 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
}

for (Element enclosedElement : element.getEnclosedElements()) {
if (!shouldProcessElement(enclosedElement)) {
if (!shouldProcessElement(enclosedElement, configMapping)) {
continue;
}

Expand Down Expand Up @@ -262,7 +267,7 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r

if (isConfigGroup(type)) {
List<ConfigDocItem> groupConfigItems = readConfigGroupItems(configPhase, rootName, name, emptyList(), type,
configSection, withinAMap, generateSeparateConfigGroupDocsFiles);
configSection, withinAMap, generateSeparateConfigGroupDocsFiles, configMapping);
DocGeneratorUtil.appendConfigItemsIntoExistingOnes(configDocItems, groupConfigItems);
} else {
final ConfigDocKey configDocKey = new ConfigDocKey();
Expand Down Expand Up @@ -293,7 +298,8 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
additionalNames = emptyList();
}
List<ConfigDocItem> groupConfigItems = readConfigGroupItems(configPhase, rootName, name,
additionalNames, type, configSection, true, generateSeparateConfigGroupDocsFiles);
additionalNames, type, configSection, true, generateSeparateConfigGroupDocsFiles,
configMapping);
DocGeneratorUtil.appendConfigItemsIntoExistingOnes(configDocItems, groupConfigItems);
continue;
} else {
Expand All @@ -320,7 +326,7 @@ private List<ConfigDocItem> recursivelyFindConfigItems(Element element, String r
configSection.setOptional(true);
List<ConfigDocItem> groupConfigItems = readConfigGroupItems(configPhase, rootName, name,
emptyList(), typeInString, configSection, withinAMap,
generateSeparateConfigGroupDocsFiles);
generateSeparateConfigGroupDocsFiles, configMapping);
DocGeneratorUtil.appendConfigItemsIntoExistingOnes(configDocItems, groupConfigItems);
continue;
} else if ((typeInString.startsWith(List.class.getName())
Expand Down Expand Up @@ -424,11 +430,15 @@ private boolean isConfigGroup(String type) {
return configGroupQualifiedNameToTypeElementMap.containsKey(type) || allConfigurationGroups.hasKey(type);
}

private boolean shouldProcessElement(final Element enclosedElement) {
private boolean shouldProcessElement(final Element enclosedElement, final boolean configMapping) {
if (enclosedElement.getKind().isField()) {
return true;
}

if (!configMapping && enclosedElement.getKind() == ElementKind.METHOD) {
return false;
}

// A ConfigMapping method
if (enclosedElement.getKind().equals(ElementKind.METHOD)) {
ExecutableElement method = (ExecutableElement) enclosedElement;
Expand Down Expand Up @@ -537,7 +547,8 @@ private List<ConfigDocItem> readConfigGroupItems(
String configGroup,
ConfigDocSection configSection,
boolean withinAMap,
boolean generateSeparateConfigGroupDocs)
boolean generateSeparateConfigGroupDocs,
boolean configMapping)
throws JsonProcessingException {

configSection.setConfigGroupType(configGroup);
Expand All @@ -557,7 +568,7 @@ private List<ConfigDocItem> readConfigGroupItems(
} else {
TypeElement configGroupTypeElement = configGroupQualifiedNameToTypeElementMap.get(configGroup);
groupConfigItems = recursivelyFindConfigItems(configGroupTypeElement, EMPTY, EMPTY, configPhase,
false, 1, generateSeparateConfigGroupDocs);
false, 1, generateSeparateConfigGroupDocs, configMapping);
allConfigurationGroups.put(configGroup, OBJECT_MAPPER.writeValueAsString(groupConfigItems));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ public void addConfigRoot(final PackageElement pkg, TypeElement clazz) {
}
}

public Set<ConfigDocGeneratedOutput> scanExtensionsConfigurationItems(Properties javaDocProperties)
public Set<ConfigDocGeneratedOutput> scanExtensionsConfigurationItems(Properties javaDocProperties, boolean configMapping)
throws IOException {

Set<ConfigDocGeneratedOutput> configDocGeneratedOutputs = new HashSet<>();
final ConfigDocItemFinder configDocItemFinder = new ConfigDocItemFinder(configRoots, configGroupsToTypeElement,
javaDocProperties, allConfigGroupGeneratedDocs, allExtensionGeneratedDocs);
javaDocProperties, allConfigGroupGeneratedDocs, allExtensionGeneratedDocs, configMapping);
final ScannedConfigDocsItemHolder inMemoryScannedItemsHolder = configDocItemFinder.findInMemoryConfigurationItems();

if (!inMemoryScannedItemsHolder.isEmpty()) {
Expand Down

0 comments on commit 5b84809

Please sign in to comment.