Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReflectionValueExtractor |
|
| 3.0;3 |
1 | package org.codehaus.plexus.util.introspection; | |
2 | ||
3 | /* | |
4 | * Licensed to the Apache Software Foundation (ASF) under one | |
5 | * or more contributor license agreements. See the NOTICE file | |
6 | * distributed with this work for additional information | |
7 | * regarding copyright ownership. The ASF licenses this file | |
8 | * to you under the Apache License, Version 2.0 (the | |
9 | * "License"); you may not use this file except in compliance | |
10 | * with the License. You may obtain a copy of the License at | |
11 | * | |
12 | * http://www.apache.org/licenses/LICENSE-2.0 | |
13 | * | |
14 | * Unless required by applicable law or agreed to in writing, | |
15 | * software distributed under the License is distributed on an | |
16 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
17 | * KIND, either express or implied. See the License for the | |
18 | * specific language governing permissions and limitations | |
19 | * under the License. | |
20 | */ | |
21 | ||
22 | import java.lang.reflect.Method; | |
23 | import java.util.HashMap; | |
24 | import java.util.Map; | |
25 | import java.util.StringTokenizer; | |
26 | ||
27 | import org.codehaus.plexus.util.StringUtils; | |
28 | ||
29 | /** | |
30 | * Using simple dotted expressions extract the values from a MavenProject | |
31 | * instance, For example we might want to extract a value like: | |
32 | * project.build.sourceDirectory | |
33 | * | |
34 | * @author <a href="mailto:jason@maven.org">Jason van Zyl </a> | |
35 | * @version $Id: ReflectionValueExtractor.java 591654 2007-11-03 17:14:56Z dennisl $ | |
36 | */ | |
37 | public class ReflectionValueExtractor | |
38 | { | |
39 | 0 | private static Class[] args = new Class[ 0 ]; |
40 | ||
41 | 0 | private static Object[] params = new Object[ 0 ]; |
42 | ||
43 | private static ClassMap classMap; | |
44 | ||
45 | 0 | private static Map classMaps = new HashMap(); |
46 | ||
47 | private ReflectionValueExtractor() | |
48 | 0 | { |
49 | 0 | } |
50 | ||
51 | public static Object evaluate( String expression, Object root ) | |
52 | throws Exception | |
53 | { | |
54 | 0 | return evaluate( expression, root, true ); |
55 | } | |
56 | ||
57 | // TODO: don't throw Exception | |
58 | public static Object evaluate( String expression, Object root, boolean trimRootToken ) | |
59 | throws Exception | |
60 | { | |
61 | // if the root token refers to the supplied root object parameter, remove it. | |
62 | 0 | if ( trimRootToken ) |
63 | { | |
64 | 0 | expression = expression.substring( expression.indexOf( '.' ) + 1 ); |
65 | } | |
66 | ||
67 | 0 | Object value = root; |
68 | ||
69 | // ---------------------------------------------------------------------- | |
70 | // Walk the dots and retrieve the ultimate value desired from the | |
71 | // MavenProject instance. | |
72 | // ---------------------------------------------------------------------- | |
73 | ||
74 | 0 | StringTokenizer parser = new StringTokenizer( expression, "." ); |
75 | ||
76 | 0 | while ( parser.hasMoreTokens() ) |
77 | { | |
78 | 0 | String token = parser.nextToken(); |
79 | ||
80 | 0 | if ( value == null ) |
81 | { | |
82 | 0 | return null; |
83 | } | |
84 | ||
85 | 0 | classMap = getClassMap( value.getClass() ); |
86 | ||
87 | 0 | String methodBase = StringUtils.capitalizeFirstLetter( token ); |
88 | ||
89 | 0 | String methodName = "get" + methodBase; |
90 | ||
91 | 0 | Method method = classMap.findMethod( methodName, args ); |
92 | ||
93 | 0 | if ( method == null ) |
94 | { | |
95 | // perhaps this is a boolean property?? | |
96 | 0 | methodName = "is" + methodBase; |
97 | ||
98 | 0 | method = classMap.findMethod( methodName, args ); |
99 | } | |
100 | ||
101 | 0 | if ( method == null ) |
102 | { | |
103 | 0 | return null; |
104 | } | |
105 | ||
106 | 0 | value = method.invoke( value, params ); |
107 | } | |
108 | ||
109 | 0 | return value; |
110 | } | |
111 | ||
112 | private static ClassMap getClassMap( Class clazz ) | |
113 | { | |
114 | 0 | classMap = (ClassMap) classMaps.get( clazz ); |
115 | ||
116 | 0 | if ( classMap == null ) |
117 | { | |
118 | 0 | classMap = new ClassMap( clazz ); |
119 | ||
120 | 0 | classMaps.put( clazz, classMap ); |
121 | } | |
122 | ||
123 | 0 | return classMap; |
124 | } | |
125 | } |