|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DynamicGetter | Line # 32 | 32 | 10 | 77,6% |
0.7755102
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(9) | |||
Result | |||
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq1 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq1 | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.module.VisitorContextTest.testContext org.qedeq.kernel.bo.module.VisitorContextTest.testContext | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.latex.ExtendedGenerateLatexTest.testGeneration org.qedeq.kernel.bo.latex.ExtendedGenerateLatexTest.testGeneration | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq2 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq2 | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq5 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq5 | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.latex.GenerateLatexTest.testGeneration org.qedeq.kernel.bo.latex.GenerateLatexTest.testGeneration | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq4 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq4 | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq3 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq3 | 1 PASS | |
0.7755102
|
org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq6 org.qedeq.kernel.bo.control.QedeqBoFactoryTest.testCreateStringQedeq6 | 1 PASS | |
1 | /* $Id: DynamicGetter.java,v 1.1 2008/07/26 07:56:13 m31 Exp $ | |
2 | * | |
3 | * This file is part of the project "Hilbert II" - http://www.qedeq.org | |
4 | * | |
5 | * Copyright 2000-2008, Michael Meyling <mime@qedeq.org>. | |
6 | * | |
7 | * "Hilbert II" is free software; you can redistribute | |
8 | * it and/or modify it under the terms of the GNU General Public | |
9 | * License as published by the Free Software Foundation; either | |
10 | * version 2 of the License, or (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | */ | |
17 | ||
18 | package org.qedeq.base.test; | |
19 | ||
20 | import java.lang.reflect.InvocationTargetException; | |
21 | import java.lang.reflect.Method; | |
22 | ||
23 | import org.qedeq.base.trace.Trace; | |
24 | ||
25 | /** | |
26 | * Get result of getter expression chain. A string is interpreted as an getter call chain and | |
27 | * is called on a given object. The resulting object is returned. | |
28 | * | |
29 | * @version $Revision: 1.1 $ | |
30 | * @author Michael Meyling | |
31 | */ | |
32 | public final class DynamicGetter { | |
33 | ||
34 | /** This class. */ | |
35 | private static final Class CLASS = DynamicGetter.class; | |
36 | ||
37 | /** | |
38 | * Constructor. | |
39 | */ | |
40 | 0 | private DynamicGetter() { |
41 | // nothing to do | |
42 | } | |
43 | ||
44 | 429487 | private static final Object getMethodResult(final Object obj, final String methodParam) |
45 | throws IllegalAccessException, InvocationTargetException { | |
46 | 429487 | String methodName = methodParam; |
47 | 429487 | Object[] param; |
48 | 429487 | int pos = methodName.indexOf("("); |
49 | 429487 | if (pos >= 0) { |
50 | 429487 | methodName = methodName.substring(0, pos); |
51 | 429487 | if (')' == methodParam.charAt(pos + 1)) { |
52 | 271086 | param = new Object[0]; |
53 | } else { | |
54 | 158401 | param = new Object[1]; |
55 | 158401 | param[0] = new Integer(Integer.parseInt(methodParam.substring(pos + 1, |
56 | methodParam.length() - 1))); | |
57 | } | |
58 | } else { | |
59 | 0 | param = new Object[0]; |
60 | } | |
61 | // mime 20050622: check parameter types and length, support string parameters | |
62 | 429487 | if (obj == null) { |
63 | 0 | Trace.trace(DynamicGetter.class, "getMethodResult(Object)", "obj = null!"); |
64 | } | |
65 | 429487 | final Method[] method = obj.getClass().getDeclaredMethods(); |
66 | 4123759 | for (int i = 0; i < method.length; i++) { |
67 | 4123759 | if (method[i].getName().equals(methodName)) { |
68 | 429487 | Trace.trace(CLASS, "getMethodResult(Object)", "invoking:", |
69 | method[i].getName()); | |
70 | 429487 | return method[i].invoke(obj, param); |
71 | } | |
72 | } | |
73 | // mime 20050622: other exception? | |
74 | 0 | throw new RuntimeException("method not found: " + methodName); |
75 | } | |
76 | ||
77 | /** | |
78 | * Interpret string as getter call chain end return result. | |
79 | * | |
80 | * @param object Call getters on this object. | |
81 | * @param methodParam Getter chain like <code>getHeader().getAuthorList().getAuthor(0)</code>. | |
82 | * @return Expression result. | |
83 | * @throws IllegalAccessException | |
84 | * @throws InvocationTargetException | |
85 | */ | |
86 | 32416 | public static final Object get(final Object object, final String methodParam) |
87 | throws IllegalAccessException, InvocationTargetException { | |
88 | 32416 | Object result = object; |
89 | 32416 | int posOld = 0; // last found "." position |
90 | 32416 | int pos = 0; // current "." position |
91 | ||
92 | 32416 | Trace.param(CLASS, "get(Object, String)", "methodParam", methodParam); |
93 | // iterate expressions | |
94 | ? | while (0 <= (pos = methodParam.indexOf(".", posOld))) { |
95 | 397071 | String method = methodParam.substring(posOld, pos); |
96 | 397071 | posOld = pos + 1; |
97 | 397071 | Trace.param(CLASS, "get(Object, String)", "method", method); |
98 | 397071 | Object result2 = getMethodResult(result, method); |
99 | 397071 | if (result2 == null) { |
100 | 0 | Trace.trace(CLASS, "get(Object, String)", "result = null"); |
101 | } | |
102 | 397071 | result = result2; |
103 | } | |
104 | ||
105 | // ???????????? | |
106 | ||
107 | // last expression (must not be an getter) | |
108 | 32416 | Trace.param(CLASS, "get(Object, String)", "method", |
109 | methodParam.substring(posOld)); | |
110 | 32416 | return getMethodResult(result, methodParam.substring(posOld)); |
111 | } | |
112 | } |
|