001/* 002 * Copyright 2002-2020 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.ast; 018 019import java.util.ArrayList; 020import java.util.List; 021 022import org.springframework.expression.PropertyAccessor; 023import org.springframework.lang.Nullable; 024 025/** 026 * Utilities methods for use in the Ast classes. 027 * 028 * @author Andy Clement 029 * @since 3.0.2 030 */ 031public abstract class AstUtils { 032 033 /** 034 * Determines the set of property resolvers that should be used to try and access a 035 * property on the specified target type. The resolvers are considered to be in an 036 * ordered list, however in the returned list any that are exact matches for the input 037 * target type (as opposed to 'general' resolvers that could work for any type) are 038 * placed at the start of the list. In addition, there are specific resolvers that 039 * exactly name the class in question and resolvers that name a specific class but it 040 * is a supertype of the class we have. These are put at the end of the specific resolvers 041 * set and will be tried after exactly matching accessors but before generic accessors. 042 * @param targetType the type upon which property access is being attempted 043 * @return a list of resolvers that should be tried in order to access the property 044 */ 045 public static List<PropertyAccessor> getPropertyAccessorsToTry( 046 @Nullable Class<?> targetType, List<PropertyAccessor> propertyAccessors) { 047 048 List<PropertyAccessor> specificAccessors = new ArrayList<>(); 049 List<PropertyAccessor> generalAccessors = new ArrayList<>(); 050 for (PropertyAccessor resolver : propertyAccessors) { 051 Class<?>[] targets = resolver.getSpecificTargetClasses(); 052 if (targets == null) { // generic resolver that says it can be used for any type 053 generalAccessors.add(resolver); 054 } 055 else { 056 if (targetType != null) { 057 for (Class<?> clazz : targets) { 058 if (clazz == targetType) { // put exact matches on the front to be tried first? 059 specificAccessors.add(resolver); 060 } 061 else if (clazz.isAssignableFrom(targetType)) { // put supertype matches at the end of the 062 // specificAccessor list 063 generalAccessors.add(resolver); 064 } 065 } 066 } 067 } 068 } 069 List<PropertyAccessor> resolvers = new ArrayList<>(specificAccessors.size() + generalAccessors.size()); 070 resolvers.addAll(specificAccessors); 071 resolvers.addAll(generalAccessors); 072 return resolvers; 073 } 074 075}