Skip to content

Commit

Permalink
Include mappings nested super types metadata (#1123)
Browse files Browse the repository at this point in the history
  • Loading branch information
radcortez authored Mar 1, 2024
1 parent cb91e59 commit 17c02e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -138,9 +141,9 @@ String getClassInternalName() {
}

List<ConfigMappingInterface> getNested() {
ArrayList<ConfigMappingInterface> nested = new ArrayList<>();
Set<ConfigMappingInterface> nested = new LinkedHashSet<>();
getNested(properties, nested);
return nested;
return new ArrayList<>(nested);
}

public byte[] getClassBytes() {
Expand Down Expand Up @@ -954,13 +957,14 @@ private static String getPropertyName(final AnnotatedElement element) {
}
}

private static void getNested(final Property[] properties, final List<ConfigMappingInterface> nested) {
private static void getNested(final Property[] properties, final Set<ConfigMappingInterface> nested) {
for (Property property : properties) {
if (property instanceof GroupProperty) {
GroupProperty groupProperty = (GroupProperty) property;
ConfigMappingInterface group = groupProperty.getGroupType();
nested.add(group);
getNested(group.properties, nested);
Collections.addAll(nested, group.superTypes);
getNested(group.getProperties(true), nested);
}

if (property instanceof OptionalProperty) {
Expand All @@ -969,7 +973,8 @@ private static void getNested(final Property[] properties, final List<ConfigMapp
GroupProperty groupProperty = (GroupProperty) optionalProperty.getNestedProperty();
ConfigMappingInterface group = groupProperty.getGroupType();
nested.add(group);
getNested(group.properties, nested);
Collections.addAll(nested, group.superTypes);
getNested(group.getProperties(true), nested);
} else if (optionalProperty.getNestedProperty() instanceof CollectionProperty) {
CollectionProperty collectionProperty = (CollectionProperty) optionalProperty.getNestedProperty();
getNested(new Property[] { collectionProperty.element }, nested);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

Expand All @@ -24,9 +25,9 @@ protected ConfigMappingObjectHolder computeValue(final Class<?> type) {
}
};

public static List<ConfigMappingMetadata> getConfigMappingsMetadata(Class<?> type) {
final List<ConfigMappingMetadata> mappings = new ArrayList<>();
final ConfigMappingInterface configurationInterface = ConfigMappingInterface.getConfigurationInterface(type);
public static List<ConfigMappingMetadata> getConfigMappingsMetadata(final Class<?> type) {
List<ConfigMappingMetadata> mappings = new ArrayList<>();
ConfigMappingInterface configurationInterface = ConfigMappingInterface.getConfigurationInterface(type);
if (configurationInterface != null) {
mappings.add(configurationInterface);
mappings.addAll(configurationInterface.getNested());
Expand All @@ -35,12 +36,12 @@ public static List<ConfigMappingMetadata> getConfigMappingsMetadata(Class<?> typ
mappings.addAll(superType.getNested());
}
}
final ConfigMappingClass configMappingClass = ConfigMappingClass.getConfigurationClass(type);
ConfigMappingClass configMappingClass = ConfigMappingClass.getConfigurationClass(type);
if (configMappingClass != null) {
mappings.add(configMappingClass);
mappings.addAll(getConfigMappingsMetadata(getConfigMapping(type).getInterfaceType()));
}
return mappings;
return List.copyOf(mappings);
}

public static ConfigMappingInterface getConfigMapping(final Class<?> type) {
Expand All @@ -58,7 +59,7 @@ static Class<?> getConfigMappingClass(final Class<?> type) {
}
}

static <T> T configMappingObject(Class<T> interfaceType, ConfigMappingContext configMappingContext) {
static <T> T configMappingObject(final Class<T> interfaceType, final ConfigMappingContext configMappingContext) {
ConfigMappingObject instance;
try {
Constructor<? extends ConfigMappingObject> constructor = CACHE.get(interfaceType).getImplementationClass()
Expand All @@ -83,7 +84,7 @@ static <T> T configMappingObject(Class<T> interfaceType, ConfigMappingContext co
}

@SuppressWarnings("unchecked")
public static <T> Class<? extends ConfigMappingObject> getImplementationClass(Class<T> type) {
public static <T> Class<? extends ConfigMappingObject> getImplementationClass(final Class<T> type) {
try {
Class<?> implementationClass = type.getClassLoader().loadClass(type.getName() + type.getName().hashCode() + "Impl");
if (type.isAssignableFrom(implementationClass)) {
Expand Down Expand Up @@ -146,7 +147,7 @@ private static Class<?> defineClass(final Class<?> parent, final String classNam
return ClassDefiner.defineClass(LOOKUP, parent, className, classBytes);
}

private static Object getClassLoaderLock(String className) {
private static Object getClassLoaderLock(final String className) {
return classLoaderLocks.computeIfAbsent(className, c -> new Object());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,4 +226,32 @@ void parentNested() {
assertTrue(mappings.contains(ServerParent.class));
assertTrue(mappings.contains(ServerParent.ServerParentNested.class));
}

interface MyConfig {
GlobalConfig global();

interface CommonConfig {
Optional<String> commonTest();

UserConfig user();
}

interface GlobalConfig extends CommonConfig {
Optional<String> globalTest();
}

interface UserConfig {
Optional<String> name();
}
}

@Test
void nestedParents() {
List<ConfigMappingMetadata> mappingsMetadata = ConfigMappingLoader.getConfigMappingsMetadata(MyConfig.class);
List<Class<?>> mappings = mappingsMetadata.stream().map(ConfigMappingMetadata::getInterfaceType).collect(toList());
assertTrue(mappings.contains(MyConfig.class));
assertTrue(mappings.contains(MyConfig.GlobalConfig.class));
assertTrue(mappings.contains(MyConfig.CommonConfig.class));
assertTrue(mappings.contains(MyConfig.UserConfig.class));
}
}

0 comments on commit 17c02e6

Please sign in to comment.