KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > cglib > transform > impl > TransformDemo


1 /*
2  * Copyright 2003 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package net.sf.cglib.transform.impl;
18
19 import net.sf.cglib.transform.*;
20 import net.sf.cglib.core.*;
21
22 import java.util.*;
23
24 /**
25  *
26  * @author baliuka
27  */

28 public class TransformDemo {
29     
30     public static void register(Class JavaDoc cls){
31       System.out.println("register " + cls);
32     }
33     
34    
35     public static void start(){
36      
37         MA ma = new MA();
38         makePersistent(ma);
39         ma.setCharP('A');
40         ma.getCharP();
41         ma.setDoubleP(554);
42         ma.setDoubleP(1.2);
43         ma.getFloatP();
44         ma.setName("testName");
45         ma.publicField = "set value";
46         ma.publicField = ma.publicField + " append value";
47         ma.setBaseTest("base test field");
48         ma.getBaseTest();
49     
50     }
51     
52     public static void makePersistent(Object JavaDoc obj){
53         System.out.println( "makePersistent " + obj.getClass() + " " + Arrays.asList(obj.getClass().getInterfaces()) );
54         InterceptFieldEnabled t = (InterceptFieldEnabled)obj;
55         t.setInterceptFieldCallback( new StateManager());
56         FieldProvider provider = (FieldProvider)obj;
57         System.out.println("Field Names " + Arrays.asList(provider.getFieldNames()) );
58         System.out.println("Field Types " + Arrays.asList(provider.getFieldTypes()) );
59         PersistenceCapable pc = (PersistenceCapable)obj;
60         pc.setPersistenceManager("Manager");
61     
62     }
63     
64     public static void main( String JavaDoc args [] )throws Exception JavaDoc{
65     
66      ClassTransformerFactory transformation =
67      
68         new ClassTransformerFactory (){
69         
70            public ClassTransformer newInstance(){
71             try{
72               InterceptFieldTransformer t1 = new InterceptFieldTransformer( new Filter() );
73         
74         
75               AddStaticInitTransformer t2 = new AddStaticInitTransformer(
76                 TransformDemo.class.getMethod("register",new Class JavaDoc[]{Class JavaDoc.class})
77               );
78         
79         
80               AddDelegateTransformer t3 = new AddDelegateTransformer(
81                           new Class JavaDoc[]{PersistenceCapable.class},
82                           PersistenceCapableImpl.class
83                     );
84                           
85               FieldProviderTransformer t4 = new FieldProviderTransformer();
86         
87               return new ClassTransformerChain( new ClassTransformer[]{t4,t1,t2,t3} );
88               
89             }catch(Exception JavaDoc e){
90               throw new CodeGenerationException(e);
91             }
92            }
93     };
94         
95         TransformingClassLoader loader = new TransformingClassLoader(
96           TransformDemo.class.getClassLoader(),
97           new ClassFilter(){
98             public boolean accept(String JavaDoc name){
99                 System.out.println("load : " + name);
100                 boolean f =
101                   Base.class.getName().equals(name) ||
102                   MA.class.getName().equals(name) ||
103                      TransformDemo.class.getName().equals(name);
104                 if(f){
105                  System.out.println("transforming " + name);
106                 }
107                 return f;
108             }
109          },
110          transformation
111         );
112         
113         
114         loader.loadClass(TransformDemo.class.getName()).getMethod("start",new Class JavaDoc[]{}).invoke(null,null);
115     
116     }
117     
118     
119    public static class Filter implements InterceptFieldFilter{
120        
121         
122         public boolean acceptRead(org.objectweb.asm.Type owner, String JavaDoc name) {
123                   return true;
124         }
125         
126         public boolean acceptWrite(org.objectweb.asm.Type owner, String JavaDoc name) {
127                   return true;
128         }
129         
130     };
131     
132
133   public static class StateManager implements InterceptFieldCallback{
134         
135        
136         public boolean readBoolean(Object JavaDoc _this, String JavaDoc name, boolean oldValue) {
137             System.out.println("read " + name + " = " + oldValue);
138             return oldValue;
139         }
140        
141         public byte readByte(Object JavaDoc _this, String JavaDoc name, byte oldValue) {
142             System.out.println("read " + name + " = " + oldValue);
143             return oldValue;
144         }
145         
146         public char readChar(Object JavaDoc _this, String JavaDoc name, char oldValue) {
147             System.out.println("read " + name + " = " + oldValue);
148             return oldValue;
149         }
150         
151         public double readDouble(Object JavaDoc _this, String JavaDoc name, double oldValue) {
152             System.out.println("read " + name + " = " + oldValue);
153             return oldValue;
154         }
155         
156         public float readFloat(Object JavaDoc _this, String JavaDoc name, float oldValue) {
157             System.out.println("read " + name + " = " + oldValue);
158             return oldValue;
159         }
160         
161         public int readInt(Object JavaDoc _this, String JavaDoc name, int oldValue) {
162             System.out.println("read " + name + " = " + oldValue);
163             return oldValue;
164         }
165         
166         public long readLong(Object JavaDoc _this, String JavaDoc name, long oldValue) {
167             System.out.println("read " + name + " = " + oldValue);
168             return oldValue;
169         }
170         
171         public Object JavaDoc readObject(Object JavaDoc _this, String JavaDoc name, Object JavaDoc oldValue) {
172             System.out.println("read " + name + " = " + oldValue);
173             return oldValue;
174         }
175         
176         public short readShort(Object JavaDoc _this, String JavaDoc name, short oldValue) {
177             System.out.println("read " + name + " = " + oldValue);
178             return oldValue;
179         }
180         
181         public boolean writeBoolean(Object JavaDoc _this, String JavaDoc name, boolean oldValue, boolean newValue) {
182             System.out.println( "write " + name + " = " + newValue);
183             return newValue;
184         }
185         
186         public byte writeByte(Object JavaDoc _this, String JavaDoc name, byte oldValue, byte newValue) {
187             System.out.println( "write " + name + " = " + newValue);
188             return newValue;
189         }
190         
191         public char writeChar(Object JavaDoc _this, String JavaDoc name, char oldValue, char newValue) {
192             System.out.println( "write " + name + " = " + newValue);
193             return newValue;
194         }
195         
196         public double writeDouble(Object JavaDoc _this, String JavaDoc name, double oldValue, double newValue) {
197             System.out.println( "write " + name + " = " + newValue);
198             return newValue;
199         }
200         
201         public float writeFloat(Object JavaDoc _this, String JavaDoc name, float oldValue, float newValue) {
202             System.out.println( "write " + name + " = " + newValue);
203             return newValue;
204         }
205         
206         public int writeInt(Object JavaDoc _this, String JavaDoc name, int oldValue, int newValue) {
207             System.out.println( "write " + name + " = " + newValue);
208             return newValue;
209         }
210         
211         public long writeLong(Object JavaDoc _this, String JavaDoc name, long oldValue, long newValue) {
212             System.out.println( "write " + name + " = " + newValue);
213             return newValue;
214         }
215         
216         public Object JavaDoc writeObject(Object JavaDoc _this, String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
217             System.out.println( "write " + name + " = " + newValue);
218             return newValue;
219         }
220         
221         public short writeShort(Object JavaDoc _this, String JavaDoc name, short oldValue, short newValue) {
222             System.out.println( "write " + name + " = " + newValue);
223             return newValue;
224         }
225         
226        }
227
228     
229     
230 }
231
Popular Tags