KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2001-2002 Renaud Pawlak <renaud@aopsys.com>
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 java.util.*;
21 import org.aopalliance.intercept.ConstructorInvocation;
22 import org.aopalliance.intercept.MethodInvocation;
23 import org.objectweb.jac.core.*;
24 import org.objectweb.jac.core.rtti.ClassItem;
25 import org.objectweb.jac.core.rtti.ClassRepository;
26
27 /**
28  * This counter must wrap the methods of which calls have to be
29  * counted. It extends the simple counting wrapper to provide 2
30  * optimization methods when client methods call several times the
31  * method wrapped by <code>incr</code>. In these cases, the counter is
32  * direcly incremented by the number of times the <code>incr</code>
33  * method has to be called.
34  *
35  * <p>In order to avoid redundancy, <code>incr</code> must not be
36  * called if <code>incrWithArg</code> or <code>incrWithField</code>
37  * have already been called. To perform this contextual test, use the
38  * before and after running wrapper methods of the aspect
39  * component. */

40
41 public class OptimizedCountingWrapper extends SimpleCountingWrapper {
42
43    /** The field on which the optimization can be done. */
44    String JavaDoc field = null;
45
46    /** The argument on which the optimization can be done. */
47    int arg = 0;
48    
49    /**
50     * Create the counter and parametrize it regarding the base program
51     * shape.
52     *
53     * @param c the used counter
54     * @param field the field that is used to optimize the counting */

55
56    public OptimizedCountingWrapper(AspectComponent ac, Counter c, String JavaDoc field) {
57       super(ac,c);
58       this.field = field;
59    }
60
61    /**
62     * Create the counter and parametrize it regarding the base program
63     * shape.
64     *
65     * @param c the used counter
66     * @param arg the argument number that used to optimize the
67     * counting */

68
69    public OptimizedCountingWrapper(AspectComponent ac, Counter c, int arg) {
70       super(ac,c);
71       this.arg = arg;
72    }
73
74    /**
75     * This wrapping method increments the counter with the field when
76     * the wrapped method is called. It is an optimization for the incr
77     * method.
78     *
79     * @return the return value of the wrapped method
80     * @see SimpleCountingWrapper#incr(Interaction) */

81
82    public Object JavaDoc incrWithField(Interaction interaction) {
83       attrdef( "tracing.globalIncr", "" );
84       Object JavaDoc ret = proceed(interaction);
85       ClassItem cl=ClassRepository.get().getClass(interaction.wrappee);
86       Object JavaDoc fval = cl.getField(field).get(interaction.wrappee);
87       if (fval == null) {
88          System.out.println( "<<< Counting aspect says: the field to count (" +
89                              field + ") is null or does not exist... >>>" );
90          return ret;
91       }
92       if (fval.getClass().isArray()) {
93          counter.incr(((Object JavaDoc[])fval).length);
94       } else if ( fval instanceof Collection ) {
95          counter.incr(((Collection)fval).size());
96       } else {
97          /** this type is not supported... */
98          System.out.println( "<<< Counting aspect says: the field to count (" +
99                              field + ") is not of a supported type... >>>" );
100          return ret;
101       }
102       printCounter();
103       return ret;
104    }
105
106    /**
107     * This wrapping method increments the counter with the argument
108     * value when the wrapped method is called. It is an optimization
109     * for the <code>incr</code> method.
110     *
111     * @return the return value of the wrapped method
112     * @see SimpleCountingWrapper#incr(Interaction) */

113
114    public Object JavaDoc incrWithArg(Interaction interaction) {
115       attrdef( "tracing.globalIncr", "" );
116       Object JavaDoc ret = proceed(interaction);
117       counter.incr( ((Integer JavaDoc)interaction.args[arg]).intValue() );
118       printCounter();
119       return ret;
120    }
121
122     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
123         if (field==null)
124             return incrWithArg((Interaction)invocation);
125         else
126             return incrWithField((Interaction)invocation);
127     }
128
129     public Object JavaDoc construct(ConstructorInvocation invocation) throws Throwable JavaDoc {
130         if (field==null)
131             return incrWithArg((Interaction)invocation);
132         else
133             return incrWithField((Interaction)invocation);
134     }
135
136 }
137
Popular Tags