KickJava   Java API By Example, From Geeks To Geeks.

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


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
28 package org.objectweb.speedo.generation.enhancer;
29
30 import org.objectweb.speedo.api.SpeedoProperties;
31 import org.objectweb.speedo.api.SpeedoException;
32 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
33 import org.objectweb.speedo.metadata.SpeedoClass;
34 import org.objectweb.speedo.metadata.SpeedoField;
35 import org.objectweb.speedo.generation.lib.NamingRules;
36 import org.objectweb.asm.ClassWriter;
37 import org.objectweb.asm.ClassReader;
38 import org.objectweb.asm.ClassVisitor;
39 import org.objectweb.asm.CodeVisitor;
40 import org.objectweb.asm.Constants;
41 import org.objectweb.asm.Attribute;
42 import org.objectweb.util.monolog.api.Logger;
43
44 import java.util.Collection JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.HashSet JavaDoc;
47 import java.util.Set JavaDoc;
48
49 public class DummyAccessorAdder extends EnhancerComponent {
50
51     public final static String JavaDoc LOGGER_NAME
52         = SpeedoProperties.LOGGER_NAME + ".generation.enhancer.fieldMdodifier";
53
54     boolean add;
55
56     public DummyAccessorAdder(boolean add) {
57         this.add = add;
58     }
59
60     public boolean init() throws SpeedoException {
61         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
62         return !scp.getXmldescriptor().isEmpty();
63     }
64
65     public void process() throws SpeedoException {
66         if (scp.getXmldescriptor() == null || scp.getXmldescriptor().isEmpty()) {
67             return;
68         }
69         Collection JavaDoc xmls = scp.getXmldescriptor().values();
70         for (Iterator JavaDoc itDesc = xmls.iterator(); itDesc.hasNext();) {
71             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
72             for (Iterator JavaDoc itclass = desc.getSpeedoClasses().iterator(); itclass.hasNext();) {
73                 Set JavaDoc fields = new HashSet JavaDoc();
74                 SpeedoClass jdoclass = (SpeedoClass) itclass.next();
75                 Iterator JavaDoc it = jdoclass.jdoField.values().iterator();
76                 while (it.hasNext()) {
77                     SpeedoField sf = (SpeedoField) it.next();
78                     if (sf.relationType != SpeedoField.NO_RELATION) {
79                         fields.add(sf);
80                     }
81                 }
82                 if (fields.isEmpty()) {
83                     continue;
84                 }
85                 ClassWriter cw = new ClassWriter(false);
86                 DummyAccessorModifier fam = new DummyAccessorModifier(
87                     cw, logger, fields);
88                 String JavaDoc name = jdoclass.getFQName();
89                 ClassReader cr = loadJavaClass(false, name, scp.output, false);
90                 cr.accept(fam, false);
91                 writeJavaClass(name, cw, scp.output);
92             }
93         }
94     }
95
96     class DummyAccessorModifier extends LoggedClassAdapter implements Constants {
97
98         private Set JavaDoc methods;
99
100         /**
101          * @param fields is a set of field names
102          */

103         public DummyAccessorModifier(ClassVisitor classVisitor,
104                                      Logger logger,
105                                      Set JavaDoc fields) {
106             super(classVisitor, logger);
107             this.methods = new HashSet JavaDoc();
108             Iterator JavaDoc i = fields.iterator();
109             while (i.hasNext()) {
110                 SpeedoField f = (SpeedoField)i.next();
111                 String JavaDoc getter = NamingRules.getterName(f.jdoClass, f.name);
112                 String JavaDoc setter = NamingRules.setterName(f.jdoClass, f.name);
113                 methods.add(getter);
114                 methods.add(setter);
115                 if (add) {
116                     // add dummy getter
117
CodeVisitor _cv = this.cv.visitMethod(ACC_PUBLIC, getter, "()" + f.desc, null, null);
118                     _cv.visitInsn(ACONST_NULL);
119                     _cv.visitInsn(ARETURN);
120                     _cv.visitMaxs(1, 1);
121                     // add dummy setter
122
_cv = this.cv.visitMethod(ACC_PUBLIC, setter, "(" + f.desc + ")V", null, null);
123                     _cv.visitInsn(RETURN);
124                     _cv.visitMaxs(2, 2);
125                 }
126             }
127         }
128
129         public CodeVisitor visitMethod(int i, String JavaDoc s, String JavaDoc s1, String JavaDoc[] strings, Attribute attrs) {
130             if (!add && methods.contains(s)) {
131                 // remove dummy getter or setter
132
return null;
133             } else {
134                 return super.visitMethod(i, s, s1, strings, attrs);
135             }
136         }
137     }
138 }
139
Popular Tags