EMMA Coverage Report (generated Tue Feb 12 22:23:49 ICT 2008)
[all classes][net.sourceforge.hiveutils.util]

COVERAGE SUMMARY FOR SOURCE FILE [PropertyPathUtils.java]

nameclass, %method, %block, %line, %
PropertyPathUtils.java100% (1/1)86%  (6/7)84%  (100/119)87%  (27/31)

COVERAGE BREAKDOWN BY CLASS AND METHOD

nameclass, %method, %block, %line, %
     
class PropertyPathUtils100% (1/1)86%  (6/7)84%  (100/119)87%  (27/31)
PropertyPathUtils (): void 0%   (0/1)0%   (0/3)0%   (0/2)
buildClassPropertyTypes (Class): Map 100% (1/1)68%  (34/50)80%  (8/10)
<static initializer> 100% (1/1)100% (5/5)100% (1/1)
getClassPropertyTypes (Class): Map 100% (1/1)100% (17/17)100% (5/5)
getPropertyType (Class, String): Class 100% (1/1)100% (6/6)100% (1/1)
getType (Class, String): Class 100% (1/1)100% (19/19)100% (6/6)
read (Object, String): Object 100% (1/1)100% (19/19)100% (6/6)

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 
15package net.sourceforge.hiveutils.util;
16 
17import java.beans.BeanInfo;
18import java.beans.Introspector;
19import java.beans.PropertyDescriptor;
20import java.util.HashMap;
21import java.util.Map;
22import java.util.StringTokenizer;
23 
24import org.apache.hivemind.ApplicationRuntimeException;
25import 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 */
33final 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}

[all classes][net.sourceforge.hiveutils.util]
EMMA 2.0.5312 (C) Vladimir Roubtsov