01: /**
02: * Copyright (C) 2001-2005 France Telecom R&D
03: *
04: * This library is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License as published by the Free Software Foundation; either
07: * version 2 of the License, or (at your option) any later version.
08: *
09: * 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: */package org.objectweb.speedo.generation.enhancer.pc;
18:
19: import org.objectweb.asm.ClassVisitor;
20: import org.objectweb.asm.CodeVisitor;
21: import org.objectweb.asm.Constants;
22: import org.objectweb.asm.Attribute;
23: import org.objectweb.util.monolog.api.Logger;
24: import org.objectweb.speedo.generation.enhancer.common.LoggedClassAdapter;
25: import org.objectweb.speedo.lib.Personality;
26: import org.objectweb.speedo.metadata.SpeedoClass;
27:
28: /**
29: * It generates a no-arg constructor or changes the modifier of the no-arg
30: * constructor if required on persistent classes.
31: *
32: * @author S.Chassande-Barrioz
33: */
34: public class NoArgConstructorAdder extends LoggedClassAdapter {
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 NoArgConstructorAdder(ClassVisitor classVisitor,
45: byte status, Personality p) {
46: super (classVisitor, p);
47: this .status = status;
48: }
49:
50: public NoArgConstructorAdder(ClassVisitor classVisitor,
51: Logger logger, byte status, Personality p) {
52: super (classVisitor, p, logger);
53: this .status = status;
54: }
55:
56: public void visit(final int version, final int access,
57: final String name, final String super Name,
58: final String[] interfaces, final String sourceFile) {
59: cv.visit(version, access, name, super Name, interfaces,
60: sourceFile);
61: //Generate a no-arg constructor if required
62: if (status == SpeedoClass.NO_NO_ARG_CONSTRUCTOR) {
63: CodeVisitor _cv = this .cv.visitMethod(Constants.ACC_PUBLIC,
64: "<init>", "()V", null, null);
65: _cv.visitVarInsn(Constants.ALOAD, 0);
66: _cv.visitMethodInsn(Constants.INVOKESPECIAL, super Name,
67: "<init>", "()V");
68: _cv.visitInsn(Constants.RETURN);
69: _cv.visitMaxs(1, 1);
70: }
71: }
72:
73: public CodeVisitor visitMethod(final int access, final String name,
74: final String desc, final String[] 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>") && desc.equals("()V")) {
80: newaccess = Constants.ACC_PUBLIC;
81: }
82: return super.visitMethod(newaccess, name, desc, exceptions,
83: attrs);
84: }
85: }
|