KickJava   Java API By Example, From Geeks To Geeks.

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


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.Attribute;
21 import org.objectweb.asm.ClassVisitor;
22 import org.objectweb.asm.CodeAdapter;
23 import org.objectweb.asm.CodeVisitor;
24 import org.objectweb.asm.Constants;
25 import org.objectweb.speedo.generation.lib.NamingRules;
26 import org.objectweb.speedo.metadata.SpeedoClass;
27 import org.objectweb.speedo.metadata.SpeedoField;
28 import org.objectweb.util.monolog.api.Logger;
29
30 import java.util.Collection JavaDoc;
31
32 /**
33  * Replaces field accesses by call to getter and setter methods.
34  *
35  * Adapted from modifyMethods and replaceInstruction in EnhancerTool.
36  */

37 public class PersistenceAwareClassModifier extends LoggedClassAdapter {
38
39     private final Collection JavaDoc xmlDescriptors;
40
41     public PersistenceAwareClassModifier(ClassVisitor cv,
42                                          Collection JavaDoc xmlDescriptors,
43                                          Logger logger) {
44         super(cv, logger);
45         this.xmlDescriptors = xmlDescriptors;
46     }
47
48     public CodeVisitor visitMethod(final int access,
49                                    final String JavaDoc name,
50                                    final String JavaDoc desc,
51                                    final String JavaDoc[] exceptions,
52                                    final Attribute attrs) {
53         CodeVisitor _cv = this.cv.visitMethod(access, name, desc, exceptions, attrs);
54         return new PersistenceAwareCodeModifier(_cv);
55     }
56
57     /**
58      * Looks for a specific <code>SpeedoField</code> in the object model.
59      *
60      * @param name the name of the field to be fetched
61      * @param className the complete class name that the field belongs to
62      * @return the corresponding SpeedoField if it exists, null either
63      */

64     SpeedoField fetchJDOField(final String JavaDoc name, final String JavaDoc className) {
65         SpeedoClass c = Util.isPersistentCapable(className, xmlDescriptors);
66         if (c != null) {
67             return (SpeedoField) c.jdoField.get(name);
68         } else {
69             return null;
70         }
71     }
72
73     class PersistenceAwareCodeModifier extends CodeAdapter {
74
75         public PersistenceAwareCodeModifier(CodeVisitor cv) {
76             super(cv);
77         }
78
79         public void visitFieldInsn(final int opcode,
80                                    final String JavaDoc owner,
81                                    final String JavaDoc name,
82                                    final String JavaDoc desc) {
83             if (opcode == Constants.PUTFIELD) {
84                 SpeedoField jdofield = fetchJDOField(name, owner.replace('/', '.'));
85                 if (jdofield != null) {
86                     String JavaDoc setterName = NamingRules.setterName(jdofield);
87                     String JavaDoc setterDesc = "(" + desc + ")V";
88                     cv.visitMethodInsn(
89                             Constants.INVOKEVIRTUAL,
90                             owner,
91                             setterName,
92                             setterDesc);
93                     return;
94                 }
95             } else if (opcode == Constants.GETFIELD) {
96                 SpeedoField jdofield = fetchJDOField(name, owner.replace('/', '.'));
97                 if (jdofield != null) {
98                     String JavaDoc getterName = NamingRules.getterName(
99                             jdofield.jdoClass,
100                             jdofield.name);
101                     String JavaDoc getterDesc = "()" + desc;
102                     cv.visitMethodInsn(
103                             Constants.INVOKEVIRTUAL,
104                             owner,
105                             getterName,
106                             getterDesc);
107                     return;
108                 }
109             }
110             cv.visitFieldInsn(opcode, owner, name, desc);
111         }
112     }
113 }
114
Popular Tags