test_class.file: integration/client-cli/client-registration-cli/src/test/java/org/keycloak/client/registration/cli/util/ReflectionUtilTest.java
test_class.identifier: ReflectionUtilTest
test_case.body: @Test
      public void testSettingAttibutes() {
          Data data = new Data();
  
          LinkedList<AttributeOperation> attrs = new LinkedList<>();
  
          attrs.add(new AttributeOperation(SET, "longAttr", "42"));
          attrs.add(new AttributeOperation(SET, "strAttr", "not null"));
          attrs.add(new AttributeOperation(SET, "strList+", "two"));
          attrs.add(new AttributeOperation(SET, "strList+", "three"));
          attrs.add(new AttributeOperation(SET, "strList[0]+", "one"));
          attrs.add(new AttributeOperation(SET, "config", "{\"key1\": \"value1\"}"));
          attrs.add(new AttributeOperation(SET, "config.key2", "value2"));
          attrs.add(new AttributeOperation(SET, "nestedConfig", "{\"key1\": {\"sub key1\": \"sub value1\"}}"));
          attrs.add(new AttributeOperation(SET, "nestedConfig.key1.\"sub key2\"", "sub value2"));
          attrs.add(new AttributeOperation(SET, "nested.strList", "[1,2,3,4]"));
          attrs.add(new AttributeOperation(SET, "nested.dataList+", "{\"baseAttr\": \"item1\", \"strList\": [\"confidential\", \"public\"]}"));
          attrs.add(new AttributeOperation(SET, "nested.dataList+", "{\"baseAttr\": \"item2\", \"strList\": [\"external\"]}"));
          attrs.add(new AttributeOperation(SET, "nested.dataList[1].baseAttr", "changed item2"));
          attrs.add(new AttributeOperation(SET, "nested.nested.strList", "[\"first\",\"second\"]"));
          attrs.add(new AttributeOperation(DELETE, "nested.strList[1]"));
          attrs.add(new AttributeOperation(SET, "nested.nested.nested", "{\"baseAttr\": \"NEW VALUE\", \"strList\": [true, false]}"));
          attrs.add(new AttributeOperation(SET, "nested.strAttr", "NOT NULL"));
          attrs.add(new AttributeOperation(DELETE, "nested.strAttr"));
  
          ReflectionUtil.setAttributes(data, attrs);
  
          Assert.assertEquals("longAttr", Long.valueOf(42), data.getLongAttr());
          Assert.assertEquals("strAttr", "not null", data.getStrAttr());
          Assert.assertEquals("strList", Arrays.asList("one", "two", "three"), data.getStrList());
  
          Map<String, String> expectedMap = new HashMap<>();
          expectedMap.put("key1", "value1");
          expectedMap.put("key2", "value2");
          Assert.assertEquals("config", expectedMap, data.getConfig());
  
  
          expectedMap = new HashMap<>();
          expectedMap.put("sub key1", "sub value1");
          expectedMap.put("sub key2", "sub value2");
  
          Assert.assertNotNull("nestedConfig", data.getNestedConfig());
          Assert.assertEquals("nestedConfig has one element", 1, data.getNestedConfig().size());
          Assert.assertEquals("nestedConfig.key1", expectedMap, data.getNestedConfig().get("key1"));
  
  
          Data nested = data.getNested();
          Assert.assertEquals("nested.strAttr", null, nested.getStrAttr());
          Assert.assertEquals("nested.strList", Arrays.asList("1", "3", "4"), nested.getStrList());
          Assert.assertEquals("nested.dataList[0].baseAttr", "item1", nested.getDataList().get(0).getBaseAttr());
          Assert.assertEquals("nested.dataList[0].strList", Arrays.asList("confidential", "public"), nested.getDataList().get(0).getStrList());
          Assert.assertEquals("nested.dataList[1].baseAttr", "changed item2", nested.getDataList().get(1).getBaseAttr());
          Assert.assertEquals("nested.dataList[1].strList", Arrays.asList("external"), nested.getDataList().get(1).getStrList());
  
          nested = nested.getNested();
          Assert.assertEquals("nested.nested.strList", Arrays.asList("first", "second"), nested.getStrList());
  
          nested = nested.getNested();
          Assert.assertEquals("nested.nested.nested.baseAttr", "NEW VALUE", nested.getBaseAttr());
          Assert.assertEquals("nested.nested.nested.strList", Arrays.asList("true", "false"), nested.getStrList());
      }
test_case.class_method_signature: ReflectionUtilTest.testSettingAttibutes()
test_case.full_signature: @Test public void testSettingAttibutes()
test_case.identifier: testSettingAttibutes
test_case.invocations: add add add add add add add add add add add add add add add add add add setAttributes assertEquals valueOf getLongAttr assertEquals getStrAttr assertEquals asList getStrList put put assertEquals getConfig put put assertNotNull getNestedConfig assertEquals size getNestedConfig assertEquals get getNestedConfig getNested assertEquals getStrAttr assertEquals asList getStrList assertEquals getBaseAttr get getDataList assertEquals asList getStrList get getDataList assertEquals getBaseAttr get getDataList assertEquals asList getStrList get getDataList getNested assertEquals asList getStrList getNested assertEquals getBaseAttr assertEquals asList getStrList
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testSettingAttibutes()
focal_class.declarator: index = new HashMap<>()
focal_class.modifier: static
focal_class.original_string: static Map<Class, Map<String, Field>> index = new HashMap<>();
focal_class.type: Map<Class, Map<String, Field>>
focal_class.var_name: index
focal_class.file: integration/client-cli/client-registration-cli/src/main/java/org/keycloak/client/registration/cli/util/ReflectionUtil.java
focal_class.identifier: ReflectionUtil
focal_class.class_method_signature: ReflectionUtil.populateAttributesIndex(Class type)
focal_class.full_signature: static void populateAttributesIndex(Class type)
focal_class.identifier: populateAttributesIndex
focal_class.modifiers: static
focal_class.parameters: (Class type)
focal_class.return: void
focal_class.signature: void populateAttributesIndex(Class type)
focal_class.class_method_signature: ReflectionUtil.getAttrFieldsForType(Type gtype)
focal_class.full_signature: public static Map<String, Field> getAttrFieldsForType(Type gtype)
focal_class.identifier: getAttrFieldsForType
focal_class.modifiers: public static
focal_class.parameters: (Type gtype)
focal_class.return: Map<String, Field>
focal_class.signature: Map<String, Field> getAttrFieldsForType(Type gtype)
focal_class.class_method_signature: ReflectionUtil.isListType(Class type)
focal_class.full_signature: public static boolean isListType(Class type)
focal_class.identifier: isListType
focal_class.modifiers: public static
focal_class.parameters: (Class type)
focal_class.return: boolean
focal_class.signature: boolean isListType(Class type)
focal_class.class_method_signature: ReflectionUtil.isBasicType(Type type)
focal_class.full_signature: public static boolean isBasicType(Type type)
focal_class.identifier: isBasicType
focal_class.modifiers: public static
focal_class.parameters: (Type type)
focal_class.return: boolean
focal_class.signature: boolean isBasicType(Type type)
focal_class.class_method_signature: ReflectionUtil.isMapType(Class type)
focal_class.full_signature: public static boolean isMapType(Class type)
focal_class.identifier: isMapType
focal_class.modifiers: public static
focal_class.parameters: (Class type)
focal_class.return: boolean
focal_class.signature: boolean isMapType(Class type)
focal_class.class_method_signature: ReflectionUtil.convertValueToType(Object value, Class<?> type)
focal_class.full_signature: public static Object convertValueToType(Object value, Class<?> type)
focal_class.identifier: convertValueToType
focal_class.modifiers: public static
focal_class.parameters: (Object value, Class<?> type)
focal_class.return: Object
focal_class.signature: Object convertValueToType(Object value, Class<?> type)
focal_class.class_method_signature: ReflectionUtil.setAttributes(Object client, List<AttributeOperation> attrs)
focal_class.full_signature: public static void setAttributes(Object client, List<AttributeOperation> attrs)
focal_class.identifier: setAttributes
focal_class.modifiers: public static
focal_class.parameters: (Object client, List<AttributeOperation> attrs)
focal_class.return: void
focal_class.signature: void setAttributes(Object client, List<AttributeOperation> attrs)
focal_class.class_method_signature: ReflectionUtil.createNewObject(Class type)
focal_class.full_signature: private static Object createNewObject(Class type)
focal_class.identifier: createNewObject
focal_class.modifiers: private static
focal_class.parameters: (Class type)
focal_class.return: Object
focal_class.signature: Object createNewObject(Class type)
focal_class.class_method_signature: ReflectionUtil.createNewList(Class type)
focal_class.full_signature: public static List createNewList(Class type)
focal_class.identifier: createNewList
focal_class.modifiers: public static
focal_class.parameters: (Class type)
focal_class.return: List
focal_class.signature: List createNewList(Class type)
focal_class.class_method_signature: ReflectionUtil.convertValueToList(String value, Class itemType)
focal_class.full_signature: public static List convertValueToList(String value, Class itemType)
focal_class.identifier: convertValueToList
focal_class.modifiers: public static
focal_class.parameters: (String value, Class itemType)
focal_class.return: List
focal_class.signature: List convertValueToList(String value, Class itemType)
focal_class.class_method_signature: ReflectionUtil.merge(T source, T dest)
focal_class.full_signature: public static void merge(T source, T dest)
focal_class.identifier: merge
focal_class.modifiers: public static
focal_class.parameters: (T source, T dest)
focal_class.return: void
focal_class.signature: void merge(T source, T dest)
focal_class.class_method_signature: ReflectionUtil.getAttributeListWithJSonTypes(Class type, AttributeKey attr)
focal_class.full_signature: public static LinkedHashMap<String, String> getAttributeListWithJSonTypes(Class type, AttributeKey attr)
focal_class.identifier: getAttributeListWithJSonTypes
focal_class.modifiers: public static
focal_class.parameters: (Class type, AttributeKey attr)
focal_class.return: LinkedHashMap<String, String>
focal_class.signature: LinkedHashMap<String, String> getAttributeListWithJSonTypes(Class type, AttributeKey attr)
focal_class.class_method_signature: ReflectionUtil.resolveField(Class type, AttributeKey attr)
focal_class.full_signature: public static Field resolveField(Class type, AttributeKey attr)
focal_class.identifier: resolveField
focal_class.modifiers: public static
focal_class.parameters: (Class type, AttributeKey attr)
focal_class.return: Field
focal_class.signature: Field resolveField(Class type, AttributeKey attr)
focal_class.class_method_signature: ReflectionUtil.getTypeString(Type type, Field field)
focal_class.full_signature: public static String getTypeString(Type type, Field field)
focal_class.identifier: getTypeString
focal_class.modifiers: public static
focal_class.parameters: (Type type, Field field)
focal_class.return: String
focal_class.signature: String getTypeString(Type type, Field field)
focal_method.body: public static void setAttributes(Object client, List<AttributeOperation> attrs) {
  
          for (AttributeOperation item: attrs) {
  
              AttributeKey attr = item.getKey();
              Object nested = client;
  
              List<AttributeKey.Component> cs = attr.getComponents();
              for (int i = 0; i < cs.size(); i++) {
                  AttributeKey.Component c = cs.get(i);
  
                  Class type = nested.getClass();
                  Field field = null;
  
                  if (!isMapType(type)) {
                      Map<String, Field> fields = getAttrFieldsForType(type);
                      if (fields == null) {
                          throw new AttributeException(attr.toString(), "Unexpected condition - unknown type: " + type);
                      }
  
                      field = fields.get(c.getName());
                      Class parent = type;
                      while (field == null) {
                          parent = parent.getSuperclass();
                          if (parent == Object.class) {
                              throw new AttributeException(attr.toString(), "Unknown attribute '" + c.getName() + "' on " + client.getClass());
                          }
  
                          fields = getAttrFieldsForType(parent);
                          field = fields.get(c.getName());
                      }
                  }
                  // if it's a 'basic' type we directly use setter
                  type = field == null ? type : field.getType();
                  if (isBasicType(type)) {
                      if (i < cs.size() - 1) {
                          throw new AttributeException(attr.toString(), "Attribute is of primitive type, and can't be nested further: " + c);
                      }
  
                      try {
                          Object val = convertValueToType(item.getValue(), type);
                          field.set(nested, val);
                      } catch (Exception e) {
                          throw new AttributeException(attr.toString(), "Failed to set attribute " + attr, e);
                      }
                  } else if (isListType(type)) {
                      if (i < cs.size() -1) {
                          // not the target component
                          try {
                              nested = field.get(nested);
                          } catch (Exception e) {
                              throw new AttributeException(attr.toString(), "Failed to get attribute \"" + c + "\" in " + attr, e);
                          }
                          if (c.getIndex() >= 0) {
                              // list item
                              // get idx-th item
                              List l = (List) nested;
                              if (c.getIndex() >= l.size()) {
                                  throw new AttributeException(attr.toString(), "Array index out of bounds for \"" + c + "\" in " + attr);
                              }
                              nested = l.get(c.getIndex());
                          }
                      } else {
                          // target component
                          Class itype = type;
                          Type gtype = field.getGenericType();
                          if (gtype instanceof ParameterizedType) {
                              Type[] typeArgs = ((ParameterizedType) gtype).getActualTypeArguments();
                              if (typeArgs.length >= 1 && typeArgs[0] instanceof Class) {
                                  itype = (Class) typeArgs[0];
                              } else {
                                  itype = String.class;
                              }
                          }
                          if (c.getIndex() >= 0 || attr.isAppend()) {
                              // some list item
                              // get the list first
                              List target;
                              try {
                                  target = (List) field.get(nested);
                              } catch (Exception e) {
                                  throw new AttributeException(attr.toString(), "Failed to get list attribute: " + attr, e);
                              }
  
                              // now replace or add idx-th item
                              if (target == null) {
                                  target = createNewList(type);
                                  try {
                                      field.set(nested, target);
                                  } catch (Exception e) {
                                      throw new AttributeException(attr.toString(), "Failed to set list attribute " + attr, e);
                                  }
                              }
                              if (c.getIndex() >= target.size()) {
                                  throw new AttributeException(attr.toString(), "Array index out of bounds for \"" + c + "\" in " + attr);
                              }
  
                              if (attr.isAppend()) {
                                  try {
                                      Object value = convertValueToType(item.getValue(), itype);
                                      if (c.getIndex() >= 0) {
                                          target.add(c.getIndex(), value);
                                      } else {
                                          target.add(value);
                                      }
                                  } catch (Exception e) {
                                      throw new AttributeException(attr.toString(), "Failed to set list attribute " + attr, e);
                                  }
  
                              } else {
                                  if (item.getType() == AttributeOperation.Type.SET) {
                                      try {
                                          Object value = convertValueToType(item.getValue(), itype);
                                          target.set(c.getIndex(), value);
                                      } catch (Exception e) {
                                          throw new AttributeException(attr.toString(), "Failed to set list attribute " + attr, e);
                                      }
                                  } else {
                                      try {
                                          target.remove(c.getIndex());
                                      } catch (Exception e) {
                                          throw new AttributeException(attr.toString(), "Failed to remove list attribute " + attr, e);
                                      }
                                  }
                              }
  
                          } else {
                              // set the whole list field itself
                              List value = createNewList(type);;
                              if (item.getType() == AttributeOperation.Type.SET) {
                                  List converted = convertValueToList(item.getValue(), itype);
                                  value.addAll(converted);
                              }
                              try {
                                  field.set(nested, value);
                              } catch (Exception e) {
                                  throw new AttributeException(attr.toString(), "Failed to set list attribute " + attr, e);
                              }
                          }
                      }
                  } else {
                      // object type
                      if (i < cs.size() -1) {
                          // not the target component
                          Object value;
                          if (field == null) {
                              if (isMapType(nested.getClass())) {
                                  value = ((Map) nested).get(c.getName());
                              } else {
                                  throw new RuntimeException("Unexpected condition while processing: " + attr);
                              }
                          } else {
                              try {
                                  value = field.get(nested);
                              } catch (Exception e) {
                                  throw new AttributeException(attr.toString(), "Failed to get attribute \"" + c + "\" in " + attr, e);
                              }
                          }
                          if (value == null) {
                              // create the target attribute
                              if (isMapType(nested.getClass())) {
                                  throw new RuntimeException("Creating nested object trees not supported");
                              } else {
                                  try {
                                      value = createNewObject(type);
                                      field.set(nested, value);
                                  } catch (Exception e) {
                                      throw new AttributeException(attr.toString(), "Failed to set attribute " + attr, e);
                                  }
                              }
                          }
                          nested = value;
                      } else {
                          // target component
                          // todo implement map put
                          if (isMapType(nested.getClass())) {
                              try {
                                  ((Map) nested).put(c.getName(), item.getValue());
                              } catch (Exception e) {
                                  throw new AttributeException(attr.toString(), "Failed to set map key " + attr, e);
                              }
                          } else {
                              try {
                                  Object value = convertValueToType(item.getValue(), type);
                                  field.set(nested, value);
                              } catch (Exception e) {
                                  throw new AttributeException(attr.toString(), "Failed to set attribute " + attr, e);
                              }
                          }
                      }
                  }
              }
          }
      }
focal_method.class_method_signature: ReflectionUtil.setAttributes(Object client, List<AttributeOperation> attrs)
focal_method.full_signature: public static void setAttributes(Object client, List<AttributeOperation> attrs)
focal_method.identifier: setAttributes
focal_method.invocations: getKey getComponents size get getClass isMapType getAttrFieldsForType toString get getName getSuperclass toString getName getClass getAttrFieldsForType get getName getType isBasicType size toString convertValueToType getValue set toString isListType size get toString getIndex getIndex size toString get getIndex getGenericType getActualTypeArguments getIndex isAppend get toString createNewList set toString getIndex size toString isAppend convertValueToType getValue getIndex add getIndex add toString getType convertValueToType getValue set getIndex toString remove getIndex toString createNewList getType convertValueToList getValue addAll set toString size isMapType getClass get getName get toString isMapType getClass createNewObject set toString isMapType getClass put getName getValue toString convertValueToType getValue set toString
focal_method.modifiers: public static
focal_method.parameters: (Object client, List<AttributeOperation> attrs)
focal_method.return: void
focal_method.signature: void setAttributes(Object client, List<AttributeOperation> attrs)