use of java.beans.BeanInfo in project CodenameOne by codenameone.
the class DefaultBeanInfoResolver method getBeanInfo.
public BeanInfo getBeanInfo(Class clazz) {
if (clazz == null) {
return null;
}
String classname = clazz.getName();
// look for .impl.basic., remove it and call getBeanInfo(class)
int index = classname.indexOf(".impl.basic");
if (index != -1 && classname.endsWith("Basic")) {
classname = classname.substring(0, index) + classname.substring(index + ".impl.basic".length(), classname.lastIndexOf("Basic"));
try {
return getBeanInfo(Class.forName(classname));
} catch (ClassNotFoundException e) {
return null;
}
} else {
try {
BeanInfo beanInfo = (BeanInfo) Class.forName(classname + "BeanInfo").newInstance();
return beanInfo;
} catch (Exception e) {
return null;
}
}
}
use of java.beans.BeanInfo in project activemq-artemis by apache.
the class PropertyUtil method getProperties.
/**
* Get properties from an object using reflection. If the passed object is null an
* empty <code>Map</code> is returned.
*
* @param object the Object whose properties are to be extracted.
* @return <Code>Map</Code> of properties extracted from the given object.
* @throws Exception if an error occurs while examining the object's properties.
*/
public static Map<String, String> getProperties(Object object) throws Exception {
if (object == null) {
return Collections.emptyMap();
}
Map<String, String> properties = new LinkedHashMap<>();
BeanInfo beanInfo = Introspector.getBeanInfo(object.getClass());
Object[] NULL_ARG = {};
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
if (propertyDescriptors != null) {
for (PropertyDescriptor pd : propertyDescriptors) {
if (pd.getReadMethod() != null && !pd.getName().equals("class") && !pd.getName().equals("properties") && !pd.getName().equals("reference")) {
Object value = pd.getReadMethod().invoke(object, NULL_ARG);
if (value != null) {
if (value instanceof Boolean || value instanceof Number || value instanceof String || value instanceof URI || value instanceof URL) {
properties.put(pd.getName(), ("" + value));
} else if (value instanceof SSLContext) {
// ignore this one..
} else {
Map<String, String> inner = getProperties(value);
for (Entry<String, String> entry : inner.entrySet()) {
properties.put(pd.getName() + "." + entry.getKey(), entry.getValue());
}
}
}
}
}
}
return properties;
}
use of java.beans.BeanInfo in project activemq-artemis by apache.
the class ActiveMQTestBase method validateGettersAndSetters.
/**
* It validates a Bean (POJO) using simple setters and getters with random values.
* You can pass a list of properties to be ignored, as some properties will have a pre-defined domain (not being possible to use random-values on them)
*/
protected void validateGettersAndSetters(final Object pojo, final String... ignoredProperties) throws Exception {
HashSet<String> ignoreSet = new HashSet<>();
for (String ignore : ignoredProperties) {
ignoreSet.add(ignore);
}
BeanInfo info = Introspector.getBeanInfo(pojo.getClass());
PropertyDescriptor[] properties = info.getPropertyDescriptors();
for (PropertyDescriptor prop : properties) {
Object value;
if (prop.getPropertyType() == String.class) {
value = RandomUtil.randomString();
} else if (prop.getPropertyType() == Integer.class || prop.getPropertyType() == Integer.TYPE) {
value = RandomUtil.randomInt();
} else if (prop.getPropertyType() == Long.class || prop.getPropertyType() == Long.TYPE) {
value = RandomUtil.randomLong();
} else if (prop.getPropertyType() == Boolean.class || prop.getPropertyType() == Boolean.TYPE) {
value = RandomUtil.randomBoolean();
} else if (prop.getPropertyType() == Double.class || prop.getPropertyType() == Double.TYPE) {
value = RandomUtil.randomDouble();
} else {
System.out.println("Can't validate property of type " + prop.getPropertyType() + " on " + prop.getName());
value = null;
}
if (value != null && prop.getWriteMethod() != null && prop.getReadMethod() == null) {
System.out.println("WriteOnly property " + prop.getName() + " on " + pojo.getClass());
} else if (value != null && prop.getWriteMethod() != null && prop.getReadMethod() != null && !ignoreSet.contains(prop.getName())) {
System.out.println("Validating " + prop.getName() + " type = " + prop.getPropertyType());
prop.getWriteMethod().invoke(pojo, value);
Assert.assertEquals("Property " + prop.getName(), value, prop.getReadMethod().invoke(pojo));
}
}
}
use of java.beans.BeanInfo in project tomcat70 by apache.
the class BeanELResolver method getFeatureDescriptors.
@Override
public Iterator<FeatureDescriptor> getFeatureDescriptors(ELContext context, Object base) {
if (base == null) {
return null;
}
try {
BeanInfo info = Introspector.getBeanInfo(base.getClass());
PropertyDescriptor[] pds = info.getPropertyDescriptors();
for (int i = 0; i < pds.length; i++) {
pds[i].setValue(RESOLVABLE_AT_DESIGN_TIME, Boolean.TRUE);
pds[i].setValue(TYPE, pds[i].getPropertyType());
}
return Arrays.asList((FeatureDescriptor[]) pds).iterator();
} catch (IntrospectionException e) {
//
}
return null;
}
use of java.beans.BeanInfo in project jdepth by Crab2died.
the class IntrospectorFeature method main.
public static void main(String[] args) {
IntrospectorFeature introspector = new IntrospectorFeature();
try {
//
PropertyDescriptor descriptor = new PropertyDescriptor("property", IntrospectorFeature.class);
// setter 函数获取
Method setMethod = descriptor.getWriteMethod();
// getter 函数获取
Method getMethod = descriptor.getReadMethod();
// 调用 setter
Object obj = setMethod.invoke(introspector, "setter方法");
// 调用getter
Object prop = getMethod.invoke(introspector);
System.out.println(prop);
BeanInfo beanInfo = Introspector.getBeanInfo(IntrospectorFeature.class);
PropertyDescriptor[] props = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor _prop : props) {
System.out.println(_prop.getDisplayName());
if ("property".equals(_prop.getName())) {
System.out.println(_prop.getDisplayName());
}
}
} catch (IllegalAccessException | InvocationTargetException | IntrospectionException e) {
e.printStackTrace();
}
}
Aggregations