com.namics.commons.random.support.BeanUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-random Show documentation
Show all versions of java-random Show documentation
This modules aims to provide a universal mechanism to create random test dummies of java objects.
/*
* Copyright 2000-2014 Namics AG. All rights reserved.
*/
package com.namics.commons.random.support;
import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* BeanUtils.
*
* @author aschaefer
* @since 21.02.14 09:55
*/
public class BeanUtils {
public static void makeAccessible(Method method) {
if ((!Modifier.isPublic(method.getModifiers()) || !Modifier.isPublic(method.getDeclaringClass().getModifiers()))
&& !method.isAccessible()) {
method.setAccessible(true);
}
}
public static List getPropertyDescriptors(Class beanClass) {
try {
BeanInfo beanInfo = null;
beanInfo = new ExtendedBeanInfo(Introspector.getBeanInfo(beanClass));
// This call is slow so we do it once.
PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors();
return Arrays.asList(pds);
} catch (IntrospectionException e) {
return Collections.emptyList();
}
}
}