01: /*
02: * Copyright 2003 The Apache Software Foundation
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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 fieldSuffix = "";
26: private ClassTransformer transformer;
27:
28: protected ClassTransformer getClassTransformer(String name[]) {
29: return transformer;
30: }
31:
32: protected void beforeExecute() {
33: ClassTransformer t1 = new AccessFieldTransformer(
34: new AccessFieldTransformer.Callback() {
35: public String getPropertyName(Type owner,
36: String fieldName) {
37: return fieldName + fieldSuffix;
38: }
39: });
40: int size = properties.size();
41: String[] names = new String[size];
42: Type[] types = new Type[size];
43: for (int i = 0; i < size; i++) {
44: NewProperty p = (NewProperty) properties.get(i);
45: names[i] = p.name;
46: types[i] = TypeUtils.parseType(p.type);
47: }
48: ClassTransformer t2 = new AddPropertyTransformer(names, types);
49: transformer = new ClassTransformerChain(new ClassTransformer[] {
50: t1, t2 });
51: }
52:
53: public static class NewProperty {
54: private String name;
55: private String type;
56:
57: public void setName(String name) {
58: this .name = name;
59: }
60:
61: public void setType(String type) {
62: this .type = type;
63: }
64: }
65:
66: public void addNewproperty(NewProperty prop) {
67: properties.add(prop);
68: }
69:
70: public void setFieldsuffix(String fieldSuffix) {
71: this.fieldSuffix = fieldSuffix;
72: }
73: }
|