From 8aa18855275cb1ddfcb0fc282c36bfa1d7770589 Mon Sep 17 00:00:00 2001 From: Holger Reise Date: Mon, 12 Jun 2023 19:03:38 +0200 Subject: [PATCH] CDR-912 fix DtoFromCompositionWalker::extractAttribute Use Introspector::getBeanInfo instead of manually creating PropertyDescriptors --- .../flattener/DtoFromCompositionWalker.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/client/src/main/java/org/ehrbase/client/flattener/DtoFromCompositionWalker.java b/client/src/main/java/org/ehrbase/client/flattener/DtoFromCompositionWalker.java index 3906b2170..c12c89a69 100644 --- a/client/src/main/java/org/ehrbase/client/flattener/DtoFromCompositionWalker.java +++ b/client/src/main/java/org/ehrbase/client/flattener/DtoFromCompositionWalker.java @@ -22,11 +22,18 @@ import com.nedap.archie.rm.datatypes.CodePhrase; import com.nedap.archie.rm.support.identification.ObjectId; import java.beans.IntrospectionException; +import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.ParameterizedType; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; import java.util.function.Function; import java.util.stream.Collectors; import org.apache.commons.lang3.reflect.FieldUtils; @@ -178,9 +185,12 @@ private void writeField(Field field, Object dto, Object value) { private Object extractAttribute(Object dto, String attributeName) { try { - PropertyDescriptor propertyDescriptor = new PropertyDescriptor(attributeName, dto.getClass()); - - return propertyDescriptor.getReadMethod().invoke(dto); + for (PropertyDescriptor p : Introspector.getBeanInfo(dto.getClass()).getPropertyDescriptors()) { + if (p.getName().equals(attributeName)) { + return p.getReadMethod().invoke(dto); + } + } + throw new ClientException("Unknown property %s for type %s".formatted(attributeName, dto.getClass())); } catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) { throw new ClientException(e.getMessage(), e); }