KickJava   Java API By Example, From Geeks To Geeks.

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


1 /**
2  * Copyright (C) 2001-2004 France Telecom R&D
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.speedo.generation.enhancer;
19
20 import org.objectweb.asm.ClassVisitor;
21 import org.objectweb.asm.CodeVisitor;
22 import org.objectweb.asm.Attribute;
23 import org.objectweb.util.monolog.api.Logger;
24 import org.objectweb.util.monolog.api.BasicLevel;
25 import org.objectweb.speedo.metadata.SpeedoField;
26 import org.objectweb.speedo.metadata.SpeedoClass;
27 import org.objectweb.speedo.generation.lib.NamingRules;
28
29 import java.util.Set JavaDoc;
30 import java.util.HashSet JavaDoc;
31 import java.util.Iterator JavaDoc;
32
33 /**
34  * This class adds/removes dummy accessors in order to permit the compilation.
35  * The jdoGetXXX and jdoSetXXX method are added before the compilation and they
36  * are removed after. Indeed these accessors are really generated in the Proxy
37  * class, and there merging with the persistent class later.
38  *
39  * @author S.Chassande-Barrioz
40  */

41 public class DummyAccessorModifier extends LoggedClassAdapter {
42
43     /**
44      * List of method names to removed
45      */

46     private Set JavaDoc methods;
47
48     public DummyAccessorModifier(ClassVisitor classVisitor,
49                                  Logger logger,
50                                  SpeedoClass sc,
51                                  boolean beforeCompilation) {
52         super(classVisitor, logger);
53         if (!beforeCompilation) {
54             methods = new HashSet JavaDoc();
55         }
56         Iterator JavaDoc it = sc.jdoField.values().iterator();
57         while (it.hasNext()) {
58             SpeedoField sf = (SpeedoField) it.next();
59             //if (sf.relationType != SpeedoField.NO_RELATION) {
60
String JavaDoc getter = NamingRules.getterName(sc, sf.name);
61                 String JavaDoc setter = NamingRules.setterName(sc, sf.name);
62                 String JavaDoc coherencesetter = NamingRules.coherentSetterName(sc, sf.name);
63                 if (debug) {
64                     logger.log(BasicLevel.DEBUG,
65                             "DummyAccessor modifier of the class "
66                             + sc.getFQName() + ": getter=" + getter);
67                 }
68                 if (beforeCompilation) {
69                     // add dummy getter
70
CodeVisitor _cv = this.cv.visitMethod(ACC_PUBLIC, getter,
71                             "()" + sf.desc, null, null);
72                     _cv.visitInsn(ACONST_NULL);
73                     _cv.visitInsn(ARETURN);
74                     _cv.visitMaxs(1, 1);
75                     // add dummy setter
76
_cv = this.cv.visitMethod(ACC_PUBLIC, setter,
77                             "(" + sf.desc + ")V", null, null);
78                     _cv.visitInsn(RETURN);
79                     _cv.visitMaxs(2, 2);
80                     // add dummy coherencesetter
81
_cv = this.cv.visitMethod(ACC_PUBLIC, coherencesetter,
82                             "(" + sf.desc + ")V", null, null);
83                     _cv.visitInsn(RETURN);
84                     _cv.visitMaxs(2, 2);
85                 } else {
86                     methods.add(getter);
87                     methods.add(setter);
88                     methods.add(coherencesetter);
89                 }
90             //}
91
}
92         if (methods != null && methods.isEmpty()) {
93             methods = null;
94         }
95     }
96
97     public CodeVisitor visitMethod(int modifier,
98                                    String JavaDoc methodName,
99                                    String JavaDoc returnType,
100                                    String JavaDoc[] paramTypes,
101                                    Attribute attrs) {
102         if (methods != null && methods.contains(methodName)) {
103             if (debug) {
104                 logger.log(BasicLevel.DEBUG, "DummyAccessor.visitMethod("
105                         + methodName + ") return null");
106             }
107             // remove dummy getter or setter
108
return null;
109         } else {
110             CodeVisitor _cv = super.visitMethod(
111                     modifier, methodName, returnType, paramTypes, attrs);
112             if (debug) {
113                 logger.log(BasicLevel.DEBUG, "DummyAccessor.visitMethod("
114                         + methodName + ") return cv=" + _cv);
115             }
116             return _cv;
117         }
118     }
119 }
120
Popular Tags