KickJava   Java API By Example, From Geeks To Geeks.

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


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.asm.Attribute;
24 import org.objectweb.util.monolog.api.Logger;
25 import org.objectweb.speedo.metadata.SpeedoClass;
26
27 /**
28  * It generate a no-arg constructor or change the modifier of the no-arg
29  * constructor if required on persistent classes.
30  *
31  * @author S.Chassande-Barrioz
32  */

33 public class EmptyConstructorAdder extends LoggedClassAdapter {
34
35     /**
36      * The status with regards to the no-arg constructor
37      * possible value:
38      * - SpeedoClass.NO_NO_ARG_CONSTRUCTOR
39      * - SpeedoClass.NON_PUBLIC_NO_ARG_CONSTRUCTOR
40      * - SpeedoClass.PUBLIC_NO_ARG_CONSTRUCTOR
41      */

42     byte status = 0;
43
44     public EmptyConstructorAdder(ClassVisitor classVisitor, byte status) {
45         super(classVisitor);
46         this.status = status;
47     }
48
49     public EmptyConstructorAdder(ClassVisitor classVisitor, Logger logger, byte status) {
50         super(classVisitor, logger);
51         this.status = status;
52     }
53
54     public void visit(final int version, final int access,
55                       final String JavaDoc name,
56                       final String JavaDoc superName,
57                       final String JavaDoc[] interfaces,
58                       final String JavaDoc sourceFile) {
59         cv.visit(version, access, name, superName, interfaces, sourceFile);
60         //Generate a no-arg constructor if required
61
if (status == SpeedoClass.NO_NO_ARG_CONSTRUCTOR) {
62             CodeVisitor _cv = this.cv.visitMethod(
63                 Constants.ACC_PUBLIC, "<init>", "()V", null, null);
64             _cv.visitVarInsn(Constants.ALOAD, 0);
65             _cv.visitMethodInsn(Constants.INVOKESPECIAL, superName, "<init>", "()V");
66             _cv.visitInsn(Constants.RETURN);
67             _cv.visitMaxs(1, 1);
68         }
69     }
70
71     public CodeVisitor visitMethod(final int access,
72                                    final String JavaDoc name,
73                                    final String JavaDoc desc,
74                                    final String JavaDoc[] exceptions,
75                                    final Attribute attrs) {
76         int newaccess = access;
77         //Change the modifier of the no-arg constructor if required
78
if (status == SpeedoClass.NON_PUBLIC_NO_ARG_CONSTRUCTOR
79             && name.equals("<init>")
80             && desc.equals("()V")) {
81             newaccess = Constants.ACC_PUBLIC;
82         }
83         return super.visitMethod(newaccess, name, desc, exceptions, attrs);
84     }
85 }
86
Popular Tags