KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 2004 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 java.lang.reflect.Method JavaDoc;
19
20 import net.sf.cglib.core.CodeEmitter;
21 import net.sf.cglib.core.Constants;
22 import net.sf.cglib.core.MethodInfo;
23 import net.sf.cglib.core.ReflectUtils;
24 import net.sf.cglib.core.Signature;
25 import net.sf.cglib.transform.ClassEmitterTransformer;
26
27 import org.objectweb.asm.Attribute;
28 import org.objectweb.asm.Type;
29
30 /**
31  * @author Mark Hobson
32  */

33 public class AddInitTransformer extends ClassEmitterTransformer {
34     private MethodInfo info;
35     
36     public AddInitTransformer(Method JavaDoc method) {
37         info = ReflectUtils.getMethodInfo(method);
38         
39         Type[] types = info.getSignature().getArgumentTypes();
40         if (types.length != 1 ||
41         !types[0].equals(Constants.TYPE_OBJECT) ||
42         !info.getSignature().getReturnType().equals(Type.VOID_TYPE)) {
43             throw new IllegalArgumentException JavaDoc(method + " illegal signature");
44         }
45     }
46     
47     public CodeEmitter begin_method(int access, Signature sig, Type[] exceptions) {
48         final CodeEmitter emitter = super.begin_method(access, sig, exceptions);
49         if (sig.getName().equals(Constants.CONSTRUCTOR_NAME)) {
50             return new CodeEmitter(emitter) {
51                 public void visitInsn(int opcode) {
52                     if (opcode == Constants.RETURN) {
53                         load_this();
54                         invoke(info);
55                     }
56                     super.visitInsn(opcode);
57                 }
58             };
59         }
60         return emitter;
61     }
62 }
63
64
Popular Tags