    001// Licensed under the Apache License, Version 2.0 (the "License");
    002// you may not use this file except in compliance with the License.
    003// You may obtain a copy of the License at
    004//
    005// http://www.apache.org/licenses/LICENSE-2.0
    006//
    007// Unless required by applicable law or agreed to in writing, software
    008// distributed under the License is distributed on an "AS IS" BASIS,
    009// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    010// See the License for the specific language governing permissions and
    011// limitations under the License.
    012
    013package org.apache.tapestry5.commons.services;
    014
    015import java.lang.reflect.Field;
    016import java.lang.reflect.Method;
    017
    018import org.apache.tapestry5.commons.AnnotationProvider;
    019
    020/**
    021 * Provides access to a single property within a class. Acts as an {@link org.apache.tapestry5.commons.AnnotationProvider};
    022 * when searching for annotations, the read method (if present) is checked first, followed by the write method, followed
    023 * by the underlying field (when the property name matches the field name).
    024 *
    025 * Starting in release 5.2, this property may actually be a public field. In 5.3, it may be a public static field.
    026 *
    027 * @see org.apache.tapestry5.commons.services.ClassPropertyAdapter
    028 */
    029public interface PropertyAdapter extends AnnotationProvider
    030{
    031    /**
    032     * Returns the name of the property (or public field).
    033     */
    034    String getName();
    035
    036    /**
    037     * Returns true if the property is readable (i.e., has a getter method or is a public field).
    038     */
    039    boolean isRead();
    040
    041    /**
    042     * Returns the method used to read the property, or null if the property is not readable (or is a public field).
    043     */
    044    public Method getReadMethod();
    045
    046    /**
    047     * Returns true if the property is writeable (i.e., has a setter method or is a non-final field).
    048     */
    049    boolean isUpdate();
    050
    051    /**
    052     * Returns the method used to update the property, or null if the property is not writeable (or a public field).
    053     */
    054    public Method getWriteMethod();
    055
    056    /**
    057     * Reads the property value.
    058     *
    059     * @param instance to read from
    060     * @throws UnsupportedOperationException if the property is write only
    061     */
    062    Object get(Object instance);
    063
    064    /**
    065     * Updates the property value. The provided value must not be null if the property type is primitive, and must
    066     * otherwise be of the proper type.
    067     *
    068     * @param instance to update
    069     * @param value    new value for the property
    070     * @throws UnsupportedOperationException if the property is read only
    071     */
    072    void set(Object instance, Object value);
    073
    074    /**
    075     * Returns the type of the property.
    076     */
    077    Class getType();
    078
    079    /**
    080     * Returns true if the return type of the read method is not the same as the property type. This can occur when the
    081     * property has been defined using generics, in which case, the method's type may be Object when the property type
    082     * is something more specific. This method is primarily used when generating runtime code related to the property.
    083     */
    084    boolean isCastRequired();
    085
    086    /**
    087     * Returns the {@link org.apache.tapestry5.commons.services.ClassPropertyAdapter} that provides access to other
    088     * properties defined by the same class.
    089     */
    090    ClassPropertyAdapter getClassAdapter();
    091
    092    /**
    093     * Returns the type of bean to which this property belongs. This is the same as
    094     * {@link org.apache.tapestry5.commons.services.ClassPropertyAdapter#getBeanType()}.
    095     */
    096    Class getBeanType();
    097
    098    /**
    099     * Returns true if the property is actually a public field (possibly, a public static field).
    100     *
    101     * @since 5.2
    102     */
    103    boolean isField();
    104
    105    /**
    106     * Returns the field if the property is a public field or null if the property is accessed via the read method.
    107     *
    108     * @since 5.2
    109     */
    110    Field getField();
    111
    112    /**
    113     * The class in which the property (or public field) is defined.
    114     *
    115     * @since 5.2
    116     */
    117    Class getDeclaringClass();
    118}