KickJava   Java API By Example, From Geeks To Geeks.

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


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.jorm.lib.PBindingImpl;
31 import org.objectweb.speedo.api.SpeedoException;
32 import org.objectweb.speedo.api.SpeedoProperties;
33 import org.objectweb.speedo.metadata.SpeedoClass;
34 import org.objectweb.speedo.metadata.SpeedoPackage;
35 import org.objectweb.speedo.metadata.SpeedoXMLDescriptor;
36 import org.objectweb.speedo.metadata.SpeedoIdentity;
37 import org.objectweb.speedo.generation.lib.NamingRules;
38 import org.objectweb.util.monolog.api.BasicLevel;
39 import org.objectweb.util.monolog.api.Logger;
40 import org.objectweb.asm.ClassReader;
41 import org.objectweb.asm.ClassWriter;
42 import org.objectweb.asm.attrs.Attributes;
43
44 import java.util.ArrayList JavaDoc;
45 import java.util.Iterator JavaDoc;
46 import java.util.Collection JavaDoc;
47 import java.util.List JavaDoc;
48 import java.util.TreeSet JavaDoc;
49 import java.util.Set JavaDoc;
50
51 /**
52  * Enhances a set of Java classes and renames it with <code>Delegate</code> as
53  * suffix. <p>The modifications to be done are described by the JDO
54  * Implementation's rules.</p>
55  *
56  * @author S.Chassande-Barrioz
57  */

58 public class PersistentClassEnhancer extends EnhancerComponent {
59
60     public final static String JavaDoc LOGGER_NAME
61             = SpeedoProperties.LOGGER_NAME + ".generation.enhancer";
62
63     /**
64      * Initializes this PersistentClassEnhancer
65      */

66     public boolean init() {
67         logger = scp.loggerFactory.getLogger(LOGGER_NAME);
68         //TODO: manage classes localized in a jar file
69
isSrcJar = false;
70         return !scp.getXmldescriptor().isEmpty();
71     }
72
73     /**
74      * Loads all binary classes described by the Object Model and applies
75      * revelant modification to each of them.
76      *
77      * @exception org.objectweb.speedo.generation.enhancer.SpeedoEnhancerException if something goes wrong
78      */

79     public void process() throws SpeedoException {
80         if (scp.getXmldescriptor().isEmpty())
81             return;
82
83         ArrayList JavaDoc except = new ArrayList JavaDoc();
84         Collection JavaDoc xmls = scp.getXmldescriptor().values();
85         Logger log = null;
86         for (Iterator JavaDoc itDesc = xmls.iterator(); itDesc.hasNext();) {
87             SpeedoXMLDescriptor desc = (SpeedoXMLDescriptor) itDesc.next();
88             for (Iterator JavaDoc itPack = desc.jdoPackage.values().iterator(); itPack.hasNext();) {
89                 SpeedoPackage sp = (SpeedoPackage) itPack.next();
90                 for (Iterator JavaDoc itclass = sp.jdoClass.values().iterator(); itclass.hasNext();) {
91                     SpeedoClass jdoclass = (SpeedoClass) itclass.next();
92
93                     String JavaDoc name = sp.name + '.' + jdoclass.name;
94                     String JavaDoc proxyName = NamingRules.proxyName(name);
95
96                     ClassWriter cw = new ClassWriter(true);
97
98                     RedundencyRemover rr = new RedundencyRemover(cw, logger);
99
100                     List JavaDoc itfs = new ArrayList JavaDoc();
101
102                     // merges the Binding class
103
Set JavaDoc oldClassNames = new TreeSet JavaDoc();
104                     oldClassNames.add(name.replace('.', '/'));
105                     ClassReader cr;
106                     MergedClassModifier mmodifier;
107                     ClassRenamer crenamer;
108                     if (jdoclass.superClassName == null) {
109                         String JavaDoc bindingName = PBindingImpl.class.getName();
110                         log = scp.loggerFactory.getLogger(LOGGER_NAME + ".merge.binding." + name);
111                         log.log(BasicLevel.DEBUG, "Merging the Binding of class: " + name);
112                         cr = loadJavaClass(false, bindingName, scp.output, true);
113                         mmodifier = new MergedClassModifier(rr, itfs, log);
114                         oldClassNames.add(bindingName.replace('.', '/'));
115                         crenamer = new ClassRenamer(mmodifier,
116                                 oldClassNames, name.replace('.', '/'), log);
117                         cr.accept(crenamer, Attributes.getDefaultAttributes(), false);
118                     }
119                     // merges the Proxy class
120
log = scp.loggerFactory.getLogger(LOGGER_NAME + ".merge.proxy." + name);
121                     log.log(BasicLevel.DEBUG, "Merging the Proxy of class: " + name);
122                     String JavaDoc superClass = jdoclass.superClassName;
123                     cr = loadJavaClass(false, proxyName, scp.output, true);
124                     if (superClass == null || superClass.length() == 0) {
125                         mmodifier = new MergedClassModifier(rr, itfs, log);
126                     } else {
127                         mmodifier = new MergedClassModifier(rr, itfs,
128                                 superClass.replace('.', '/'), log);
129                     }
130                     crenamer = new ClassRenamer(mmodifier, oldClassNames,
131                             name.replace('.', '/'), logger);
132                     cr.accept(crenamer, Attributes.getDefaultAttributes(), false);
133
134                     // modifies the original code
135
log = scp.loggerFactory.getLogger(LOGGER_NAME + ".fieldaccess." + name);
136                     log.log(BasicLevel.DEBUG, "Modifying the field access of class: " + name);
137                     cr = loadJavaClass(false, name, scp.output, false);
138                     ClassAccessorModifier caccessmodifier =
139                             new ClassAccessorModifier(rr, xmls, log);
140                     ClassInterfaceAdder citfadder = new ClassInterfaceAdder(
141                             caccessmodifier, itfs, log);
142                     JDOImplRegistrationAdder reg = new JDOImplRegistrationAdder(citfadder, jdoclass, logger);
143                     cr.accept(reg, Attributes.getDefaultAttributes(), false);
144                     writeJavaClass(name, cw, scp.output);
145
146                     if (jdoclass.identityType == SpeedoIdentity.USER_ID
147                         && jdoclass.objectidClass != null
148                         && (jdoclass.superClassName == null //I have not a parent
149
|| !jdoclass.objectidClass.equals(jdoclass.getSuper().objectidClass))
150                                 ) {
151                         log = scp.loggerFactory.getLogger(LOGGER_NAME + ".userid." + name);
152                         cw = new ClassWriter(true);
153                         String JavaDoc cn = jdoclass.objectidClass;
154                         cr = loadJavaClass(false, cn, scp.output, true);
155                         
156                         cr.accept(new UserIdEnhancer(cw, jdoclass, log),
157                                 Attributes.getDefaultAttributes(), false);
158                         writeJavaClass(cn, cw, scp.output);
159
160                         cw = new ClassWriter(false);
161                         cn = NamingRules.pnameName(jdoclass.objectidClass);
162                         cr = loadJavaClass(false, cn, scp.output, true);
163                         cr.accept(new PNameEnhancer(cw, jdoclass, log),
164                                 Attributes.getDefaultAttributes(), false);
165                         writeJavaClass(cn, cw, scp.output);
166                     }
167                 }
168             }
169         }
170
171         // Displays thrown exceptions
172
if (!except.isEmpty() && logger.isLoggable(BasicLevel.WARN)) {
173             for (Iterator JavaDoc it = except.iterator(); it.hasNext();) {
174                 Exception JavaDoc e = (Exception JavaDoc) it.next();
175                 logger.log(BasicLevel.WARN, e.getLocalizedMessage());
176             }
177         }
178     }
179 }
180
Popular Tags