Clover Coverage Report
Coverage timestamp: Sat Sep 18 2010 04:09:52 UTC
../../../../img/srcFileCovDistChart9.png 34% of files have more coverage
41   123   16   10.25
16   81   0.39   4
4     4  
1    
 
  ObjectProxy       Line # 32 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-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.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    * @version $Revision: 1.1 $
30    * @author Michael Meyling
31    */
 
32    public final class ObjectProxy implements InvocationHandler {
33   
34    /** This class. */
35    private static final Class CLASS = ObjectProxy.class;
36   
37    private Object target;
38   
39    private static int level;
40   
41    private String history = "";
42   
43    /**
44    * Create proxy for given object.
45    *
46    * @param obj Object to proxy.
47    * @return Proxy for <code>obj</code>.
48    */
 
49  18 toggle public static Object createProxy(final Object obj) {
50  18 return createProxy(obj, null);
51    }
52   
 
53  30422 toggle private static Object createProxy(final Object obj, final Object parent) {
54  30422 if (obj == null) {
55  194 return null;
56    }
57  30228 if (obj instanceof Proxy) {
58  6 Trace.trace(CLASS, "createProxy", "object is already proxy!");
59  6 return obj;
60    }
61  30222 Trace.trace(CLASS, "createProxy", "instanciating");
62  30222 return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass()
63    .getInterfaces(), new ObjectProxy(obj, parent));
64    }
65   
 
66  30222 toggle private ObjectProxy(final Object obj, final Object parent) {
67  30222 target = obj;
68  30222 if (parent != null) {
69  30210 Trace.trace(CLASS, "QedeqProxy(Object, Object)", parent.getClass().getName());
70  30210 history = ((ObjectProxy) Proxy.getInvocationHandler(parent)).history + "/"
71    + StringUtility.getClassName(obj.getClass());
72    } else {
73  12 history = StringUtility.getClassName(obj.getClass());
74    }
75    }
76   
 
77  42503 toggle public Object invoke(final Object proxy, final Method method, final Object[] args)
78    throws Throwable {
79  42503 level++;
80  42503 final StringBuffer buffer = new StringBuffer();
81  85006 for (int i = 0; i < level; i++) {
82  42503 buffer.append("-");
83    }
84  42503 Trace.trace(CLASS, "invoke", buffer.toString() + "> " + method.getName());
85  42503 Trace.trace(CLASS, "invoke", "> " + history);
86  42503 Object result = null;
87  42503 try {
88  42503 Object[] proxyArgs = null;
89  42503 if (args != null) {
90  9895 proxyArgs = new Object[args.length];
91  19790 for (int i = 0; i < args.length; i++) {
92  9895 if (method.getParameterTypes()[i].isPrimitive()) {
93  9895 proxyArgs[i] = args[i];
94    } else { // TODO determine interfaces, but other than by
95    // getReturnType.getInterfaces()
96  0 proxyArgs[i] = createProxy(args[i], proxy);
97    }
98    }
99    }
100  42503 result = method.invoke(target, proxyArgs);
101    } catch (InvocationTargetException e) {
102  0 Trace.trace(CLASS, "invoke", e);
103  0 throw e.getCause();
104    } catch (Throwable e) {
105  0 e.printStackTrace();
106  0 throw e;
107    } finally {
108  42503 Trace.trace(CLASS, "invoke", buffer.toString() + "< " + method.getName());
109  42503 level--;
110    }
111  42503 if (method.getReturnType().getName().startsWith("java.")
112    || method.getReturnType().isPrimitive()
113    || method.getReturnType().equals(String.class)) {
114  12099 Trace.trace(CLASS, "invoke", "creating no proxy for " + method.getReturnType());
115  12099 Trace.trace(CLASS, "invoke", "result is: >" + result + "<");
116  12099 return result;
117    // TODO determine interfaces, but other than by getReturnType.getInterfaces()
118    }
119  30404 Trace.trace(CLASS, "invoke", "creating proxy for " + method.getReturnType());
120  30404 return createProxy(result, proxy);
121    }
122   
123    }