szetszwo commented on code in PR #12128:
URL: https://github.com/apache/ozone/pull/12128#discussion_r4669647012
##########
hadoop-hdds/config/src/main/java/org/apache/hadoop/hdds/conf/ConfigurationReflectionUtil.java:
##########
@@ -240,43 +242,37 @@ public static <T> void
updateConfiguration(ConfigurationTarget config, T configO
}
}
- public static Optional<String> getDefaultValue(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::defaultValue);
- }
+ public static Optional<Config> getConfigByKey(Class<?> configClass, String
key) {
+ for (Class<?> clazz = configClass; clazz != null;) {
+ final List<Config> configs = Stream.of(clazz.getDeclaredFields())
+ .map(f -> f.getAnnotation(Config.class))
+ .filter(c -> c != null && c.key().equals(key))
+ .collect(Collectors.toList());
- public static Optional<String> getKey(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::key);
- }
+ final int size = configs.size();
+ if (size == 1) {
+ return Optional.of(configs.get(0));
+ } else if (size > 1) {
+ throw new IllegalStateException("Duplicated key \"" + key + "\": found
" + size + " fields in " + configClass);
+ }
- public static Optional<ConfigType> getType(Class<?> configClass,
- String fieldName) {
- return findFieldConfigAnnotationByName(configClass, fieldName)
- .map(Config::type);
+ clazz = clazz.getSuperclass();
+ if (Object.class.equals(clazz)) {
+ break;
+ }
+ }
+ return Optional.empty();
}
- private static Optional<Config> findFieldConfigAnnotationByName(
- final Class<?> configClass, String fieldName) {
- Class<?> theClass = configClass;
- while (theClass != null) {
- Optional<Config> config = Stream.of(theClass.getDeclaredFields())
- .filter(f -> f.getName().equals(fieldName))
- .findFirst()
- .map(f -> f.getAnnotation(Config.class));
-
- if (config.isPresent()) {
- return config;
+ public static void assertNoDuplicatedKeys(Class<?> configClass) {
Review Comment:
No. We should add testNoDuplicatedKeys tests for the other classes using @
Config.
I originally put this method in TestConfigurationReflectionUtil but
TestOzoneClientConfig cannot see it since it does not have maven dependency.
So, I moved it in ConfigurationReflectionUtil. Do you think
- Is it okay?
- Or we should move it to somewhere else?
- Or we should add maven dependency?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]