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