KickJava   Java API By Example, From Geeks To Geeks.

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


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.Constants;
23 import org.objectweb.speedo.metadata.SpeedoClass;
24 import org.objectweb.speedo.metadata.SpeedoField;
25 import org.objectweb.speedo.naming.api.UserId;
26 import org.objectweb.speedo.naming.api.UserIdFactory;
27 import org.objectweb.util.monolog.api.Logger;
28 import org.objectweb.util.monolog.api.BasicLevel;
29
30 import java.util.Iterator JavaDoc;
31
32 /**
33  * It adds the implementation of the UserIfFactory interface in a PName.
34  *
35  * @author S.Chassande-Barrioz
36  */

37 public class PNameEnhancer extends LoggedClassAdapter {
38
39     public final static String JavaDoc USER_ID_DESC =
40             "L" + UserId.class.getName().replace('.', '/') + ";";
41     public final static String JavaDoc PTYPE_CLASS_NAME =
42             "org/objectweb/jorm/type/api/PType";
43     public final static String JavaDoc PTYPE_DESC = "L" + PTYPE_CLASS_NAME + ";";
44
45     protected SpeedoClass jdoClass;
46
47     public PNameEnhancer(ClassVisitor classVisitor,
48                           SpeedoClass jdoClass,
49                          Logger logger) {
50         super(classVisitor, logger);
51         this.jdoClass = jdoClass;
52     }
53
54     public void visit(final int version, final int access,
55                       final String JavaDoc className,
56                       final String JavaDoc superName,
57                       final String JavaDoc[] interfaces,
58                       final String JavaDoc sourceFile) {
59         String JavaDoc[] itfs;
60         String JavaDoc uifcn = UserIdFactory.class.getName().replace('.', '/');
61         boolean alreadyUIF = false;
62         if (interfaces == null || interfaces.length==0) {
63             itfs = new String JavaDoc[]{uifcn};
64         } else {
65             int i=0;
66             while(i<interfaces.length && !interfaces[i].equals(uifcn)) {
67                 i++;
68             }
69             alreadyUIF = i<interfaces.length;
70             if (alreadyUIF) {
71                 itfs = interfaces;
72             } else {
73                 itfs = new String JavaDoc[interfaces.length + 1];
74                 System.arraycopy(interfaces, 0, itfs, 1, interfaces.length);
75                 itfs[0] = uifcn;
76             }
77         }
78         if (alreadyUIF) {
79             logger.log(BasicLevel.DEBUG, "The class " + className
80                     + " already contains the interface " + uifcn);
81             super.visit(version, access, className, superName, interfaces, sourceFile);
82             return;
83         }
84         logger.log(BasicLevel.DEBUG, "Add to the class " + className
85                 + " the interface " + uifcn);
86         super.visit(version, access, className, superName, itfs, sourceFile);
87
88         //Add the method getNewUserId
89
String JavaDoc uicn = jdoClass.objectidClass.replace('.', '/');
90         String JavaDoc methodName = "getNewUserId";
91         if (debug) {
92             logger.log(BasicLevel.DEBUG, "Add the method '" + methodName
93                     + "' in the class '" + className + "'");
94         }
95         logger.log(BasicLevel.DEBUG, "Add to the class " + className
96                 + " the method " + methodName);
97         CodeVisitor _cv = this.cv.visitMethod(
98                 Constants.ACC_PUBLIC,
99                 methodName,
100                 "()" + USER_ID_DESC,
101                 null,
102                 null);
103
104         // GENERATE: Titi t = new Titi();
105
// GENERATE: Titi is the class of the user identifier
106
logger.log(BasicLevel.DEBUG, "add code: " + uicn + " t = new " + uicn + "();");
107         _cv.visitTypeInsn(Constants.NEW, uicn);
108         _cv.visitInsn(Constants.DUP);
109         _cv.visitMethodInsn(Constants.INVOKESPECIAL, uicn, "<init>", "()V");
110         _cv.visitVarInsn(Constants.ASTORE, 1);
111
112         //t.f1 = this.f1
113
boolean hasLongOrDouble = false;
114         SpeedoClass current = jdoClass;
115         while(current != null) {
116             for(Iterator JavaDoc it = current.jdoField.values().iterator(); it.hasNext();) {
117                 SpeedoField sp = (SpeedoField) it.next();
118                 if (sp.primaryKey) {
119                     hasLongOrDouble |= sp.desc.charAt(0) == 'D'
120                         || sp.desc.charAt(0) == 'J';
121                     logger.log(BasicLevel.DEBUG, "add code: this." + sp.name + " = t." + sp.name);
122                     _cv.visitVarInsn(Constants.ALOAD, 1); //Can be a long|double value
123
_cv.visitVarInsn(Constants.ALOAD, 0);
124                     _cv.visitFieldInsn(Constants.GETFIELD, className, sp.name, sp.desc);
125                     _cv.visitFieldInsn(Constants.PUTFIELD, uicn, sp.name, sp.desc);
126                 }
127             }
128             SpeedoClass tmp = current.getSuper();
129             current = (tmp == current ? null : tmp);
130         }
131
132         // GENERATE: t.jdoSetPersistentClassName(this.getPType().getJormName());
133
logger.log(BasicLevel.DEBUG, "add code: t.jdoSetPersistentClassName(this.getPType().getJormName());");
134         _cv.visitVarInsn(Constants.ALOAD, 1);
135         _cv.visitVarInsn(Constants.ALOAD, 0);
136         _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
137                 className, "getPType", "()" + PTYPE_DESC);
138         _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
139                 PTYPE_CLASS_NAME, "getJormName", "()Ljava/lang/String;");
140         _cv.visitMethodInsn(Constants.INVOKEVIRTUAL,
141                 uicn, "jdoSetPersistentClassName", "(Ljava/lang/String;)V");
142         logger.log(BasicLevel.DEBUG, "add code: return t;");
143         _cv.visitVarInsn(Constants.ALOAD, 1);
144         _cv.visitInsn(Constants.ARETURN);
145         _cv.visitMaxs((hasLongOrDouble ? 3 : 2), 2);
146     }
147 }
148
Popular Tags