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