use of java.beans.BeanInfo in project apex-core by apache.
the class AppDataPushAgent method extractFields.
private JSONObject extractFields(Object o) {
List<Field> fields;
Map<String, Method> methods;
if (cacheFields.containsKey(o.getClass())) {
fields = cacheFields.get(o.getClass());
} else {
fields = new ArrayList<>();
for (Class<?> c = o.getClass(); c != Object.class; c = c.getSuperclass()) {
Field[] declaredFields = c.getDeclaredFields();
for (Field field : declaredFields) {
field.setAccessible(true);
AutoMetric rfa = field.getAnnotation(AutoMetric.class);
if (rfa != null) {
field.setAccessible(true);
try {
fields.add(field);
} catch (Exception ex) {
LOG.debug("Error extracting fields for app data: {}. Ignoring.", ex.getMessage());
}
}
}
}
cacheFields.put(o.getClass(), fields);
}
JSONObject result = new JSONObject();
for (Field field : fields) {
try {
result.put(field.getName(), field.get(o));
} catch (Exception ex) {
// ignore
}
}
if (cacheGetMethods.containsKey(o.getClass())) {
methods = cacheGetMethods.get(o.getClass());
} else {
methods = new HashMap<>();
try {
BeanInfo info = Introspector.getBeanInfo(o.getClass());
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
Method method = pd.getReadMethod();
if (pd.getReadMethod() != null) {
AutoMetric rfa = method.getAnnotation(AutoMetric.class);
if (rfa != null) {
methods.put(pd.getName(), method);
}
}
}
} catch (IntrospectionException ex) {
// ignore
}
cacheGetMethods.put(o.getClass(), methods);
}
for (Map.Entry<String, Method> entry : methods.entrySet()) {
try {
result.put(entry.getKey(), entry.getValue().invoke(o));
} catch (Exception ex) {
// ignore
}
}
return result;
}
use of java.beans.BeanInfo in project querydsl by querydsl.
the class QBean method initMethods.
private List<Method> initMethods(Map<String, ? extends Expression<?>> args) {
try {
List<Method> methods = new ArrayList<Method>(args.size());
BeanInfo beanInfo = Introspector.getBeanInfo(getType());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (Map.Entry<String, ? extends Expression<?>> entry : args.entrySet()) {
String property = entry.getKey();
Expression<?> expr = entry.getValue();
Method setter = null;
for (PropertyDescriptor prop : propertyDescriptors) {
if (prop.getName().equals(property)) {
setter = prop.getWriteMethod();
if (!isAssignableFrom(prop.getPropertyType(), expr.getType())) {
typeMismatch(prop.getPropertyType(), expr);
}
break;
}
}
if (setter == null) {
propertyNotFound(expr, property);
}
methods.add(setter);
}
return methods;
} catch (IntrospectionException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
use of java.beans.BeanInfo in project goci by EBISPOT.
the class EmbeddableDocument method embed.
public void embed(Document document) {
try {
Set<String> excludeNames = new HashSet<>();
BeanInfo objectInfo = Introspector.getBeanInfo(Document.class);
for (PropertyDescriptor descriptor : objectInfo.getPropertyDescriptors()) {
excludeNames.add(descriptor.getName());
}
try {
BeanInfo docInfo = Introspector.getBeanInfo(document.getClass());
for (PropertyDescriptor descriptor : docInfo.getPropertyDescriptors()) {
if (!excludeNames.contains(descriptor.getName())) {
boolean isExcluded = false;
Method readMethod = descriptor.getReadMethod();
readMethod.setAccessible(true);
if (readMethod.isAnnotationPresent(NonEmbeddableField.class)) {
isExcluded = true;
} else {
try {
Field field = document.getClass().getDeclaredField(descriptor.getName());
field.setAccessible(true);
if (field.isAnnotationPresent(NonEmbeddableField.class)) {
isExcluded = true;
}
} catch (NoSuchFieldException e) {
// no field with this name, skip
}
}
if (!isExcluded) {
try {
// determine method to update this document with doc being embedded
Method propertyAdderMethod = findPropertyAdder(descriptor);
// invoke read method on passed document
Object fieldToEmbed = descriptor.getReadMethod().invoke(document);
propertyAdderMethod.invoke(this, fieldToEmbed);
} catch (InvocationTargetException | IllegalAccessException e) {
throw new DocumentEmbeddingException("Failed to read property '" + descriptor.getName() + "'", e);
}
}
}
}
} catch (IntrospectionException e) {
throw new DocumentEmbeddingException("Failed to analyse document in preparation for embedding", e);
}
} catch (IntrospectionException e) {
throw new DocumentEmbeddingException("Failed to read Object.class when determining which properties to exclude", e);
}
}
use of java.beans.BeanInfo in project goci by EBISPOT.
the class EmbeddableDocumentTest method testIntrospection.
/*@Test
public void testEmbedTrait() {
try {
traitDoc.embed(associationDoc);
}
catch (DocumentEmbeddingException e) {
e.printStackTrace();
fail();
}
}*/
@Test
public void testIntrospection() {
try {
BeanInfo beanInfo = Introspector.getBeanInfo(studyDoc.getClass());
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
System.out.println("Display name: " + pd.getDisplayName());
System.out.println("Name: " + pd.getName());
System.out.println("Read method: " + pd.getReadMethod());
System.out.println("\t" + pd);
}
} catch (IntrospectionException e) {
e.printStackTrace();
fail();
}
}
use of java.beans.BeanInfo in project groovy-core by groovy.
the class MetaClassImpl method addProperties.
private void addProperties() {
BeanInfo info;
final Class stopClass;
// introspect
try {
if (isBeanDerivative(theClass)) {
info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IntrospectionException {
return Introspector.getBeanInfo(theClass, Introspector.IGNORE_ALL_BEANINFO);
}
});
} else {
info = (BeanInfo) AccessController.doPrivileged(new PrivilegedExceptionAction() {
public Object run() throws IntrospectionException {
return Introspector.getBeanInfo(theClass);
}
});
}
} catch (PrivilegedActionException pae) {
throw new GroovyRuntimeException("exception during bean introspection", pae.getException());
}
PropertyDescriptor[] descriptors = info.getPropertyDescriptors();
// build up the metaproperties based on the public fields, property descriptors,
// and the getters and setters
setupProperties(descriptors);
EventSetDescriptor[] eventDescriptors = info.getEventSetDescriptors();
for (EventSetDescriptor descriptor : eventDescriptors) {
Method[] listenerMethods = descriptor.getListenerMethods();
for (Method listenerMethod : listenerMethods) {
final MetaMethod metaMethod = CachedMethod.find(descriptor.getAddListenerMethod());
// we skip that here
if (metaMethod == null)
continue;
addToAllMethodsIfPublic(metaMethod);
String name = listenerMethod.getName();
if (listeners.containsKey(name)) {
listeners.put(name, AMBIGUOUS_LISTENER_METHOD);
} else {
listeners.put(name, metaMethod);
}
}
}
}
Aggregations