| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveutils.util; |
| 16 | |
| 17 | import java.beans.BeanInfo; |
| 18 | import java.beans.Introspector; |
| 19 | import java.beans.PropertyDescriptor; |
| 20 | import java.util.HashMap; |
| 21 | import java.util.Map; |
| 22 | import java.util.StringTokenizer; |
| 23 | |
| 24 | import org.apache.hivemind.ApplicationRuntimeException; |
| 25 | import org.apache.hivemind.util.PropertyUtils; |
| 26 | |
| 27 | /** |
| 28 | * Utility class to ease access to an access property through the use of "paths" |
| 29 | * (dotted chain of property names). |
| 30 | * |
| 31 | * @author Jean-Francois Poilpret |
| 32 | */ |
| 33 | final public class PropertyPathUtils |
| 34 | { |
| 35 | private PropertyPathUtils() |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | static public Object read(Object target, String propertyPath) |
| 40 | { |
| 41 | StringTokenizer tokenize = new StringTokenizer(propertyPath, "."); |
| 42 | Object currentTarget = target; |
| 43 | while (tokenize.hasMoreTokens()) |
| 44 | { |
| 45 | currentTarget = PropertyUtils.read(currentTarget, tokenize.nextToken()); |
| 46 | } |
| 47 | return currentTarget; |
| 48 | } |
| 49 | |
| 50 | static public Class getType(Class target, String propertyPath) |
| 51 | { |
| 52 | StringTokenizer tokenize = new StringTokenizer(propertyPath, "."); |
| 53 | Class currentTarget = target; |
| 54 | while (tokenize.hasMoreTokens()) |
| 55 | { |
| 56 | currentTarget = getPropertyType(currentTarget, tokenize.nextToken()); |
| 57 | } |
| 58 | return currentTarget; |
| 59 | } |
| 60 | |
| 61 | static private Class getPropertyType(Class target, String property) |
| 62 | { |
| 63 | return getClassPropertyTypes(target).get(property); |
| 64 | } |
| 65 | |
| 66 | static private Map<String, Class> getClassPropertyTypes(Class target) |
| 67 | { |
| 68 | Map<String, Class> properties = _classes.get(target); |
| 69 | if (properties == null) |
| 70 | { |
| 71 | properties = buildClassPropertyTypes(target); |
| 72 | _classes.put(target, properties); |
| 73 | } |
| 74 | return properties; |
| 75 | } |
| 76 | |
| 77 | //CSOFF: IllegalCatchCheck |
| 78 | static private Map<String, Class> buildClassPropertyTypes(Class target) |
| 79 | { |
| 80 | try |
| 81 | { |
| 82 | BeanInfo info = Introspector.getBeanInfo(target); |
| 83 | PropertyDescriptor[] props = info.getPropertyDescriptors(); |
| 84 | Map<String, Class> types = new HashMap<String, Class>(); |
| 85 | for (int i = 0; i < props.length; i++) |
| 86 | { |
| 87 | PropertyDescriptor desc = props[i]; |
| 88 | if (desc.getReadMethod() != null) |
| 89 | { |
| 90 | types.put(desc.getName(), desc.getPropertyType()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | return types; |
| 95 | } |
| 96 | catch (Exception ex) |
| 97 | { |
| 98 | throw new ApplicationRuntimeException( |
| 99 | "Could not introspect class " + target, |
| 100 | target, |
| 101 | null, |
| 102 | ex); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static private final Map<Class, Map<String, Class>> _classes = |
| 107 | new HashMap<Class, Map<String, Class>>(); |
| 108 | } |