KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jac > aspects > tracing > SimpleCountingWrapper


1 /*
2   Copyright (C) 2001-2003 Renaud Pawlak
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU Lesser General Public License as
6   published by the Free Software Foundation; either version 2 of the
7   License, or (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12   GNU Lesser General Public License for more details.
13
14   You should have received a copy of the GNU Lesser General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */

17
18 package org.objectweb.jac.aspects.tracing;
19
20 import org.aopalliance.intercept.ConstructorInvocation;
21 import org.aopalliance.intercept.MethodInvocation;
22 import org.objectweb.jac.core.*;
23
24 /**
25  * This simple counter must wrap the methods of which calls have to be
26  * counted.
27  *
28  * <p>In some cases, optimizations can be achieved by grouping
29  * counts. See <code>OptimizedCountingWrapper</code>.
30  *
31  * @see OptimizedCountingWrapper */

32
33 public class SimpleCountingWrapper extends Wrapper {
34
35     /** Stores the counter. */
36     Counter counter = null;
37
38     /**
39      * Creates a new wrapper that uses the given counter.
40      *
41      * @param counter the counter */

42
43     public SimpleCountingWrapper(AspectComponent ac, Counter counter) {
44         super(ac);
45         this.counter = counter;
46     }
47
48     /**
49      * This wrapping method increments the counter when the wrapped
50      * method is called.
51      *
52      * @return the return value of the wrapped method */

53
54     public Object JavaDoc incr(Interaction interaction) {
55         Object JavaDoc ret = proceed(interaction);
56         counter.incr(1);
57         printCounter();
58         return ret;
59     }
60
61     /** Role method: set the counter value.
62      *
63      * @param value the new counter value
64      * @see #getCounter()
65      * @see #incr(Interaction) */

66
67     public void setCounter(int value) {
68         counter.set(value);
69     }
70    
71     /** Role method: get the counter value.
72      *
73      * @return the counter value
74      * @see #setCounter(int)
75      * @see #incr(Interaction) */

76
77     public int getCounter() {
78         return counter.get();
79     }
80
81     /**
82      * Prints the counter in <code>System.out</code>.
83      */

84     public void printCounter() {
85         System.out.println("<<< Counting aspect says : " +
86                            counter.get() +
87                            " line(s) printed. >>>");
88     }
89
90     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
91         return incr((Interaction)invocation);
92     }
93
94     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
95         return incr((Interaction)invocation);
96     }
97
98 }
99
100
101
102
Popular Tags