¶Ô±ÈÐÂÎļþ |
| | |
| | | /* |
| | | * Copyright (c) 2018-2028, DreamLu All rights reserved. |
| | | * |
| | | * Redistribution and use in source and binary forms, with or without |
| | | * modification, are permitted provided that the following conditions are met: |
| | | * |
| | | * Redistributions of source code must retain the above copyright notice, |
| | | * this list of conditions and the following disclaimer. |
| | | * Redistributions in binary form must reproduce the above copyright |
| | | * notice, this list of conditions and the following disclaimer in the |
| | | * documentation and/or other materials provided with the distribution. |
| | | * Neither the name of the dreamlu.net developer nor the names of its |
| | | * contributors may be used to endorse or promote products derived from |
| | | * this software without specific prior written permission. |
| | | * Author: DreamLu 墿¥æ¢¦ (596392912@qq.com) |
| | | */ |
| | | package org.springblade.core.tool.utils; |
| | | |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.cglib.core.CodeGenerationException; |
| | | import org.springframework.core.convert.Property; |
| | | import org.springframework.core.convert.TypeDescriptor; |
| | | import org.springframework.lang.Nullable; |
| | | import org.springframework.util.ReflectionUtils; |
| | | |
| | | import java.beans.PropertyDescriptor; |
| | | import java.lang.annotation.Annotation; |
| | | import java.lang.reflect.Field; |
| | | import java.lang.reflect.Method; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * åå°å·¥å
·ç±» |
| | | * |
| | | * @author L.cm |
| | | */ |
| | | public class ReflectUtil extends ReflectionUtils { |
| | | |
| | | /** |
| | | * è·å Bean çææ getæ¹æ³ |
| | | * |
| | | * @param type ç±» |
| | | * @return PropertyDescriptoræ°ç» |
| | | */ |
| | | public static PropertyDescriptor[] getBeanGetters(Class type) { |
| | | return getPropertiesHelper(type, true, false); |
| | | } |
| | | |
| | | /** |
| | | * è·å Bean çææ setæ¹æ³ |
| | | * |
| | | * @param type ç±» |
| | | * @return PropertyDescriptoræ°ç» |
| | | */ |
| | | public static PropertyDescriptor[] getBeanSetters(Class type) { |
| | | return getPropertiesHelper(type, false, true); |
| | | } |
| | | |
| | | /** |
| | | * è·å Bean çææ PropertyDescriptor |
| | | * |
| | | * @param type ç±» |
| | | * @param read è¯»åæ¹æ³ |
| | | * @param write åæ¹æ³ |
| | | * @return PropertyDescriptoræ°ç» |
| | | */ |
| | | public static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) { |
| | | try { |
| | | PropertyDescriptor[] all = BeanUtil.getPropertyDescriptors(type); |
| | | if (read && write) { |
| | | return all; |
| | | } else { |
| | | List<PropertyDescriptor> properties = new ArrayList<>(all.length); |
| | | for (PropertyDescriptor pd : all) { |
| | | if (read && pd.getReadMethod() != null) { |
| | | properties.add(pd); |
| | | } else if (write && pd.getWriteMethod() != null) { |
| | | properties.add(pd); |
| | | } |
| | | } |
| | | return properties.toArray(new PropertyDescriptor[0]); |
| | | } |
| | | } catch (BeansException ex) { |
| | | throw new CodeGenerationException(ex); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * è·å bean ç屿§ä¿¡æ¯ |
| | | * @param propertyType ç±»å |
| | | * @param propertyName 屿§å |
| | | * @return {Property} |
| | | */ |
| | | @Nullable |
| | | public static Property getProperty(Class<?> propertyType, String propertyName) { |
| | | PropertyDescriptor propertyDescriptor = BeanUtil.getPropertyDescriptor(propertyType, propertyName); |
| | | if (propertyDescriptor == null) { |
| | | return null; |
| | | } |
| | | return ReflectUtil.getProperty(propertyType, propertyDescriptor, propertyName); |
| | | } |
| | | |
| | | /** |
| | | * è·å bean ç屿§ä¿¡æ¯ |
| | | * @param propertyType ç±»å |
| | | * @param propertyDescriptor PropertyDescriptor |
| | | * @param propertyName 屿§å |
| | | * @return {Property} |
| | | */ |
| | | public static Property getProperty(Class<?> propertyType, PropertyDescriptor propertyDescriptor, String propertyName) { |
| | | Method readMethod = propertyDescriptor.getReadMethod(); |
| | | Method writeMethod = propertyDescriptor.getWriteMethod(); |
| | | return new Property(propertyType, readMethod, writeMethod, propertyName); |
| | | } |
| | | |
| | | /** |
| | | * è·å bean ç屿§ä¿¡æ¯ |
| | | * @param propertyType ç±»å |
| | | * @param propertyName 屿§å |
| | | * @return {Property} |
| | | */ |
| | | @Nullable |
| | | public static TypeDescriptor getTypeDescriptor(Class<?> propertyType, String propertyName) { |
| | | Property property = ReflectUtil.getProperty(propertyType, propertyName); |
| | | if (property == null) { |
| | | return null; |
| | | } |
| | | return new TypeDescriptor(property); |
| | | } |
| | | |
| | | /** |
| | | * è·å ç±»å±æ§ä¿¡æ¯ |
| | | * @param propertyType ç±»å |
| | | * @param propertyDescriptor PropertyDescriptor |
| | | * @param propertyName 屿§å |
| | | * @return {Property} |
| | | */ |
| | | public static TypeDescriptor getTypeDescriptor(Class<?> propertyType, PropertyDescriptor propertyDescriptor, String propertyName) { |
| | | Method readMethod = propertyDescriptor.getReadMethod(); |
| | | Method writeMethod = propertyDescriptor.getWriteMethod(); |
| | | Property property = new Property(propertyType, readMethod, writeMethod, propertyName); |
| | | return new TypeDescriptor(property); |
| | | } |
| | | |
| | | /** |
| | | * è·å ç±»å±æ§ |
| | | * @param clazz ç±»ä¿¡æ¯ |
| | | * @param fieldName 屿§å |
| | | * @return Field |
| | | */ |
| | | @Nullable |
| | | public static Field getField(Class<?> clazz, String fieldName) { |
| | | while (clazz != Object.class) { |
| | | try { |
| | | return clazz.getDeclaredField(fieldName); |
| | | } catch (NoSuchFieldException e) { |
| | | clazz = clazz.getSuperclass(); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | /** |
| | | * è·å ææ field 屿§ä¸ç注解 |
| | | * @param clazz ç±» |
| | | * @param fieldName 屿§å |
| | | * @param annotationClass 注解 |
| | | * @param <T> 注解æ³å |
| | | * @return 注解 |
| | | */ |
| | | @Nullable |
| | | public static <T extends Annotation> T getAnnotation(Class<?> clazz, String fieldName, Class<T> annotationClass) { |
| | | Field field = ReflectUtil.getField(clazz, fieldName); |
| | | if (field == null) { |
| | | return null; |
| | | } |
| | | return field.getAnnotation(annotationClass); |
| | | } |
| | | } |