public static void main(String[] args)throws Exception { BeanInfo bi = Introspector.getBeanInfo( BadBeanHidden.class ); PropertyDescriptor[] ps = bi.getPropertyDescriptors(); for ( int i = 0; i < ps.length; i++ ) { System.out.println( i + " : " + ps[i]); System.out.println(" Read : " + ps[i].getReadMethod() ); System.out.println(" Write : " + ps[i].getWriteMethod() ); System.out.println(" TYPE " + ps[i].getPropertyType() ); if ( ps[i] instanceof IndexedPropertyDescriptor ) { System.out.println(" I Read : " + ((IndexedPropertyDescriptor)ps[i]).getIndexedReadMethod() ); System.out.println(" I Write : " +((IndexedPropertyDescriptor)ps[i]).getIndexedWriteMethod() ); System.out.println(" TYPE " + ((IndexedPropertyDescriptor)ps[i]).getIndexedPropertyType() ); } } }
private static IndexedPropertyDescriptor _cloneIndexedPropertyDescriptor( IndexedPropertyDescriptor oldDescriptor ) { try { IndexedPropertyDescriptor newDescriptor = new IndexedPropertyDescriptor( oldDescriptor.getName(), oldDescriptor.getReadMethod(), oldDescriptor.getWriteMethod(), oldDescriptor.getIndexedReadMethod(), oldDescriptor.getIndexedWriteMethod()); // copy the rest of the attributes _copyPropertyDescriptor(oldDescriptor, newDescriptor); return newDescriptor; } catch (Exception e) { _LOG.severe(e); return null; } }
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) { for (PropertyDescriptor pd : this.propertyDescriptors) { final Class<?> candidateType; final String candidateName = pd.getName(); if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; candidateType = ipd.getIndexedPropertyType(); if (candidateName.equals(propertyName) && (candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) { return pd; } } else { candidateType = pd.getPropertyType(); if (candidateName.equals(propertyName) && (candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) { return pd; } } } return null; }
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj != null && obj instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor other = (IndexedPropertyDescriptor) obj; if (!compareMethods(getIndexedReadMethod(), other.getIndexedReadMethod())) { return false; } if (!compareMethods(getIndexedWriteMethod(), other.getIndexedWriteMethod())) { return false; } if (getIndexedPropertyType() != other.getIndexedPropertyType()) { return false; } return PropertyDescriptorUtils.equals(this, obj); } return false; }
private static Property makeProperty(Object o, PropertyDescriptor propertyDescriptor) { Class propertyType = propertyDescriptor.getPropertyType(); if (propertyType != null && Vector.class.isAssignableFrom(propertyType)) { return new VectorProperty(o, propertyDescriptor); } if (propertyType != null && IAtomList.class.isAssignableFrom(propertyType)) { return new AtomListProperty(o, propertyDescriptor); } if (propertyType != null && IMoleculeList.class.isAssignableFrom(propertyType)) { return new MoleculeListProperty(o, propertyDescriptor); } if (!(propertyDescriptor instanceof IndexedPropertyDescriptor) && propertyType.isArray() && !(propertyType.getComponentType().isPrimitive() || propertyType.getComponentType().equals(String.class))) { return new ArrayProperty(o, propertyDescriptor); } return new InstanceProperty(o, propertyDescriptor); }
private static PropertyDescriptor create(PropertyDescriptor pd) { try { if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; return new IndexedPropertyDescriptor( ipd.getName(), ipd.getReadMethod(), ipd.getWriteMethod(), ipd.getIndexedReadMethod(), ipd.getIndexedWriteMethod()); } else { return new PropertyDescriptor( pd.getName(), pd.getReadMethod(), pd.getWriteMethod()); } } catch (IntrospectionException exception) { exception.printStackTrace(); return null; } }
private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) { PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size"); if (pd != null) { test(type, "read", read, null != pd.getReadMethod()); test(type, "write", write, null != pd.getWriteMethod()); if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod()); test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod()); } else if (readIndexed || writeIndexed) { error(type, "indexed property does not exist"); } } else if (read || write || readIndexed || writeIndexed) { error(type, "property does not exist"); } }
public static void main(String[] args) throws Exception { Class<?> bt = Accessor.getBeanType(); Class<?> lt = Accessor.getListenerType(); // test PropertyDescriptor PropertyDescriptor pd = new PropertyDescriptor("boolean", bt); test(pd.getReadMethod()); test(pd.getWriteMethod()); // test IndexedPropertyDescriptor IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor("indexed", bt); test(ipd.getReadMethod()); test(ipd.getWriteMethod()); test(ipd.getIndexedReadMethod()); test(ipd.getIndexedWriteMethod()); // test EventSetDescriptor EventSetDescriptor esd = new EventSetDescriptor(bt, "test", lt, "process"); test(esd.getAddListenerMethod()); test(esd.getRemoveListenerMethod()); test(esd.getGetListenerMethod()); test(esd.getListenerMethods()); }
/** * Return the Java Class representing the property type of the specified property, or * <code>null</code> if there is no such property for the specified bean. This method * follows the same name resolution rules used by <code>getPropertyDescriptor()</code>, * so if the last element of a name reference is indexed, the type of the property itself * will be returned. If the last (or only) element has no property with the specified * name, <code>null</code> is returned. * * @param bean Bean for which a property descriptor is requested * @param name Possibly indexed and/or nested name of the property for which a property * descriptor is requested * @throws IllegalAccessException if the caller does not have access to the property * accessor method * @throws IllegalArgumentException if <code>bean</code> or <code>name</code> are null * or if a nested reference to a property returns null * @throws InvocationTargetException if the property accessor method throws an exception * @throws NoSuchMethodException if an accessor method for this property cannot be * found */ public static Class getPropertyType(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if(bean == null) throw new IllegalArgumentException("No bean specified"); if(name == null) throw new IllegalArgumentException("No name specified"); PropertyDescriptor descriptor = getPropertyDescriptor(bean, name); if(descriptor == null) { return null; } else if(descriptor instanceof IndexedPropertyDescriptor) { return (((IndexedPropertyDescriptor) descriptor).getIndexedPropertyType()); } else if(descriptor instanceof MappedPropertyDescriptor) { return (((MappedPropertyDescriptor) descriptor).getMappedPropertyType()); } else { return descriptor.getPropertyType(); } }
public void test_MixedBooleanSimpleClass7() throws Exception { BeanInfo info = Introspector .getBeanInfo(MixedBooleanSimpleClass7.class); Method getter = MixedBooleanSimpleClass7.class .getDeclaredMethod("isList"); Method setter = MixedBooleanSimpleClass7.class.getDeclaredMethod( "setList", boolean.class); for (PropertyDescriptor pd : info.getPropertyDescriptors()) { if (propertyName.equals(pd.getName())) { assertFalse(pd instanceof IndexedPropertyDescriptor); assertEquals(getter, pd.getReadMethod()); assertEquals(setter, pd.getWriteMethod()); } } }
public void testSetIndexedWriteMethod_return() throws IntrospectionException, NoSuchMethodException, NoSuchMethodException { String propertyName = "PropertyFour"; Class<MockJavaBean> beanClass = MockJavaBean.class; Method readMethod = beanClass.getMethod("get" + propertyName, (Class[]) null); Method writeMethod = beanClass.getMethod("set" + propertyName, new Class[] { String[].class }); Method indexedReadMethod = beanClass.getMethod("get" + propertyName, new Class[] { Integer.TYPE }); IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor( propertyName, readMethod, writeMethod, indexedReadMethod, null); assertNull(ipd.getIndexedWriteMethod()); Method badArgType = beanClass.getMethod("setPropertyFourInvalid", new Class[] { Integer.TYPE, String.class }); ipd.setIndexedWriteMethod(badArgType); assertEquals(String.class, ipd.getIndexedPropertyType()); assertEquals(String[].class, ipd.getPropertyType()); assertEquals(Integer.TYPE, ipd.getIndexedWriteMethod().getReturnType()); }