|
| 1 | +/* |
| 2 | + * Copyright 2014-2025 TNG Technology Consulting GmbH |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.tngtech.archunit.core.domain; |
| 17 | + |
| 18 | +import java.lang.reflect.Array; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.stream.IntStream; |
| 24 | + |
| 25 | +import com.google.common.base.Joiner; |
| 26 | + |
| 27 | +import static com.google.common.base.Preconditions.checkNotNull; |
| 28 | +import static java.util.function.Function.identity; |
| 29 | +import static java.util.stream.Collectors.joining; |
| 30 | +import static java.util.stream.Collectors.toList; |
| 31 | + |
| 32 | +class AnnotationFormatter { |
| 33 | + private final Function<JavaClass, String> annotationTypeFormatter; |
| 34 | + private final AnnotationPropertiesFormatter propertiesFormatter; |
| 35 | + |
| 36 | + AnnotationFormatter(Function<JavaClass, String> annotationTypeFormatter, AnnotationPropertiesFormatter propertiesFormatter) { |
| 37 | + this.annotationTypeFormatter = annotationTypeFormatter; |
| 38 | + this.propertiesFormatter = propertiesFormatter; |
| 39 | + } |
| 40 | + |
| 41 | + String format(JavaClass annotationType, Map<String, Object> annotationProperties) { |
| 42 | + return String.format("@%s(%s)", annotationTypeFormatter.apply(annotationType), propertiesFormatter.formatProperties(annotationProperties)); |
| 43 | + } |
| 44 | + |
| 45 | + static Builder formatAnnotationType(Function<JavaClass, String> annotationTypeFormatter) { |
| 46 | + return new Builder(annotationTypeFormatter); |
| 47 | + } |
| 48 | + |
| 49 | + static class Builder { |
| 50 | + private final Function<JavaClass, String> annotationTypeFormatter; |
| 51 | + |
| 52 | + private Builder(Function<JavaClass, String> annotationTypeFormatter) { |
| 53 | + this.annotationTypeFormatter = annotationTypeFormatter; |
| 54 | + } |
| 55 | + |
| 56 | + AnnotationFormatter formatProperties(Consumer<AnnotationPropertiesFormatter.Builder> config) { |
| 57 | + AnnotationPropertiesFormatter.Builder propertiesFormatterBuilder = AnnotationPropertiesFormatter.configure(); |
| 58 | + config.accept(propertiesFormatterBuilder); |
| 59 | + return new AnnotationFormatter(annotationTypeFormatter, propertiesFormatterBuilder.build()); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + static class AnnotationPropertiesFormatter { |
| 64 | + private final Function<List<String>, String> arrayFormatter; |
| 65 | + private final Function<Class<?>, String> typeFormatter; |
| 66 | + private final Function<String, String> stringFormatter; |
| 67 | + private final boolean omitOptionalIdentifierForSingleElementAnnotations; |
| 68 | + |
| 69 | + private AnnotationPropertiesFormatter(Builder builder) { |
| 70 | + this.arrayFormatter = checkNotNull(builder.arrayFormatter); |
| 71 | + this.typeFormatter = checkNotNull(builder.typeFormatter); |
| 72 | + this.stringFormatter = checkNotNull(builder.stringFormatter); |
| 73 | + this.omitOptionalIdentifierForSingleElementAnnotations = builder.omitOptionalIdentifierForSingleElementAnnotations; |
| 74 | + } |
| 75 | + |
| 76 | + String formatProperties(Map<String, Object> properties) { |
| 77 | + // see Builder#omitOptionalIdentifierForSingleElementAnnotations() for documentation |
| 78 | + if (properties.size() == 1 && properties.containsKey("value") && omitOptionalIdentifierForSingleElementAnnotations) { |
| 79 | + return formatValue(properties.get("value")); |
| 80 | + } |
| 81 | + |
| 82 | + return properties.entrySet().stream() |
| 83 | + .map(entry -> entry.getKey() + "=" + formatValue(entry.getValue())) |
| 84 | + .collect(joining(", ")); |
| 85 | + } |
| 86 | + |
| 87 | + String formatValue(Object input) { |
| 88 | + if (input instanceof Class<?>) { |
| 89 | + return typeFormatter.apply((Class<?>) input); |
| 90 | + } |
| 91 | + if (input instanceof String) { |
| 92 | + return stringFormatter.apply((String) input); |
| 93 | + } |
| 94 | + if (!input.getClass().isArray()) { |
| 95 | + return String.valueOf(input); |
| 96 | + } |
| 97 | + |
| 98 | + List<String> elemToString = IntStream.range(0, Array.getLength(input)) |
| 99 | + .mapToObj(i -> formatValue(Array.get(input, i))) |
| 100 | + .collect(toList()); |
| 101 | + return arrayFormatter.apply(elemToString); |
| 102 | + } |
| 103 | + |
| 104 | + static Builder configure() { |
| 105 | + return new Builder(); |
| 106 | + } |
| 107 | + |
| 108 | + static class Builder { |
| 109 | + private Function<List<String>, String> arrayFormatter; |
| 110 | + private Function<Class<?>, String> typeFormatter; |
| 111 | + private Function<String, String> stringFormatter = identity(); |
| 112 | + private boolean omitOptionalIdentifierForSingleElementAnnotations = false; |
| 113 | + |
| 114 | + Builder formattingArraysWithSquareBrackets() { |
| 115 | + arrayFormatter = input -> "[" + Joiner.on(", ").join(input) + "]"; |
| 116 | + return this; |
| 117 | + } |
| 118 | + |
| 119 | + Builder formattingArraysWithCurlyBrackets() { |
| 120 | + arrayFormatter = input -> "{" + Joiner.on(", ").join(input) + "}"; |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + Builder formattingTypesToString() { |
| 125 | + typeFormatter = String::valueOf; |
| 126 | + return this; |
| 127 | + } |
| 128 | + |
| 129 | + Builder formattingTypesAsClassNames() { |
| 130 | + typeFormatter = input -> input.getName() + ".class"; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + Builder quotingStrings() { |
| 135 | + stringFormatter = input -> "\"" + input + "\""; |
| 136 | + return this; |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Configures that the identifier is omitted if the annotation is a |
| 141 | + * <a href="https://docs.oracle.com/javase/specs/jls/se11/html/jls-9.html#jls-9.7.3">single-element annotation</a> |
| 142 | + * and the identifier of the only element is "value". |
| 143 | + * |
| 144 | + * <ul><li>Example with this configuration: {@code @Copyright("2020 Acme Corporation")}</li> |
| 145 | + * <li>Example without this configuration: {@code @Copyright(value="2020 Acme Corporation")}</li></ul> |
| 146 | + */ |
| 147 | + Builder omitOptionalIdentifierForSingleElementAnnotations() { |
| 148 | + omitOptionalIdentifierForSingleElementAnnotations = true; |
| 149 | + return this; |
| 150 | + } |
| 151 | + |
| 152 | + AnnotationPropertiesFormatter build() { |
| 153 | + return new AnnotationPropertiesFormatter(this); |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | +} |
0 commit comments