001/*
002 * Copyright 2002-2014 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.expression;
018
019
020/**
021 * A property accessor is able to read from (and possibly write to) an object's properties.
022 * This interface places no restrictions, and so implementors are free to access properties
023 * directly as fields or through getters or in any other way they see as appropriate.
024 *
025 * <p>A resolver can optionally specify an array of target classes for which it should be
026 * called. However, if it returns {@code null} from {@link #getSpecificTargetClasses()},
027 * it will be called for all property references and given a chance to determine if it
028 * can read or write them.
029 *
030 * <p>Property resolvers are considered to be ordered and each will be called in turn.
031 * The only rule that affects the call order is that any naming the target class directly
032 * in {@link #getSpecificTargetClasses()} will be called first, before the general resolvers.
033 *
034 * @author Andy Clement
035 * @since 3.0
036 */
037public interface PropertyAccessor {
038
039        /**
040         * Return an array of classes for which this resolver should be called.
041         * <p>>Returning {@code null} indicates this is a general resolver that
042         * can be called in an attempt to resolve a property on any type.
043         * @return an array of classes that this resolver is suitable for
044         * (or {@code null} if a general resolver)
045         */
046        Class<?>[] getSpecificTargetClasses();
047
048        /**
049         * Called to determine if a resolver instance is able to access a specified property
050         * on a specified target object.
051         * @param context the evaluation context in which the access is being attempted
052         * @param target the target object upon which the property is being accessed
053         * @param name the name of the property being accessed
054         * @return true if this resolver is able to read the property
055         * @throws AccessException if there is any problem determining whether the property can be read
056         */
057        boolean canRead(EvaluationContext context, Object target, String name) throws AccessException;
058
059        /**
060         * Called to read a property from a specified target object.
061         * Should only succeed if {@link #canRead} also returns {@code true}.
062         * @param context the evaluation context in which the access is being attempted
063         * @param target the target object upon which the property is being accessed
064         * @param name the name of the property being accessed
065         * @return a TypedValue object wrapping the property value read and a type descriptor for it
066         * @throws AccessException if there is any problem accessing the property value
067         */
068        TypedValue read(EvaluationContext context, Object target, String name) throws AccessException;
069
070        /**
071         * Called to determine if a resolver instance is able to write to a specified
072         * property on a specified target object.
073         * @param context the evaluation context in which the access is being attempted
074         * @param target the target object upon which the property is being accessed
075         * @param name the name of the property being accessed
076         * @return true if this resolver is able to write to the property
077         * @throws AccessException if there is any problem determining whether the
078         * property can be written to
079         */
080        boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException;
081
082        /**
083         * Called to write to a property on a specified target object.
084         * Should only succeed if {@link #canWrite} also returns {@code true}.
085         * @param context the evaluation context in which the access is being attempted
086         * @param target the target object upon which the property is being accessed
087         * @param name the name of the property being accessed
088         * @param newValue the new value for the property
089         * @throws AccessException if there is any problem writing to the property value
090         */
091        void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException;
092
093}