use of java.beans.BeanInfo in project opennars by opennars.
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 cosmic by MissionCriticalCloud.
the class ReflectUtil method flattenProperties.
private static List<String> flattenProperties(final Object target, final Class<?> clazz, final ImmutableSet<String> excludedProperties) {
assert clazz != null;
if (target == null) {
return emptyList();
}
assert clazz.isAssignableFrom(target.getClass());
try {
final BeanInfo beanInfo = getBeanInfo(clazz);
final PropertyDescriptor[] descriptors = beanInfo.getPropertyDescriptors();
final List<String> serializedProperties = new ArrayList<>();
for (final PropertyDescriptor descriptor : descriptors) {
if (excludedProperties.contains(descriptor.getName())) {
continue;
}
serializedProperties.add(descriptor.getName());
final Object value = descriptor.getReadMethod().invoke(target);
serializedProperties.add(value != null ? value.toString() : "null");
}
return unmodifiableList(serializedProperties);
} catch (final IntrospectionException e) {
s_logger.warn("Ignored IntrospectionException when serializing class " + target.getClass().getCanonicalName(), e);
} catch (final IllegalArgumentException e) {
s_logger.warn("Ignored IllegalArgumentException when serializing class " + target.getClass().getCanonicalName(), e);
} catch (final IllegalAccessException e) {
s_logger.warn("Ignored IllegalAccessException when serializing class " + target.getClass().getCanonicalName(), e);
} catch (final InvocationTargetException e) {
s_logger.warn("Ignored InvocationTargetException when serializing class " + target.getClass().getCanonicalName(), e);
}
return emptyList();
}
use of java.beans.BeanInfo in project shiro by apache.
the class PrincipalTag method getPrincipalProperty.
private String getPrincipalProperty(Object principal, String property) throws JspTagException {
String strValue = null;
try {
BeanInfo bi = Introspector.getBeanInfo(principal.getClass());
// Loop through the properties to get the string value of the specified property
boolean foundProperty = false;
for (PropertyDescriptor pd : bi.getPropertyDescriptors()) {
if (pd.getName().equals(property)) {
Object value = pd.getReadMethod().invoke(principal, (Object[]) null);
strValue = String.valueOf(value);
foundProperty = true;
break;
}
}
if (!foundProperty) {
final String message = "Property [" + property + "] not found in principal of type [" + principal.getClass().getName() + "]";
if (log.isErrorEnabled()) {
log.error(message);
}
throw new JspTagException(message);
}
} catch (Exception e) {
final String message = "Error reading property [" + property + "] from principal of type [" + principal.getClass().getName() + "]";
if (log.isErrorEnabled()) {
log.error(message, e);
}
throw new JspTagException(message, e);
}
return strValue;
}
use of java.beans.BeanInfo in project typescript-generator by vojtechhabarta.
the class Jackson2Parser method parseEnumOrObjectEnum.
private DeclarationModel parseEnumOrObjectEnum(SourceType<Class<?>> sourceClass) {
final JsonFormat jsonFormat = sourceClass.type.getAnnotation(JsonFormat.class);
if (jsonFormat != null && jsonFormat.shape() == JsonFormat.Shape.OBJECT) {
return parseBean(sourceClass);
}
final boolean isNumberBased = jsonFormat != null && (jsonFormat.shape() == JsonFormat.Shape.NUMBER || jsonFormat.shape() == JsonFormat.Shape.NUMBER_FLOAT || jsonFormat.shape() == JsonFormat.Shape.NUMBER_INT);
final List<EnumMemberModel> enumMembers = new ArrayList<>();
if (sourceClass.type.isEnum()) {
final Class<?> enumClass = (Class<?>) sourceClass.type;
try {
Method valueMethod = null;
final BeanInfo beanInfo = Introspector.getBeanInfo(enumClass);
for (PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) {
final Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod.isAnnotationPresent(JsonValue.class)) {
valueMethod = readMethod;
}
}
int index = 0;
for (Field field : enumClass.getFields()) {
if (field.isEnumConstant()) {
if (isNumberBased) {
final Number value = getNumberEnumValue(field, valueMethod, index++);
enumMembers.add(new EnumMemberModel(field.getName(), value, null));
} else {
final String value = getStringEnumValue(field, valueMethod);
enumMembers.add(new EnumMemberModel(field.getName(), value, null));
}
}
}
} catch (Exception e) {
System.out.println(String.format("Cannot get enum values for '%s' enum", enumClass.getName()));
e.printStackTrace(System.out);
}
}
return new EnumModel(sourceClass.type, isNumberBased ? EnumKind.NumberBased : EnumKind.StringBased, enumMembers, null);
}
use of java.beans.BeanInfo in project fabric8 by fabric8io.
the class UserConfigurationCompare method configEqualKubernetesDTO.
/**
* Compares 2 instances of the given Kubernetes DTO class to see if the user has changed their configuration.
* <p/>
* This method will ignore properties {@link #ignoredProperties} such as status or timestamp properties
*/
protected static boolean configEqualKubernetesDTO(@NotNull Object entity1, @NotNull Object entity2, @NotNull Class<?> clazz) {
// lets iterate through the objects making sure we've not
BeanInfo beanInfo = null;
try {
beanInfo = Introspector.getBeanInfo(clazz);
} catch (IntrospectionException e) {
LOG.warn("Failed to get beanInfo for " + clazz.getName() + ". " + e, e);
return false;
}
try {
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
String name = propertyDescriptor.getName();
if (ignoredProperties.contains(name)) {
continue;
}
Method readMethod = propertyDescriptor.getReadMethod();
if (readMethod != null) {
Object value1 = invokeMethod(entity1, readMethod);
Object value2 = invokeMethod(entity2, readMethod);
if (!configEqual(value1, value2)) {
return false;
}
}
}
return true;
} catch (Exception e) {
return false;
}
}
Aggregations