List of usage examples for java.beans Introspector getBeanInfo
public static BeanInfo getBeanInfo(Class<?> beanClass) throws IntrospectionException
From source file:org.dspace.app.rest.model.hateoas.DSpaceResource.java
public DSpaceResource(T data, Utils utils, String... rels) { this.data = data; if (data != null) { try {/* www . ja v a 2s.c o m*/ LinksRest links = data.getClass().getDeclaredAnnotation(LinksRest.class); if (links != null && rels != null) { List<String> relsList = Arrays.asList(rels); for (LinkRest linkAnnotation : links.links()) { if (!relsList.contains(linkAnnotation.name())) { continue; } String name = linkAnnotation.name(); Link linkToSubResource = utils.linkToSubResource(data, name); String apiCategory = data.getCategory(); String model = data.getType(); LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory, model, linkAnnotation.name()); if (!linkRepository.isEmbeddableRelation(data, linkAnnotation.name())) { continue; } try { //RestModel linkClass = linkAnnotation.linkClass().newInstance(); Method[] methods = linkRepository.getClass().getMethods(); boolean found = false; for (Method m : methods) { if (StringUtils.equals(m.getName(), linkAnnotation.method())) { // TODO add support for single linked object other than for collections Page<? extends Serializable> pageResult = (Page<? extends RestModel>) m.invoke( linkRepository, null, ((BaseObjectRest) data).getId(), null, null); EmbeddedPage ep = new EmbeddedPage(linkToSubResource.getHref(), pageResult, null); embedded.put(name, ep); found = true; } } // TODO custom exception if (!found) { throw new RuntimeException("Method for relation " + linkAnnotation.name() + " not found: " + linkAnnotation.method()); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } } for (PropertyDescriptor pd : Introspector.getBeanInfo(data.getClass()).getPropertyDescriptors()) { Method readMethod = pd.getReadMethod(); String name = pd.getName(); if (readMethod != null && !"class".equals(name)) { LinkRest linkAnnotation = readMethod.getAnnotation(LinkRest.class); if (linkAnnotation != null) { if (StringUtils.isNotBlank(linkAnnotation.name())) { name = linkAnnotation.name(); } Link linkToSubResource = utils.linkToSubResource(data, name); // no method is specified to retrieve the linked object(s) so check if it is already here if (StringUtils.isBlank(linkAnnotation.method())) { this.add(linkToSubResource); Object linkedObject = readMethod.invoke(data); Object wrapObject = linkedObject; if (linkedObject instanceof RestModel) { RestModel linkedRM = (RestModel) linkedObject; wrapObject = utils .getResourceRepository(linkedRM.getCategory(), linkedRM.getType()) .wrapResource(linkedRM); } else { if (linkedObject instanceof List) { List<RestModel> linkedRMList = (List<RestModel>) linkedObject; if (linkedRMList.size() > 0) { DSpaceRestRepository<RestModel, ?> resourceRepository = utils .getResourceRepository(linkedRMList.get(0).getCategory(), linkedRMList.get(0).getType()); // TODO should we force pagination also of embedded resource? // This will force a pagination with size 10 for embedded collections as well // int pageSize = 1; // PageImpl<RestModel> page = new PageImpl( // linkedRMList.subList(0, // linkedRMList.size() > pageSize ? pageSize : linkedRMList.size()), new PageRequest(0, pageSize), linkedRMList.size()); PageImpl<RestModel> page = new PageImpl(linkedRMList); wrapObject = new EmbeddedPage(linkToSubResource.getHref(), page.map(resourceRepository::wrapResource), linkedRMList); } else { wrapObject = null; } } } if (linkedObject != null) { embedded.put(name, wrapObject); } else { embedded.put(name, null); } Method writeMethod = pd.getWriteMethod(); writeMethod.invoke(data, new Object[] { null }); } else { // call the link repository try { //RestModel linkClass = linkAnnotation.linkClass().newInstance(); String apiCategory = data.getCategory(); String model = data.getType(); LinkRestRepository linkRepository = utils.getLinkResourceRepository(apiCategory, model, linkAnnotation.name()); Method[] methods = linkRepository.getClass().getMethods(); boolean found = false; for (Method m : methods) { if (StringUtils.equals(m.getName(), linkAnnotation.method())) { // TODO add support for single linked object other than for collections Page<? extends Serializable> pageResult = (Page<? extends RestModel>) m .invoke(linkRepository, null, ((BaseObjectRest) data).getId(), null, null); EmbeddedPage ep = new EmbeddedPage(linkToSubResource.getHref(), pageResult, null); embedded.put(name, ep); found = true; } } // TODO custom exception if (!found) { throw new RuntimeException("Method for relation " + linkAnnotation.name() + " not found: " + linkAnnotation.method()); } } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } } } else if (RestModel.class.isAssignableFrom(readMethod.getReturnType())) { Link linkToSubResource = utils.linkToSubResource(data, name); this.add(linkToSubResource); RestModel linkedObject = (RestModel) readMethod.invoke(data); if (linkedObject != null) { embedded.put(name, utils .getResourceRepository(linkedObject.getCategory(), linkedObject.getType()) .wrapResource(linkedObject)); } else { embedded.put(name, null); } Method writeMethod = pd.getWriteMethod(); writeMethod.invoke(data, new Object[] { null }); } } } } catch (IntrospectionException | IllegalArgumentException | IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e.getMessage(), e); } this.add(utils.linkToSingleResource(data, Link.REL_SELF)); } }
From source file:org.saiku.adhoc.utils.TemplateUtils.java
public static void mergeElementFormats(SaikuElementFormat source, SaikuElementFormat target) throws Exception { BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass()); for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { if (descriptor.getWriteMethod() != null) { Object sourceValue = descriptor.getReadMethod().invoke(source); if (sourceValue != null) { descriptor.getWriteMethod().invoke(target, sourceValue); }/*from ww w .j a v a2s .com*/ } } }
From source file:com.easyget.commons.csv.bean.HeaderColumnNameMappingStrategy.java
private PropertyDescriptor[] loadDescriptors(Class<T> cls) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(cls); return beanInfo.getPropertyDescriptors(); }
From source file:org.zht.framework.util.ZBeanUtil.java
@SuppressWarnings("rawtypes") public static Object convertMapToBean(Map map, Class<?> type) { Object obj = null;/*from w w w . j ava 2 s . c o m*/ try { BeanInfo beanInfo = null; beanInfo = Introspector.getBeanInfo(type); obj = type.newInstance(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor property : propertyDescriptors) { String properName = property.getName(); if (map.containsKey(property.getName())) { try {// ?? Object value = map.get(properName); Method setter = property.getWriteMethod(); setter.invoke(obj, value); //Unable to find non-private method // access.invoke(obj,"set" + ZStrUtil.toUpCaseFirst(properName),value); } catch (Exception e) { e.printStackTrace(); } } } } catch (Exception e) { e.printStackTrace(); return obj; } return obj; }
From source file:com.swingtech.commons.util.ClassUtil.java
public static List getPropertyDescriptorsFromClass(final Class pClass, final String filterRegEx) { BeanInfo vBeanInfo = null;/*ww w . j av a 2s . c om*/ PropertyDescriptor[] vPropDescList = null; PropertyDescriptor vPropDesc = null; String vPropName = null; final Vector vPropertyNameList = new Vector(); try { vBeanInfo = Introspector.getBeanInfo(pClass); } catch (final IntrospectionException e) { logger.warn("Utility.getPropertyNamesFromClass. Error trying to use Introspector", e); return null; } vPropDescList = vBeanInfo.getPropertyDescriptors(); for (int i = 0; i < vPropDescList.length; i++) { vPropDesc = vPropDescList[i]; vPropName = vPropDesc.getName(); if (Utility.isNullOrEmpty(filterRegEx) || vPropName.matches(filterRegEx)) { vPropertyNameList.add(vPropDesc); } } if (vPropertyNameList.isEmpty()) { return null; } return vPropertyNameList; }
From source file:org.rhq.core.pluginapi.util.ObjectUtil.java
/** * Looks up deep object graph attributes using a dot delimited java bean spec style path. So if I have an object A * with a Member object B with a String value C I can refer to it as "b.c" and pass in the object A and I'll get * back the value of a.getB().getC()//from www.j a va2 s. c o m * * @param value The object to look into * @param propertyPath the property path to search * * @return the value read from the object's property path */ public static Object lookupDeepAttributeProperty(Object value, String propertyPath) { if (value == null) return null; String[] ps = propertyPath.split("\\.", 2); String searchProperty = ps[0]; if (value instanceof CompositeData) { CompositeData compositeData = ((CompositeData) value); if (compositeData.containsKey(searchProperty)) { value = compositeData.get(searchProperty); } else { LOG.debug("Unable to read attribute property [" + propertyPath + "] from composite data value"); } } else { // Try to use reflection try { PropertyDescriptor[] pds = Introspector.getBeanInfo(value.getClass()).getPropertyDescriptors(); for (PropertyDescriptor pd : pds) { if (pd.getName().equals(searchProperty)) { value = pd.getReadMethod().invoke(value); } } } catch (Exception e) { LOG.debug("Unable to read property from measurement attribute [" + searchProperty + "] not found on [" + ((value != null) ? value.getClass().getSimpleName() : "null") + "]"); } } if (ps.length > 1) { value = lookupDeepAttributeProperty(value, ps[1]); } return value; }
From source file:net.cpollet.jixture.hibernate3.helper.Hibernate3MappingDefinitionHolder.java
private BeanInfo getBeanInfo(Field field) { try {//from w ww . j a v a 2s. c om return Introspector.getBeanInfo(field.getDeclaringClass()); } catch (IntrospectionException e) { throw ExceptionUtils.wrapInRuntimeException(e); } }
From source file:net.sourceforge.vulcan.web.struts.forms.PluginConfigForm.java
public void introspect(HttpServletRequest request) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { Class<?> cls = null;/* w w w . ja va2 s . com*/ if ("pluginConfig".equals(focus)) { cls = pluginConfig.getClass(); this.breadCrumbs.clear(); this.breadCrumbs.add("Setup"); if (isProjectPlugin()) { this.breadCrumbs.add("Projects"); this.breadCrumbs.add(projectName); this.breadCrumbs.add(this.pluginConfig.getPluginName()); } else { this.breadCrumbs.add("Plugins"); this.breadCrumbs.add(this.pluginConfig.getPluginName()); } } else { cls = PropertyUtils.getPropertyType(this, focus); if (cls.isArray()) { cls = cls.getComponentType(); } } final String prefix = focus + "."; final PropertyDescriptor[] pds; if (PluginConfigDto.class.isAssignableFrom(cls)) { final PluginConfigDto pluginConfig = (PluginConfigDto) getFocusObject(); final List<PropertyDescriptor> tmp = pluginConfig.getPropertyDescriptors(request.getLocale()); pds = tmp.toArray(new PropertyDescriptor[tmp.size()]); if (pluginConfig instanceof PluginProfileDto) { ((PluginProfileDto) pluginConfig).checkPoint(); } } else { final BeanInfo beanInfo = Introspector.getBeanInfo(cls); Introspector.flushFromCaches(cls); pds = beanInfo.getPropertyDescriptors(); } if (isNested()) { for (PropertyDescriptor pd : propertyDescriptors) { if (focus.startsWith(pd.getName())) { breadCrumbs.add(pd.getDisplayName()); } } } types.clear(); choices.clear(); propertyDescriptors.clear(); hiddenPasswords.clear(); for (PropertyDescriptor pd : pds) { final String name = prefix + pd.getName(); final PropertyDescriptor cp = new PropertyDescriptor(pd.getName(), pd.getReadMethod(), pd.getWriteMethod()); cp.setShortDescription(pd.getShortDescription()); cp.setDisplayName(pd.getDisplayName()); cp.setName(name); propertyDescriptors.add(cp); types.put(name, getTypeAndPrepare(name, pd)); } putBreadCrumbsInRequest(request); }
From source file:org.snaker.engine.access.jdbc.BeanPropertyHandler.java
/** * IntrospectorBeanInfo????PropertyDescriptor[] * @param c//w w w .j a v a2s.com * @return PropertyDescriptor[] * @throws SQLException */ private PropertyDescriptor[] propertyDescriptors(Class<?> c) throws SQLException { BeanInfo beanInfo = null; try { beanInfo = Introspector.getBeanInfo(c); } catch (IntrospectionException e) { throw new SQLException("Bean introspection failed: " + e.getMessage()); } return beanInfo.getPropertyDescriptors(); }
From source file:org.echocat.redprecursor.annotations.utils.AccessAlsoProtectedMembersReflectivePropertyAccessor.java
@Nonnull private Map<String, PropertyDescriptor> getPropertyNameToDescriptorFor(@Nonnull Class<?> clazz) { Map<String, PropertyDescriptor> propertyNameToDescriptor = TYPE_TO_PROPERTY_DESCRIPTORS.get(clazz); if (propertyNameToDescriptor == null) { propertyNameToDescriptor = new HashMap<String, PropertyDescriptor>(); final BeanInfo beanInfo; try {/* www .j a v a 2s.co m*/ beanInfo = Introspector.getBeanInfo(clazz); } catch (IntrospectionException e) { throw new RuntimeException("Could not load beanInfo of " + clazz.getName() + ".", e); } final PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : propertyDescriptors) { propertyNameToDescriptor.put(propertyDescriptor.getName(), propertyDescriptor); } TYPE_TO_PROPERTY_DESCRIPTORS.put(clazz, propertyNameToDescriptor); } return propertyNameToDescriptor; }