List of usage examples for java.beans PropertyDescriptor getReadMethod
public synchronized Method getReadMethod()
From source file:com.cloudbees.plugins.credentials.matchers.BeanPropertyMatcher.java
/** * {@inheritDoc}/*from ww w. j a v a2s . co m*/ */ @Override public boolean matches(@NonNull Credentials item) { try { BeanInfo beanInfo = Introspector.getBeanInfo(item.getClass()); for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) { if (name.equals(pd.getName())) { Method readMethod = pd.getReadMethod(); if (readMethod == null) { return false; // we cannot read it therefore it cannot be a match } try { Object actual = readMethod.invoke(item); return expected == null ? actual == null : expected.equals(actual); } catch (IllegalAccessException e) { return false; // if we cannot access it then it's not a match } catch (InvocationTargetException e) { return false; // if we cannot access it then it's not a match } } } return false; // if there is no corresponding property then it cannot be a match } catch (IntrospectionException e) { return false; // if we cannot introspect it then it cannot be a match } }
From source file:net.sf.beanlib.provider.BeanChecker.java
/** * @param fBean from bean// ww w . j a va2s . 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:no.sesat.search.datamodel.DataModelFactoryImplTest.java
/** * * @throws java.lang.Exception//from w ww . j a va 2 s . co m */ @Test public void testDataObjectGetters() throws Exception { LOG.info("testDataObjectGetters()"); scan(DataObject.class, DataModel.class, new Command() { public void execute(Object... args) { try { final Class<?> cls = (Class<?>) args[0]; final Object dataObject = testInstantiate(cls); final PropertyDescriptor[] properties = Introspector.getBeanInfo(cls).getPropertyDescriptors(); for (PropertyDescriptor property : properties) { if (null != property.getReadMethod()) { final Object value = invoke(property.getReadMethod(), dataObject, new Object[0]); LOG.info(" Getter on " + property.getName() + " returned " + value); } if (property instanceof MappedPropertyDescriptor) { final MappedPropertyDescriptor mappedProperty = (MappedPropertyDescriptor) property; if (null != mappedProperty.getReadMethod()) { final Object value = invoke(mappedProperty.getMappedReadMethod(), dataObject, ""); LOG.info(" Getter on " + mappedProperty.getName() + " returned " + value); } } } } catch (IntrospectionException ie) { LOG.info(ie.getMessage(), ie); throw new RuntimeException(ie.getMessage(), ie); } } }); }
From source file:net.urosk.reportEngine.BirtConfigs.java
public void printOutProperties() throws Exception { final BeanWrapper wrapper = new BeanWrapperImpl(this); for (final PropertyDescriptor descriptor : wrapper.getPropertyDescriptors()) { logger.info(descriptor.getName() + ":" + descriptor.getReadMethod().invoke(this)); }/*from ww w . java 2 s .c om*/ }
From source file:org.codehaus.groovy.grails.commons.GrailsClassUtils.java
/** * Checks whether the specified property is inherited from a super class * * @param clz The class to check//from w w w .ja va 2s . c o m * @param propertyName The property name * @return true if the property is inherited */ @SuppressWarnings("rawtypes") public static boolean isPropertyInherited(Class clz, String propertyName) { if (clz == null) return false; Assert.isTrue(!StringUtils.isBlank(propertyName), "Argument [propertyName] cannot be null or blank"); Class<?> superClass = clz.getSuperclass(); PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(superClass, propertyName); if (pd != null && pd.getReadMethod() != null) { return true; } return false; }
From source file:com.webpagebytes.cms.engine.JSONToFromObjectConverter.java
public org.json.JSONObject JSONFromObject(Object object) { org.json.JSONObject json = new org.json.JSONObject(); if (null == object) return json; Class<? extends Object> objClass = object.getClass(); Field[] fields = objClass.getDeclaredFields(); for (Field field : fields) { Object storeAdn = field.getAnnotation(WPBAdminFieldStore.class); if (storeAdn == null) { storeAdn = field.getAnnotation(WPBAdminFieldKey.class); if (storeAdn == null) { storeAdn = field.getAnnotation(WPBAdminFieldTextStore.class); if (storeAdn == null) { storeAdn = field.getAnnotation(WPBAdminField.class); }/*from ww w. jav a 2 s .co m*/ } } if (storeAdn != null) { String fieldName = field.getName(); try { PropertyDescriptor pd = new PropertyDescriptor(fieldName, objClass); Object value = pd.getReadMethod().invoke(object); String fieldValue = JSONStringFromField(value, field.getType()); if (fieldValue != null) { json.put(fieldName, fieldValue); } } catch (Exception e) { // do nothing, there is no write method for our field } } } return json; }
From source file:org.codehaus.enunciate.modules.xfire.EnunciatedJAXWSWebFaultHandler.java
@Override protected Object getFaultBean(Throwable fault, MessagePartInfo faultPart, MessageContext context) { Class<? extends Throwable> faultClass = fault.getClass(); boolean conformsToJAXWSFaultPattern = conformsToJAXWSFaultPattern(faultClass); if (conformsToJAXWSFaultPattern) { try {/*w w w .j a va 2s . c om*/ return faultClass.getMethod("getFaultInfo").invoke(fault); } catch (NoSuchMethodException e) { //fall through. doesn't conform to the spec pattern. } catch (IllegalAccessException e) { throw new XFireRuntimeException("Couldn't invoke getFaultInfo method.", e); } catch (InvocationTargetException e) { throw new XFireRuntimeException("Couldn't invoke getFaultInfo method.", e); } } //doesn't conform to the spec pattern, use the generated fault bean class. Class faultBeanClass = getFaultBeanClass(faultClass); if (faultBeanClass == null) { return null; } try { BeanInfo beanInfo = Introspector.getBeanInfo(faultBeanClass, Object.class); Object faultBean = faultBeanClass.newInstance(); for (PropertyDescriptor property : beanInfo.getPropertyDescriptors()) { if ((property.getWriteMethod() != null) && (property.getReadMethod() != null)) { Method getter = faultClass.getMethod(property.getReadMethod().getName()); property.getWriteMethod().invoke(faultBean, getter.invoke(fault)); } } return faultBean; } catch (IntrospectionException e) { throw new XFireRuntimeException("Unable to introspect fault bean class.", e); } catch (IllegalAccessException e) { throw new XFireRuntimeException("Unable to create fault bean.", e); } catch (InstantiationException e) { throw new XFireRuntimeException("Unable to create fault bean.", e); } catch (NoSuchMethodException e) { throw new XFireRuntimeException("The fault " + faultClass.getName() + " doesn't have a needed getter method used to fill in its fault bean.", e); } catch (InvocationTargetException e) { throw new XFireRuntimeException("Unable to create fault bean.", e); } }
From source file:com.emc.ecs.sync.config.ConfigWrapper.java
public ConfigWrapper(Class<C> targetClass) { try {// w ww. j a v a 2s.c om this.targetClass = targetClass; if (targetClass.isAnnotationPresent(StorageConfig.class)) this.uriPrefix = targetClass.getAnnotation(StorageConfig.class).uriPrefix(); if (targetClass.isAnnotationPresent(FilterConfig.class)) this.cliName = targetClass.getAnnotation(FilterConfig.class).cliName(); if (targetClass.isAnnotationPresent(Label.class)) this.label = targetClass.getAnnotation(Label.class).value(); if (targetClass.isAnnotationPresent(Documentation.class)) this.documentation = targetClass.getAnnotation(Documentation.class).value(); BeanInfo beanInfo = Introspector.getBeanInfo(targetClass); for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) { if (descriptor.getReadMethod().isAnnotationPresent(Option.class)) { propertyMap.put(descriptor.getName(), new ConfigPropertyWrapper(descriptor)); } } for (MethodDescriptor descriptor : beanInfo.getMethodDescriptors()) { Method method = descriptor.getMethod(); if (method.isAnnotationPresent(UriParser.class)) { if (method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 1 && method.getParameterTypes()[0].equals(String.class)) { uriParser = method; } else { log.warn("illegal signature for @UriParser method {}.{}", targetClass.getSimpleName(), method.getName()); } } else if (method.isAnnotationPresent(UriGenerator.class)) { if (method.getReturnType().equals(String.class) && method.getParameterTypes().length == 0) { uriGenerator = method; } else { log.warn("illegal signature for @UriGenerator method {}.{}", targetClass.getSimpleName(), method.getName()); } } } if (propertyMap.isEmpty()) log.info("no @Option annotations found in {}", targetClass.getSimpleName()); } catch (IntrospectionException e) { throw new RuntimeException(e); } }
From source file:com.github.mrgoro.interactivedata.spring.service.SwaggerService.java
/** * Get Parameters from a// w w w . j av a 2s. c o m * {@link com.github.mrgoro.interactivedata.api.data.operations.OperationData OperationData}-Class. * * @param dataClass Operation Data Class * @param type Type of operation * @return Map of parameters with their names as keys */ private Map<String, Parameter> getParametersForClass(Class<?> dataClass, String type) { Map<String, Parameter> parameters = new HashMap<>(); try { PropertyDescriptor[] propertyDescriptors = Introspector.getBeanInfo(dataClass).getPropertyDescriptors(); for (PropertyDescriptor pd : propertyDescriptors) { // Class is a property descriptor => filter out if (pd.getReadMethod() != null && !"class".equals(pd.getName())) { parameters.put(pd.getName(), new QueryParameter().name(pd.getName()).description(type + " param for " + pd.getName()) .required(false).property(getProperty(pd.getPropertyType()))); } } } catch (IntrospectionException e) { log.error("Exception using introspection for generating Interactive Data API (Parameters)", e); } return parameters; }
From source file:com.github.dactiv.common.bundle.BeanResourceBundle.java
/** * ?bean?mapkey/*from ww w .j ava 2 s.c om*/ */ @Override public Enumeration<String> getKeys() { Vector<String> vector = new Vector<String>(); PropertyDescriptor[] propertyDescriptors = BeanUtils.getPropertyDescriptors(bean.getClass()); //???bean for (PropertyDescriptor pd : propertyDescriptors) { //get/set??? if ((pd.getWriteMethod() == null) || pd.getReadMethod() == null) { continue; } String key = pd.getName(); /* * ?map?: * 1.include * 2.?exclude * 3.ignoreNullValuetrue,?null */ if (isIncludeProperty(key) && !isExcludeProperty(key)) { if (ignoreNullValue && ReflectionUtils.invokeGetterMethod(bean, key) == null) { continue; } vector.addElement(key); } } //?mapkey return vector.elements(); }