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