Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart8.png 62% of files have more coverage
37   116   11   12.33
16   65   0.3   3
3     3.67  
1    
 
  DynamicGetter       Line # 29 37 11 75% 0.75
 
  (132)
 
1    /* This file is part of the project "Hilbert II" - http://www.qedeq.org
2    *
3    * Copyright 2000-2013, 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    * @author Michael Meyling
28    */
 
29    public final class DynamicGetter {
30   
31    /** This class. */
32    private static final Class CLASS = DynamicGetter.class;
33   
34    /**
35    * Constructor.
36    */
 
37  0 toggle private DynamicGetter() {
38    // nothing to do
39    }
40   
 
41  18828250 toggle private static final Object getMethodResult(final Object obj, final String methodParam)
42    throws IllegalAccessException, InvocationTargetException {
43  18828250 String methodName = methodParam;
44  18828250 Object[] param;
45  18828250 int pos = methodName.indexOf("(");
46  18828250 if (pos >= 0) {
47  18828250 methodName = methodName.substring(0, pos);
48  18828250 if (')' == methodParam.charAt(pos + 1)) {
49  11498279 param = new Object[0];
50    } else {
51  7329971 param = new Object[1];
52  7329971 param[0] = new Integer(Integer.parseInt(methodParam.substring(pos + 1,
53    methodParam.length() - 1)));
54    }
55    } else {
56  0 param = new Object[0];
57    }
58    // mime 20050622: check parameter types and length, support string parameters
59  18828250 if (obj == null) {
60  0 Trace.trace(DynamicGetter.class, "getMethodResult(Object)", "obj = null!");
61  0 throw new RuntimeException("trying to execute method on null object: \"" + methodName + "\"");
62    }
63  18828250 Method[] method = obj.getClass().getMethods();
64  213260234 for (int i = 0; i < method.length; i++) {
65  213260234 if (method[i].getName().equals(methodName)) {
66  18828250 Trace.trace(CLASS, "getMethodResult(Object)", "invoking:",
67    method[i].getName());
68  18828250 return method[i].invoke(obj, param);
69    }
70    }
71    // mime 20050622: other exception?
72  0 System.out.println(obj.getClass());
73  0 System.out.println(obj);
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  1580035 toggle public static final Object get(final Object object, final String methodParam)
87    throws IllegalAccessException, InvocationTargetException {
88  1580035 Object result = object;
89  1580035 int posOld = 0; // last found "." position
90  1580035 int pos = 0; // current "." position
91   
92  1580035 if (methodParam.length() == 0) {
93  278 return result;
94    }
95  1579757 Trace.param(CLASS, "get(Object, String)", "methodParam", methodParam);
96    // iterate expressions
97  ? while (0 <= (pos = methodParam.indexOf(".", posOld))) {
98  17248493 String method = methodParam.substring(posOld, pos);
99  17248493 posOld = pos + 1;
100  17248493 Trace.param(CLASS, "get(Object, String)", "method", method);
101    // System.out.println(method);
102  17248493 Object result2 = getMethodResult(result, method);
103  17248493 if (result2 == null) {
104  0 Trace.trace(CLASS, "get(Object, String)", "result = null");
105    }
106  17248493 result = result2;
107    }
108   
109    // ????????????
110   
111    // last expression (must not be an getter)
112  1579757 Trace.param(CLASS, "get(Object, String)", "method",
113    methodParam.substring(posOld));
114  1579757 return getMethodResult(result, methodParam.substring(posOld));
115    }
116    }