1 /*
2  * Copyright 2002-2009 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.springframework.expression;
18 
19 
20 /**
21  * A property accessor is able to read (and possibly write) to object properties. The interface places no restrictions
22  * and so implementors are free to access properties directly as fields or through getters or in any other way they see
23  * as appropriate. A resolver can optionally specify an array of target classes for which it should be called - but if
24  * it returns null from getSpecificTargetClasses() then it will be called for all property references and given a chance
25  * to determine if it can read or write them. Property resolvers are considered to be ordered and each will be called in
26  * turn. The only rule that affects the call order is that any naming the target class directly in
27  * getSpecifiedTargetClasses() will be called first, before the general resolvers.
28  *
29  * @author Andy Clement
30  * @since 3.0
31  */
32 public interface PropertyAccessor {
33 
34 	/**
35 	 * Return an array of classes for which this resolver should be called. Returning null indicates this is a general
36 	 * resolver that can be called in an attempt to resolve a property on any type.
37 	 * @return an array of classes that this resolver is suitable for (or null if a general resolver)
38 	 */
getSpecificTargetClasses()39 	Class[] getSpecificTargetClasses();
40 
41 	/**
42 	 * Called to determine if a resolver instance is able to access a specified property on a specified target object.
43 	 * @param context the evaluation context in which the access is being attempted
44 	 * @param target the target object upon which the property is being accessed
45 	 * @param name the name of the property being accessed
46 	 * @return true if this resolver is able to read the property
47 	 * @throws AccessException if there is any problem determining whether the property can be read
48 	 */
canRead(EvaluationContext context, Object target, String name)49 	boolean canRead(EvaluationContext context, Object target, String name) throws AccessException;
50 
51 	/**
52 	 * Called to read a property from a specified target object
53 	 * @param context the evaluation context in which the access is being attempted
54 	 * @param target the target object upon which the property is being accessed
55 	 * @param name the name of the property being accessed
56 	 * @return a TypedValue object wrapping the property value read and a type descriptor for it
57 	 * @throws AccessException if there is any problem accessing the property value
58 	 */
read(EvaluationContext context, Object target, String name)59 	TypedValue read(EvaluationContext context, Object target, String name) throws AccessException;
60 
61 	/**
62 	 * Called to determine if a resolver instance is able to write to a specified property on a specified target object.
63 	 * @param context the evaluation context in which the access is being attempted
64 	 * @param target the target object upon which the property is being accessed
65 	 * @param name the name of the property being accessed
66 	 * @return true if this resolver is able to write to the property
67 	 * @throws AccessException if there is any problem determining whether the property can be written to
68 	 */
canWrite(EvaluationContext context, Object target, String name)69 	boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException;
70 
71 	/**
72 	 * Called to write to a property on a specified target object. Should only succeed if canWrite() also returns true.
73 	 * @param context the evaluation context in which the access is being attempted
74 	 * @param target the target object upon which the property is being accessed
75 	 * @param name the name of the property being accessed
76 	 * @param newValue the new value for the property
77 	 * @throws AccessException if there is any problem writing to the property value
78 	 */
write(EvaluationContext context, Object target, String name, Object newValue)79 	void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException;
80 
81 }
82