MeasuringWrapper.java

biz/hammurapi/metrics/MeasuringWrapper.java

Violations

Inspector Message Severity Location
Java Inspector 048 Copyrights information should be present in each file. 1
Java Inspector 089 Constructor is not properly documented 2 45:9
Java Inspector 089 Undocumented parameter consumer 2 45:9
Java Inspector 089 Undocumented method 2 70:41
Java Inspector 026 Avoid hardwired string literals. Allowed literals: [] 3 76:107
Java Inspector 051 It is good practice to call in any case super() in a constructor. 3 45:9

Source code

1/*
2 * hgcommons 9
3 * Hammurapi Group Common Library
4 * Copyright (C) 2003 Hammurapi Group
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * URL: http://www.hammurapi.biz/hammurapi-biz/ef/xmenu/hammurapi-group/products/products/hgcommons/index.html
21 * e-Mail: support@hammurapi.biz
22 */
23package biz.hammurapi.metrics;
24
25import java.lang.reflect.InvocationHandler;
26import java.lang.reflect.Method;
27import java.lang.reflect.Proxy;
28
29import biz.hammurapi.wrap.WrapperHandler;
30
31/**
32 * Wraps objects to measure object method's performance.
33 *
34 * @author Pavel Vlasov
35 *
36 * @version $Revision: 1.3 $
37 */
38public class MeasuringWrapper {
39
40 private MeasurementConsumer consumer;
41
42 /**
43 *
44 */
45 public MeasuringWrapper(MeasurementConsumer consumer) {
46 this.consumer=consumer;
47 }
48
49 /**
50 * Wraps object to measure methods performance.
51 * @param obj Object to be wrapped.
52 * @return proxy object which implements all interfaces of the original object.
53 */
54 public Object wrap(final Object obj) {
55 return wrap(obj, consumer);
56 }
57
58 /**
59 * Wraps object to measure methods performance.
60 * @param obj Object to be wrapped.
61 * @param consumer Metric consumer.
62 * @return proxy object which implements all interfaces of the original object.
63 */
64 public static Object wrap(final Object obj, final MeasurementConsumer consumer) {
65 return Proxy.newProxyInstance(
66 obj.getClass().getClassLoader(),
67 WrapperHandler.getClassInterfaces(obj.getClass()),
68 new InvocationHandler() {
69
70 public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
71 long start=System.currentTimeMillis();
72 try {
73 return method.invoke(obj, args);
74 } finally {
75 long finish=System.currentTimeMillis();
76 consumer.addMeasurement(obj.getClass().toString()+": "+method, finish-start, finish);
77 }
78 }
79
80 });
81 }
82
83}
84