📄 propertydescriptor.java
字号:
return constrained; } /** Set whether the property is constrained. ** If the set method throws <CODE>java.beans.PropertyVetoException</CODE> ** (or subclass thereof) and the bean implements addVetoableChangeListener() ** and removeVetoableChangeListener(), then setConstrained(true) may safely ** be called. Otherwise, the system behavior is undefined. ** <B>Spec note:</B> given those strict parameters, it would be nice if it ** got set automatically by detection, but oh well.<P> ** When a property is constrained, its set method is required to:<P> ** <OL> ** <LI>Fire the <CODE>VetoableChangeListener.vetoableChange()</CODE> ** event notifying others of the change and allowing them a chance to ** say it is a bad thing.</LI> ** <LI>If any of the listeners throws a PropertyVetoException, then ** it must fire another vetoableChange() event notifying the others ** of a reversion to the old value (though, of course, the change ** was never made). Then it rethrows the PropertyVetoException and ** exits.</LI> ** <LI>If all has gone well to this point, the value may be changed.</LI> ** </OL> ** @param constrained whether the property is constrained or not. **/ public void setConstrained(boolean constrained) { this.constrained = constrained; } /** Get the PropertyEditor class. Defaults to null. **/ public Class getPropertyEditorClass() { return propertyEditorClass; } /** Set the PropertyEditor class. If the class does not implement ** the PropertyEditor interface, you will likely get an exception ** late in the game. ** @param propertyEditorClass the PropertyEditor class for this ** class to use. **/ public void setPropertyEditorClass(Class propertyEditorClass) { this.propertyEditorClass = propertyEditorClass; } private void findMethods( Class beanClass, String getMethodName1, String getMethodName2, String setMethodName) throws IntrospectionException { try { // Try the first get method name if (getMethodName1 != null) { try { getMethod = beanClass.getMethod(getMethodName1, new Class[0]); } catch (NoSuchMethodException e) {} } // Fall back to the second get method name if (getMethod == null && getMethodName2 != null) { try { getMethod = beanClass.getMethod(getMethodName2, new Class[0]); } catch (NoSuchMethodException e) {} } // Try the set method name if (setMethodName != null) { if (getMethod != null) { // If there is a get method, use its return type to help // select the corresponding set method. Class propertyType = getMethod.getReturnType(); if (propertyType == Void.TYPE) { String msg = "The property's read method has return type 'void'"; throw new IntrospectionException(msg); } Class[] setArgs = new Class[] { propertyType }; try { setMethod = beanClass.getMethod(setMethodName, setArgs); } catch (NoSuchMethodException e) {} } else if (getMethodName1 == null && getMethodName2 == null) { // If this is a write-only property, choose the first set method // with the required name, one parameter and return type 'void' Method[] methods = beanClass.getMethods(); for (int i = 0; i < methods.length; i++) { if (methods[i].getName().equals(setMethodName) && methods[i].getParameterTypes().length == 1 && methods[i].getReturnType() == Void.TYPE) { setMethod = methods[i]; break; } } } } } catch (SecurityException e) { // FIXME -- shouldn't we just allow SecurityException to propagate? String msg = "SecurityException thrown on attempt to access methods."; throw new IntrospectionException(msg); } } /** Checks whether the given <code>Method</code> instances are legal read and * write methods. The following requirements must be met:<br/> * <ul> * <li>the read method must not have an argument</li> * <li>the read method must have a non void return type</li> * <li>the read method may not exist</li> * <li>the write method must have a single argument</li> * <li>the property type and the read method's return type must be assignable from the * write method's argument type</li> * <li>the write method may not exist</li> * </ul> * While checking the methods a common new property type is calculated. If the method * succeeds this property type is returned.<br/> * <br/> * For compatibility this has to be noted:<br/> * The two methods are allowed to be defined in two distinct classes and may both be null. * * @param readMethod The new read method to check. * @param writeMethod The new write method to check. * @return The common property type of the two method. * @throws IntrospectionException If any of the above requirements are not met. */ private Class checkMethods(Method readMethod, Method writeMethod) throws IntrospectionException { Class newPropertyType = propertyType; // a valid read method has zero arguments and a non-void return type. if (readMethod != null) { if (readMethod.getParameterTypes().length > 0) { throw new IntrospectionException("read method has unexpected parameters"); } newPropertyType = readMethod.getReturnType(); if (newPropertyType == Void.TYPE) { throw new IntrospectionException("read method return type is void"); } } // a valid write method has one argument which can be assigned to the property if (writeMethod != null) { if (writeMethod.getParameterTypes().length != 1) { String msg = "write method does not have exactly one parameter"; throw new IntrospectionException(msg); } if (readMethod == null) { // changes the property type if there is no read method newPropertyType = writeMethod.getParameterTypes()[0]; } else { // checks whether the write method can be assigned to the return type of the read // method (if this is not the case, the methods are not compatible) // note: newPropertyType may be null if no methods or method names have been // delivered in the constructor. if (newPropertyType != null && !newPropertyType.isAssignableFrom( writeMethod.getParameterTypes()[0])) { // note: newPropertyType is the same as readMethod.getReturnType() at this point throw new IntrospectionException("read and write method are not compatible"); } /* note: the check whether both method are defined in related classes makes sense but is not * done in the JDK. * I leave this code here in case someone at Sun decides to add that functionality in later versions (rschuster) if ((!readMethod .getDeclaringClass() .isAssignableFrom(writeMethod.getDeclaringClass())) && (!writeMethod .getDeclaringClass() .isAssignableFrom(readMethod.getDeclaringClass()))) { String msg = "set and get methods are not in the same class."; throw new IntrospectionException(msg); } */ } } return newPropertyType; } /** * Return a hash code for this object, conforming to the contract described * in {@link Object#hashCode()}. * @return the hash code * @since 1.5 */ public int hashCode() { return ((propertyType == null ? 0 : propertyType.hashCode()) | (propertyEditorClass == null ? 0 : propertyEditorClass.hashCode()) | (bound ? Boolean.TRUE : Boolean.FALSE).hashCode() | (constrained ? Boolean.TRUE : Boolean.FALSE).hashCode() | (getMethod == null ? 0 : getMethod.hashCode()) | (setMethod == null ? 0 : setMethod.hashCode())); } /** Compares this <code>PropertyDescriptor</code> against the * given object. * Two PropertyDescriptors are equals if * <ul> * <li>the read methods are equal</li> * <li>the write methods are equal</li> * <li>the property types are equals</li> * <li>the property editor classes are equal</li> * <li>the flags (constrained and bound) are equal</li> * </ul> * @return Whether both objects are equal according to the rules given above. * @since 1.4 */ public boolean equals(Object o) { if (o instanceof PropertyDescriptor) { PropertyDescriptor that = (PropertyDescriptor) o; // compares the property types and checks the case where both are null boolean samePropertyType = (propertyType == null) ? that.propertyType == null : propertyType.equals(that.propertyType); // compares the property editor classes and checks the case where both are null boolean samePropertyEditorClass = (propertyEditorClass == null) ? that.propertyEditorClass == null : propertyEditorClass.equals(that.propertyEditorClass); // compares the flags for equality boolean sameFlags = bound == that.bound && constrained == that.constrained; // compares the read methods and checks the case where both are null boolean sameReadMethod = (getMethod == null) ? that.getMethod == null : getMethod.equals(that.getMethod); boolean sameWriteMethod = (setMethod == null) ? that.setMethod == null : setMethod.equals(that.setMethod); return samePropertyType && sameFlags && sameReadMethod && sameWriteMethod && samePropertyEditorClass; } else { return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -