// TODO check for non-existing readMethod Object value = new PropertyDescriptor("name", Person.class).getReadMethod().invoke(person);
Method getter = new PropertyDescriptor(propertyName, beanClass).getReadMethod();
private static void setRendererEncoding(Object renderer, String encoding) throws IllegalAccessException, InvocationTargetException { try { PropertyDescriptor encodingProperty = new PropertyDescriptor("encoding", renderer.getClass()); Method method = encodingProperty.getWriteMethod(); if (method != null) { method.invoke(renderer, encoding); } } catch (IntrospectionException ignored) { // ignored - maybe this renderer doesn't have a encoding property } }
/** * Creates a property descriptor for a fluent API property. * * @param m the set method for the fluent API property * @param propertyName the name of the corresponding property * @return the descriptor * @throws IntrospectionException if an error occurs */ private PropertyDescriptor createFluentPropertyDescritor(final Method m, final String propertyName) throws IntrospectionException { return new PropertyDescriptor(propertyName(m), null, m); } }
private PropertyDescriptor createFluentPropertyDescritor(Method m, String propertyName) throws IntrospectionException { return new PropertyDescriptor(propertyName(m), null, m); }
/** * Creates a property descriptor for a fluent API property. * * @param m the set method for the fluent API property * @param propertyName the name of the corresponding property * @return the descriptor * @throws IntrospectionException if an error occurs */ private PropertyDescriptor createFluentPropertyDescritor(final Method m, final String propertyName) throws IntrospectionException { return new PropertyDescriptor(propertyName(m), null, m); } }
private static Method getSetter(Object obj, String fieldName) throws IOException { PropertyDescriptor desc; try { desc = new PropertyDescriptor(fieldName, obj.getClass()); } catch (IntrospectionException e) { throw new IOException(e); } Method setter = desc.getWriteMethod(); if (setter == null) { throw new IOException( String.format("No setter for the name \"%s\".", fieldName)); } if (setter.getParameterCount() != 1) { // not a setter /* * Actually should not happen as it is not considered as a * writer method so an exception would be thrown earlier. */ throw new IOException( String.format("The setter \"%s\" for the name \"%s\" does not take exactly 1 parameter.", setter.getName(), fieldName)); } return setter; }
private Object getPropertyValue(String name, Object obj) throws HystrixCacheKeyGenerationException { try { return new PropertyDescriptor(name, obj.getClass()) .getReadMethod().invoke(obj); } catch (IllegalAccessException e) { throw new HystrixCacheKeyGenerationException(e); } catch (IntrospectionException e) { throw new HystrixCacheKeyGenerationException(e); } catch (InvocationTargetException e) { throw new HystrixCacheKeyGenerationException(e); } }
@Override public PropertyDescriptor[] getPropertyDescriptors() { try { return new PropertyDescriptor[] { new PropertyDescriptor("property", BeanWithInfo.class, "property", null) }; } catch (IntrospectionException e) { throw new AssertionError("Introspection exception", e); } } }
PropertyDescriptor desc = new PropertyDescriptor(field, obj.getClass()); Method getter = desc.getReadMethod();
for(int i=0; i<columnNames.length; i++) { String propertyName = columnNames[i]; PropertyDescriptor desc = new PropertyDescriptor(propertyName, entityClass); Method readMethod = desc.getReadMethod(); Object propertyValue = readMethod.invoke(target);
@Test public void propertyDescriptorComparator() throws IntrospectionException { ExtendedBeanInfo.PropertyDescriptorComparator c = new ExtendedBeanInfo.PropertyDescriptorComparator(); assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("a", null, null)), equalTo(0)); assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abc", null, null)), equalTo(0)); assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("b", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor("b", null, null), new PropertyDescriptor("a", null, null)), greaterThan(0)); assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("abd", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor("xyz", null, null), new PropertyDescriptor("123", null, null)), greaterThan(0)); assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("abc", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("a", null, null)), greaterThan(0)); assertThat(c.compare(new PropertyDescriptor("abc", null, null), new PropertyDescriptor("b", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor(" ", null, null), new PropertyDescriptor("a", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor("1", null, null), new PropertyDescriptor("a", null, null)), lessThan(0)); assertThat(c.compare(new PropertyDescriptor("a", null, null), new PropertyDescriptor("A", null, null)), greaterThan(0)); }
continue; PropertyDescriptor pd = new PropertyDescriptor(field.getName(), clazz); Method getMethod = pd.getReadMethod(); Object o1 = getMethod.invoke(pojo1);
desc = new PropertyDescriptor(fieldName, Configuration.class); } catch (IntrospectionException ex) { throw new Exception("cannot get property descriptor for '" + fieldName + "'");
/** * There are some nasty bugs for introspection with generics. This method addresses those nasty bugs and tries to find proper methods if available * http://bugs.sun.com/view_bug.do?bug_id=6788525 * http://bugs.sun.com/view_bug.do?bug_id=6528714 * * @param clazz type to work on * @param descriptor property pair (get/set) information * @return descriptor */ private static PropertyDescriptor fixGenericDescriptor(Class<?> clazz, PropertyDescriptor descriptor) { Method readMethod = descriptor.getReadMethod(); if (readMethod != null && (readMethod.isBridge() || readMethod.isSynthetic())) { String propertyName = descriptor.getName(); //capitalize the first letter of the string; String baseName = Character.toUpperCase(propertyName.charAt(0)) + propertyName.substring(1); String setMethodName = "set" + baseName; String getMethodName = "get" + baseName; Method getMethod = findPreferablyNonSyntheticMethod(getMethodName, clazz); Method setMethod = findPreferablyNonSyntheticMethod(setMethodName, clazz); try { return new PropertyDescriptor(propertyName, getMethod, setMethod); } catch (IntrospectionException e) { //move on } } return descriptor; }
PropertyDescriptor descriptor = new PropertyDescriptor( propName, bean.getClass());
@Override public PropertyDescriptor[] getPropertyDescriptors() { try { PropertyDescriptor pd = new PropertyDescriptor("value", ValueBean.class); pd.setPropertyEditorClass(MyNumberEditor.class); return new PropertyDescriptor[] {pd}; } catch (IntrospectionException ex) { throw new FatalBeanException("Couldn't create PropertyDescriptor", ex); } } }
public static <T> void findBooleanIsMethods(Class<T> clazz, PropertyDescriptor descriptor) throws IntrospectionException { if (descriptor.getReadMethod() == null && descriptor.getPropertyType() == Boolean.class) { try { PropertyDescriptor pd = new PropertyDescriptor(descriptor.getName(), clazz); descriptor.setReadMethod(pd.getReadMethod()); } catch (IntrospectionException e) { } } }
@Override public PropertyDescriptor[] getPropertyDescriptors() { Class beanClass = getBeanClass(); String[] properties = getPropertyNames(); PropertyDescriptor[] descriptors = new PropertyDescriptor[properties.length]; try { // For each property, create a property descriptor and set the // name and description using the localized data. for (int i = 0; i < descriptors.length; i++) { PropertyDescriptor newDescriptor = new PropertyDescriptor(properties[i], beanClass); if (bundle != null) { newDescriptor.setDisplayName(bundle.getString(properties[i] + ".displayName")); newDescriptor.setShortDescription(bundle.getString(properties[i] + ".shortDescription")); } descriptors[i] = newDescriptor; } return descriptors; } catch (IntrospectionException ie) { Log.error(ie.getMessage(), ie); throw new Error(ie.toString()); } }
@Test public void nonIndexedEquality() throws IntrospectionException, SecurityException, NoSuchMethodException { Object pd1 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null); assertThat(pd1, equalTo(pd1)); Object pd2 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, null); assertThat(pd1, equalTo(pd2)); assertThat(pd2, equalTo(pd1)); @SuppressWarnings("unused") class C { public Object setFoo(String foo) { return null; } public String getFoo() { return null; } } Method wm1 = C.class.getMethod("setFoo", String.class); Object pd3 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", null, wm1); assertThat(pd1, not(equalTo(pd3))); assertThat(pd3, not(equalTo(pd1))); Method rm1 = C.class.getMethod("getFoo"); Object pd4 = new ExtendedBeanInfo.SimplePropertyDescriptor("foo", rm1, null); assertThat(pd1, not(equalTo(pd4))); assertThat(pd4, not(equalTo(pd1))); Object pd5 = new PropertyDescriptor("foo", null, null); assertThat(pd1, equalTo(pd5)); assertThat(pd5, equalTo(pd1)); Object pd6 = "not a PD"; assertThat(pd1, not(equalTo(pd6))); assertThat(pd6, not(equalTo(pd1))); Object pd7 = null; assertThat(pd1, not(equalTo(pd7))); assertThat(pd7, not(equalTo(pd1))); }