本文整理汇总了Java中java.beans.PropertyDescriptor.getReadMethod方法的典型用法代码示例。如果您正苦于以下问题:Java PropertyDescriptor.getReadMethod方法的具体用法?Java PropertyDescriptor.getReadMethod怎么用?Java PropertyDescriptor.getReadMethod使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.beans.PropertyDescriptor
的用法示例。
在下文中一共展示了PropertyDescriptor.getReadMethod方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getAttributes
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Maps object attributes.
* @param type the class to reflect.
* @param object the instance to address.
* @param <T> the class to reflect.
* @return the attributes mapping.
* @throws IntrospectionException when errors in reflection.
* @throws InvocationTargetException when errors in reflection.
* @throws IllegalAccessException when errors in reflection.
*/
public static <T> Map<String,Object> getAttributes(Class<T> type, T object)
throws IntrospectionException, InvocationTargetException, IllegalAccessException {
Map<String,Object> propsmap = new HashMap<>();
final BeanInfo beanInfo = Introspector.getBeanInfo(type);
final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor pd : propertyDescriptors) {
if (pd.getName().equals("class")) continue;
final Method getter = pd.getReadMethod();
if (getter != null) {
final String attrname = pd.getName();
final Object attrvalue = getter.invoke(object);
propsmap.put(attrname, attrvalue);
}
}
return propsmap;
}
示例2: getterOrSetter
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* <p>根据java对象属性{@link Field}获取该属性的getter或setter方法名,
* 另对{@link boolean}及{@link Boolean}做了行管处理</p>
*
* @param clazz 操作对象
* @param fieldName 对象属性
* @param methodType 方法类型,getter或setter枚举
* @return getter或setter方法
* @throws IntrospectionException 异常
* @author Crab2Died
*/
public static Method getterOrSetter(Class clazz, String fieldName, FieldAccessType methodType)
throws IntrospectionException {
if (null == fieldName || "".equals(fieldName))
return null;
BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor prop : props) {
if (fieldName.equals(prop.getName())) {
if (FieldAccessType.SETTER == methodType) {
return prop.getWriteMethod();
}
if (FieldAccessType.GETTER == methodType) {
return prop.getReadMethod();
}
}
}
throw new IntrospectionException("Can not get the getter or setter method");
}
示例3: fillValidations
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
private static Optional<List<Validator>> fillValidations(PropertyDescriptor fieldDescriptor,
Optional<List<Validator>> validations) {
List<Annotation> annotations = new ArrayList<>();
Method setter = fieldDescriptor.getWriteMethod();
Method getter = fieldDescriptor.getReadMethod();
if (setter != null) {
annotations.addAll(Arrays.asList(setter.getAnnotations()));
}
if (getter != null) {
annotations.addAll(Arrays.asList(getter.getAnnotations()));
}
if (!annotations.isEmpty()) {
validations = Optional.ofNullable(getValidators(annotations));
}
return validations;
}
示例4: init
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public static List<SortableField> init(Class aClass)throws IntrospectionException{
List<SortableField> sortableFields = new ArrayList<>();
//获取所有的属性
Field[] fields = aClass.getDeclaredFields();
for (Field field:fields){
//获取类属性的注解
ExcelDesc excelDesc = field.getAnnotation(ExcelDesc.class);
if (excelDesc != null && !excelDesc.ignoreField()){
//根据类属性获取属性对应的get方法,用户通过反射机制获取元素的值
PropertyDescriptor pd = new PropertyDescriptor(field.getName(),
aClass);
Method method = pd.getReadMethod();
SortableField sortableField = new SortableField(excelDesc, field, method);
sortableFields.add(sortableField);
}
}
//对获取到的属性进行排序
Collections.sort(sortableFields, new Comparator<SortableField>() {
public int compare(SortableField o1, SortableField o2) {
return o1.getExcelDesc().order()-o2.getExcelDesc().order();
}
});
return sortableFields;
}
示例5: getColumnToPropertyOverrides
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
private Map<String, String> getColumnToPropertyOverrides() {
Map<String, String> maps = new HashMap<String, String>();
PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[0];
try {
propertyDescriptors = propertyDescriptors(clazz);
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
Method gmethod = propertyDescriptor.getReadMethod();
if (null != gmethod) {
/**
* 反射字段名称
*/
Column column = gmethod.getAnnotation(Column.class);
String columnName = "";
if (column == null) {
columnName = propertyToColumn(propertyDescriptor.getName());
} else {
columnName = column.name();
}
maps.put(columnName.toUpperCase(), propertyDescriptor.getName());
}
}
return maps;
} catch (SQLException e) {
return null;
}
}
示例6: getMethod
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public static Method getMethod(Field field, Class clazz){
String name = field.getName();
try {
PropertyDescriptor pd = new PropertyDescriptor(name,clazz);
Method readMethod = pd.getReadMethod();
return readMethod;
} catch (Exception e) {
throw new RuntimeException(String.format("%s的%s字段Get方法不存在", clazz.getSimpleName(), name));
}
}
示例7: testPropertyDescriptor
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
private static void testPropertyDescriptor(Class type, Class read, Class write) {
PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(type, "foo");
if (!read.equals(pd.getReadMethod().getDeclaringClass())) {
throw new Error("unexpected read method: " + pd.getReadMethod());
}
if (!write.equals(pd.getWriteMethod().getDeclaringClass())) {
throw new Error("unexpected write method: " + pd.getWriteMethod());
}
}
示例8: introspectProperty
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Return the value of the specified property.
*
* @param <T> Type of the returned property.
* @param propertyName Name of the property.
* @param errors Map of error codes and error messages if a problem is encountered.
* @param fibre fibre to introspect
* @param context query context
* @return Value of the specified property.
*/
@JsonIgnore
public static <T> T introspectProperty(final String propertyName, final Fibre fibre,
final Map<OperationErrorCode, String> errors, final OperationContext context) {
if (RESERVED_WORDS.contains(propertyName)) {
errors.put(OperationErrorCode.NotReadableField, propertyName + " is not a valid property");
return null;
}
PropertyResult<T> entityPropertyResult = getEntityProperty(propertyName, fibre, errors, context);
if (entityPropertyResult.isFound()) {
return entityPropertyResult.getReadValue();
} else {
// Try introspection
IntrospectionContext iContext = fibre.getIntrospectionContextForProperty(propertyName);
T readValue = null;
BeanWrapper wrapper = PropertyAccessorFactory.forBeanPropertyAccess(iContext.getObject());
for (PropertyDescriptor des : wrapper.getPropertyDescriptors()) {
if (des.getName().equalsIgnoreCase(iContext.getProperty())) {
try {
Method readMethod = des.getReadMethod();
if (readMethod != null) {
readValue = (T) readMethod.invoke(iContext.getObject());
} else {
errors.put(OperationErrorCode.NotReadableField, propertyName + " is not readable");
}
} catch (IllegalAccessException | InvocationTargetException e) {
errors.put(OperationErrorCode.NotReadableField, e.toString());
}
break;
}
}
return readValue;
}
}
示例9: EnumControl
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public EnumControl(final Class<? extends Enum> c, final Object f, PropertyDescriptor p) {
super();
final String name = p.getName();
final Method r = p.getReadMethod(), w = p.getWriteMethod();
setterMap.put(name, this);
clazz = f;
write = w;
read = r;
setLayout(new GridLayout(1, 0));
// setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
setAlignmentX(ALIGNMENT);
final JLabel label = new JLabel(name);
label.setAlignmentX(ALIGNMENT);
label.setFont(label.getFont().deriveFont(fontSize));
addTip(p, label);
add(label);
control = new JComboBox(c.getEnumConstants());
control.setFont(control.getFont().deriveFont(fontSize));
// control.setHorizontalAlignment(SwingConstants.LEADING);
add(label);
add(control);
refresh();
control.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
w.invoke(clazz, control.getSelectedItem());
} catch (Exception e2) {
e2.printStackTrace();
}
}
});
}
示例10: checkIfAddField
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
private boolean checkIfAddField(Optional<Field> field, PropertyDescriptor propertyDescriptor, boolean isRequest) {
boolean res = true;
if (field.isPresent()) {
res = !field.get().isAnnotationPresent(JsonIgnore.class);
if (isRequest) {
res = res && !field.get().isAnnotationPresent(Auditable.class);
res = res && propertyDescriptor.getReadMethod() != null;
res = res && !propertyDescriptor.getReadMethod().isAnnotationPresent(Auditable.class);
Method setter = propertyDescriptor.getWriteMethod();
if (setter != null)
res = res && !setter.isAnnotationPresent(Auditable.class);
}
}
return res;
}
示例11: getDiff
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* @param oldBean
* @param newBean
* @return
*/
public static <T> T getDiff(T oldBean, T newBean) {
if (oldBean == null && newBean != null) {
return newBean;
} else if (newBean == null) {
return null;
} else {
Class<?> cls1 = oldBean.getClass();
try {
@SuppressWarnings("unchecked")
T object = (T)cls1.newInstance();
BeanInfo beanInfo = Introspector.getBeanInfo(cls1);
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
// 得到property对应的setter方法
Method setter = property.getWriteMethod();
Object oldValue = getter.invoke(oldBean);
Object newValue = getter.invoke(newBean);
if (setter != null && newValue != null && !newValue.equals(oldValue)) {
setter.invoke(object, newValue);
}
}
}
return object;
} catch (Exception e) {
throw new DataParseException(e);
}
}
}
示例12: findAnnotation
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* Find the annotation
* <p>
* The following search order processed
* <ol>
* <li>Check the field with the same name as the property, process through
* all superclasses</li>
* <li>Check the read method</li>
* <li>Check the write method</li>
* </ol>
* </p>
*
* @param pd
* the property descriptor to check
* @param clazz
* class instance
* @return the annotation or <code>null</code> if none was found
*/
protected <T extends Annotation> T findAnnotation ( final PropertyDescriptor pd, final Class<?> clazz, final Class<T> annotationClazz )
{
final String name = pd.getName ();
try
{
final Field field = findField ( name, clazz );
final T itemName = field.getAnnotation ( annotationClazz );
if ( itemName != null )
{
return itemName;
}
}
catch ( final NoSuchFieldException e )
{
}
if ( pd.getReadMethod () != null && pd.getReadMethod ().getAnnotation ( annotationClazz ) != null )
{
return pd.getReadMethod ().getAnnotation ( annotationClazz );
}
if ( pd.getWriteMethod () != null && pd.getWriteMethod ().getAnnotation ( annotationClazz ) != null )
{
return pd.getWriteMethod ().getAnnotation ( annotationClazz );
}
return null;
}
示例13: getProperties
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* 通过方法获取属性
*
* @param entityClass
* @return
*/
public List<EntityField> getProperties(Class<?> entityClass) {
Map<String, Class<?>> genericMap = _getGenericTypeMap(entityClass);
List<EntityField> entityFields = new ArrayList<EntityField>();
BeanInfo beanInfo;
try {
beanInfo = Introspector.getBeanInfo(entityClass);
} catch (IntrospectionException e) {
throw new MapperException(e);
}
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor desc : descriptors) {
if (desc != null && !"class".equals(desc.getName())) {
EntityField entityField = new EntityField(null, desc);
if (desc.getReadMethod() != null
&& desc.getReadMethod().getGenericReturnType() != null
&& desc.getReadMethod().getGenericReturnType() instanceof TypeVariable) {
entityField.setJavaType(genericMap.get(((TypeVariable) desc.getReadMethod().getGenericReturnType()).getName()));
} else if (desc.getWriteMethod() != null
&& desc.getWriteMethod().getGenericParameterTypes() != null
&& desc.getWriteMethod().getGenericParameterTypes().length == 1
&& desc.getWriteMethod().getGenericParameterTypes()[0] instanceof TypeVariable) {
entityField.setJavaType(genericMap.get(((TypeVariable) desc.getWriteMethod().getGenericParameterTypes()[0]).getName()));
}
entityFields.add(entityField);
}
}
return entityFields;
}
示例14: getter
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
public static Object getter(Object object, String propertyName) {
try {
PropertyDescriptor pd = new PropertyDescriptor(propertyName, object.getClass());
Method method = pd.getReadMethod();
if (method == null) {
throw new IllegalArgumentException("method may not exists");
}
return method.invoke(object);
} catch (Exception ex) {
throw new RuntimeException();
}
}
示例15: _getProperties
import java.beans.PropertyDescriptor; //导入方法依赖的package包/类
/**
* 通过方法获取属性
*
* @param entityClass
* @return
*/
private static List<EntityField> _getProperties(Class<?> entityClass) {
Map<String, Class<?>> genericMap = _getGenericTypeMap(entityClass);
List<EntityField> entityFields = new ArrayList<EntityField>();
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(entityClass);
} catch (IntrospectionException e) {
throw new RuntimeException(e);
}
PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor desc : descriptors) {
if (desc != null && !"class".equals(desc.getName())) {
EntityField entityField = new EntityField(null, desc);
if (desc.getReadMethod() != null
&& desc.getReadMethod().getGenericReturnType() != null
&& desc.getReadMethod().getGenericReturnType() instanceof TypeVariable) {
entityField.setJavaType(genericMap.get(((TypeVariable) desc.getReadMethod().getGenericReturnType()).getName()));
} else if (desc.getWriteMethod() != null
&& desc.getWriteMethod().getGenericParameterTypes() != null
&& desc.getWriteMethod().getGenericParameterTypes().length == 1
&& desc.getWriteMethod().getGenericParameterTypes()[0] instanceof TypeVariable) {
entityField.setJavaType(genericMap.get(((TypeVariable) desc.getWriteMethod().getGenericParameterTypes()[0]).getName()));
}
entityFields.add(entityField);
}
}
return entityFields;
}