Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 1ed90a5

Browse files
author
John Gardiner Myers
committed
Replace AutoValue with record in configuration
1 parent c6b0acf commit 1ed90a5

22 files changed

Lines changed: 223 additions & 166 deletions

configuration/pom.xml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,6 @@
5757
<artifactId>byte-buddy</artifactId>
5858
</dependency>
5959

60-
<dependency>
61-
<groupId>com.google.auto.value</groupId>
62-
<artifactId>auto-value-annotations</artifactId>
63-
<optional>true</optional>
64-
</dependency>
65-
6660
<!-- for testing -->
6761
<dependency>
6862
<groupId>org.testng</groupId>

configuration/src/main/java/com/proofpoint/configuration/ConditionalModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
@Beta
2525
public class ConditionalModule<T>
26-
extends AbstractConfigurationAwareModule
26+
extends AbstractConfigurationAwareModule
2727
{
2828
public static <T> Module installModuleIf(Class<T> config, Predicate<T> predicate, Module module)
2929
{

configuration/src/main/java/com/proofpoint/configuration/ConfigBinder.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,46 +30,45 @@
3030
*
3131
* <pre>
3232
* configBinder(binder).bind(FooConfig.class);</pre>
33-
*
33+
* <p>
3434
* Binds the configuration class {@code FooConfig} to instances that are
3535
* created by the configuration subsystem (specifically its
3636
* {@link ConfigurationFactory}).
37-
*
37+
* <p>
3838
* As configuration classes are mutable, the binding is with default scope
3939
* so each injection point gets a separate instance.
40-
*
40+
* <p>
4141
* Does not work with private binders.
4242
*
4343
* <pre>
4444
* configBinder(binder)
4545
* .bind(FooConfig.class)
4646
* .annotatedWith(Red.class);</pre>
47-
*
47+
* <p>
4848
* Binds the configuration class {@code Key.get(FooConfig.class, Red.class)}
4949
* to instances that are created by the configuration subsystem.
5050
*
5151
* <pre>
5252
* configBinder(binder)
5353
* .bind(FooConfig.class)
5454
* .annotatedWith(Names.named("red"));</pre>
55-
*
55+
* <p>
5656
* Binds the configuration class {@code Key.get(FooConfig.class, Names.named("red"))}
5757
* to instances that are created by the configuration subsystem.
5858
*
5959
* <pre>
6060
* configBinder(binder)
6161
* .bind(FooConfig.class)
6262
* .prefixedWith("prefix");</pre>
63-
*
63+
* <p>
6464
* The {@code .prefixedWith()} method causes the configuration properties that
6565
* the bound configuration instances consume from to be prefixed by the
6666
* specified parameter, followed by a ".". For example, if an attribute of the
6767
* configuration class consumes the "size" configuration property, that
6868
* attribute of the bound instance will instead consume the "prefix.size"
6969
* configuration property.
70-
*
70+
* <p>
7171
* May be combined with {@code .annotatedWith()}.
72-
*
7372
*/
7473
public final class ConfigBinder
7574
{

configuration/src/main/java/com/proofpoint/configuration/ConfigurationAwareProvider.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/**
88
* A provider with access to the Platform {@link ConfigurationFactory}.
9-
*
9+
* <p>
1010
* Implementing this interface ensures that the provider gets access to the
1111
* {@link ConfigurationFactory} before the first
1212
* call to {@link Provider#get()}.
@@ -28,9 +28,9 @@ public interface ConfigurationAwareProvider<T> extends Provider<T>
2828
* objects that this provider will need
2929
*
3030
* @param modules The application modules. The provider may inspect these
31-
* in order to determine which config objects to build.
31+
* in order to determine which config objects to build.
3232
* @throws ConfigurationException when a programming error prevents
33-
* this building.
33+
* this building.
3434
*/
3535
default void buildConfigObjects(Iterable<? extends Module> modules)
3636
{

configuration/src/main/java/com/proofpoint/configuration/ConfigurationFactory.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
import static com.google.common.base.CaseFormat.LOWER_CAMEL;
4848
import static com.google.common.base.CaseFormat.UPPER_CAMEL;
4949
import static com.google.common.collect.Sets.newConcurrentHashSet;
50-
import static com.proofpoint.configuration.ConfigurationIdentity.configurationIdentity;
5150
import static com.proofpoint.configuration.ConfigurationMetadata.getConfigurationMetadata;
5251
import static com.proofpoint.configuration.ConfigurationMetadata.isConfigClass;
5352
import static com.proofpoint.configuration.Problems.exceptionFor;
@@ -142,9 +141,9 @@ public <T> T build(Class<T> configClass, @Nullable String prefix)
142141
* This is used by the configuration provider
143142
*/
144143
<T> T build(Class<T> configClass, @Nullable String prefix, Key<T> key)
145-
{
144+
{
146145
Problems problems;
147-
if (registeredConfigs.add(configurationIdentity(configClass, prefix, key))) {
146+
if (registeredConfigs.add(new ConfigurationIdentity(configClass, prefix, key))) {
148147
problems = new Problems(monitor);
149148
}
150149
else {
@@ -455,7 +454,7 @@ private Object getInjectedValue(AttributeMetadata attribute, InjectionPointMetaD
455454
return finalValue;
456455
}
457456

458-
private <K,V> Map<K, V> getInjectedMap(AttributeMetadata attribute, InjectionPointMetaData injectionPoint, String name, Problems problems, Class<K> keyClass, Class<V> valueClass)
457+
private <K, V> Map<K, V> getInjectedMap(AttributeMetadata attribute, InjectionPointMetaData injectionPoint, String name, Problems problems, Class<K> keyClass, Class<V> valueClass)
459458
{
460459
final boolean valueIsConfigClass = isConfigClass(valueClass);
461460

configuration/src/main/java/com/proofpoint/configuration/ConfigurationFactoryBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public final class ConfigurationFactoryBuilder
3131
private PropertiesBuilder propertiesBuilder = new PropertiesBuilder();
3232
private final Set<String> expectToUse = new HashSet<>();
3333
private Monitor monitor = Problems.NULL_MONITOR;
34-
private Map<String,String> applicationDefaults = ImmutableMap.of();
35-
private Map<String,String> moduleDefaults = ImmutableMap.of();
34+
private Map<String, String> applicationDefaults = ImmutableMap.of();
35+
private Map<String, String> moduleDefaults = ImmutableMap.of();
3636
private Map<String, ConfigurationDefaultingModule> moduleDefaultSource = ImmutableMap.of();
3737

3838
/**

configuration/src/main/java/com/proofpoint/configuration/ConfigurationIdentity.java

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,42 @@
1515
*/
1616
package com.proofpoint.configuration;
1717

18-
import com.google.auto.value.AutoValue;
1918
import com.google.inject.Key;
2019
import jakarta.annotation.Nullable;
2120

22-
@AutoValue
23-
abstract class ConfigurationIdentity<T>
24-
{
25-
private Key<T> key;
21+
import java.util.Objects;
22+
23+
import static java.util.Objects.requireNonNull;
24+
25+
record ConfigurationIdentity<T>(
26+
Class<T> configClass,
27+
28+
@Nullable
29+
String prefix,
2630

27-
static <T> ConfigurationIdentity<T> configurationIdentity(Class<T> clazz, @Nullable String prefix, @Nullable Key<T> key)
31+
// Must not participate in equals()
32+
@Nullable
33+
Key<T> key
34+
)
35+
{
36+
ConfigurationIdentity
2837
{
29-
ConfigurationIdentity<T> identity = new AutoValue_ConfigurationIdentity<>(clazz, prefix);
30-
identity.key = key;
31-
return identity;
38+
requireNonNull(configClass, "configClass is null");
3239
}
3340

34-
abstract Class<T> getConfigClass();
35-
36-
@Nullable
37-
abstract String getPrefix();
41+
@Override
42+
public boolean equals(Object o)
43+
{
44+
if (o == null || getClass() != o.getClass()) {
45+
return false;
46+
}
47+
ConfigurationIdentity<?> that = (ConfigurationIdentity<?>) o;
48+
return Objects.equals(prefix, that.prefix) && Objects.equals(configClass, that.configClass);
49+
}
3850

39-
// Must not participate in equals()
40-
Key<T> getKey()
51+
@Override
52+
public int hashCode()
4153
{
42-
return key;
54+
return Objects.hash(configClass, prefix);
4355
}
4456
}

configuration/src/main/java/com/proofpoint/configuration/ConfigurationInspector.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static class ConfigRecord<T> implements Comparable<ConfigRecord<?>>
8484

8585
static <T> ConfigRecord<T> createConfigRecord(ConfigurationFactory configurationFactory, ConfigurationIdentity<T> configurationIdentity)
8686
{
87-
return new ConfigRecord<>(configurationFactory, configurationIdentity.getConfigClass(), configurationIdentity.getPrefix(), configurationIdentity.getKey());
87+
return new ConfigRecord<>(configurationFactory, configurationIdentity.configClass(), configurationIdentity.prefix(), configurationIdentity.key());
8888
}
8989

9090
private ConfigRecord(ConfigurationFactory configurationFactory, Class<T> configClass, @Nullable String prefix, @Nullable Key<T> key)

configuration/src/main/java/com/proofpoint/configuration/ConfigurationMetadata.java

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ else if (!this.defunctConfig.add(defunct)) {
104104
try {
105105
constructor = configClass.getDeclaredConstructor();
106106
constructor.setAccessible(true);
107-
} catch (Exception e) {
107+
}
108+
catch (Exception e) {
108109
problems.addError("Configuration class [%s] does not have a no-arg constructor", configClass.getName());
109110
}
110111
this.constructor = constructor;
@@ -127,7 +128,8 @@ else if (!this.defunctConfig.add(defunct)) {
127128
}
128129
}
129130

130-
public static boolean isConfigClass(Class<?> classz) {
131+
public static boolean isConfigClass(Class<?> classz)
132+
{
131133
for (Method method : classz.getDeclaredMethods()) {
132134
if (method.isAnnotationPresent(Config.class)) {
133135
return true;
@@ -188,7 +190,8 @@ private boolean validateAnnotations(Method configMethod)
188190
if (arrayEntry == null || arrayEntry.isEmpty()) {
189191
problems.addError("@LegacyConfig method [%s] annotation contains null or empty value", configMethod.toGenericString());
190192
isValid = false;
191-
} else if (arrayEntry.equals(config.value())) {
193+
}
194+
else if (arrayEntry.equals(config.value())) {
192195
problems.addError("@Config property name '%s' appears in @LegacyConfig annotation for method [%s]", config.value(), configMethod.toGenericString());
193196
isValid = false;
194197
}
@@ -343,7 +346,7 @@ private AttributeMetadata buildAttributeMetadata(Class<T> configClass, Method co
343346
}
344347

345348
if (defunctConfig.contains(propertyName)) {
346-
problems.addError("@Config property '%s' on method [%s] is defunct on class [%s]", propertyName, configMethod, configClass);
349+
problems.addError("@Config property '%s' on method [%s] is defunct on class [%s]", propertyName, configMethod, configClass);
347350
}
348351

349352
// Add the injection point for the current setter/property
@@ -364,12 +367,18 @@ private AttributeMetadata buildAttributeMetadata(Class<T> configClass, Method co
364367
@Override
365368
public boolean equals(Object o)
366369
{
367-
if (this == o) return true;
368-
if (o == null || getClass() != o.getClass()) return false;
370+
if (this == o) {
371+
return true;
372+
}
373+
if (o == null || getClass() != o.getClass()) {
374+
return false;
375+
}
369376

370377
ConfigurationMetadata<?> that = (ConfigurationMetadata<?>) o;
371378

372-
if (!configClass.equals(that.configClass)) return false;
379+
if (!configClass.equals(that.configClass)) {
380+
return false;
381+
}
373382

374383
return true;
375384
}
@@ -453,13 +462,21 @@ public boolean isLegacy()
453462
@Override
454463
public boolean equals(Object o)
455464
{
456-
if (this == o) return true;
457-
if (o == null || getClass() != o.getClass()) return false;
465+
if (this == o) {
466+
return true;
467+
}
468+
if (o == null || getClass() != o.getClass()) {
469+
return false;
470+
}
458471

459472
InjectionPointMetaData that = (InjectionPointMetaData) o;
460473

461-
if (!configClass.equals(that.configClass)) return false;
462-
if (!property.equals(that.property)) return false;
474+
if (!configClass.equals(that.configClass)) {
475+
return false;
476+
}
477+
if (!property.equals(that.property)) {
478+
return false;
479+
}
463480

464481
return true;
465482
}
@@ -497,7 +514,7 @@ public static class AttributeMetadata
497514
private final MapClasses mapClasses;
498515

499516
private AttributeMetadata(Class<?> configClass, String name, String description, boolean securitySensitive, @Nullable MapClasses mapClasses, Method getter,
500-
InjectionPointMetaData injectionPoint, Set<InjectionPointMetaData> legacyInjectionPoints)
517+
InjectionPointMetaData injectionPoint, Set<InjectionPointMetaData> legacyInjectionPoints)
501518
{
502519
requireNonNull(configClass);
503520
requireNonNull(name);
@@ -559,13 +576,21 @@ public Set<InjectionPointMetaData> getLegacyInjectionPoints()
559576
@Override
560577
public boolean equals(Object o)
561578
{
562-
if (this == o) return true;
563-
if (o == null || getClass() != o.getClass()) return false;
579+
if (this == o) {
580+
return true;
581+
}
582+
if (o == null || getClass() != o.getClass()) {
583+
return false;
584+
}
564585

565586
AttributeMetadata that = (AttributeMetadata) o;
566587

567-
if (!configClass.equals(that.configClass)) return false;
568-
if (!name.equals(that.name)) return false;
588+
if (!configClass.equals(that.configClass)) {
589+
return false;
590+
}
591+
if (!name.equals(that.name)) {
592+
return false;
593+
}
569594

570595
return true;
571596
}
@@ -675,7 +700,7 @@ private static Collection<Method> findSensitiveConfigMethods(Class<?> configClas
675700
*
676701
* @param configClass the class to analyze
677702
* @return a map that associates a concrete method to the actual method tagged
678-
* (which may belong to a different class in class hierarchy)
703+
* (which may belong to a different class in class hierarchy)
679704
*/
680705
private static Collection<Method> findAnnotatedMethods(Class<?> configClass, Class<? extends java.lang.annotation.Annotation> annotation)
681706
{
@@ -711,7 +736,8 @@ public static Method findAnnotatedMethod(Class<?> configClass, Class<? extends j
711736
if (method != null && method.isAnnotationPresent(annotation)) {
712737
return method;
713738
}
714-
} catch (NoSuchMethodException e) {
739+
}
740+
catch (NoSuchMethodException e) {
715741
// ignore
716742
}
717743

@@ -747,29 +773,32 @@ private Set<InjectionPointMetaData> findLegacySetters(Class<?> configClass, Stri
747773
if (validateSetter(method)) {
748774
for (String property : method.getAnnotation(LegacyConfig.class).value()) {
749775
if (defunctConfig.contains(property)) {
750-
problems.addError("@LegacyConfig property '%s' on method [%s] is defunct on class [%s]", property, method, configClass);
776+
problems.addError("@LegacyConfig property '%s' on method [%s] is defunct on class [%s]", property, method, configClass);
751777
}
752778

753779
if (!property.equals(propertyName)) {
754780
setters.add(InjectionPointMetaData.newLegacy(configClass, property, method));
755-
} else {
781+
}
782+
else {
756783
problems.addError("@LegacyConfig property '%s' on method [%s] is replaced by @Config property of same name on method [%s]",
757784
property, method.toGenericString(), setterName);
758785
}
759786
}
760787
}
761-
} else if (method.isAnnotationPresent(LegacyConfig.class)
788+
}
789+
else if (method.isAnnotationPresent(LegacyConfig.class)
762790
&& method.getAnnotation(LegacyConfig.class).replacedBy().equals(propertyName)) {
763791
// Found @LegacyConfig setter linked by replacedBy() property
764792
if (validateSetter(method)) {
765793
for (String property : method.getAnnotation(LegacyConfig.class).value()) {
766794
if (defunctConfig.contains(property)) {
767-
problems.addError("@LegacyConfig property '%s' on method [%s] is defunct on class [%s]", property, method, configClass);
795+
problems.addError("@LegacyConfig property '%s' on method [%s] is defunct on class [%s]", property, method, configClass);
768796
}
769797

770798
if (!property.equals(propertyName)) {
771799
setters.add(InjectionPointMetaData.newLegacy(configClass, property, method));
772-
} else {
800+
}
801+
else {
773802
problems.addError("@LegacyConfig property '%s' on method [%s] is replaced by @Config property of same name on method [%s]",
774803
property, method.toGenericString(), setterName);
775804
}
@@ -795,7 +824,8 @@ private Method findGetter(Class<?> configClass, Method configMethod, String attr
795824
if (method.getName().equals(getterName) || method.getName().equals(isName)) {
796825
if (isUsableMethod(method) && !method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 0) {
797826
getters.add(method);
798-
} else {
827+
}
828+
else {
799829
unusableGetters.add(method);
800830
}
801831
}

0 commit comments

Comments
 (0)