List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:de.hasait.clap.impl.CLAPClassNode.java
public CLAPClassNode(final CLAP pCLAP, final Class<T> pClass) { super(pCLAP); _class = pClass; _propertyDescriptorByOptionMap = new HashMap<CLAPValue<?>, PropertyDescriptor>(); _keywordNodes = new HashSet<CLAPKeywordNode>(); BeanInfo beanInfo;//from www . j av a 2s . c o m try { beanInfo = Introspector.getBeanInfo(pClass); } catch (final IntrospectionException e) { throw new RuntimeException(e); } final List<Item> annotations = new ArrayList<Item>(); final CLAPKeywords classKeywords = findAnnotation(pClass, CLAPKeywords.class); if (classKeywords != null) { for (final CLAPKeyword classKeyword : classKeywords.value()) { annotations.add(new Item(classKeyword.order(), classKeyword, null, null, null)); } } final CLAPHelpCategory classHelpCategory = findAnnotation(pClass, CLAPHelpCategory.class); if (classHelpCategory != null) { setHelpCategory(classHelpCategory.order(), classHelpCategory.titleNLSKey()); } final CLAPUsageCategory classUsageCategory = findAnnotation(pClass, CLAPUsageCategory.class); if (classUsageCategory != null) { setUsageCategory(classUsageCategory.order(), classUsageCategory.titleNLSKey()); } for (final PropertyDescriptor propertyDescriptor : beanInfo.getPropertyDescriptors()) { final CLAPOption pdOption = getAnnotation(propertyDescriptor, CLAPOption.class); final CLAPDelegate pdDelegate = getAnnotation(propertyDescriptor, CLAPDelegate.class); final CLAPDecision pdDecision = getAnnotation(propertyDescriptor, CLAPDecision.class); final CLAPHelpCategory pdHelpCategory = getAnnotation(propertyDescriptor, CLAPHelpCategory.class); final CLAPUsageCategory pdUsageCategory = getAnnotation(propertyDescriptor, CLAPUsageCategory.class); if ((pdOption != null ? 1 : 0) + (pdDelegate != null ? 1 : 0) + (pdDecision != null ? 1 : 0) > 1) { throw new IllegalArgumentException(); } if (pdOption != null) { annotations.add( new Item(pdOption.order(), pdOption, propertyDescriptor, pdHelpCategory, pdUsageCategory)); } if (pdDelegate != null) { annotations.add(new Item(pdDelegate.order(), pdDelegate, propertyDescriptor, pdHelpCategory, pdUsageCategory)); } if (pdDecision != null) { annotations.add(new Item(pdDecision.order(), pdDecision, propertyDescriptor, pdHelpCategory, pdUsageCategory)); } } Collections.sort(annotations, new Comparator<Item>() { @Override public int compare(final Item pO1, final Item pO2) { return Integer.valueOf(pO1._order).compareTo(Integer.valueOf(pO2._order)); } }); for (final Item entry : annotations) { final Object annotation = entry._annotation; final AbstractCLAPNode node; if (annotation instanceof CLAPKeyword) { node = processCLAPKeyword((CLAPKeyword) annotation); } else if (annotation instanceof CLAPOption) { node = processCLAPOption(entry._propertyDescriptor.getPropertyType(), entry._propertyDescriptor, (CLAPOption) annotation); } else if (annotation instanceof CLAPDelegate) { node = processCLAPDelegate(entry._propertyDescriptor.getPropertyType(), entry._propertyDescriptor, (CLAPDelegate) annotation); } else if (annotation instanceof CLAPDecision) { node = processCLAPDecision(entry._propertyDescriptor.getPropertyType(), entry._propertyDescriptor, (CLAPDecision) annotation); } else { throw new RuntimeException(); } if (entry._helpCategory != null) { node.setHelpCategory(entry._helpCategory.order(), entry._helpCategory.titleNLSKey()); } if (entry._usageCategory != null) { node.setUsageCategory(entry._usageCategory.order(), entry._usageCategory.titleNLSKey()); } } }
From source file:org.yccheok.jstock.gui.analysis.ObjectInspectorJPanel.java
public void setBean(Object bean) { this.bean = bean; BeanInfo beanInfo = null;/* ww w .j a v a2 s .com*/ try { beanInfo = Introspector.getBeanInfo(bean.getClass()); } catch (IntrospectionException exp) { log.error(null, exp); } this.setBeanInfo(beanInfo); Property[] properties = this.getProperties(); for (int i = 0, c = properties.length; i < c; i++) { properties[i].readFromObject(bean); } //sheet.revalidate(); }
From source file:net.sf.beanlib.provider.BeanChecker.java
/** * @param fBean from bean/*from w w w . j a v a2s. co m*/ * @param tBean to bean * @return true if the two beans are equal in a JavaBean sense. * ie. if they have the same number of properties, * and the properties that can be read contain the same values. * * TODO: unit test me */ public boolean beanEquals(Object fBean, Object tBean) { if (fBean == tBean) return true; if (fBean == null || tBean == null) return false; try { BeanInfo bi_f = Introspector.getBeanInfo(fBean.getClass()); PropertyDescriptor[] pda_f = bi_f.getPropertyDescriptors(); Map<?, ?> tMap = beanGetter.getPropertyName2DescriptorMap(tBean.getClass()); if (pda_f.length != tMap.size()) return false; for (int i = pda_f.length - 1; i > -1; i--) { PropertyDescriptor pd_f = pda_f[i]; PropertyDescriptor pd_t = (PropertyDescriptor) tMap.get(pd_f.getName()); Method m_f = pd_f.getReadMethod(); Method m_t = pd_t.getReadMethod(); if (m_f == null) { if (m_t == null) continue; return false; } if (m_t == null) return false; Object v_f = m_f.invoke(fBean); Object v_t = m_t.invoke(tBean); if (!new EqualsBuilder().append(v_f, v_t).isEquals()) return false; } return true; } catch (IntrospectionException e) { log.error("", e); throw new BeanlibException(e); } catch (IllegalAccessException e) { log.error("", e); throw new BeanlibException(e); } catch (InvocationTargetException e) { log.error("", e.getTargetException()); throw new BeanlibException(e.getTargetException()); } }
From source file:org.apache.syncope.core.provisioning.java.jexl.JexlUtils.java
public static void addFieldsToContext(final Object object, final JexlContext jexlContext) { Set<PropertyDescriptor> cached = FIELD_CACHE.get(object.getClass()); if (cached == null) { cached = new HashSet<>(); FIELD_CACHE.put(object.getClass(), cached); try {// w ww .j a v a2s. co m for (PropertyDescriptor desc : Introspector.getBeanInfo(object.getClass()) .getPropertyDescriptors()) { if ((!desc.getName().startsWith("pc")) && (!ArrayUtils.contains(IGNORE_FIELDS, desc.getName())) && (!Iterable.class.isAssignableFrom(desc.getPropertyType())) && (!desc.getPropertyType().isArray())) { cached.add(desc); } } } catch (IntrospectionException ie) { LOG.error("Reading class attributes error", ie); } } for (PropertyDescriptor desc : cached) { String fieldName = desc.getName(); Class<?> fieldType = desc.getPropertyType(); try { Object fieldValue; if (desc.getReadMethod() == null) { final Field field = object.getClass().getDeclaredField(fieldName); field.setAccessible(true); fieldValue = field.get(object); } else { fieldValue = desc.getReadMethod().invoke(object); } fieldValue = fieldValue == null ? StringUtils.EMPTY : (fieldType.equals(Date.class) ? FormatUtils.format((Date) fieldValue, false) : fieldValue); jexlContext.set(fieldName, fieldValue); LOG.debug("Add field {} with value {}", fieldName, fieldValue); } catch (Exception iae) { LOG.error("Reading '{}' value error", fieldName, iae); } } if (object instanceof Any && ((Any<?>) object).getRealm() != null) { jexlContext.set("realm", ((Any<?>) object).getRealm().getFullPath()); } else if (object instanceof AnyTO && ((AnyTO) object).getRealm() != null) { jexlContext.set("realm", ((AnyTO) object).getRealm()); } else if (object instanceof Realm) { jexlContext.set("fullPath", ((Realm) object).getFullPath()); } else if (object instanceof RealmTO) { jexlContext.set("fullPath", ((RealmTO) object).getFullPath()); } }
From source file:org.jkcsoft.java.util.Beans.java
/** * Doesn't work yet!/* www . j a va 2 s . c om*/ * * @param bean */ public static void cleanBean(Object bean) { if (bean == null) return; try { BeanInfo bi = Introspector.getBeanInfo(bean.getClass()); PropertyDescriptor pds[] = bi.getPropertyDescriptors(); PropertyDescriptor pd = null; for (int i = 0; i < pds.length; i++) { pd = pds[i]; //Method getter = pd.getReadMethod(); Method setter = pd.getWriteMethod(); if (pd.getPropertyType() == Integer.class) { setter.invoke(bean, new Object[] { new Integer(0) }); } else if (pd.getPropertyType() == Double.class) { setter.invoke(bean, new Object[] { new Double(0) }); } else { try { setter.invoke(bean, new Object[] { null }); } catch (Throwable e) { log.warn("cleanBean()", e); } } } } catch (Throwable e) { log.warn("cleanBean()", e); } }
From source file:org.bibsonomy.layout.util.JabRefModelConverter.java
/** * Converts a BibSonomy post into a JabRef BibtexEntry * /* w ww . j a v a2s. c o m*/ * @param post * @param urlGen * - the URLGenerator to create the biburl-field * @return */ public static BibtexEntry convertPost(final Post<? extends Resource> post, URLGenerator urlGen) { try { /* * what we have */ final BibTex bibtex = (BibTex) post.getResource(); /* * what we want */ final BibtexEntry entry = new BibtexEntry(); /* * each entry needs an ID (otherwise we get a NPE) ... let JabRef * generate it */ /* * we use introspection to get all fields ... */ final BeanInfo info = Introspector.getBeanInfo(bibtex.getClass()); final PropertyDescriptor[] descriptors = info.getPropertyDescriptors(); /* * iterate over all properties */ for (final PropertyDescriptor pd : descriptors) { final Method getter = pd.getReadMethod(); // loop over all String attributes final Object o = getter.invoke(bibtex, (Object[]) null); if (String.class.equals(pd.getPropertyType()) && (o != null) && !JabRefModelConverter.EXCLUDE_FIELDS.contains(pd.getName())) { final String value = ((String) o); if (present(value)) entry.setField(pd.getName().toLowerCase(), value); } } /* * convert entry type (Is never null but getType() returns null for * unknown types and JabRef knows less types than we.) * * FIXME: a nicer solution would be to implement the corresponding * classes for the missing entrytypes. */ final BibtexEntryType entryType = BibtexEntryType.getType(bibtex.getEntrytype()); entry.setType(entryType == null ? BibtexEntryType.OTHER : entryType); if (present(bibtex.getMisc()) || present(bibtex.getMiscFields())) { // parse the misc fields and loop over them bibtex.parseMiscField(); if (bibtex.getMiscFields() != null) for (final String key : bibtex.getMiscFields().keySet()) { if ("id".equals(key)) { // id is used by jabref entry.setField("misc_id", bibtex.getMiscField(key)); continue; } if (key.startsWith("__")) // ignore fields starting with // __ - jabref uses them for // control continue; entry.setField(key, bibtex.getMiscField(key)); } } final String month = bibtex.getMonth(); if (present(month)) { /* * try to convert the month abbrev like JabRef does it */ final String longMonth = Globals.MONTH_STRINGS.get(month); if (present(longMonth)) { entry.setField("month", longMonth); } else { entry.setField("month", month); } } final String bibAbstract = bibtex.getAbstract(); if (present(bibAbstract)) entry.setField("abstract", bibAbstract); /* * concatenate tags using the JabRef keyword separator */ final Set<Tag> tags = post.getTags(); final StringBuffer tagsBuffer = new StringBuffer(); for (final Tag tag : tags) { tagsBuffer.append(tag.getName() + jabRefKeywordSeparator); } /* * remove last separator */ if (!tags.isEmpty()) { tagsBuffer.delete(tagsBuffer.lastIndexOf(jabRefKeywordSeparator), tagsBuffer.length()); } final String tagsBufferString = tagsBuffer.toString(); if (present(tagsBufferString)) entry.setField(BibTexUtils.ADDITIONAL_MISC_FIELD_KEYWORDS, tagsBufferString); // set groups - will be used in jabref when exporting to bibsonomy if (present(post.getGroups())) { final Set<Group> groups = post.getGroups(); final StringBuffer groupsBuffer = new StringBuffer(); for (final Group group : groups) groupsBuffer.append(group.getName() + " "); final String groupsBufferString = groupsBuffer.toString().trim(); if (present(groupsBufferString)) entry.setField("groups", groupsBufferString); } // set comment + description final String description = post.getDescription(); if (present(description)) { entry.setField(BibTexUtils.ADDITIONAL_MISC_FIELD_DESCRIPTION, post.getDescription()); entry.setField("comment", post.getDescription()); } if (present(post.getDate())) { entry.setField("added-at", sdf.format(post.getDate())); } if (present(post.getChangeDate())) { entry.setField("timestamp", sdf.format(post.getChangeDate())); } if (present(post.getUser())) entry.setField("username", post.getUser().getName()); // set URL to bibtex version of this entry (bibrecord = ...) entry.setField(BibTexUtils.ADDITIONAL_MISC_FIELD_BIBURL, urlGen.getPublicationUrl(bibtex, post.getUser()).toString()); return entry; } catch (final Exception e) { log.error("Could not convert BibSonomy post into a JabRef BibTeX entry.", e); } return null; }
From source file:org.vmguys.reflect.BeanSearchUtil.java
/** Constructs an instance with given class. * @param cls <code>Class</code> instance. *//*from w w w . j av a 2s. c om*/ public BeanSearchUtil(Class cls) throws IntrospectionException { beanInfo = Introspector.getBeanInfo(cls); properties = beanInfo.getPropertyDescriptors(); }
From source file:BeanUtility.java
public static void setProperty(Object o, String propertyName, Object value) throws Exception { PropertyDescriptor[] pds = Introspector.getBeanInfo(o.getClass()).getPropertyDescriptors(); for (int i = 0; i < pds.length; i++) { if (pds[i].getName().equals(propertyName)) { pds[i].getWriteMethod().invoke(o, value); return; }//from w w w . j av a 2 s .com } throw new Exception("Property not found."); }
From source file:com.zuora.api.object.Dynamic.java
/** * Answers the name and values of the both static and dynamic properties of this object * @return this object's properties, as string-object pairs */// ww w. j a va 2s. c o m private Collection<Entry<String, Object>> propertyValues() { try { return collect(Arrays.asList(Introspector.getBeanInfo(this.getClass()).getPropertyDescriptors()), new Transformer() { public Object transform(Object input) { PropertyDescriptor p = (PropertyDescriptor) input; return new DefaultMapEntry(p.getName(), NaiveProperties.get(Dynamic.this, p.getName())); } }); } catch (Exception e) { throw MuleSoftException.soften(e); } }
From source file:org.grails.datastore.mapping.reflect.ClassPropertyFetcher.java
private void init() { List<Class> allClasses = resolveAllClasses(clazz); for (Class c : allClasses) { Field[] fields = c.getDeclaredFields(); for (Field field : fields) { processField(field);/*from w w w . j a va 2 s . c o m*/ } Method[] methods = c.getDeclaredMethods(); for (Method method : methods) { processMethod(method); } } try { propertyDescriptors = Introspector.getBeanInfo(clazz).getPropertyDescriptors(); } catch (IntrospectionException e) { // ignore } if (propertyDescriptors == null) { return; } for (PropertyDescriptor desc : propertyDescriptors) { propertyDescriptorsByName.put(desc.getName(), desc); final Class<?> propertyType = desc.getPropertyType(); if (propertyType == null) continue; List<PropertyDescriptor> pds = typeToPropertyMap.get(propertyType); if (pds == null) { pds = new ArrayList<PropertyDescriptor>(); typeToPropertyMap.put(propertyType, pds); } pds.add(desc); Method readMethod = desc.getReadMethod(); if (readMethod != null) { boolean staticReadMethod = Modifier.isStatic(readMethod.getModifiers()); if (staticReadMethod) { List<PropertyFetcher> propertyFetchers = staticFetchers.get(desc.getName()); if (propertyFetchers == null) { staticFetchers.put(desc.getName(), propertyFetchers = new ArrayList<PropertyFetcher>()); } propertyFetchers.add(new GetterPropertyFetcher(readMethod, true)); } else { instanceFetchers.put(desc.getName(), new GetterPropertyFetcher(readMethod, false)); } } } }