Clover Coverage Report
Coverage timestamp: Fri May 24 2013 13:47:27 UTC
../../../../img/srcFileCovDistChart9.png 45% of files have more coverage
41   122   16   10.25
16   81   0.39   4
4     4  
1    
 
  ObjectProxy       Line # 31 41 16 90.2% 0.90163934
 
  (1)
 
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.InvocationHandler;
19    import java.lang.reflect.InvocationTargetException;
20    import java.lang.reflect.Method;
21    import java.lang.reflect.Proxy;
22   
23    import org.qedeq.base.trace.Trace;
24    import org.qedeq.base.utility.StringUtility;
25   
26    /**
27    * For testing QEDEQ modules.
28    *
29    * @author Michael Meyling
30    */
 
31    public final class ObjectProxy implements InvocationHandler {
32   
33    /** This class. */
34    private static final Class CLASS = ObjectProxy.class;
35   
36    private Object target;
37   
38    private static int level;
39   
40    private String history = "";
41   
42    /**
43    * Create proxy for given object.
44    *
45    * @param obj Object to proxy.
46    * @return Proxy for <code>obj</code>.
47    */
 
48  18 toggle public static Object createProxy(final Object obj) {
49  18 return createProxy(obj, null);
50    }
51   
 
52  41492 toggle private static Object createProxy(final Object obj, final Object parent) {
53  41492 if (obj == null) {
54  278 return null;
55    }
56  41214 if (obj instanceof Proxy) {
57  6 Trace.trace(CLASS, "createProxy", "object is already proxy!");
58  6 return obj;
59    }
60  41208 Trace.trace(CLASS, "createProxy", "instanciating");
61  41208 return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass()
62    .getInterfaces(), new ObjectProxy(obj, parent));
63    }
64   
 
65  41208 toggle private ObjectProxy(final Object obj, final Object parent) {
66  41208 target = obj;
67  41208 if (parent != null) {
68  41196 Trace.trace(CLASS, "QedeqProxy(Object, Object)", parent.getClass().getName());
69  41196 history = ((ObjectProxy) Proxy.getInvocationHandler(parent)).history + "/"
70    + StringUtility.getClassName(obj.getClass());
71    } else {
72  12 history = StringUtility.getClassName(obj.getClass());
73    }
74    }
75   
 
76  60786 toggle public Object invoke(final Object proxy, final Method method, final Object[] args)
77    throws Throwable {
78  60786 level++;
79  60786 final StringBuffer buffer = new StringBuffer();
80  121572 for (int i = 0; i < level; i++) {
81  60786 buffer.append("-");
82    }
83  60786 Trace.trace(CLASS, "invoke", buffer.toString() + "> " + method.getName());
84  60786 Trace.trace(CLASS, "invoke", "> " + history);
85  60786 Object result = null;
86  60786 try {
87  60786 Object[] proxyArgs = null;
88  60786 if (args != null) {
89  13881 proxyArgs = new Object[args.length];
90  27762 for (int i = 0; i < args.length; i++) {
91  13881 if (method.getParameterTypes()[i].isPrimitive()) {
92  13881 proxyArgs[i] = args[i];
93    } else { // TODO determine interfaces, but other than by
94    // getReturnType.getInterfaces()
95  0 proxyArgs[i] = createProxy(args[i], proxy);
96    }
97    }
98    }
99  60786 result = method.invoke(target, proxyArgs);
100    } catch (InvocationTargetException e) {
101  0 Trace.trace(CLASS, "invoke", e);
102  0 throw e.getCause();
103    } catch (Throwable e) {
104  0 e.printStackTrace();
105  0 throw e;
106    } finally {
107  60786 Trace.trace(CLASS, "invoke", buffer.toString() + "< " + method.getName());
108  60786 level--;
109    }
110  60786 if (method.getReturnType().getName().startsWith("java.")
111    || method.getReturnType().isPrimitive()
112    || method.getReturnType().equals(String.class)) {
113  19312 Trace.trace(CLASS, "invoke", "creating no proxy for " + method.getReturnType());
114  19312 Trace.trace(CLASS, "invoke", "result is: >" + result + "<");
115  19312 return result;
116    // TODO determine interfaces, but other than by getReturnType.getInterfaces()
117    }
118  41474 Trace.trace(CLASS, "invoke", "creating proxy for " + method.getReturnType());
119  41474 return createProxy(result, proxy);
120    }
121   
122    }