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