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.annotation.Annotation;
016import java.util.List;
017
018/**
019 * Organizes all {@link org.apache.tapestry5.commons.services.PropertyAdapter}s for a particular class.
020 *
021 * Only provides access to <em>simple</em> properties. Indexed properties are ignored.
022 *
023 * When accessing properties by name, the case of the name is ignored.
024 */
025public interface ClassPropertyAdapter
026{
027    /**
028     * Returns the names of all properties, sorted into alphabetic order. This includes true properties
029     * (as defined in the JavaBeans specification), but also public fields. Starting in Tapestry 5.3, even public static fields are included.
030     * @return the property names.
031     */
032    List<String> getPropertyNames();
033
034    /**
035     * Returns the type of bean this adapter provides properties for.
036     * @return the type of the bean.
037     */
038    @SuppressWarnings("rawtypes")
039    Class getBeanType();
040
041    /**
042     * Returns the property adapter with the given name, or null if no such adapter exists.
043     *
044     * @param name of the property (case is ignored)
045     * @return the PropertyAdapter instance associated with that property
046     */
047    PropertyAdapter getPropertyAdapter(String name);
048
049    /**
050     * Reads the value of a property.
051     *
052     * @param instance     the object to read a value from
053     * @param propertyName the name of the property to read (case is ignored)
054     * @throws UnsupportedOperationException if the property is write only
055     * @throws IllegalArgumentException      if property does not exist
056     * @return the value
057     */
058    Object get(Object instance, String propertyName);
059
060    /**
061     * Updates the value of a property.
062     *
063     * @param instance     the object to update
064     * @param propertyName the name of the property to update (case is ignored)
065     * @param value        the value to be set
066     * @throws UnsupportedOperationException if the property is read only
067     * @throws IllegalArgumentException      if property does not exist
068     */
069    void set(Object instance, String propertyName, Object value);
070
071    /**
072     * Returns the annotation of a given property for the specified type if such an annotation is present, else null.
073     *
074     * @param instance     the object to read a value from
075     * @param propertyName the name of the property to read (case is ignored)
076     * @param annotationClass the type of annotation to return
077     * @return the Annotation instance
078     * @throws IllegalArgumentException      if property does not exist
079     *
080     * @since 5.4
081     */
082    Annotation getAnnotation(Object instance, String propertyName, Class<? extends Annotation> annotationClass);
083}