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;
020import java.lang.reflect.Modifier;
021import java.util.List;
022
023import org.springframework.core.convert.TypeDescriptor;
024import org.springframework.expression.AccessException;
025import org.springframework.expression.EvaluationContext;
026import org.springframework.expression.MethodExecutor;
027
028/**
029 * A {@link org.springframework.expression.MethodResolver} variant for data binding
030 * purposes, using reflection to access instance methods on a given target object.
031 *
032 * <p>This accessor does not resolve static methods and also no technical methods
033 * on {@code java.lang.Object} or {@code java.lang.Class}.
034 * For unrestricted resolution, choose {@link ReflectiveMethodResolver} instead.
035 *
036 * @author Juergen Hoeller
037 * @since 4.3.15
038 * @see #forInstanceMethodInvocation()
039 * @see DataBindingPropertyAccessor
040 */
041public class DataBindingMethodResolver extends ReflectiveMethodResolver {
042
043        private DataBindingMethodResolver() {
044                super();
045        }
046
047        @Override
048        public MethodExecutor resolve(EvaluationContext context, Object targetObject, String name,
049                        List<TypeDescriptor> argumentTypes) throws AccessException {
050
051                if (targetObject instanceof Class) {
052                        throw new IllegalArgumentException("DataBindingMethodResolver does not support Class targets");
053                }
054                return super.resolve(context, targetObject, name, argumentTypes);
055        }
056
057        @Override
058        protected boolean isCandidateForInvocation(Method method, Class<?> targetClass) {
059                if (Modifier.isStatic(method.getModifiers())) {
060                        return false;
061                }
062                Class<?> clazz = method.getDeclaringClass();
063                return (clazz != Object.class && clazz != Class.class && !ClassLoader.class.isAssignableFrom(targetClass));
064        }
065
066
067        /**
068         * Create a new data-binding method resolver for instance method resolution.
069         */
070        public static DataBindingMethodResolver forInstanceMethodInvocation() {
071                return new DataBindingMethodResolver();
072        }
073
074}