reflectionutils.makeaccessible
时间: 2023-04-13 08:04:57 浏览: 424
reflectionutils.makeaccessible是Spring框架中的一个工具类方法,用于设置Java反射对象的可访问性。该方法可以使得原本无法访问的私有成员变量或方法变得可访问,从而可以进行操作。
相关问题
List<R> insertList = insertLists.stream().map(e -> { R clone = (R) SerializationUtils.clone((Serializable) saveObject); ReflectionUtils.makeAccessible(coverField); ReflectionUtils.setField(coverField, clone, e); return clone; }).collect(Collectors.toList());
这段代码使用了 Java 8 中的 Stream API,对一个 insertLists 列表进行了映射和转换,最终生成了一个新的 List<R> 类型的列表 insertList。具体来说:
1. 使用 stream() 方法将 insertLists 转换为一个 Stream 对象。
2. 使用 map() 方法对 Stream 中的每一个元素进行转换,转换的逻辑在 lambda 表达式中实现。对于每一个元素 e,首先使用 SerializationUtils.clone() 方法进行深拷贝,生成一个新的对象 clone;然后使用 ReflectionUtils.makeAccessible() 方法和 ReflectionUtils.setField() 方法,将 e 对象的属性值设置到 clone 对应的属性上;最后返回这个新的对象 clone。
3. 使用 collect() 方法将转换后的 Stream 对象收集为一个新的 List<R> 类型的列表 insertList。
总的来说,这段代码的作用是将 insertLists 列表中的每个元素转换为一个新的 R 类型的对象,并将其中的属性值设置到新对象中,最终生成一个新的列表。
public void initialize(InvoiceEnum constraintAnnotation) { Class<?> enumClazz = constraintAnnotation.value(); Object[] enumConstants = enumClazz.getEnumConstants(); if (null == enumConstants) { return; } Method method; try { method = enumClazz.getMethod(constraintAnnotation.method()); } catch (Exception e) { logger.error("获取枚举类方法异常",e); throw new BusinessException(CommonEnums.BUSINESS_ERROR, "枚举未找到方法" + constraintAnnotation.method()); } if (null == method) { throw new BusinessException(CommonEnums.BUSINESS_ERROR, "枚举未找到方法" + constraintAnnotation.method()); } ReflectionUtils.makeAccessible(method); try { for (Object enumConstant : enumConstants) { values.add(method.invoke(enumConstant).toString()); } values.add(""); } catch (Exception e) { throw new BusinessException(CommonEnums.BUSINESS_ERROR, "获取枚举值失败"); } }
这段代码是一个方法的实现,根据传入的注解参数来初始化一些枚举值。让我解释一下这段代码的功能:
1. 首先,从注解参数中获取枚举类型的 Class 对象。
2. 接着,通过反射获取枚举类中的所有枚举常量。
3. 然后,使用反射获取指定的方法。
4. 如果方法不存在,则抛出异常。
5. 通过反射调用方法获取每个枚举常量的值,并将其添加到一个值列表中。
6. 最后,将一个空字符串添加到值列表中。
这段代码主要用于初始化枚举常量的值列表,并在需要时进行验证。如果在获取枚举类型、方法或枚举值时出现异常,将抛出自定义的 BusinessException 异常。
希望对你有所帮助!如果你还有其他问题,请随时提问。
相关推荐
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![exe](https://img-home.csdnimg.cn/images/20210720083343.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)