KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.log4j.Category;
28
29 import alt.jiapi.InstrumentationContext;
30 import alt.jiapi.InstrumentationDescriptor;
31 import alt.jiapi.reflect.Signature;
32
33 import alt.jiapi.event.EventProducer;
34
35 /**
36  * InvocationInterceptor intercepts method invocations and
37  * delegates invocations to InvokeHandler.<p>
38  * Following code snippet is an example, how to initialize
39  * FieldInterceptor:
40  *
41  * <pre>
42  * InstrumentationContext ctx = new InstrumentationContext();
43  * InstrumentationDescriptor id = new InstrumentationDescriptor();
44  * id.addInclusionRule("samples.*");
45  * ctx.addInstrumentationDescriptor(id);
46  *
47  * FieldInterceptor fi = new FieldInterceptor(id, "samples*", this);
48  * </pre>
49  *
50  * FieldInterceptor intercepts only accessess to public fields.
51  *
52  * NOTE: ONLY FIELD GETS ARE SUPPORTED AT THE MOMENT
53  *
54  * @author Mika Riekkinen
55  * @version $Revision: 1.2 $ $Date: 2004/04/11 14:22:58 $
56  */

57 public class FieldInterceptor extends EventProducer {
58     private AccessAdvisor advisor;
59
60     /**
61      * Constructor. Resolution is set to '*'.
62      *
63      * @param id Instrumentation decsriptor, that this InvocationInterceptor
64      * registers itself to.
65      * @param handler InvocationHandler
66      */

67     public FieldInterceptor(InstrumentationDescriptor id,
68                              AccessAdvisor advisor){
69         this(id, "*", advisor);
70     }
71
72
73     /**
74      * Creates new InvocationInterceptor. Resolution tells, which
75      * methods are to be intercepted.
76      *
77      * @param id Instrumentation decsriptor, that this InvocationInterceptor
78      * registers itself to.
79      * @param resolution Resolution, that is used to select which
80      * methods will be intercepted.
81      * @param handler InvocationHandler
82      */

83     public FieldInterceptor(InstrumentationDescriptor id,
84                             String JavaDoc resolution, AccessAdvisor advisor){
85         this(id, new String JavaDoc[] { resolution }, advisor);
86     }
87
88
89     /**
90      * Creates new InvocationInterceptor.
91      *
92      * @param id Instrumentation decsriptor, that this InvocationInterceptor
93      * registers itself to.
94      * @param resolutions Resolutions, that is used further to select which
95      * methods will trigger events to be produced.
96      * @param handler InvocationHandler
97      */

98     public FieldInterceptor(InstrumentationDescriptor id,
99                             String JavaDoc[] resolutions,
100                             AccessAdvisor advisor) {
101         super(resolutions);
102         this.advisor = advisor;
103
104         id.addInstrumentor(new FieldAdvisorInstrumentor(this, advisor));
105     }
106
107
108
109     /**
110      * Called by Jiapi runtime.
111      *
112      * @param o if field-access is being made to an static field,
113      * this parameter holds a Class of that field,
114      * otherwise this is the instance, that field-access is being
115      * acted on.
116      * @param name of the field
117      * @param value value to set
118      * @return The value that is set on the field
119      */

120     public Object JavaDoc setField(Object JavaDoc o, String JavaDoc name, Object JavaDoc value) {
121         try {
122             return advisor.set(o, name, value);
123         }
124         catch(Throwable JavaDoc t) {
125             t.printStackTrace();
126         }
127
128         return value;
129     }
130
131     public Object JavaDoc getField(Object JavaDoc o, String JavaDoc name, Object JavaDoc value) {
132         try {
133             return advisor.get(o, name, value);
134         }
135         catch(Throwable JavaDoc t) {
136             t.printStackTrace();
137         }
138
139         return value;
140     }
141 }
142
143
Popular Tags