KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > alt > jiapi > interceptor > FieldInterceptor2


1 /*
2  * Copyright(C) 2001 Mika Riekkinen, Joni Suominen
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or(at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package alt.jiapi.interceptor;
20
21 import java.lang.reflect.Field JavaDoc;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.apache.log4j.Category;
29
30 import alt.jiapi.InstrumentationContext;
31 import alt.jiapi.InstrumentationDescriptor;
32 import alt.jiapi.reflect.Signature;
33
34 import alt.jiapi.event.EventProducer;
35
36 /**
37  * FieldInterceptor2 intercepts public field accesses and
38  * delegates task to FieldHandler.<p>
39  * Following code snippet is an example, how to initialize
40  * FieldInterceptor2:
41  *
42  * <pre>
43  * InstrumentationContext ctx = new InstrumentationContext();
44  * InstrumentationDescriptor id = new InstrumentationDescriptor();
45  * id.addInclusionRule("samples.*");
46  * ctx.addInstrumentationDescriptor(id);
47  *
48  * FieldInterceptor2 fi = new FieldInterceptor2(id, "samples*", this);
49  * </pre>
50  *
51  * FieldInterceptor2 intercepts only accessess to public fields.<p>
52  *
53  * NOTE: THIS CLASS IS OBSOLETE. USE FieldInterceptor INSTEAD
54  *
55  * @author Mika Riekkinen
56  * @version $Revision: 1.1 $ $Date: 2004/04/11 14:25:21 $
57  */

58 public class FieldInterceptor2 extends EventProducer {
59     private FieldHandler handler;
60     private HashMap JavaDoc rfCache = new HashMap JavaDoc();
61
62     /**
63      * Constructor. Resolution is set to '*'.
64      *
65      * @param id Instrumentation decsriptor, that this FieldInterceptor2
66      * registers itself to.
67      * @param handler FieldHandler
68      */

69     public FieldInterceptor2(InstrumentationDescriptor id,
70                             FieldHandler handler){
71         this(id, "*", handler);
72     }
73
74
75     /**
76      * Creates new FieldInterceptor2. Resolution tells, which
77      * fields trigger interception.
78      *
79      * @param id Instrumentation decsriptor, that this FieldInterceptor2
80      * registers itself to.
81      * @param resolution Resolution, that is used to select which
82      * fields trigger interception
83      * @param handler FieldHandler
84      */

85     public FieldInterceptor2(InstrumentationDescriptor id,
86                             String JavaDoc resolution, FieldHandler handler){
87         this(id, new String JavaDoc[] { resolution }, handler);
88     }
89
90
91     /**
92      * Creates new FieldInterceptor2.
93      *
94      * @param id Instrumentation decsriptor, that this FieldInterceptor2
95      * registers itself to.
96      * @param resolutions Resolutions, that is used to select which
97      * fields trigger interception
98      * @param handler FieldHandler
99      */

100     public FieldInterceptor2(InstrumentationDescriptor id,
101                              String JavaDoc[] resolutions,
102                              FieldHandler handler) {
103         super(resolutions);
104         this.handler = handler;
105
106         id.addInstrumentor(new FieldInstrumentor(this, handler));
107     }
108
109
110
111     /**
112      * Called by Jiapi runtime.
113      *
114      * @param o if target field is static,
115      * this parameter holds a Class of the target field,
116      * otherwise this is the instance, that fieldset should be
117      * acted on.
118      * @param name Fully qualified name of the field
119      * @param value Value to set
120      */

121     public void setField(Object JavaDoc o, String JavaDoc name, Object JavaDoc value) throws Throwable JavaDoc {
122         try {
123             Field JavaDoc f = null;;
124             Class JavaDoc c = null;
125
126             if (o instanceof Class JavaDoc) { // invokestatic
127
c = (Class JavaDoc)o;
128             }
129             else { // invokevirtual
130
c = o.getClass();
131             }
132
133             f = getReflectionField(c, name);
134
135             handler.setField(o, f, value);
136         }
137         catch(Throwable JavaDoc t) {
138             t.printStackTrace();
139             throw t;
140         }
141     }
142
143
144     /**
145      * Called by Jiapi runtime.
146      *
147      * @param o if target field is static,
148      * this parameter holds a Class of the target field,
149      * otherwise this is the instance, that fieldset should be
150      * acted on.
151      * @param name Fully qualified name of the field
152      */

153     public Object JavaDoc getField(Object JavaDoc o, String JavaDoc name) throws Throwable JavaDoc {
154         try {
155             Field JavaDoc f = null;
156             Class JavaDoc c = null;
157
158             if (o instanceof Class JavaDoc) { // invokestatic
159
c = (Class JavaDoc)o;
160             }
161             else { // invokevirtual
162
c = o.getClass();
163             }
164
165             f = getReflectionField(c, name);
166
167             Object JavaDoc r = handler.getField(o, f);
168             return r;
169         }
170         catch(Throwable JavaDoc t) {
171             t.printStackTrace();
172             throw t;
173         }
174     }
175
176
177     private Field JavaDoc getReflectionField(Class JavaDoc c, String JavaDoc name) {
178         Field JavaDoc f = null;
179
180         // NOTE: cache should be configurable. It can potentially
181
// consume a lot of memory
182
f = (Field JavaDoc)rfCache.get(name);
183         if (f != null) {
184             return f;
185         }
186
187         String JavaDoc fName = name.substring(name.lastIndexOf('.') + 1);
188         try {
189             f = c.getDeclaredField(fName);
190         }
191         catch(Exception JavaDoc e) {
192             e.printStackTrace();
193         }
194
195         rfCache.put(name, f);
196
197         return f;
198     }
199 }
200
201
Popular Tags