1 /*
2 * Copyright 2002-2014 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 from (and possibly write to) an object's properties.
22 * This interface places no restrictions, and so implementors are free to access properties
23 * directly as fields or through getters or in any other way they see as appropriate.
24 *
25 * <p>A resolver can optionally specify an array of target classes for which it should be
26 * called. However, if it returns {@code null} from {@link #getSpecificTargetClasses()},
27 * it will be called for all property references and given a chance to determine if it
28 * can read or write them.
29 *
30 * <p>Property resolvers are considered to be ordered and each will be called in turn.
31 * The only rule that affects the call order is that any naming the target class directly
32 * in {@link #getSpecificTargetClasses()} will be called first, before the general resolvers.
33 *
34 * @author Andy Clement
35 * @since 3.0
36 */
37 public interface PropertyAccessor {
38
39 /**
40 * Return an array of classes for which this resolver should be called.
41 * <p>>Returning {@code null} indicates this is a general resolver that
42 * can be called in an attempt to resolve a property on any type.
43 * @return an array of classes that this resolver is suitable for
44 * (or {@code null} if a general resolver)
45 */
46 Class<?>[] getSpecificTargetClasses();
47
48 /**
49 * Called to determine if a resolver instance is able to access a specified property
50 * on a specified target object.
51 * @param context the evaluation context in which the access is being attempted
52 * @param target the target object upon which the property is being accessed
53 * @param name the name of the property being accessed
54 * @return true if this resolver is able to read the property
55 * @throws AccessException if there is any problem determining whether the property can be read
56 */
57 boolean canRead(EvaluationContext context, Object target, String name) throws AccessException;
58
59 /**
60 * Called to read a property from a specified target object.
61 * Should only succeed if {@link #canRead} also returns {@code true}.
62 * @param context the evaluation context in which the access is being attempted
63 * @param target the target object upon which the property is being accessed
64 * @param name the name of the property being accessed
65 * @return a TypedValue object wrapping the property value read and a type descriptor for it
66 * @throws AccessException if there is any problem accessing the property value
67 */
68 TypedValue read(EvaluationContext context, Object target, String name) throws AccessException;
69
70 /**
71 * Called to determine if a resolver instance is able to write to a specified
72 * property on a specified target object.
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 * @return true if this resolver is able to write to the property
77 * @throws AccessException if there is any problem determining whether the
78 * property can be written to
79 */
80 boolean canWrite(EvaluationContext context, Object target, String name) throws AccessException;
81
82 /**
83 * Called to write to a property on a specified target object.
84 * Should only succeed if {@link #canWrite} also returns {@code true}.
85 * @param context the evaluation context in which the access is being attempted
86 * @param target the target object upon which the property is being accessed
87 * @param name the name of the property being accessed
88 * @param newValue the new value for the property
89 * @throws AccessException if there is any problem writing to the property value
90 */
91 void write(EvaluationContext context, Object target, String name, Object newValue) throws AccessException;
92
93 }