001/* 002 * Copyright 2002-2019 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 019import org.springframework.lang.Nullable; 020 021/** 022 * A property accessor is able to read from (and possibly write to) an object's properties. 023 * 024 * <p>This interface places no restrictions, and so implementors are free to access properties 025 * directly as fields or through getters or in any other way they see as appropriate. 026 * 027 * <p>A resolver can optionally specify an array of target classes for which it should be 028 * called. However, if it returns {@code null} from {@link #getSpecificTargetClasses()}, 029 * it will be called for all property references and given a chance to determine if it 030 * can read or write them. 031 * 032 * <p>Property resolvers are considered to be ordered, and each will be called in turn. 033 * The only rule that affects the call order is that any resolver naming the target 034 * class directly in {@link #getSpecificTargetClasses()} will be called first, before 035 * the general resolvers. 036 * 037 * @author Andy Clement 038 * @since 3.0 039 */ 040public interface PropertyAccessor { 041 042 /** 043 * Return an array of classes for which this resolver should be called. 044 * <p>Returning {@code null} indicates this is a general resolver that 045 * can be called in an attempt to resolve a property on any type. 046 * @return an array of classes that this resolver is suitable for 047 * (or {@code null} if a general resolver) 048 */ 049 @Nullable 050 Class<?>[] getSpecificTargetClasses(); 051 052 /** 053 * Called to determine if a resolver instance is able to access a specified property 054 * on a specified target object. 055 * @param context the evaluation context in which the access is being attempted 056 * @param target the target object upon which the property is being accessed 057 * @param name the name of the property being accessed 058 * @return true if this resolver is able to read the property 059 * @throws AccessException if there is any problem determining whether the property can be read 060 */ 061 boolean canRead(EvaluationContext context, @Nullable Object target, String name) throws AccessException; 062 063 /** 064 * Called to read a property from a specified target object. 065 * Should only succeed if {@link #canRead} also returns {@code true}. 066 * @param context the evaluation context in which the access is being attempted 067 * @param target the target object upon which the property is being accessed 068 * @param name the name of the property being accessed 069 * @return a TypedValue object wrapping the property value read and a type descriptor for it 070 * @throws AccessException if there is any problem accessing the property value 071 */ 072 TypedValue read(EvaluationContext context, @Nullable Object target, String name) throws AccessException; 073 074 /** 075 * Called to determine if a resolver instance is able to write to a specified 076 * property on a specified target object. 077 * @param context the evaluation context in which the access is being attempted 078 * @param target the target object upon which the property is being accessed 079 * @param name the name of the property being accessed 080 * @return true if this resolver is able to write to the property 081 * @throws AccessException if there is any problem determining whether the 082 * property can be written to 083 */ 084 boolean canWrite(EvaluationContext context, @Nullable Object target, String name) throws AccessException; 085 086 /** 087 * Called to write to a property on a specified target object. 088 * Should only succeed if {@link #canWrite} also returns {@code true}. 089 * @param context the evaluation context in which the access is being attempted 090 * @param target the target object upon which the property is being accessed 091 * @param name the name of the property being accessed 092 * @param newValue the new value for the property 093 * @throws AccessException if there is any problem writing to the property value 094 */ 095 void write(EvaluationContext context, @Nullable Object target, String name, @Nullable Object newValue) 096 throws AccessException; 097 098}