List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:org.openehr.adl.rm.RmBeanReflector.java
private Map<String, RmAttribute> buildAttributes(Class<?> beanClass) throws ReflectiveOperationException, IntrospectionException { Map<String, RmAttribute> properties; properties = new LinkedHashMap<>(); BeanInfo info = Introspector.getBeanInfo(beanClass); PropertyDescriptor[] pds = info.getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (pd.getName().equals("class")) continue; ;// w w w .j av a2s . co m String attribute = getAttributeForField(pd.getName()); MultiplicityInterval occurrences = getOccurrences(beanClass, pd); final Class<?> targetType = List.class.isAssignableFrom(pd.getPropertyType()) ? extractGenericType(beanClass, pd) : pd.getPropertyType(); RmAttribute rp = new RmAttribute(attribute, pd, occurrences, targetType); properties.put(attribute, rp); } rmClassAttributeMap.put(beanClass, ImmutableMap.copyOf(properties)); return properties; }
From source file:things.thing.ThingUtils.java
public Map<String, Map<String, String>> getRegisteredTypeProperties() { if (typePropertiesMap == null) { Map<String, Map<String, String>> temp = Maps.newTreeMap(); for (String type : tr.getAllTypes()) { Class typeClass = tr.getTypeClass(type); BeanInfo info = null; try { info = Introspector.getBeanInfo(typeClass); } catch (IntrospectionException e) { throw new TypeRuntimeException("Can't generate info for type: " + type, type, e); }/*from w w w .jav a2 s .c o m*/ Map<String, String> properties = Maps.newTreeMap(); for (PropertyDescriptor desc : info.getPropertyDescriptors()) { String name = desc.getName(); if ("class".equals(name) || "id".equals(name)) { continue; } Class propClass = desc.getPropertyType(); properties.put(name, propClass.getSimpleName()); } temp.put(type, properties); } typePropertiesMap = ImmutableMap.copyOf(temp); } return typePropertiesMap; }
From source file:com.ebay.jetstream.management.HtmlResourceFormatter.java
@Override protected final void formatBean(Object bean) throws Exception { Class<?> bclass = bean.getClass(); ManagedResource mr = bclass.getAnnotation(ManagedResource.class); String help = mr == null ? null : mr.description(); formatBean(false, bclass, help);/*www .ja v a2s . c o m*/ boolean section = false; for (PropertyDescriptor pd : Introspector.getBeanInfo(bclass).getPropertyDescriptors()) { Method getter = pd.getReadMethod(); if (!XMLSerializationManager.isHidden(getter)) { if (!section) { section = true; formatSection(false, "Properties"); } formatProperty(bean, pd); } } if (section) { formatSection(true, "Properties"); } section = false; for (Method method : bean.getClass().getMethods()) { ManagedOperation mo = method.getAnnotation(ManagedOperation.class); if (mo != null && method.getParameterTypes().length == 0) { help = mo.description(); if (!section) { section = true; formatSection(false, "Operations"); } formatOperation(method); } if (section) { formatSection(true, "Operations"); } } }
From source file:com.artistech.protobuf.TuioProtoConverter.java
@Override public GeneratedMessage.Builder convertToProtobuf(Object obj) { GeneratedMessage.Builder builder;/* w w w. j a v a 2 s. co m*/ if (obj.getClass().getName().equals(TUIO.TuioTime.class.getName())) { builder = TuioProtos.Time.newBuilder(); } else if (obj.getClass().getName().equals(TUIO.TuioCursor.class.getName())) { builder = TuioProtos.Cursor.newBuilder(); } else if (obj.getClass().getName().equals(TUIO.TuioObject.class.getName())) { builder = TuioProtos.Object.newBuilder(); } else if (obj.getClass().getName().equals(TUIO.TuioBlob.class.getName())) { builder = TuioProtos.Blob.newBuilder(); } else if (obj.getClass().getName().equals(TUIO.TuioPoint.class.getName())) { builder = TuioProtos.Point.newBuilder(); } else { return null; } try { PropertyDescriptor[] objProps = Introspector.getBeanInfo(obj.getClass()).getPropertyDescriptors(); BeanInfo beanInfo = Introspector.getBeanInfo(builder.getClass()); PropertyDescriptor[] builderProps = beanInfo.getPropertyDescriptors(); Method[] methods = builder.getClass().getMethods(); for (PropertyDescriptor prop1 : objProps) { for (PropertyDescriptor prop2 : builderProps) { if (prop1.getName().equals(prop2.getName())) { Method readMethod = prop1.getReadMethod(); ArrayList<Method> methodsToTry = new ArrayList<>(); for (Method m : methods) { if (m.getName().equals(readMethod.getName().replaceFirst("get", "set"))) { methodsToTry.add(m); } } for (Method setMethod : methodsToTry) { try { if (Iterable.class.isAssignableFrom(readMethod.getReturnType())) { if (DeepCopy) { ArrayList<Method> methodsToTry2 = new ArrayList<>(); for (Method m : methods) { if (m.getName() .equals(readMethod.getName().replaceFirst("get", "add"))) { methodsToTry2.add(m); } } boolean success = false; for (Method setMethod2 : methodsToTry2) { Iterable iter = (Iterable) readMethod.invoke(obj); Iterator it = iter.iterator(); while (it.hasNext()) { Object o = it.next(); //call set.. for (ProtoConverter converter : services) { if (converter.supportsConversion(o)) { try { GeneratedMessage.Builder convertToProtobuf = converter .convertToProtobuf(o); setMethod2.invoke(builder, convertToProtobuf); success = true; break; } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { } } } } if (success) { break; } } } } else { boolean primitiveOrWrapper = ClassUtils .isPrimitiveOrWrapper(readMethod.getReturnType()); if (primitiveOrWrapper) { setMethod.invoke(builder, readMethod.invoke(obj)); } else { Object invoke = readMethod.invoke(obj); com.google.protobuf.GeneratedMessage.Builder val = convertToProtobuf( invoke); setMethod.invoke(builder, val); break; } } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException ex) { // Logger.getLogger(TuioProtoConverter.class.getName()).log(Level.SEVERE, null, ex); } break; } } } } } catch (IntrospectionException ex) { logger.fatal(ex); } return builder; }
From source file:org.mandarax.dsl.util.DefaultResolver.java
@Override public Member getMember(Context context, String name, String className, String... paramTypeNames) throws ResolverException { Class clazz = getType(context, className); boolean isFieldRef = paramTypeNames == null; if (isFieldRef) { Field field = null;//from w w w . java 2s .com try { field = clazz.getField(name); } catch (Exception e) { // LOGGER.error(e); } if (field != null && Modifier.isPublic(field.getModifiers())) { LOGGER.debug("Resolving feature " + name + " in " + className + " with parameters " + paramTypeNames + " to " + field); return field; } // try to use bean framework Exception x = null; try { PropertyDescriptor[] properties = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); for (PropertyDescriptor p : properties) { if (p.getName().equals(name)) { Method reader = p.getReadMethod(); if (reader != null) { LOGGER.debug("Resolving feature " + name + " in " + className + " with parameters " + paramTypeNames + " to " + reader); return reader; } else { break; } } } } catch (IntrospectionException e) { x = e; } // try to locate method with exact name: this will map property hasWife to method hasWife() instead of getHasWife() try { @SuppressWarnings("unchecked") Method m = clazz.getMethod(name, new Class[] {}); if (m != null && Modifier.isPublic(m.getModifiers())) { LOGGER.debug("Resolving feature " + name + " in " + className + " with parameters " + paramTypeNames + " to " + m); return m; } } catch (Exception e2) { x = e2; } throw new ResolverException("Cannot resolve property " + name + " in " + clazz.getName(), x); } // method access Class[] paramTypes = new Class[paramTypeNames.length]; for (int i = 0; i < paramTypeNames.length; i++) { paramTypes[i] = getType(context, paramTypeNames[i]); } try { Method m = MethodUtils.getMatchingAccessibleMethod(clazz, name, paramTypes); if (Modifier.isPublic(m.getModifiers())) { LOGGER.debug("Resolving feature " + name + " in " + className + " with parameters " + paramTypeNames + " to " + m); return m; } else { throw new ResolverException("Method " + m + " in " + clazz.getName() + " is not visible"); } } catch (Exception e) { throw new ResolverException("Cannot resolve method " + name + " in " + clazz.getName(), e); } }
From source file:com.impetus.kundera.metadata.processor.AbstractEntityFieldProcessor.java
/** * Populates @Id accesser methods like, getId and setId of clazz to * metadata.//from w w w . j av a2 s . com * * @param metadata * the metadata * @param clazz * the clazz * @param f * the f */ protected final void populateIdAccessorMethods(EntityMetadata metadata, Class<?> clazz, Field f) { try { BeanInfo info = Introspector.getBeanInfo(clazz); for (PropertyDescriptor descriptor : info.getPropertyDescriptors()) { if (descriptor.getName().equals(f.getName())) { metadata.setReadIdentifierMethod(descriptor.getReadMethod()); metadata.setWriteIdentifierMethod(descriptor.getWriteMethod()); return; } } } catch (IntrospectionException e) { throw new RuntimeException(e); } }
From source file:com.heren.turtle.server.utils.TransUtils.java
public Map<String, Object> beanToMap(Object obj) { Map<String, Object> resultMap = new HashMap<>(); if (obj == null) { return null; }/*ww w .ja v a 2 s.c o m*/ try { BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String key = property.getName(); if (!key.equals("class")) { Method getter = property.getReadMethod(); Object value = getter.invoke(obj); resultMap.put(key, value); } } } catch (Exception e) { e.printStackTrace(); System.out.println("transBean2Map Error " + e); } return resultMap; }
From source file:org.apache.niolex.commons.bean.BeanUtil.java
/** * Parse all the write methods and prepare them to the hash map. * * @param toClass the class to be introspected * @return the hash map/*from w w w. j av a2s . com*/ * @throws IntrospectionException */ public static Map<String, Method> prepareWriteMethodMap(Class<?> toClass) throws IntrospectionException { BeanInfo toInfo = Introspector.getBeanInfo(toClass); HashMap<String, Method> writeMap = Maps.newHashMap(); // Iterate over all the attributes of to, prepare write methods. for (PropertyDescriptor descriptor : toInfo.getPropertyDescriptors()) { Method writeMethod = descriptor.getWriteMethod(); if (writeMethod == null) { continue; } writeMap.put(descriptor.getName(), writeMethod); } return writeMap; }
From source file:org.eclipse.scada.da.core.VariantBeanHelper.java
public static void apply(final Map<String, Variant> data, final Object target, final WriteAttributeResults results) throws IntrospectionException { final BeanInfo bi = Introspector.getBeanInfo(target.getClass()); for (final Map.Entry<String, Variant> entry : data.entrySet()) { final PropertyDescriptor pd = findDescriptor(bi, entry.getKey()); if (pd != null) { try { applyValue(target, pd, entry.getValue()); results.put(entry.getKey(), WriteAttributeResult.OK); } catch (final Exception e) { results.put(entry.getKey(), new WriteAttributeResult(e)); }/*from w ww . jav a 2 s . c o m*/ } else { results.put(entry.getKey(), new WriteAttributeResult( new IllegalArgumentException(String.format("'%s' is not a property name of '%s'", entry.getKey(), target.getClass().getName())))); } } }
From source file:com.cloudera.csd.validation.references.components.ReflectionHelper.java
/** * Return the set of getter methods for the class. * * @param clazz the class.//w w w .j a v a 2 s . c o m * @return the set of getter methods. */ public static Set<Method> getterMethods(Class<?> clazz) { Preconditions.checkNotNull(clazz); try { ImmutableSet.Builder<Method> builder = ImmutableSet.builder(); BeanInfo info = Introspector.getBeanInfo(clazz); for (PropertyDescriptor p : info.getPropertyDescriptors()) { Method getter = p.getReadMethod(); if (getter != null) { // Don't want to include any of the methods inherited from object. if (!getter.getDeclaringClass().equals(Object.class)) { builder.add(getter); } } } return builder.build(); } catch (IntrospectionException e) { throw new IllegalStateException("Could not introspect on " + clazz, e); } }