KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > speedo > generation > enhancer > MergedClassModifier


1 /**
2  * Speedo: an implementation of JDO compliant personality on top of JORM generic
3  * I/O sub-system.
4  * Copyright (C) 2001-2004 France Telecom R&D
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  *
21  *
22  * Contact: speedo@objectweb.org
23  *
24  * Authors: S.Chassande-Barrioz.
25  *
26  */

27 package org.objectweb.speedo.generation.enhancer;
28
29 import org.objectweb.asm.Attribute;
30 import org.objectweb.asm.ClassVisitor;
31 import org.objectweb.asm.CodeVisitor;
32 import org.objectweb.asm.Constants;
33 import org.objectweb.asm.Type;
34 import org.objectweb.util.monolog.api.Logger;
35
36 import java.util.List JavaDoc;
37
38 public class MergedClassModifier extends LoggedClassAdapter {
39
40     private List JavaDoc itfs;
41
42     private String JavaDoc persistenceCapableSuperClass;
43
44     private boolean fixProxyMethodOverriding;
45
46     public MergedClassModifier(ClassVisitor classVisitor,
47                                List JavaDoc itfs,
48                                Logger logger) {
49         super(classVisitor, logger);
50         this.itfs = itfs;
51         this.fixProxyMethodOverriding = false;
52     }
53
54     public MergedClassModifier(ClassVisitor classVisitor,
55                                List JavaDoc itfs,
56                                String JavaDoc persistenceCapableSuperClass,
57                                Logger logger) {
58         super(classVisitor, logger);
59         this.itfs = itfs;
60         this.fixProxyMethodOverriding = false;
61         this.persistenceCapableSuperClass = persistenceCapableSuperClass;
62     }
63
64     // IMPLEMENTATION OF THE ClassVisitor INTERFACE //
65
// ---------------------------------------------//
66

67     public void visit(final int version, final int access,
68                       final String JavaDoc name,
69                       final String JavaDoc superName,
70                       final String JavaDoc[] interfaces,
71                       final String JavaDoc sourceFile) {
72         // just stores the interfaces implemented by the visited class
73
if (interfaces != null) {
74             for (int i = 0; i < interfaces.length; ++i) {
75                 if (!itfs.contains(interfaces[i])) {
76                     itfs.add(interfaces[i]);
77                 }
78             }
79         }
80     }
81
82     public CodeVisitor visitMethod(final int access,
83                                    final String JavaDoc name,
84                                    final String JavaDoc desc,
85                                    final String JavaDoc[] exceptions,
86                                    final Attribute attrs) {
87         if ((access & Constants.ACC_ABSTRACT) != 0
88                 || name.equals("<init>")
89                 || name.equals("this")) {
90             // do not merge constructors and abstract methods
91
return null;
92         } else {
93             CodeVisitor c = cv.visitMethod(access, name, desc, exceptions, attrs);
94             if (fixProxyMethodOverriding
95                 && (name.equals("jdoLoadFieldsInAccessor")
96                     || name.equals("jdoLoadFieldsFromAccessor")
97                     || name.equals("jdoMakePersistent")
98                     || name.equals("jdoLoadFields")
99                     )) {
100                 int off = 1;
101                 c.visitVarInsn(Constants.ALOAD, 0);
102                 Type[] args = Type.getArgumentTypes(desc);
103                 for (int i = 0; i < args.length; ++i) {
104                     c.visitVarInsn(args[i].getOpcode(Constants.ILOAD), off);
105                     off += args[i].getSize();
106                 }
107                 c.visitMethodInsn(Constants.INVOKESPECIAL,
108                     persistenceCapableSuperClass, name, desc);
109             }
110             return c;
111         }
112     }
113
114     public void visitEnd() {
115         // does nothing
116
}
117 }
118
Popular Tags