001/* 002 * Copyright 2002-2018 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.spel.support; 018 019import java.lang.reflect.Method; 020 021/** 022 * A {@link org.springframework.expression.PropertyAccessor} variant for data binding 023 * purposes, using reflection to access properties for reading and possibly writing. 024 * 025 * <p>A property can be referenced through a public getter method (when being read) 026 * or a public setter method (when being written), and also as a public field. 027 * 028 * <p>This accessor is explicitly designed for user-declared properties and does not 029 * resolve technical properties on {@code java.lang.Object} or {@code java.lang.Class}. 030 * For unrestricted resolution, choose {@link ReflectivePropertyAccessor} instead. 031 * 032 * @author Juergen Hoeller 033 * @since 4.3.15 034 * @see #forReadOnlyAccess() 035 * @see #forReadWriteAccess() 036 * @see SimpleEvaluationContext 037 * @see StandardEvaluationContext 038 * @see ReflectivePropertyAccessor 039 */ 040public class DataBindingPropertyAccessor extends ReflectivePropertyAccessor { 041 042 /** 043 * Create a new property accessor for reading and possibly also writing. 044 * @param allowWrite whether to also allow for write operations 045 * @see #canWrite 046 */ 047 private DataBindingPropertyAccessor(boolean allowWrite) { 048 super(allowWrite); 049 } 050 051 @Override 052 protected boolean isCandidateForProperty(Method method, Class<?> targetClass) { 053 Class<?> clazz = method.getDeclaringClass(); 054 return (clazz != Object.class && clazz != Class.class && !ClassLoader.class.isAssignableFrom(targetClass)); 055 } 056 057 058 /** 059 * Create a new data-binding property accessor for read-only operations. 060 */ 061 public static DataBindingPropertyAccessor forReadOnlyAccess() { 062 return new DataBindingPropertyAccessor(false); 063 } 064 065 /** 066 * Create a new data-binding property accessor for read-write operations. 067 */ 068 public static DataBindingPropertyAccessor forReadWriteAccess() { 069 return new DataBindingPropertyAccessor(true); 070 } 071 072}