use of java.beans.PropertyDescriptor in project powermock by powermock.
the class ReflectUtils method getPropertiesHelper.
private static PropertyDescriptor[] getPropertiesHelper(Class type, boolean read, boolean write) {
try {
BeanInfo info = Introspector.getBeanInfo(type, Object.class);
PropertyDescriptor[] all = info.getPropertyDescriptors();
if (read && write) {
return all;
}
List properties = new ArrayList(all.length);
for (int i = 0; i < all.length; i++) {
PropertyDescriptor pd = all[i];
if ((read && pd.getReadMethod() != null) || (write && pd.getWriteMethod() != null)) {
properties.add(pd);
}
}
return (PropertyDescriptor[]) properties.toArray(new PropertyDescriptor[properties.size()]);
} catch (IntrospectionException e) {
throw new CodeGenerationException(e);
}
}
use of java.beans.PropertyDescriptor in project powermock by powermock.
the class ReflectUtils method getPropertyMethods.
public static Method[] getPropertyMethods(PropertyDescriptor[] properties, boolean read, boolean write) {
Set methods = new HashSet();
for (int i = 0; i < properties.length; i++) {
PropertyDescriptor pd = properties[i];
if (read) {
methods.add(pd.getReadMethod());
}
if (write) {
methods.add(pd.getWriteMethod());
}
}
methods.remove(null);
return (Method[]) methods.toArray(new Method[methods.size()]);
}
use of java.beans.PropertyDescriptor in project powermock by powermock.
the class BeanMapEmitter method generateGetPropertyType.
private void generateGetPropertyType(final Map allProps, String[] allNames) {
final CodeEmitter e = begin_method(Constants.ACC_PUBLIC, GET_PROPERTY_TYPE, null);
e.load_arg(0);
EmitUtils.string_switch(e, allNames, Constants.SWITCH_STYLE_HASH, new ObjectSwitchCallback() {
public void processCase(Object key, Label end) {
PropertyDescriptor pd = (PropertyDescriptor) allProps.get(key);
EmitUtils.load_class(e, Type.getType(pd.getPropertyType()));
e.return_value();
}
public void processDefault() {
e.aconst_null();
e.return_value();
}
});
e.end_method();
}
use of java.beans.PropertyDescriptor in project Gargoyle by callakrsos.
the class ObjectUtil method toMap.
/**
* @작성자 : KYJ
* @작성일 : 2017. 3. 31.
* @param obj
* @param filter
* @return
* @throws IntrospectionException
*/
@SuppressWarnings("rawtypes")
public static Map toMap(Object t, Predicate<String> fieldNameFilter) {
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(t.getClass());
} catch (IntrospectionException e1) {
throw new RuntimeException(e1);
}
Map<String, Object> hashMap = new HashMap<String, Object>();
// Iterate over all the attributes
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {
// write메소드와 read메소드가 존재할때만.
Method writeMethod = descriptor.getWriteMethod();
Method readMethod = descriptor.getReadMethod();
if (ValueUtil.isEmpty(writeMethod) || ValueUtil.isEmpty(readMethod)) {
continue;
}
// Class<?> returnType = readMethod.getReturnType();
String methodName = ValueUtil.getSimpleMethodName(readMethod.getName());
if (fieldNameFilter != null) {
if (fieldNameFilter.test(methodName))
continue;
}
Object originalValue = null;
try {
originalValue = readMethod.invoke(t);
} catch (Exception e) {
}
if (ValueUtil.isNotEmpty(originalValue)) {
hashMap.put(methodName, originalValue);
} else {
hashMap.put(methodName, null);
}
}
return hashMap;
}
use of java.beans.PropertyDescriptor in project JMRI by JMRI.
the class DefaultJavaBeanConfigXML method unpack.
Object unpack(Element e) throws Exception {
String classname = e.getAttributeValue("beanClass");
Class<?> cl = Class.forName(classname);
Constructor<?> ctor = cl.getConstructor(new Class<?>[] {});
Object o = ctor.newInstance(new Object[] {});
// reflect through and add parameters
BeanInfo b = Introspector.getBeanInfo(o.getClass());
PropertyDescriptor[] properties = b.getPropertyDescriptors();
// add properties
List<Element> children = e.getChildren("property");
for (int i = 0; i < children.size(); i++) {
// unpack XML
Element property = children.get(i);
Element eName = property.getChild("name");
Element eValue = property.getChild("value");
String name = eName.getText();
String value = eValue.getText();
String type = eName.getAttributeValue("type");
// find matching method
for (int j = 0; j < properties.length; j++) {
if (properties[j].getName().equals(name)) {
// match, set this one by first finding method
Method m = properties[j].getWriteMethod();
// sort by type
if (type.equals("class java.lang.String")) {
m.invoke(o, new Object[] { value });
} else if (type.equals("int")) {
m.invoke(o, new Object[] { Integer.valueOf(value) });
} else {
log.error("Can't handle type: " + type);
}
break;
}
}
}
return o;
}
Aggregations