001/* 002 * Copyright 2002-2017 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.beans; 018 019import org.springframework.lang.Nullable; 020 021/** 022 * Utility methods for classes that perform bean property access 023 * according to the {@link PropertyAccessor} interface. 024 * 025 * @author Juergen Hoeller 026 * @since 1.2.6 027 */ 028public abstract class PropertyAccessorUtils { 029 030 /** 031 * Return the actual property name for the given property path. 032 * @param propertyPath the property path to determine the property name 033 * for (can include property keys, for example for specifying a map entry) 034 * @return the actual property name, without any key elements 035 */ 036 public static String getPropertyName(String propertyPath) { 037 int separatorIndex = (propertyPath.endsWith(PropertyAccessor.PROPERTY_KEY_SUFFIX) ? 038 propertyPath.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) : -1); 039 return (separatorIndex != -1 ? propertyPath.substring(0, separatorIndex) : propertyPath); 040 } 041 042 /** 043 * Check whether the given property path indicates an indexed or nested property. 044 * @param propertyPath the property path to check 045 * @return whether the path indicates an indexed or nested property 046 */ 047 public static boolean isNestedOrIndexedProperty(@Nullable String propertyPath) { 048 if (propertyPath == null) { 049 return false; 050 } 051 for (int i = 0; i < propertyPath.length(); i++) { 052 char ch = propertyPath.charAt(i); 053 if (ch == PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR || 054 ch == PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) { 055 return true; 056 } 057 } 058 return false; 059 } 060 061 /** 062 * Determine the first nested property separator in the 063 * given property path, ignoring dots in keys (like "map[my.key]"). 064 * @param propertyPath the property path to check 065 * @return the index of the nested property separator, or -1 if none 066 */ 067 public static int getFirstNestedPropertySeparatorIndex(String propertyPath) { 068 return getNestedPropertySeparatorIndex(propertyPath, false); 069 } 070 071 /** 072 * Determine the first nested property separator in the 073 * given property path, ignoring dots in keys (like "map[my.key]"). 074 * @param propertyPath the property path to check 075 * @return the index of the nested property separator, or -1 if none 076 */ 077 public static int getLastNestedPropertySeparatorIndex(String propertyPath) { 078 return getNestedPropertySeparatorIndex(propertyPath, true); 079 } 080 081 /** 082 * Determine the first (or last) nested property separator in the 083 * given property path, ignoring dots in keys (like "map[my.key]"). 084 * @param propertyPath the property path to check 085 * @param last whether to return the last separator rather than the first 086 * @return the index of the nested property separator, or -1 if none 087 */ 088 private static int getNestedPropertySeparatorIndex(String propertyPath, boolean last) { 089 boolean inKey = false; 090 int length = propertyPath.length(); 091 int i = (last ? length - 1 : 0); 092 while (last ? i >= 0 : i < length) { 093 switch (propertyPath.charAt(i)) { 094 case PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR: 095 case PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR: 096 inKey = !inKey; 097 break; 098 case PropertyAccessor.NESTED_PROPERTY_SEPARATOR_CHAR: 099 if (!inKey) { 100 return i; 101 } 102 } 103 if (last) { 104 i--; 105 } 106 else { 107 i++; 108 } 109 } 110 return -1; 111 } 112 113 /** 114 * Determine whether the given registered path matches the given property path, 115 * either indicating the property itself or an indexed element of the property. 116 * @param propertyPath the property path (typically without index) 117 * @param registeredPath the registered path (potentially with index) 118 * @return whether the paths match 119 */ 120 public static boolean matchesProperty(String registeredPath, String propertyPath) { 121 if (!registeredPath.startsWith(propertyPath)) { 122 return false; 123 } 124 if (registeredPath.length() == propertyPath.length()) { 125 return true; 126 } 127 if (registeredPath.charAt(propertyPath.length()) != PropertyAccessor.PROPERTY_KEY_PREFIX_CHAR) { 128 return false; 129 } 130 return (registeredPath.indexOf(PropertyAccessor.PROPERTY_KEY_SUFFIX_CHAR, propertyPath.length() + 1) == 131 registeredPath.length() - 1); 132 } 133 134 /** 135 * Determine the canonical name for the given property path. 136 * Removes surrounding quotes from map keys:<br> 137 * {@code map['key']} -> {@code map[key]}<br> 138 * {@code map["key"]} -> {@code map[key]} 139 * @param propertyName the bean property path 140 * @return the canonical representation of the property path 141 */ 142 public static String canonicalPropertyName(@Nullable String propertyName) { 143 if (propertyName == null) { 144 return ""; 145 } 146 147 StringBuilder sb = new StringBuilder(propertyName); 148 int searchIndex = 0; 149 while (searchIndex != -1) { 150 int keyStart = sb.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX, searchIndex); 151 searchIndex = -1; 152 if (keyStart != -1) { 153 int keyEnd = sb.indexOf( 154 PropertyAccessor.PROPERTY_KEY_SUFFIX, keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length()); 155 if (keyEnd != -1) { 156 String key = sb.substring(keyStart + PropertyAccessor.PROPERTY_KEY_PREFIX.length(), keyEnd); 157 if ((key.startsWith("'") && key.endsWith("'")) || (key.startsWith("\"") && key.endsWith("\""))) { 158 sb.delete(keyStart + 1, keyStart + 2); 159 sb.delete(keyEnd - 2, keyEnd - 1); 160 keyEnd = keyEnd - 2; 161 } 162 searchIndex = keyEnd + PropertyAccessor.PROPERTY_KEY_SUFFIX.length(); 163 } 164 } 165 } 166 return sb.toString(); 167 } 168 169 /** 170 * Determine the canonical names for the given property paths. 171 * @param propertyNames the bean property paths (as array) 172 * @return the canonical representation of the property paths 173 * (as array of the same size) 174 * @see #canonicalPropertyName(String) 175 */ 176 @Nullable 177 public static String[] canonicalPropertyNames(@Nullable String[] propertyNames) { 178 if (propertyNames == null) { 179 return null; 180 } 181 String[] result = new String[propertyNames.length]; 182 for (int i = 0; i < propertyNames.length; i++) { 183 result[i] = canonicalPropertyName(propertyNames[i]); 184 } 185 return result; 186 } 187 188}