KickJava   Java API By Example, From Geeks To Geeks.

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


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 package net.sf.cglib.transform.impl;
17
18 import net.sf.cglib.transform.*;
19 import java.util.*;
20 import net.sf.cglib.core.TypeUtils;
21 import org.objectweb.asm.Type;
22
23 public class ExampleTask extends AbstractTransformTask {
24     private List properties = new ArrayList();
25     private String JavaDoc fieldSuffix = "";
26     private ClassTransformer transformer;
27
28     protected ClassTransformer getClassTransformer(String JavaDoc name[]) {
29         return transformer;
30     }
31     
32     protected void beforeExecute() {
33         ClassTransformer t1 = new AccessFieldTransformer(new AccessFieldTransformer.Callback() {
34             public String JavaDoc getPropertyName(Type owner, String JavaDoc fieldName) {
35                 return fieldName + fieldSuffix;
36             }
37         });
38         int size = properties.size();
39         String JavaDoc[] names = new String JavaDoc[size];
40         Type[] types = new Type[size];
41         for (int i = 0; i < size; i++) {
42             NewProperty p = (NewProperty)properties.get(i);
43             names[i] = p.name;
44             types[i] = TypeUtils.parseType(p.type);
45         }
46         ClassTransformer t2 = new AddPropertyTransformer(names, types);
47         transformer = new ClassTransformerChain(new ClassTransformer[]{ t1, t2 });
48     }
49
50     public static class NewProperty {
51         private String JavaDoc name;
52         private String JavaDoc type;
53
54         public void setName(String JavaDoc name) {
55             this.name = name;
56         }
57
58         public void setType(String JavaDoc type) {
59             this.type = type;
60         }
61     }
62
63     public void addNewproperty(NewProperty prop) {
64         properties.add(prop);
65     }
66
67     public void setFieldsuffix(String JavaDoc fieldSuffix) {
68         this.fieldSuffix = fieldSuffix;
69     }
70 }
71
Popular Tags